diff --git a/src/GlucoseCheck.py b/src/GlucoseCheck.py index 8f757f1..4fac405 100644 --- a/src/GlucoseCheck.py +++ b/src/GlucoseCheck.py @@ -10,7 +10,7 @@ from collections import deque broker_address = "192.168.1.160" broker_port = 1883 recieve_topic = "telegrambot/in/glucose/gj" -send_topic = "telegrambot/out/gj" +telegram_topic = "telegrambot/out/gj" brokercreds = ["hass","Bruk#5"] ##### Variables ##### @@ -58,6 +58,8 @@ GLUC_SLOPE_PERIOD = 30 # in minutes GLUC_BUFFER_LENGTH = round(GLUC_SLOPE_PERIOD/GLUC_READOUT_PERIOD) gluc_buffer = deque([6.0] * GLUC_BUFFER_LENGTH, maxlen=GLUC_BUFFER_LENGTH) # The gluc_buffer always contains only the last xx elements. Initialized on 6.0 +glucose_alert_is_active = False + ##### Main ##### def glucose_value(data_glucose): #Inputs json string and unbinds glucose value @@ -69,6 +71,7 @@ def glucose_value(data_glucose): #Inputs json string and unbinds glucose valu def main(url_main, headers_main): global last_bolus_time global last_bolus_type + global glucose_alert_is_active while True: try: # Send the Get request @@ -80,6 +83,8 @@ def main(url_main, headers_main): gluc_value = glucose_value(data) print(f"current level: {gluc_value}") + Mqtt.publish("glucose/glucose_level", f"{gluc_value}") # inform Home Assistant of new glucose level + # let's determine the slope of the last 30 minutes gluc_buffer.append(gluc_value) old_value = list(gluc_buffer)[0] @@ -100,7 +105,14 @@ def main(url_main, headers_main): if ((gluc_value > gluc_upper_limit) or (gluc_slope > limits_table[last_bolus_type][int(hours_since_last_bolus)][1])): print("Hey joh check je glucosepeil !") - Mqtt.publish(send_topic, f"Hey joh check peil ! (lastbolus -{round(hours_since_last_bolus, 1)})") + Mqtt.publish(telegram_topic, f"Hey joh check peil ! (lastbolus -{round(hours_since_last_bolus, 1)})") + if not glucose_alert_is_active: + Mqtt.publish("glucose/glucose_alarm_state", "1") # we just entered alarm state + glucose_alert_is_active = True + else: + if glucose_alert_is_active: + Mqtt.publish("glucose/glucose_alarm_state", "0") # we exit alarm state + glucose_alert_is_active = False else: # Request has failed print(f"❌ Error: Could not connect to server: {response.status_code}") @@ -135,7 +147,7 @@ def on_message(client, userdata, msg): #This runs everytime a mqtt message last_bolus_time = time.time() - 3600 * float(args_list[1]) last_bolus_type = 0 except Exception as e: - Mqtt.publish(send_topic, f"Error: {e}") + Mqtt.publish(telegram_topic, f"Error: {e}") else: last_bolus_type = 0 last_bolus_time = time.time() @@ -145,7 +157,7 @@ def on_message(client, userdata, msg): #This runs everytime a mqtt message last_bolus_time = time.time() - 3600 * float(args_list[1]) last_bolus_type = 1 except Exception as e: - Mqtt.publish(send_topic, f"Error: {e}") + Mqtt.publish(telegram_topic, f"Error: {e}") else: last_bolus_type = 1 last_bolus_time = time.time() @@ -161,6 +173,7 @@ Mqtt.connect(broker_address, broker_port, 60) #Set Connection Mqtt.loop_start() #Start connecting -Mqtt.publish(send_topic, "Started Glucose Checker :-)") +Mqtt.publish(telegram_topic, "Started Glucose Checker :-)") +Mqtt.publish("glucose/glucose_alarm_state", "0") if __name__ == "__main__": main(url, headers)