Compare commits
2 Commits
2e2687d3f7
...
9f477a61de
| Author | SHA1 | Date |
|---|---|---|
|
|
9f477a61de | |
|
|
9a715056f1 |
|
|
@ -1,8 +1,11 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
import gi
|
import gi
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
|
import re
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
|
@ -19,6 +22,7 @@ oldtmpfile = ''
|
||||||
oldalertcount = 0
|
oldalertcount = 0
|
||||||
menu = Gtk.Menu()
|
menu = Gtk.Menu()
|
||||||
|
|
||||||
|
#fp = open("/home/agc/local/bin/mqtt-systray.yaml")
|
||||||
fp = open("mqtt-systray.yaml")
|
fp = open("mqtt-systray.yaml")
|
||||||
conf = yaml.safe_load(fp)
|
conf = yaml.safe_load(fp)
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
@ -65,18 +69,18 @@ def get_alert_text_warning(devicea, sensora):
|
||||||
alert_text = ''
|
alert_text = ''
|
||||||
if 'value' in conf['devices'][devicea]['values'][sensora]:
|
if 'value' in conf['devices'][devicea]['values'][sensora]:
|
||||||
if 'max_warning' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] > conf['devices'][devicea]['values'][sensora]['max_warning']:
|
if 'max_warning' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] > conf['devices'][devicea]['values'][sensora]['max_warning']:
|
||||||
alert_text = '!!high!! '
|
alert_text = '!!high!!'
|
||||||
elif 'min_warning' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] < conf['devices'][devicea]['values'][sensora]['min_warning']:
|
elif 'min_warning' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] < conf['devices'][devicea]['values'][sensora]['min_warning']:
|
||||||
alert_text = '!!low!! '
|
alert_text = '!!low!!'
|
||||||
return alert_text
|
return alert_text
|
||||||
|
|
||||||
def get_alert_text_critical(devicea, sensora):
|
def get_alert_text_critical(devicea, sensora):
|
||||||
alert_text = ''
|
alert_text = ''
|
||||||
if 'value' in conf['devices'][devicea]['values'][sensora]:
|
if 'value' in conf['devices'][devicea]['values'][sensora]:
|
||||||
if 'max_critical' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] > conf['devices'][devicea]['values'][sensora]['max_critical']:
|
if 'max_critical' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] > conf['devices'][devicea]['values'][sensora]['max_critical']:
|
||||||
alert_text = '!!HIGH!! '
|
alert_text = '!!HIGH!!'
|
||||||
elif 'min_critical' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] < conf['devices'][devicea]['values'][sensora]['min_critical']:
|
elif 'min_critical' in conf['devices'][devicea]['values'][sensora] and conf['devices'][devicea]['values'][sensora]['value'] < conf['devices'][devicea]['values'][sensora]['min_critical']:
|
||||||
alert_text = '!!LOW!! '
|
alert_text = '!!LOW!!'
|
||||||
return alert_text
|
return alert_text
|
||||||
|
|
||||||
def get_alert_text(devicea, sensora):
|
def get_alert_text(devicea, sensora):
|
||||||
|
|
@ -141,13 +145,16 @@ def build_menu():
|
||||||
menu = Gtk.Menu()
|
menu = Gtk.Menu()
|
||||||
for device1 in conf['devices']:
|
for device1 in conf['devices']:
|
||||||
devname = conf['devices'][device1]['name']
|
devname = conf['devices'][device1]['name']
|
||||||
conf['devices'][device1]['menuitem'] = Gtk.MenuItem(label=devname)
|
#conf['devices'][device1]['menuitem'] = Gtk.MenuItem(label=devname)
|
||||||
submenu = Gtk.Menu()
|
#submenu = Gtk.Menu()
|
||||||
|
#for sensor in conf['devices'][device1]['values'].keys():
|
||||||
|
# conf['devices'][device1]['values'][sensor]['menuitem'] = Gtk.MenuItem(label=conf['devices'][device1]['values'][sensor]['name'])
|
||||||
|
# submenu.append(conf['devices'][device1]['values'][sensor]['menuitem'])
|
||||||
|
#conf['devices'][device1]['menuitem'].set_submenu(submenu)
|
||||||
|
#menu.append(conf['devices'][device1]['menuitem'])
|
||||||
for sensor in conf['devices'][device1]['values'].keys():
|
for sensor in conf['devices'][device1]['values'].keys():
|
||||||
conf['devices'][device1]['values'][sensor]['menuitem'] = Gtk.MenuItem(label=conf['devices'][device1]['values'][sensor]['name'])
|
conf['devices'][device1]['values'][sensor]['menuitem'] = Gtk.MenuItem(label=devname+' - '+conf['devices'][device1]['values'][sensor]['name'])
|
||||||
submenu.append(conf['devices'][device1]['values'][sensor]['menuitem'])
|
menu.append(conf['devices'][device1]['values'][sensor]['menuitem'])
|
||||||
conf['devices'][device1]['menuitem'].set_submenu(submenu)
|
|
||||||
menu.append(conf['devices'][device1]['menuitem'])
|
|
||||||
|
|
||||||
item_dashboard = Gtk.MenuItem(label='Open Dashboard')
|
item_dashboard = Gtk.MenuItem(label='Open Dashboard')
|
||||||
item_dashboard.connect('activate', gtkdashboard)
|
item_dashboard.connect('activate', gtkdashboard)
|
||||||
|
|
@ -197,19 +204,29 @@ def build_menu():
|
||||||
def update_menu():
|
def update_menu():
|
||||||
appindicator.set_menu(build_menu())
|
appindicator.set_menu(build_menu())
|
||||||
|
|
||||||
|
def search_topic(devices):
|
||||||
|
for device in conf['devices'].keys():
|
||||||
|
if device.find('+') > 0:
|
||||||
|
re_topic = device.replace('+', '.+')
|
||||||
|
if(len(re.findall(re_topic, devices)) > 0):
|
||||||
|
return device
|
||||||
|
return None
|
||||||
|
|
||||||
def set_value(topic, value):
|
def set_value(topic, value):
|
||||||
global oldalertcount
|
global oldalertcount
|
||||||
topicds = topic.rsplit("/", 1)
|
topicds = topic.rsplit("/", 1)
|
||||||
devices=topicds[0]
|
devices=topicds[0]
|
||||||
devices = devices.removeprefix(mqttprefix+'/')
|
devices = devices.removeprefix(mqttprefix+'/')
|
||||||
conf['devices'][devices]['values'][topicds[1]]['value'] = float(value)
|
ctopic = topicds[1]
|
||||||
|
if search_topic(devices): devices=search_topic(devices)
|
||||||
|
conf['devices'][devices]['values'][ctopic]['value'] = float(value)
|
||||||
#update_menu()
|
#update_menu()
|
||||||
|
|
||||||
alert_text = get_alert_text(devices, topicds[1])
|
alert_text = get_alert_text(devices, ctopic)
|
||||||
text='{}{}: {} {}'.format(alert_text, conf['devices'][devices]['values'][topicds[1]]['name'], conf['devices'][devices]['values'][topicds[1]]['value'], conf['devices'][devices]['values'][topicds[1]]['unit'])
|
text='{} - {}: {} {} {}'.format(conf['devices'][devices]['name'], conf['devices'][devices]['values'][topicds[1]]['name'], round(conf['devices'][devices]['values'][topicds[1]]['value'], 2), conf['devices'][devices]['values'][topicds[1]]['unit'], alert_text)
|
||||||
conf['devices'][devices]['values'][topicds[1]]['menuitem'].set_label(text)
|
conf['devices'][devices]['values'][ctopic]['menuitem'].set_label(text)
|
||||||
#conf['devices'][devices]['values'][topicds[1]]['menuitem'].set_label("{}".format(conf['devices'][devices]['values'][topicds[1]]['value']))
|
#conf['devices'][devices]['values'][ctopic]['menuitem'].set_label("{}".format(conf['devices'][devices]['values'][topicds[1]]['value']))
|
||||||
devname = conf['devices'][devices]['name']
|
#devname = conf['devices'][devices]['name']
|
||||||
#print(get_alert(devices))
|
#print(get_alert(devices))
|
||||||
#if get_alert(devices): devname = "! " + devname
|
#if get_alert(devices): devname = "! " + devname
|
||||||
#if get_alert(devices): devname = "!!ALERT!! " + devname
|
#if get_alert(devices): devname = "!!ALERT!! " + devname
|
||||||
|
|
@ -218,7 +235,7 @@ def set_value(topic, value):
|
||||||
if oldalertcount != (alert_count()):
|
if oldalertcount != (alert_count()):
|
||||||
oldalertcount = alert_count()
|
oldalertcount = alert_count()
|
||||||
appindicator.set_icon_full(get_icon(), "mqtt-icon")
|
appindicator.set_icon_full(get_icon(), "mqtt-icon")
|
||||||
os.system("notify-send '{}' '{}'".format(conf['devices'][devices]['name'], text))
|
# os.system("notify-send '{}' '{}'".format(conf['devices'][devices]['name'], text))
|
||||||
|
|
||||||
def subscribe_mqtt(mqttc):
|
def subscribe_mqtt(mqttc):
|
||||||
for device in conf['devices']:
|
for device in conf['devices']:
|
||||||
|
|
@ -231,10 +248,11 @@ def subscribe_mqtt(mqttc):
|
||||||
|
|
||||||
def on_connect(mqttc, obj, flags, rc):
|
def on_connect(mqttc, obj, flags, rc):
|
||||||
print("rc: " + str(rc))
|
print("rc: " + str(rc))
|
||||||
|
subscribe_mqtt(mqttc)
|
||||||
#def on_subscribe(mqttc, obj, mid, granted_qos):
|
#def on_subscribe(mqttc, obj, mid, granted_qos):
|
||||||
# print("Subscribed: " + str(mid) + " " + str(granted_qos))
|
# print("Subscribed: " + str(mid) + " " + str(granted_qos))
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
print(msg.topic+" "+str(msg.payload))
|
#print(msg.topic+" "+str(msg.payload))
|
||||||
set_value(msg.topic, msg.payload)
|
set_value(msg.topic, msg.payload)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -245,7 +263,7 @@ appindicator.set_title('MQTT Alert')
|
||||||
appindicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
|
appindicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
|
||||||
appindicator.set_menu(build_menu())
|
appindicator.set_menu(build_menu())
|
||||||
|
|
||||||
set_value("model1/1/1/temperature1C", "25.1")
|
#set_value("model1/1/1/temperature1C", "25.1")
|
||||||
#set_value("model2/2/2/humidity2", "75")
|
#set_value("model2/2/2/humidity2", "75")
|
||||||
#print(conf)
|
#print(conf)
|
||||||
|
|
||||||
|
|
@ -264,6 +282,4 @@ while connected == False:
|
||||||
connected=True
|
connected=True
|
||||||
mqttc.loop_start()
|
mqttc.loop_start()
|
||||||
|
|
||||||
subscribe_mqtt(mqttc)
|
|
||||||
|
|
||||||
Gtk.main()
|
Gtk.main()
|
||||||
|
|
|
||||||
24
src/main.ino
24
src/main.ino
|
|
@ -77,14 +77,19 @@ JsonDocument analyzeTH_type1(JsonArray payloadrh) {
|
||||||
|
|
||||||
JsonDocument analyzeTH_type2(JsonArray payloadrh) {
|
JsonDocument analyzeTH_type2(JsonArray payloadrh) {
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
int16_t payloadtemp = payloadrh[2].as<unsigned char>();
|
uint8_t errcode = payloadrh[1].as<unsigned char>();
|
||||||
payloadtemp += payloadrh[1].as<unsigned char>() << 8;
|
doc["errcode"] = errcode;
|
||||||
doc["temperature_C"] = payloadtemp / 100.0;
|
if(errcode == 0) {
|
||||||
uint8_t payloadhumidity = payloadrh[3].as<unsigned char>();
|
int16_t payloadtemp = payloadrh[3].as<unsigned char>();
|
||||||
doc["humidity"] = payloadhumidity;
|
payloadtemp += payloadrh[2].as<unsigned char>() << 8;
|
||||||
|
doc["temperature_C"] = payloadtemp / 100.0;
|
||||||
|
uint8_t payloadhumidity = payloadrh[4].as<unsigned char>();
|
||||||
|
doc["humidity"] = payloadhumidity;
|
||||||
|
}
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void logJson(JsonDocument jsondata) {
|
void logJson(JsonDocument jsondata) {
|
||||||
if(jsondata["model"].is<String>())
|
if(jsondata["model"].is<String>())
|
||||||
{
|
{
|
||||||
|
|
@ -107,11 +112,14 @@ void logJson(JsonDocument jsondata) {
|
||||||
jsondata["battery_V"] = adoc["battery_V"];
|
jsondata["battery_V"] = adoc["battery_V"];
|
||||||
jsondata["battery_percent"] = adoc["battery_percent"];
|
jsondata["battery_percent"] = adoc["battery_percent"];
|
||||||
jsondata["temperature_C"] = adoc["temperature_C"];
|
jsondata["temperature_C"] = adoc["temperature_C"];
|
||||||
} else if(jsondata["len"].as<int>() == 4 && ((payloadrh[0].as<unsigned char>() & 0b00100000 ) > 0)) {
|
} else if(jsondata["len"].as<int>() == 5 && ((payloadrh[0].as<unsigned char>() & 0b00100000 ) > 0)) {
|
||||||
treatrh = true;
|
treatrh = true;
|
||||||
JsonDocument adoc = analyzeTH_type2(payloadrh);
|
JsonDocument adoc = analyzeTH_type2(payloadrh);
|
||||||
jsondata["temperature_C"] = adoc["temperature_C"];
|
jsondata["errcode"] = adoc["errcode"];
|
||||||
jsondata["humidity"] = adoc["humidity"];
|
if(jsondata["errcode"] == 0) {
|
||||||
|
jsondata["temperature_C"] = adoc["temperature_C"];
|
||||||
|
jsondata["humidity"] = adoc["humidity"];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(treatrh == true) {
|
if(treatrh == true) {
|
||||||
jsondata.remove("from");
|
jsondata.remove("from");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue