diff --git a/docs/Glucosemeter Project Logboek.docx b/docs/Glucosemeter Project Logboek.docx index 83833a8..49e5915 100644 Binary files a/docs/Glucosemeter Project Logboek.docx and b/docs/Glucosemeter Project Logboek.docx differ diff --git a/src/GlucoseCheck.py b/src/GlucoseCheck.py index 957cf56..e291760 100644 --- a/src/GlucoseCheck.py +++ b/src/GlucoseCheck.py @@ -120,17 +120,18 @@ def on_connect(client, userdata, flags, reason_code, properties): #This run Mqtt.subscribe(recieve_topic) if reason_code > 0: # error processing - print(f"Failed to connect to MQTT broker. Error code: {rc}") + print(f"Failed to connect to MQTT broker. Error code: {reason_code}") def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved global last_bolus_time global last_bolus_type args = msg.payload.decode() if not args: + #Empty string return - elif args[0] == "c": + elif args[0] == "c": #Sets compesation var last_bolus_type = 0 - elif args[0] == "e": + elif args[0] == "e": #Sets eating var last_bolus_type = 1 last_bolus_time = time.time() print("Bolussen") diff --git a/src/ServerCheck.py b/src/ServerCheck.py index 105020c..01caa22 100644 --- a/src/ServerCheck.py +++ b/src/ServerCheck.py @@ -137,29 +137,30 @@ def on_connect(client, userdata, flags, reason_code, properties): #This runs o Mqtt.subscribe(recieve_topic) if reason_code > 0: # error processing - print(f"Failed to connect to MQTT broker. Error code: {rc}") + print(f"Failed to connect to MQTT broker. Error code: {reason_code}") -def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved - checkWebServer(webServers) #Update list of websites status - messageStatus = statusCheck(True) #True returns every server and state - user_topic = msg.topic.split("/")[-1] +def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved + checkWebServer(webServers) #Update list of websites status + messageStatus = statusCheck(True) #True returns every server and state + user_topic = msg.topic.split("/")[-1] #Get the last word from subsription "/tele/out/bart" this get "bart" args = msg.payload.decode() - print(user_topic) - keys = list(User.keys()) + keys = list(User.keys()) #Gives list of all keys in User dict if not args: + #Empty string return elif args[0] == "m": - for i in keys: + for i in keys: #This checks if the name in the user_topic is in the User dict and sends the message to them if User[i].name.lower() == user_topic: print(f"right: {user_topic}: {i}") - if User[i].mute > 0: + if User[i].mute > 0: #Removes mute for user User[keys[0]].mute = 0 User[keys[1]].mute = 0 + #User[i].mute = 0 else: - User[i].mute = mute_time//interval_offline_message #This is defined in the setup variables + User[i].mute = mute_time//interval_offline_message #This mutes for a time elif args[0] == "s": print("s") - Mqtt.publish(send_topic + user_topic , messageStatus) + Mqtt.publish(send_topic + user_topic , messageStatus) #Sends a overview of server to user Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Creates a mqqt object Mqtt.on_connect = on_connect #Create callback on connect @@ -175,13 +176,13 @@ def main(): #This is the main loop while True: checkWebServer(webServers) #Update list of websites status messageStatus = statusCheck(False) #False is only on state change - if User[keys[0]].mute == 0 | User[keys[1]].mute == 0: + if User[keys[0]].mute == 0 | User[keys[1]].mute == 0: # If anyone has muted it wont send message Mqtt.publish(send_topic + "broadcast", messageStatus) sleep(30) - if User[keys[0]].mute > 0: + if User[keys[0]].mute > 0: #Counts down mute for User 0 User[keys[0]].mute = User[keys[0]].mute - 1 - if User[keys[1]].mute > 0: + if User[keys[1]].mute > 0: #Counts down mute for User 1 User[keys[1]].mute = User[keys[1]].mute - 1 print(User[keys[0]].mute) print(User[keys[1]].mute) diff --git a/src/TeleBot.py b/src/TeleBot.py index c53b332..99f77e3 100644 --- a/src/TeleBot.py +++ b/src/TeleBot.py @@ -13,12 +13,12 @@ recieve_topic = "telegrambot/out/#" send_topic_server = "telegrambot/in/server/" send_topic_glucose = "telegrambot/in/glucose/" brokercreds = ["hass","Bruk#5"] -x = "AS" +message = " " flag = " " ##### Bot Variabels ##### BotToken = '7690417088:AAGcmBegbLHBDXMBKiE6MEgCp3rnLz5vPCE' -class User: # Custom classes for my dictionaries +class User: # Custom classes for my dictionary def __init__(self, name, lastName, userID, mute): self.name = name self.lastName = lastName @@ -33,14 +33,16 @@ User = { async def subscribe(update: Update, context: ContextTypes.DEFAULT_TYPE): #This command can be run to test setting up a database for users await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.chat.id) #Mqtt.publish(send_topic,1) -async def gluc_command(update: Update, context: CallbackContext): #This command can be run to test setting up a database for users + +async def gluc_command(update: Update, context: CallbackContext): args = ' '.join(context.args) - current_user = User[str(update.effective_chat.id)].name - Mqtt.publish(send_topic_glucose + current_user.lower(), args) + current_user = User[str(update.effective_chat.id)].name #Checks wich user sends the message + Mqtt.publish(send_topic_glucose + current_user.lower(), args) #Sends message with current_user added to the end of topic + async def server_command(update: Update, context: CallbackContext): args = ' '.join(context.args) - current_user = User[str(update.effective_chat.id)].name - Mqtt.publish(send_topic_server + current_user.lower(), args) + current_user = User[str(update.effective_chat.id)].name #Checks wich user sends the message + Mqtt.publish(send_topic_server + current_user.lower(), args) #Sends message with current_user added to the end of topic def on_connect(client, userdata, flags, reason_code, properties): print(f"Connected with result code {reason_code}") @@ -50,39 +52,38 @@ def on_connect(client, userdata, flags, reason_code, properties): Mqtt.subscribe(recieve_topic) if reason_code > 0: # error processing - print(f"Failed to connect to MQTT broker. Error code: {rc}") + print(f"Failed to connect to MQTT broker. Error code: {reason_code}") def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved - global x + global message global flag - print(msg.topic) - x = msg.payload.decode() - flag = msg.topic.split("/")[-1] + message = msg.payload.decode() #Decode message into string + flag = msg.topic.split("/")[-1] #Get the last word from subsription "/tele/out/bart" this get "bart" async def Cat(context: ContextTypes.DEFAULT_TYPE): #This runs evert second to check for if a mqtt message is recieved - global x + global message global flag - keys = list(User.keys()) + keys = list(User.keys()) #Gives list of all keys in User dict print(flag) - if flag == "broadcast": - await context.bot.send_message(chat_id=keys[0], text=x) - await context.bot.send_message(chat_id=keys[1], text=x) + if flag == "broadcast": #If true it sends the message to everyone + await context.bot.send_message(chat_id=keys[0], text=message) + await context.bot.send_message(chat_id=keys[1], text=message) flag = " " else: - for i in keys: + for i in keys: #This checks if the name in the flag is in the User dict and sends the message to them if User[i].name.lower() == flag: - #await context.bot.send_message(chat_id=keys[0], text=x) - await context.bot.send_message(chat_id=i, text=x) + #await context.bot.send_message(chat_id=keys[0], text=message) + await context.bot.send_message(chat_id=i, text=message) flag = " " flag = " " -Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) -Mqtt.on_connect = on_connect -Mqtt.on_message = on_message +Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Create Mqtt object +Mqtt.on_connect = on_connect #Create callback on connect +Mqtt.on_message = on_message #Create callback on message -Mqtt.username_pw_set(brokercreds[0], password=brokercreds[1]) -Mqtt.connect(broker_address, broker_port, 60) -Mqtt.loop_start() +Mqtt.username_pw_set(brokercreds[0], password=brokercreds[1]) #Set User and Password +Mqtt.connect(broker_address, broker_port, 60) #Set Connection settings +Mqtt.loop_start() #Start Mqtt def main():#This is the main loop application = ApplicationBuilder().token(BotToken).build()