Skip to content

Commit

Permalink
auto reconnect hwinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mhwlng committed Nov 13, 2022
1 parent f1a406b commit 4f6df25
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 70 deletions.
104 changes: 55 additions & 49 deletions fipha/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,6 @@ protected override void OnStartup(StartupEventArgs evtArgs)
Log.Error("No Home Assistant Connection for Flight Instrument Panels");
}

splashScreen.Dispatcher.Invoke(() =>
splashScreen.ProgressText.Text = "Getting sensor data from HWInfo...");

HWInfo.ReadMem("HWINFO.INC");

if (HWInfo.SensorData.Any())
{
Log.Info($"Writing {HWInfo.SensorData.Count} HWINFO Sensors to hwinfo.json");

HWInfo.SaveDataToFile(@"Data\hwinfo.json");
}
else
{
Log.Error("No HWINFO Sensors Found");
}

Dispatcher.Invoke(() =>
{
var window = Current.MainWindow = new MainWindow();
Expand Down Expand Up @@ -403,51 +387,64 @@ protected override void OnStartup(StartupEventArgs evtArgs)

MQTT.FillConfig();

if (File.Exists(Path.Combine(App.ExePath, "mqtt.config")) && HWInfo.SensorData.Any())
var incPath = Path.Combine(App.ExePath, "HWINFO.INC");

var onlyOnce = true;

if (File.Exists(Path.Combine(App.ExePath, "mqtt.config")) && File.Exists(incPath))
{
HWInfoTask = Task.Run(async () =>
{
await MQTT.Connect();
await MQTT.Connect();

Log.Info($"HWInfo task started, polling interval {MQTT.MqttPollingInterval} ms");

if (HWInfo.SensorData.Any())
while (true)
{
foreach (var sensor in HWInfo.SensorData)
if (hwInfoToken.IsCancellationRequested)
{
foreach (var element in sensor.Value.Elements)
{
var mqttValue = JsonConvert.SerializeObject(new HWInfo.MQTTDiscoveryObj
{
device_class = element.Value.DeviceClass,
name = element.Value.Name,
state_topic =
$"homeassistant/{element.Value.Component}/{element.Value.Node}/state",
unit_of_measurement = element.Value.Unit,
value_template = "{{ value_json.value}}",
unique_id = element.Value.Node,
state_class = "measurement"
}, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

await MQTT.Publish(
$"homeassistant/{element.Value.Component}/{element.Value.Node}/config",
mqttValue);

}
hwInfoToken.ThrowIfCancellationRequested();
}

while (true)
HWInfo.ReadMem();

if (HWInfo.SensorData.Any())
{
if (hwInfoToken.IsCancellationRequested)
if (onlyOnce)
{
hwInfoToken.ThrowIfCancellationRequested();
}
onlyOnce = false;

Log.Info($"HWINFO Sensors found, Writing all HWINFO Sensors to hwinfo.json");

HWInfo.SaveDataToFile(@"Data\hwinfo.json");

foreach (var sensor in HWInfo.SensorData)
{
foreach (var element in sensor.Value.Elements)
{
var mqttValue = JsonConvert.SerializeObject(new HWInfo.MQTTDiscoveryObj
{
device_class = element.Value.DeviceClass,
name = element.Value.Name,
state_topic =
$"homeassistant/{element.Value.Component}/{element.Value.Node}/state",
unit_of_measurement = element.Value.Unit,
value_template = "{{ value_json.value}}",
unique_id = element.Value.Node,
state_class = "measurement"
}, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

HWInfo.ReadMem("HWINFO.INC");
await MQTT.Publish(
$"homeassistant/{element.Value.Component}/{element.Value.Node}/config",
mqttValue);

}
}
}

foreach (var sensor in HWInfo.SensorData)
{
foreach (var element in sensor.Value.Elements)
Expand All @@ -463,11 +460,20 @@ await MQTT.Publish(
}

}

//!!!FipHandler.RefreshHWInfoPages();
}
else
{
Log.Error("No HWINFO Sensors Found");

await Task.Delay(MQTT.MqttPollingInterval, _hwInfoTokenSource.Token); // repeat every 5 seconds
onlyOnce = true;

Thread.Sleep(5000);
}

await Task.Delay(MQTT.MqttPollingInterval,
_hwInfoTokenSource.Token); // repeat every 5 seconds
}

}, hwInfoToken);
Expand Down
26 changes: 13 additions & 13 deletions fipha/HWInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,37 +349,37 @@ public static string DeviceClass(SENSOR_TYPE sensorType, string unit)

return deviceClass;
}
public static void ReadMem(string incPath)
public static void ReadMem()
{
lock (RefreshHWInfoLock)
{
try
{
var mmf = MemoryMappedFile.OpenExisting(HWINFO_SHARED_MEM_FILE_NAME, MemoryMappedFileRights.Read);
var accessor = mmf.CreateViewAccessor(0L, Marshal.SizeOf(typeof(_HWiNFO_SHARED_MEM)), MemoryMappedFileAccess.Read);

var mmf = MemoryMappedFile.OpenExisting(HWINFO_SHARED_MEM_FILE_NAME,
MemoryMappedFileRights.Read);
var accessor = mmf.CreateViewAccessor(0L, Marshal.SizeOf(typeof(_HWiNFO_SHARED_MEM)),
MemoryMappedFileAccess.Read);

accessor.Read(0L, out _HWiNFO_SHARED_MEM hWiNFOMemory);

ReadSensors(mmf, hWiNFOMemory);

if (IncData == null)
{
incPath = Path.Combine(App.ExePath, incPath);
var parser = new FileIniDataParser();

if (File.Exists(incPath))
{
var parser = new FileIniDataParser();
var incPath = Path.Combine(App.ExePath, "HWINFO.INC");

IncData = parser.ReadFile(incPath);
}
IncData = parser.ReadFile(incPath);
}

ParseIncFile();

}
catch (Exception ex)
{
Log.Error("HWINFO Shared Memory Read Problem", ex);
SensorData = new Dictionary<int, SensorObj>();
Log.Error($"HWINFO Shared Memory Read Problem {HWINFO_SHARED_MEM_FILE_NAME}", ex);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions fipha/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("0.0.1.1")]
[assembly: AssemblyFileVersion("0.0.1.1")]
[assembly: AssemblyVersion("0.0.1.2")]
[assembly: AssemblyFileVersion("0.0.1.2")]

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
8 changes: 4 additions & 4 deletions fipha/fipha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@
<Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
<Reference Include="MQTTnet, Version=4.1.2.350, Culture=neutral, PublicKeyToken=fdb7629f2e364a63, processorArchitecture=MSIL">
<HintPath>..\packages\MQTTnet.4.1.2.350\lib\net461\MQTTnet.dll</HintPath>
<Reference Include="MQTTnet, Version=4.1.3.436, Culture=neutral, PublicKeyToken=fdb7629f2e364a63, processorArchitecture=MSIL">
<HintPath>..\packages\MQTTnet.4.1.3.436\lib\net461\MQTTnet.dll</HintPath>
</Reference>
<Reference Include="MQTTnet.Extensions.ManagedClient, Version=4.1.2.350, Culture=neutral, PublicKeyToken=fdb7629f2e364a63, processorArchitecture=MSIL">
<HintPath>..\packages\MQTTnet.Extensions.ManagedClient.4.1.2.350\lib\net461\MQTTnet.Extensions.ManagedClient.dll</HintPath>
<Reference Include="MQTTnet.Extensions.ManagedClient, Version=4.1.3.436, Culture=neutral, PublicKeyToken=fdb7629f2e364a63, processorArchitecture=MSIL">
<HintPath>..\packages\MQTTnet.Extensions.ManagedClient.4.1.3.436\lib\net461\MQTTnet.Extensions.ManagedClient.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand Down
4 changes: 2 additions & 2 deletions fipha/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<package id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net48" />
<package id="Microsoft.CSharp" version="4.7.0" targetFramework="net472" />
<package id="Microsoft.Win32.Registry" version="5.0.0" targetFramework="net472" />
<package id="MQTTnet" version="4.1.2.350" targetFramework="net48" />
<package id="MQTTnet.Extensions.ManagedClient" version="4.1.2.350" targetFramework="net48" />
<package id="MQTTnet" version="4.1.3.436" targetFramework="net48" />
<package id="MQTTnet.Extensions.ManagedClient" version="4.1.3.436" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
<package id="RazorEngine" version="3.10.0" targetFramework="net472" />
<package id="SharpDX" version="4.2.0" targetFramework="net472" />
Expand Down

0 comments on commit 4f6df25

Please sign in to comment.