Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit cc4e022

Browse files
committed
Added MQTT support
1 parent 9bee15d commit cc4e022

File tree

9 files changed

+231
-13
lines changed

9 files changed

+231
-13
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,6 @@ ASALocalRun/
337337
.localhistory/
338338

339339
# BeatPulse healthcheck temp database
340-
healthchecksdb
340+
healthchecksdb
341+
342+
mqtt.config

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ A configuration tool, to link sensor ids to variables in the HWINFO.inc file, ca
2121

2222
![hwinfo tool](https://i.imgur.com/Px6jvw4.png)
2323

24+
The HWINFO sensor data can optionally be sent to an MQTT server, by creating a file called mqtt.config (this file doesn't exist by default)
25+
26+
```
27+
<?xml version="1.0" encoding="utf-8" ?>
28+
<configuration>
29+
<mqtt>
30+
<add key="mqttURI" value="192.168.2.34" />
31+
<add key="mqttUser" value="mqttusername" />
32+
<add key="mqttPassword" value="secretpassword" />
33+
<add key="mqttPort" value="1883" />
34+
<add key="mqttSecure" value="False" />
35+
</mqtt>
36+
</configuration>
37+
```
38+
39+
![MQTT](https://i.imgur.com/X8IkHPg.png)
2440

2541
A sound is played when menu options are selected.
2642
This sound can be changed or disabled by editing the 'clickSound' key in in appsettings.config

fiphwinfo/App.xaml.cs

+4
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ protected override void OnStartup(StartupEventArgs evtArgs)
193193

194194
HWInfoTask = Task.Run(async () =>
195195
{
196+
var result = await MQTT.Connect();
197+
196198
Log.Info("HWInfo task started");
197199

198200
while (true)
@@ -212,7 +214,9 @@ protected override void OnStartup(StartupEventArgs evtArgs)
212214
}, hwInfoToken);
213215

214216
});
217+
215218
}
219+
216220

217221
protected override void OnExit(ExitEventArgs e)
218222
{

fiphwinfo/HWInfo.cs

+32-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Xml;
1414
using IniParser;
1515
using IniParser.Model;
16+
using Newtonsoft.Json.Serialization;
1617
using Formatting = Newtonsoft.Json.Formatting;
1718

1819
namespace fiphwinfo
@@ -97,6 +98,13 @@ public class ElementObj
9798
public string ValueAvg;
9899
}
99100

101+
public class MQTTObj
102+
{
103+
public float Value;
104+
public string Unit;
105+
}
106+
107+
100108
public class SensorObj
101109
{
102110
public uint SensorId;
@@ -290,6 +298,14 @@ private static void ParseIncFile()
290298
{
291299
if (IncData != null && FullSensorData.Any())
292300
{
301+
var serverName = Environment.MachineName;
302+
303+
DefaultContractResolver contractResolver = new DefaultContractResolver
304+
{
305+
NamingStrategy = new CamelCaseNamingStrategy()
306+
};
307+
308+
293309
int index = -1;
294310

295311
foreach (var section in IncData.Sections.Where(x => x.SectionName != "Variables"))
@@ -351,9 +367,24 @@ private static void ParseIncFile()
351367

352368
SensorData.Add(index, sensor);
353369
}
354-
370+
355371
SensorData[index].Elements[elementKey] = element;
356372

373+
var t1 = serverName.Replace(' ', '_');
374+
var t2 = SensorData[index].SensorNameUser.Replace(' ', '_');
375+
var t3 = element.LabelUser.Replace(' ', '_');
376+
377+
var task = Task.Run<bool>(async () => await MQTT.Publish($"HWINFO/{t1}/{t2}/{t3}",
378+
JsonConvert.SerializeObject(new MQTTObj
379+
{
380+
Value = element.NumericValue,
381+
Unit = element.Unit
382+
},
383+
new JsonSerializerSettings
384+
{
385+
ContractResolver = contractResolver
386+
})));
387+
357388
if (!SensorTrends.ContainsKey(elementKey))
358389
{
359390
SensorTrends.Add(elementKey, new ChartCircularBuffer(fullSensorDataElement.SensorType, fullSensorDataElement.Unit));

fiphwinfo/Mqtt.cs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Specialized;
4+
using System.Configuration;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using System.Xml;
12+
using MQTTnet;
13+
using MQTTnet.Client;
14+
using MQTTnet.Client.Connecting;
15+
using MQTTnet.Client.Options;
16+
using MQTTnet.Client.Publishing;
17+
18+
namespace fiphwinfo
19+
{
20+
public static class MQTT
21+
{
22+
private static MqttFactory factory = new MqttFactory();
23+
private static IMqttClient mqttClient = factory.CreateMqttClient();
24+
private static string ClientId = Guid.NewGuid().ToString();
25+
26+
private static string mqttURI;
27+
private static string mqttUser;
28+
private static string mqttPassword;
29+
private static int mqttPort;
30+
private static bool mqttSecure;
31+
32+
public static async Task<bool> Publish(string channel, string value)
33+
{
34+
if (mqttClient.IsConnected == false)
35+
{
36+
return false;
37+
}
38+
39+
var message = new MqttApplicationMessageBuilder()
40+
.WithTopic(channel)
41+
.WithPayload(value)
42+
.WithAtMostOnceQoS()
43+
//.WithRetainFlag()
44+
.Build();
45+
46+
var result = await mqttClient.PublishAsync(message);
47+
48+
return result.ReasonCode == MqttClientPublishReasonCode.Success;
49+
}
50+
51+
public static async Task<bool> Connect()
52+
{
53+
if (File.Exists(Path.Combine(App.ExePath, "mqtt.config")))
54+
{
55+
var configMap = new ExeConfigurationFileMap
56+
{ExeConfigFilename = Path.Combine(App.ExePath, "mqtt.config")};
57+
58+
var config =
59+
ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
60+
61+
var myParamsSection = config.GetSection("mqtt");
62+
63+
var myParamsSectionRawXml = myParamsSection.SectionInformation.GetRawXml();
64+
var sectionXmlDoc = new XmlDocument();
65+
sectionXmlDoc.Load(new StringReader(myParamsSectionRawXml));
66+
var handler = new NameValueSectionHandler();
67+
68+
var appSection =
69+
handler.Create(null, null, sectionXmlDoc.DocumentElement) as NameValueCollection;
70+
71+
mqttURI = appSection["mqttURI"];
72+
mqttUser = appSection["mqttUser"];
73+
mqttPassword = appSection["mqttPassword"];
74+
mqttPort = Convert.ToInt32(appSection["mqttPort"]);
75+
mqttSecure = appSection["mqttSecure"] == "True";
76+
77+
if (string.IsNullOrEmpty(mqttURI)) return false;
78+
}
79+
else return false;
80+
81+
var messageBuilder = new MqttClientOptionsBuilder()
82+
.WithClientId(ClientId)
83+
.WithCredentials(mqttUser, mqttPassword)
84+
.WithTcpServer(mqttURI, mqttPort)
85+
.WithCleanSession();
86+
87+
var options = mqttSecure
88+
? messageBuilder
89+
.WithTls()
90+
.Build()
91+
: messageBuilder
92+
.Build();
93+
94+
try
95+
{
96+
var result = await mqttClient.ConnectAsync(options, CancellationToken.None);
97+
98+
if (result.ResultCode != MqttClientConnectResultCode.Success)
99+
{
100+
App.Log.Error($"MQTT CONNECT FAILED: {result.ResultCode} {result.ReasonString}");
101+
}
102+
103+
return result.ResultCode == MqttClientConnectResultCode.Success;
104+
}
105+
catch (Exception ex)
106+
{
107+
// ignore this exception
108+
App.Log.Error($"MQTT CONNECT FAILED", ex);
109+
}
110+
111+
return false;
112+
113+
}
114+
115+
}
116+
117+
}

fiphwinfo/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.0.0.0")]
35-
[assembly: AssemblyFileVersion("1.0.0.0")]
34+
[assembly: AssemblyVersion("1.0.1.0")]
35+
[assembly: AssemblyFileVersion("1.0.1.0")]
3636

3737
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

fiphwinfo/app.config

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
4242
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
4343
</dependentAssembly>
44+
<dependentAssembly>
45+
<assemblyIdentity name="Microsoft.Win32.Registry" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
46+
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
47+
</dependentAssembly>
4448
</assemblyBinding>
4549
</runtime>
4650
<razorEngine>

fiphwinfo/fiphwinfo.csproj

+40-6
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
<ApplicationManifest>app.manifest</ApplicationManifest>
5353
</PropertyGroup>
5454
<ItemGroup>
55-
<Reference Include="Hardcodet.Wpf.TaskbarNotification, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
56-
<HintPath>..\packages\Hardcodet.NotifyIcon.Wpf.1.0.8\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll</HintPath>
55+
<Reference Include="Hardcodet.NotifyIcon.Wpf, Version=1.1.0.0, Culture=neutral, PublicKeyToken=682384a853a08aad, processorArchitecture=MSIL">
56+
<HintPath>..\packages\Hardcodet.NotifyIcon.Wpf.1.1.0\lib\net472\Hardcodet.NotifyIcon.Wpf.dll</HintPath>
5757
</Reference>
5858
<Reference Include="HtmlRenderer, Version=1.5.0.6, Culture=neutral, processorArchitecture=MSIL">
5959
<HintPath>..\packages\HtmlRenderer.Core.1.5.0.6\lib\net45\HtmlRenderer.dll</HintPath>
@@ -70,11 +70,35 @@
7070
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
7171
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.5.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
7272
</Reference>
73-
<Reference Include="NAudio, Version=1.10.0.0, Culture=neutral, processorArchitecture=MSIL">
74-
<HintPath>..\packages\NAudio.1.10.0\lib\net35\NAudio.dll</HintPath>
73+
<Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
74+
<HintPath>..\packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
7575
</Reference>
76-
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
77-
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
76+
<Reference Include="MQTTnet, Version=3.0.15.0, Culture=neutral, PublicKeyToken=b69712f52770c0a7, processorArchitecture=MSIL">
77+
<HintPath>..\packages\MQTTnet.3.0.15\lib\net461\MQTTnet.dll</HintPath>
78+
</Reference>
79+
<Reference Include="NAudio, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
80+
<HintPath>..\packages\NAudio.2.0.0\lib\netstandard2.0\NAudio.dll</HintPath>
81+
</Reference>
82+
<Reference Include="NAudio.Asio, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
83+
<HintPath>..\packages\NAudio.Asio.2.0.0\lib\netstandard2.0\NAudio.Asio.dll</HintPath>
84+
</Reference>
85+
<Reference Include="NAudio.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
86+
<HintPath>..\packages\NAudio.Core.2.0.0\lib\netstandard2.0\NAudio.Core.dll</HintPath>
87+
</Reference>
88+
<Reference Include="NAudio.Midi, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
89+
<HintPath>..\packages\NAudio.Midi.2.0.0\lib\netstandard2.0\NAudio.Midi.dll</HintPath>
90+
</Reference>
91+
<Reference Include="NAudio.Wasapi, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
92+
<HintPath>..\packages\NAudio.Wasapi.2.0.0\lib\netstandard2.0\NAudio.Wasapi.dll</HintPath>
93+
</Reference>
94+
<Reference Include="NAudio.WinForms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
95+
<HintPath>..\packages\NAudio.WinForms.2.0.0\lib\net472\NAudio.WinForms.dll</HintPath>
96+
</Reference>
97+
<Reference Include="NAudio.WinMM, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
98+
<HintPath>..\packages\NAudio.WinMM.2.0.0\lib\netstandard2.0\NAudio.WinMM.dll</HintPath>
99+
</Reference>
100+
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
101+
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
78102
</Reference>
79103
<Reference Include="PresentationCore" />
80104
<Reference Include="PresentationFramework" />
@@ -100,6 +124,12 @@
100124
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
101125
</Reference>
102126
<Reference Include="System.Runtime.Serialization" />
127+
<Reference Include="System.Security.AccessControl, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
128+
<HintPath>..\packages\System.Security.AccessControl.5.0.0\lib\net461\System.Security.AccessControl.dll</HintPath>
129+
</Reference>
130+
<Reference Include="System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
131+
<HintPath>..\packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
132+
</Reference>
103133
<Reference Include="System.ServiceModel" />
104134
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
105135
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
@@ -131,6 +161,7 @@
131161
<Compile Include="Audio\CachedSoundSampleProvider.cs" />
132162
<Compile Include="ChartCircularBuffer.cs" />
133163
<Compile Include="HWInfo.cs" />
164+
<Compile Include="Mqtt.cs" />
134165
<Compile Include="SplashScreenWindow.xaml.cs">
135166
<DependentUpon>SplashScreenWindow.xaml</DependentUpon>
136167
</Compile>
@@ -165,6 +196,9 @@
165196
<ItemGroup>
166197
<None Include="app.config" />
167198
<None Include="app.manifest" />
199+
<None Include="mqtt.config">
200+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
201+
</None>
168202
<None Include="appsettings.config">
169203
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
170204
</None>

fiphwinfo/packages.config

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.8" targetFramework="net472" />
3+
<package id="Hardcodet.NotifyIcon.Wpf" version="1.1.0" targetFramework="net472" />
44
<package id="HtmlRenderer.Core" version="1.5.0.6" targetFramework="net472" />
55
<package id="HtmlRenderer.WinForms" version="1.5.0.6" targetFramework="net472" />
66
<package id="ini-parser" version="2.5.2" targetFramework="net472" />
77
<package id="log4net" version="2.0.12" targetFramework="net472" />
88
<package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net472" />
99
<package id="Microsoft.Bcl.AsyncInterfaces" version="5.0.0" targetFramework="net472" />
1010
<package id="Microsoft.CSharp" version="4.7.0" targetFramework="net472" />
11-
<package id="NAudio" version="1.10.0" targetFramework="net472" />
12-
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
11+
<package id="Microsoft.Win32.Registry" version="5.0.0" targetFramework="net472" />
12+
<package id="MQTTnet" version="3.0.15" targetFramework="net472" />
13+
<package id="NAudio" version="2.0.0" targetFramework="net472" />
14+
<package id="NAudio.Asio" version="2.0.0" targetFramework="net472" />
15+
<package id="NAudio.Core" version="2.0.0" targetFramework="net472" />
16+
<package id="NAudio.Midi" version="2.0.0" targetFramework="net472" />
17+
<package id="NAudio.Wasapi" version="2.0.0" targetFramework="net472" />
18+
<package id="NAudio.WinForms" version="2.0.0" targetFramework="net472" />
19+
<package id="NAudio.WinMM" version="2.0.0" targetFramework="net472" />
20+
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
1321
<package id="RazorEngine" version="3.10.0" targetFramework="net472" />
1422
<package id="SharpDX" version="4.2.0" targetFramework="net472" />
1523
<package id="SharpDX.DirectInput" version="4.2.0" targetFramework="net472" />
1624
<package id="System.ComponentModel.Annotations" version="5.0.0" targetFramework="net472" />
1725
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net472" />
26+
<package id="System.Security.AccessControl" version="5.0.0" targetFramework="net472" />
27+
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net472" />
1828
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
1929
</packages>

0 commit comments

Comments
 (0)