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>'?