0

I want to access the OpenHardwareMonitor DLL file with node.js using the edge.js module and monitor the computer's CPU and GPU temperatures. I created a C# console application in Visual Studio and added the DLL file to the references. When I ran the Program.cs file below, I successfully reached the CPU temperature.

Program.cs;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;

namespace Get_CPU_Temp5
{
    class Program
    {
        public class UpdateVisitor : IVisitor
        {
            public void VisitComputer(IComputer computer)
            {
                computer.Traverse(this);
            }
            public void VisitHardware(IHardware hardware)
            {
                hardware.Update();
                foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
            }
            public void VisitSensor(ISensor sensor) { }
            public void VisitParameter(IParameter parameter) { }
        }
        static void GetSystemInfo()
        {
            UpdateVisitor updateVisitor = new UpdateVisitor();
            Computer computer = new Computer();
            computer.Open();
            computer.CPUEnabled = true;
            computer.Accept(updateVisitor);
            for (int i = 0; i < computer.Hardware.Length; i++)
            {
                if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                {
                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                            Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                    }
                }
            }
            computer.Close();
        }
        static void Main(string[] args)
        {
            while (true)
            {
                GetSystemInfo();
            }
        }
    }
}

I want to run the same code in my Nodejs project with edge.js and access the CPU temperature. I tried this but I've got errors. I checked the datatype of computer.Hardware[i].Sensors[j].Value . It's called System.Single. I think I need a return value but I don't quite understand how to do it.

I'm not sure where I went wrong. I will be glad if you help

    var edge = require('edge-js');

    var add7 = edge.func({
    source: function() {/*


        using System;
        using System.Data;
        using System.Threading.Tasks;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using OpenHardwareMonitor.Hardware;

        public class Startup
        {
             public async Task<object> Invoke(object input)
            {              
                   
                    Computer computer = new Computer() ;
                    computer.Open();
                    computer.CPUEnabled = true ;

                    for(int i = 0 ; i<computer.Hardware.Length;i++){
                        
                        if(computer.Hardware[i].HardwareType == HardwareType.CPU){
                            for(int j = 0 ; j< computer.Hardware[i].Sensors.Length;j++){
                                if(computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString()+"\r");
                               
                            }
                        }
                    }

                    computer.Close();

                
            }
        }
    */},
    references: [ 'System.Data.dll' ,'OpenHardwareMonitorLib.dll' ]
    //references: [ 'OpenHardwareMonitorLib.dll' ],
    
});

And this is the error;

return edge.initializeClrFunc(options);
                ^
Error: Unable to compile C# code.
----> Errors when compiling as a CLR library:
c:\Users\Elessar\AppData\Local\Temp\uphki0bx\uphki0bx.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kullanarak engelleyici olmayan API çağrılarını beklemeyi veya 'await Task.Run(...)' kullanarak bir arka plan iş parçacığında CPU bağlantılı iş yapmayı düşünün.
c:\Users\Elessar\AppData\Local\Temp\uphki0bx\uphki0bx.0.cs(14,40) : error CS0161: 'Startup.Invoke(object)': tüm kod yolları değer döndürmez
----> Errors when compiling as a CLR async lambda expression:
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(5,42) : error CS1513: } bekleniyor
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(43,9) : error CS1519: class, struct veya interface üyesi bildiriminde geçersiz 'return' belirteci
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(43,35) : error CS1001: Tanımlayıcı bekleniyor
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(45,1) : error CS1022: Tür veya ad alanı tanımı ya da dosya sonu bekleniyor
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(16,22) : error CS0542: 'Startup': üye adları kendilerini kapsayan türle aynı olamaz
    at Object.exports.func (C:\Users\Elessar\Desktop\Remote-Control\backend\node_modules\edge-js\lib\edge.js:177:17)
    at Object.<anonymous> (C:\Users\Elessar\Desktop\Remote-Control\backend\app\controllers\pc-control\dll.js:13:17)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  Message: 'Unable to compile C# code.\n' +
    '----> Errors when compiling as a CLR library:\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\uphki0bx\\uphki0bx.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kull

C:\Users\Elessar\Desktop\Remote-Control\backend\app\controllers\pc-control>node dll.js

C:\Users\Elessar\Desktop\Remote-Control\backend\node_modules\edge-js\lib\edge.js:177
    return edge.initializeClrFunc(options);
                ^
Error: Unable to compile C# code.
----> Errors when compiling as a CLR library:
c:\Users\Elessar\AppData\Local\Temp\ktjpgbj0\ktjpgbj0.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kullanarak engelleyici olmayan API çağrılarını beklemeyi veya 'await Task.Run(...)' kullanarak bir arka plan iş parçacığında CPU bağlantılı iş yapmayı düşünün.
c:\Users\Elessar\AppData\Local\Temp\ktjpgbj0\ktjpgbj0.0.cs(14,40) : error CS0161: 'Startup.Invoke(object)': tüm kod yolları değer döndürmez
----> Errors when compiling as a CLR async lambda expression:
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(5,42) : error CS1513: } bekleniyor
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(43,9) : error CS1519: class, struct veya interface üyesi bildiriminde geçersiz 'return' belirteci
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(43,35) : error CS1001: Tanımlayıcı bekleniyor
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(45,1) : error CS1022: Tür veya ad alanı tanımı ya da dosya sonu bekleniyor
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(16,22) : error CS0542: 'Startup': üye adları kendilerini kapsayan türle aynı olamaz
    at Object.exports.func (C:\Users\Elessar\Desktop\Remote-Control\backend\node_modules\edge-js\lib\edge.js:177:17)
    at Object.<anonymous> (C:\Users\Elessar\Desktop\Remote-Control\backend\app\controllers\pc-control\dll.js:13:17)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  Message: 'Unable to compile C# code.\n' +
    '----> Errors when compiling as a CLR library:\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\ktjpgbj0\\ktjpgbj0.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kullanarak engelleyici olmayan API çağrılarını beklemeyi veya 'await Task.Run(...)' kullanarak bir arka plan iş parçacığında CPU bağlantılı iş yapmayı düşünün.\n" +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\ktjpgbj0\\ktjpgbj0.0.cs(14,40) : error CS0161: 'Startup.Invoke(object)': tüm kod yolları değer döndürmez\n" +
    '----> Errors when compiling as a CLR async lambda expression:\n' +
    'c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(5,42) : error CS1513: } bekleniyor\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(43,9) : error CS1519: class, struct veya interface üyesi bildiriminde geçersiz 'return' belirteci\n" +
    'c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(43,35) : error CS1001: Tanımlayıcı bekleniyor\n' +
    'c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(45,1) : error CS1022: Tür veya ad alanı tanımı ya da dosya sonu bekleniyor\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(16,22) : error CS0542: 'Startup': üye adları kendilerini kapsayan türle aynı olamaz",
  Data: {},
  InnerException: null,
  TargetSite: {},
  StackTrace: '   konum: EdgeCompiler.CompileFunc(IDictionary`2 parameters)',
  HelpLink: null,
  Source: 'edge-cs',
  HResult: -2146233079,
  name: 'System.InvalidOperationException'
}

--EDIT--

When I tried this ;

public class Startup
    {
         public class UpdateVisitor : IVisitor
                    {
                        public void VisitComputer(IComputer computer)
                        {
                            computer.Traverse(this);
                        }
                        public void VisitHardware(IHardware hardware)
                        {
                            hardware.Update();
                            foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
                        }
                        public void VisitSensor(ISensor sensor) { }
                        public void VisitParameter(IParameter parameter) { }
                    }

                public async Task<object> Invoke(object _)
                {
                   
                    
                        while (true)
                        {
                            GetSystemInfo();
                            return await Task.FromResult<object>(null);
                        }
                    
                }

            static void GetSystemInfo()
            {
                UpdateVisitor updateVisitor = new UpdateVisitor();
                Computer computer = new Computer();
                computer.Open();
                computer.CPUEnabled = true;
                computer.Accept(updateVisitor);
                for (int i = 0; i < computer.Hardware.Length; i++)
                {
                    if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                    {
                        for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                        {
                            if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                    Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                        }
                    }
                }
                computer.Close();
            }
    }

I got this error;

CS5001 Program does not contain a static Main method suitable for an entry point

I also tried ;

 public async Task Invoke(object _)
    {     
       while (true)
          {
             GetSystemInfo();
             return await Task.FromResult<object>(null);
           }
                    
    }

And it says;

Since 'Startup.Invoke(object)' is an async method that returns 'Task', a return keyword must not be followed by an object expression. Did you intend to return 'Task<T>'?
Didem
  • 63
  • 1
  • 4
  • 10
  • I doubt you can link to native code like this – Daniel A. White Aug 02 '22 at 15:00
  • @DanielA.White: Calling non-JS code appears to be the whole point of this `edgejs`. But he needs to fix the compile errors. I can't read whatever language his compiler output is set to, but evidently errors like "missing `}`" and "reaching end of non-void-returning function without a return statement". OP should look at the temp files `edgejs` creates and find out why they aren't valid C# syntax. – Ben Voigt Aug 02 '22 at 15:22
  • @BenVoigt I didn't say anything about clr code. but native ones. perhaps edgejs compiles to wasm. and those can't load native dlls – Daniel A. White Aug 02 '22 at 15:35
  • @DanielA.White: That definitely sounds possible, it just looks like this is much earlier in the process -- the C# code shown is not capable of being compiled at all, no matter the output encoding. – Ben Voigt Aug 02 '22 at 19:40

1 Answers1

0

async Task<object> promises a return value, but your code doesn't have one.

Try Task instead of Task<object>.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I edited the question. I tried the change you mentioned but it didn't work. – Didem Aug 03 '22 at 11:23
  • @Didem: Based on your edit, that change clearly fixed all the errors you were having. When you got farther in the process, you discovered additional problems with your code. In particular, when you compile the temp file standalone with `csc.exe`, a `Main` function is required, but when edgejs does its "when compiling as a CLR library" thing, it's fine to not have Main. – Ben Voigt Aug 03 '22 at 15:36
  • @Didem: But you're going to have to actually think about what your error messages say, it's not good enough to just paste them into your question. – Ben Voigt Aug 03 '22 at 15:37