-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMQTT_Sensors.hgx
296 lines (255 loc) · 8.73 KB
/
MQTT_Sensors.hgx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?xml version="1.0" encoding="utf-16"?>
<ProgramBlock xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ScriptSetup>IPHostEntry host;
string localIP = "1";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ip.ToString();
localIP = localIP.Substring(localIP.LastIndexOf(".") + 1);
}
}
Program.Setup(() => {
Program
.AddOption("ClientId", "hg-" + localIP, "0. Enter unique ID for this client", "text")
.AddOption("ServerAddress", "", "1. MQTT server address", "text")
.AddOption("ServerPort", "1883", "2. MQTT server port", "text")
.AddOption("TLS", "", "3. Connect using TLS/SSL", "checkbox")
.AddOption("WebSockets", "", "4. Connect using WebSockets", "checkbox")
.AddOption("ServerTopic", "#", "5. Topic", "text")
.AddOption("Username", "", "6. Username (optional)", "text")
.AddOption("Password", "", "7. Password (optional)", "text")
.AddOption("LogEnable", "", "8. Enable log file", "checkbox")
.AddOption("LogFilePath", "/home/pi/log/Sensors.log", "9. Log file location", "text")
.AddFeature("", "", "MQTT.SensorPublish", "Publish this module events", "checkbox");
});
Program.Run();
</ScriptSetup>
<ScriptSource>string server = Program.Option("ServerAddress").Value.Trim();
int port = 1883; int.TryParse(Program.Option("ServerPort").Value, out port);
string topic = Program.Option("ServerTopic").Value.Trim();
string clientid = Program.Option("ClientId").Value.Trim();
string username = Program.Option("Username").Value.Trim();
string password = Program.Option("Password").Value.Trim();
string LogFilePath = Program.Option("LogFilePath").Value.Trim();
bool LogEnabled = Program.Option("LogEnable").Value == "On" ? true : false;
bool useWebSockets = Program.Option("WebSockets").Value == "On" ? true : false;
bool useTls = Program.Option("TLS").Value == "On" ? true : false;
var MqttIsConnected = false;
MqttClient.Reset();
Action<string, string>
Log = (string LogPath, string logtext) => {
if (LogEnabled)
{
string text = DateTime.Now.ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss.fffffff") + " ; " + logtext + "\n";
System.IO.File.AppendAllText(LogPath, text);
}
};
Action<string, string, double>
WriteToSensor = (string sensorId, string dataType, double dataVal) => {
// sensorId = module
// dataType = parameter name
// dataVal = parameter value
var module = Modules.InDomain("MQTT:Sensors").WithAddress(sensorId).Get();
if (module.Instance == null)
{
Program.AddVirtualModule("MQTT:Sensors", sensorId, "Sensor", "homegenie/generic/sensor");
module = Modules.InDomain("MQTT:Sensors").WithAddress(sensorId).Get();
}
// if(module.Parameter(dataType).Value != dataVal.ToString("0.0")){
module.Parameter(dataType).Value = dataVal.ToString("0.0");
//Program.RaiseEvent(module, dataType, dataVal.ToString("0.0"), "");
// }
};
if (server == "")
{
Program.Notify("Please configure server address");
Pause(5);
return;
}
MqttClient.Subscribe(topic, (mtopic, bytespayload) => {
var parts = mtopic.Split('/');
// convert binary message to string
string mpayload = Encoding.UTF8.GetString(bytespayload);
switch(parts[0])
{
case "rtl_433":
switch(parts[1])
{
case "Acurite-5n1":
var item = mpayload.Split(' ');
string dataType = "";
double dataVal = 0;
switch (parts[3])
{
case "temperature_F":
dataType = "Sensor.Temperature";
dataVal = (5.0 / 9.0) * (double.Parse(item[0]) - 32);
WriteToSensor(parts[1],dataType,dataVal);
break;
case "wind_dir_deg":
dataType = "Sensor.Winddir";
dataVal = float.Parse(item[0]);
WriteToSensor(parts[1],dataType,dataVal);
break;
case "wind_avg_km_h":
dataType = "Sensor.Windspeed";
dataVal = float.Parse(item[0]);
WriteToSensor(parts[1],dataType,dataVal);
break;
case "humidity":
dataType = "Sensor.Humidity";
dataVal = float.Parse(item[0]);
WriteToSensor(parts[1],dataType,dataVal);
break;
case "rain_in":
dataType = "Sensor.Rainfall";
dataVal = float.Parse(item[0]);
WriteToSensor(parts[1],dataType,dataVal);
break;
case "battery_ok":
dataType = "Status.Battery";
dataVal = float.Parse(item[0])*100;
WriteToSensor(parts[1],dataType,dataVal);
break;
}
break;
default:
break;
}
break;
case "GunSafe":
case "GunSafe1":
case "GunSafe2":
case "GunSafe3":
parts = mtopic.Split('/');
// convert binary message to string
mpayload = Encoding.UTF8.GetString(bytespayload);
foreach (var data in mpayload.Split(','))
{
if (data.Length > 0)
{
string dataType = "";
double dataVal = 0;
switch (parts[1])
{
case "temperature":
dataType = "Sensor.Temperature";
dataVal = double.Parse(mpayload);
//Log(LogFilePath, mtopic + " " + dataType + " " + dataVal.ToString());
break;
case "humidity":
dataType = "Sensor.Humidity";
dataVal = float.Parse(mpayload);
//Log(LogFilePath, mtopic + " " + dataType + " " + dataVal.ToString());
break;
}
WriteToSensor(parts[0],dataType,dataVal);
}
}
break;
/*
case "blueline":
foreach (var data in mpayload.Split(','))
{
var item = data.Split('=');
switch (item[0])
{
case "TempF":
dataType = "Sensor.Temperature";
dataVal = (5.0 / 9.0) * (double.Parse(item[1]) - 32);
break;
case "CurrentPower":
dataType = "Meter.Watts";
dataVal = float.Parse(item[1]);
break;
case "TotalEnergy":
dataType = "Sensor.WattHours";
dataVal = float.Parse(item[1]);
break;
case "Battery":
dataType = "Status.Battery";
dataVal = float.Parse(item[1])*100;
break;
}
WriteToSensor(mtopic,dataType,dataVal);
}
break;
case "acurite592tx":
foreach (var data in mpayload.Split(','))
{
if (data.Length > 0)
{
var item = data.Split('=');
var channel = item[0].Substring(item[0].Length-1);
switch (item[0].Substring(0,item[0].Length-1))
{
case "Temp":
dataType = "Sensor.Temperature";
dataVal = (5.0 / 9.0) * (double.Parse(item[1]) - 32);
break;
case "Battery":
dataType = "Status.Battery";
dataVal = float.Parse(item[1])*100;
break;
}
WriteToSensor(mtopic+channel,dataType,dataVal);
}
}
break;
*/
default:
Program.Notify("MQTT message: default", mtopic);
break;
}
});
//Program.GoBackground();
while (Program.IsRunning) {
if (!MqttIsConnected) {
MqttIsConnected = true;
Program.Notify("Connecting to '" + server + ":" + port + "' ...");
try
{
if (username != "")
{
MqttClient.WithCredentials(username, password);
}
MqttClient
.Service(server)
.UsingWebSockets(useWebSockets)
.WithTls(useTls)
.Connect(port, clientid, (connected)=>{
MqttIsConnected = connected;
if (connected) {
Program.Notify("Connected!");
} else {
Program.Notify("Disconnected!");
}
});
}
catch (Exception e)
{
Program.Notify(e.Message);
Pause(5);
return;
}
}
Pause(30);
}
</ScriptSource>
<ScriptErrors />
<Domain>HomeAutomation.HomeGenie.Automation</Domain>
<Address>1001</Address>
<Name>MQTT Sensors</Name>
<Description>Share modules with other HomeGenie servers connected to the same MQTT network.
Other servers will automatically inherit the published module once an event is generated from it.</Description>
<Group>NEW</Group>
<Features />
<AutoRestartEnabled>false</AutoRestartEnabled>
<ActivationTime xsi:nil="true" />
<TriggerTime xsi:nil="true" />
<Type>CSharp</Type>
<IsEnabled>false</IsEnabled>
</ProgramBlock>