Skip to content

Commit

Permalink
change utf-8 character logic for hwinfo api
Browse files Browse the repository at this point in the history
  • Loading branch information
mhwlng committed May 16, 2023
1 parent cc4e022 commit aefe51b
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 89 deletions.
19 changes: 2 additions & 17 deletions fiphwinfo/HWINFO.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ RAMTemp-SensorId=0xf3000002
RAMTemp-SensorInstance=0x0
RAMTemp-EntryId=0x1000000

WaterTemp-SensorId=0xfc04a100
WaterTemp-SensorInstance=0x30
WaterTemp-EntryId=0x1000000

GPUTemp-SensorId=0xe0002000
GPUTemp-SensorInstance=0x0
GPUTemp-EntryId=0x1000000
Expand All @@ -35,14 +31,6 @@ GPUFanSpeed-SensorId=0xe0002000
GPUFanSpeed-SensorInstance=0x0
GPUFanSpeed-EntryId=0x3000000

PumpSpeed-SensorId=0xfc04a100
PumpSpeed-SensorInstance=0x30
PumpSpeed-EntryId=0x3000002

RadiatorFanSpeed-SensorId=0xfc04a100
RadiatorFanSpeed-SensorInstance=0x30
RadiatorFanSpeed-EntryId=0x3000000

ChipsetFanSpeed-SensorId=0xf0ec0502
ChipsetFanSpeed-SensorInstance=0x0
ChipsetFanSpeed-EntryId=0x3000002
Expand Down Expand Up @@ -74,20 +62,17 @@ FrameRate=Frame Rate
CPUUsage=CPU
GPUUsage=GPU

[HWINFO-Config-TEMPERATURES]
[HWINFO-Config-TEMPERATURE]
CPUTemp=CPU
GPUTemp=GPU
RAMTemp=RAM
SSDTemp=SSD
VRMTemp=VRM
ChipsetTemp=Chipset
MotherboardTemp=Motherboard
WaterTemp=Water

[HWINFO-Config-FAN SPEEDS]
[HWINFO-Config-FAN SPEED]
GPUFanSpeed=GPU
PumpSpeed=Pump
RadiatorFanSpeed=Radiator
ChipsetFanSpeed=Chipset

[HWINFO-Config-POWER]
Expand Down
181 changes: 158 additions & 23 deletions fiphwinfo/HWInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ public class _HWiNFO_SENSOR
{
public uint SensorId;
public uint SensorInstance;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public string SensorNameOrig;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public string SensorNameUser;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public byte[] SensorNameOrig;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public byte[] SensorNameUser;

// Version 2+ new:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public byte[] UtfSensorNameUser; // Sensor name displayed, which might be translated or renamed by user [UTF-8 string]

}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
Expand All @@ -67,16 +72,22 @@ public class _HWiNFO_ELEMENT
public SENSOR_TYPE SensorType;
public uint SensorIndex;
public uint ElementId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public string LabelOrig;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public string LabelUser;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HWINFO_UNIT_STRING_LEN)]
public string Unit;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public byte[] LabelOrig;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public byte[] LabelUser;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_UNIT_STRING_LEN)]
public byte[] Unit;
public double Value;
public double ValueMin;
public double ValueMax;
public double ValueAvg;

// Version 2+ new:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_SENSORS_STRING_LEN)]
public byte[] UtfLabelUser; // Label displayed, which might be translated or renamed by user [UTF-8 string]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HWINFO_UNIT_STRING_LEN)]
public byte[] UtfUnit; // e.g. "RPM" [UTF-8 string]
}

public class ElementObj
Expand Down Expand Up @@ -141,7 +152,16 @@ public static string NumberFormat(SENSOR_TYPE sensorType, string unit, double va
valstr = value.ToString("N3");
break;
case SENSOR_TYPE.SENSOR_TYPE_POWER:
valstr = value.ToString("N3");

if (unit == "W")
{
valstr = value.ToString("N1");
}
else
{
valstr = value.ToString("N3");
}

break;

case SENSOR_TYPE.SENSOR_TYPE_CLOCK:
Expand Down Expand Up @@ -190,6 +210,75 @@ public static string NumberFormat(SENSOR_TYPE sensorType, string unit, double va
return (valstr + " " + unit).Trim();
}


public static double RoundValue(SENSOR_TYPE sensorType, string unit, double value)
{
double val = value;

switch (sensorType)
{
case SENSOR_TYPE.SENSOR_TYPE_VOLT:
val = Math.Round(value,3);
break;
case SENSOR_TYPE.SENSOR_TYPE_CURRENT:
val = Math.Round(value,3);
break;
case SENSOR_TYPE.SENSOR_TYPE_POWER:

if (unit == "W")
{
val = Math.Round(value, 1);
}
else
{
val = Math.Round(value, 3);
}

break;

case SENSOR_TYPE.SENSOR_TYPE_CLOCK:
val = Math.Round(value,1);
break;
case SENSOR_TYPE.SENSOR_TYPE_USAGE:
val = Math.Round(value,1);
break;
case SENSOR_TYPE.SENSOR_TYPE_TEMP:
val = Math.Round(value,1);
break;

case SENSOR_TYPE.SENSOR_TYPE_FAN:
val = Math.Round(value,0);
break;

case SENSOR_TYPE.SENSOR_TYPE_OTHER:

if (unit == "Yes/No")
{

}
else if (unit.EndsWith("GT/s") || unit == "x" || unit == "%")
{
val = Math.Round(value,1);
}
else if (unit.EndsWith("/s"))
{
val = Math.Round(value,3);
}
else if (unit.EndsWith("MB") || unit.EndsWith("GB") || unit == "T" || unit == "FPS")
{
val = Math.Round(value,0);
}

break;

case SENSOR_TYPE.SENSOR_TYPE_NONE:

break;

}

return val;
}
public static void ReadMem(string incPath)
{
lock (RefreshHWInfoLock)
Expand Down Expand Up @@ -239,12 +328,27 @@ private static void ReadSensors(MemoryMappedFile mmf, _HWiNFO_SHARED_MEM hWiNFOM

if (!FullSensorData.ContainsKey(index))
{
//var sensorNameOrig = Encoding.GetEncoding(1252).GetString(structure.SensorNameOrig).TrimEnd((char)0);
var sensorNameOrig = Encoding.UTF8.GetString(structure.SensorNameOrig).TrimEnd((char)0);

var sensorName = "";

if (hWiNFOMemory.Version > 1)
{
sensorName = Encoding.UTF8.GetString(structure.UtfSensorNameUser).TrimEnd((char)0);
}
else
{
//sensorName = Encoding.GetEncoding(1252).GetString(structure.SensorNameUser).TrimEnd((char)0);
sensorName = Encoding.UTF8.GetString(structure.SensorNameUser).TrimEnd((char)0);
}

var sensor = new SensorObj
{
SensorId = structure.SensorId,
SensorInstance = structure.SensorInstance,
SensorNameOrig = structure.SensorNameOrig,
SensorNameUser = structure.SensorNameUser,
SensorNameOrig = sensorNameOrig,
SensorNameUser = sensorName,
Elements = new Dictionary<string, ElementObj>()
};

Expand Down Expand Up @@ -273,20 +377,46 @@ private static void ReadElements(MemoryMappedFile mmf, _HWiNFO_SHARED_MEM hWiNFO

var elementKey = sensor.SensorId + "-" + sensor.SensorInstance + "-" + structure.ElementId;

//var labelOrig = Encoding.GetEncoding(1252).GetString(structure.LabelOrig).TrimEnd((char)0);
var labelOrig = Encoding.UTF8.GetString(structure.LabelOrig).TrimEnd((char)0);

var unit = "";

if (hWiNFOMemory.Version > 1)
{
unit = Encoding.UTF8.GetString(structure.UtfUnit).TrimEnd((char)0);
} else
{
//unit = Encoding.GetEncoding(1252).GetString(structure.Unit).TrimEnd((char)0);
unit = Encoding.UTF8.GetString(structure.Unit).TrimEnd((char)0);
}

var label = "?";

if (hWiNFOMemory.Version > 1)
{
label = Encoding.UTF8.GetString(structure.UtfLabelUser).TrimEnd((char)0);
}
else
{
//label = System.Text.Encoding.GetEncoding(1252).GetString(structure.LabelUser).TrimEnd((char)0);
label = System.Text.Encoding.UTF8.GetString(structure.LabelUser).TrimEnd((char)0);
}

var element = new ElementObj
{
ElementKey = elementKey,

SensorType = structure.SensorType,
ElementId = structure.ElementId,
LabelOrig = structure.LabelOrig,
LabelUser = structure.LabelUser,
Unit = structure.Unit,
NumericValue = (float)structure.Value,
Value = NumberFormat(structure.SensorType, structure.Unit, structure.Value),
ValueMin = NumberFormat(structure.SensorType, structure.Unit, structure.ValueMin),
ValueMax = NumberFormat(structure.SensorType, structure.Unit, structure.ValueMax),
ValueAvg = NumberFormat(structure.SensorType, structure.Unit,structure.ValueAvg)
LabelOrig = labelOrig,
LabelUser = label,
Unit = unit,
NumericValue = (float)RoundValue(structure.SensorType, unit, structure.Value),
Value = NumberFormat(structure.SensorType, unit, structure.Value),
ValueMin = NumberFormat(structure.SensorType, unit, structure.ValueMin),
ValueMax = NumberFormat(structure.SensorType, unit, structure.ValueMax),
ValueAvg = NumberFormat(structure.SensorType, unit,structure.ValueAvg),
};

sensor.Elements[elementKey] = element;
Expand Down Expand Up @@ -322,7 +452,8 @@ private static void ParseIncFile()
var sensorInstanceStr = IncData["Variables"][key.KeyName + "-SensorInstance"];
var elementIdStr = IncData["Variables"][key.KeyName + "-EntryId"];

if (sensorIdStr?.StartsWith("0x") == true && sensorInstanceStr?.StartsWith("0x") == true &&
if (sensorIdStr?.StartsWith("0x") == true &&
sensorInstanceStr?.StartsWith("0x") == true &&
elementIdStr?.StartsWith("0x") == true)
{
var sensorId = Convert.ToUInt32(sensorIdStr.Replace("0x", ""), 16);
Expand Down Expand Up @@ -387,7 +518,9 @@ private static void ParseIncFile()

if (!SensorTrends.ContainsKey(elementKey))
{
SensorTrends.Add(elementKey, new ChartCircularBuffer(fullSensorDataElement.SensorType, fullSensorDataElement.Unit));
SensorTrends.Add(elementKey,
new ChartCircularBuffer(fullSensorDataElement.SensorType,
fullSensorDataElement.Unit));
}

SensorTrends[elementKey].Put(fullSensorDataElement.NumericValue);
Expand All @@ -406,6 +539,8 @@ public static void SaveDataToFile(string path)
{
path = Path.Combine(App.ExePath, path);

Directory.CreateDirectory(Path.GetDirectoryName(path));

using (var fs = File.Create(path))
{
var json = new UTF8Encoding(true).GetBytes(JsonConvert.SerializeObject(FullSensorData, Formatting.Indented));
Expand Down
7 changes: 3 additions & 4 deletions fiphwinfo/Mqtt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
using System.Xml;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Options;
using MQTTnet.Client.Publishing;
using MQTTnet.Protocol;

namespace fiphwinfo
{
Expand All @@ -39,7 +37,8 @@ public static async Task<bool> Publish(string channel, string value)
var message = new MqttApplicationMessageBuilder()
.WithTopic(channel)
.WithPayload(value)
.WithAtMostOnceQoS()
//.WithAtMostOnceQoS()
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce)
//.WithRetainFlag()
.Build();

Expand Down
6 changes: 3 additions & 3 deletions fiphwinfo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("fiphwinfo")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -31,7 +31,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
17 changes: 16 additions & 1 deletion fiphwinfo/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
Expand All @@ -52,4 +52,19 @@
<add namespace="fiphwinfo" />
</namespaces>
</razorEngine>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
Loading

0 comments on commit aefe51b

Please sign in to comment.