mqtt prep 6

This commit is contained in:
Jokob-sk
2022-11-27 21:58:29 +11:00
parent 9c81ce9612
commit c790b1ee88

View File

@@ -194,14 +194,6 @@ def main ():
# Upgrade DB if needed # Upgrade DB if needed
upgradeDB() upgradeDB()
# Start MQTT thread if configured
if reportMQTT and mqtt_thread_up == False:
print ('Establishing MQTT thread...')
mqtt_thread_up = True # prevent this code to be run multiple times concurrently
# start_mqtt_thread (connect_mqtt())
# connect_mqtt()
mqtt_start()
# determine run/scan type based on passed time # determine run/scan type based on passed time
if last_internet_IP_scan + timedelta(minutes=3) < time_now: if last_internet_IP_scan + timedelta(minutes=3) < time_now:
cycle = 'internet_IP' cycle = 'internet_IP'
@@ -1626,10 +1618,18 @@ def email_reporting ():
send_pushsafer (mail_text) send_pushsafer (mail_text)
else : else :
print (' Skip PUSHSAFER...') print (' Skip PUSHSAFER...')
# Update MQTT entities
if reportMQTT:
print (' Establishing MQTT thread...')
# mqtt_thread_up = True # prevent this code to be run multiple times concurrently
# start_mqtt_thread ()
mqtt_start()
else :
print (' Skip MQTT...')
else : else :
print (' No changes to report...') print (' No changes to report...')
openDB()
# Clean Pending Alert Events # Clean Pending Alert Events
sql.execute ("""UPDATE Devices SET dev_LastNotification = ? sql.execute ("""UPDATE Devices SET dev_LastNotification = ?
@@ -1904,7 +1904,6 @@ class device_sensor:
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def mqtt_start(): def mqtt_start():
def on_disconnect(client, userdata, rc): def on_disconnect(client, userdata, rc):
global mqtt_connected_to_broker global mqtt_connected_to_broker
mqtt_connected_to_broker = 0 mqtt_connected_to_broker = 0
@@ -1944,7 +1943,7 @@ def mqtt_start():
payload += '"'+column+'": ' + str(row[column]) +',' payload += '"'+column+'": ' + str(row[column]) +','
publish_mqtt(client, "system-sensors/sensor/pialert/state", publish_mqtt(client, "system-sensors/sensor/pialert/state",
'{ \ '{ \
'+ payload[:-1] +'\ '+ payload[:-1] +'\
}' }'
) )
@@ -1993,27 +1992,27 @@ def mqtt_start():
# qos=1, # qos=1,
# retain=True, # retain=True,
# ) # )
time.sleep(10)
client.loop() client.loop()
# #------------------------------------------------------------------------------- # #-------------------------------------------------------------------------------
# def start_mqtt_thread (client): def start_mqtt_thread ():
# # start a MQTT thread loop which will continuously report on devices to the broker # start a MQTT thread loop which will continuously report on devices to the broker
# # daemon=True - makes sure the thread dies with the process if interrupted # daemon=True - makes sure the thread dies with the process if interrupted
# global mqtt_thread_up global mqtt_thread_up
# # flag to check if thread is running # flag to check if thread is running
# mqtt_thread_up = True mqtt_thread_up = True
# print(" Starting MQTT sending") print(" Starting MQTT sending")
# # x = threading.Thread(target=start_sending_mqtt, args=(client,), daemon=True) x = threading.Thread(target=mqtt_start, args=(1,), daemon=True)
# start_sending_mqtt(client) # start_sending_mqtt(client)
# print(" Threading: Starting MQTT thread") print(" Threading: Starting MQTT thread")
# # x.start() x.start()
#=============================================================================== #===============================================================================