Added Comments
heb comments toegevoegt aan de script en ook de docs bijgewerkt.
This commit is contained in:
parent
54b2f34c22
commit
02b4061c91
4 changed files with 46 additions and 43 deletions
Binary file not shown.
|
|
@ -120,17 +120,18 @@ def on_connect(client, userdata, flags, reason_code, properties): #This run
|
||||||
Mqtt.subscribe(recieve_topic)
|
Mqtt.subscribe(recieve_topic)
|
||||||
if reason_code > 0:
|
if reason_code > 0:
|
||||||
# error processing
|
# 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
|
def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved
|
||||||
global last_bolus_time
|
global last_bolus_time
|
||||||
global last_bolus_type
|
global last_bolus_type
|
||||||
args = msg.payload.decode()
|
args = msg.payload.decode()
|
||||||
if not args:
|
if not args:
|
||||||
|
#Empty string
|
||||||
return
|
return
|
||||||
elif args[0] == "c":
|
elif args[0] == "c": #Sets compesation var
|
||||||
last_bolus_type = 0
|
last_bolus_type = 0
|
||||||
elif args[0] == "e":
|
elif args[0] == "e": #Sets eating var
|
||||||
last_bolus_type = 1
|
last_bolus_type = 1
|
||||||
last_bolus_time = time.time()
|
last_bolus_time = time.time()
|
||||||
print("Bolussen")
|
print("Bolussen")
|
||||||
|
|
|
||||||
|
|
@ -137,29 +137,30 @@ def on_connect(client, userdata, flags, reason_code, properties): #This runs o
|
||||||
Mqtt.subscribe(recieve_topic)
|
Mqtt.subscribe(recieve_topic)
|
||||||
if reason_code > 0:
|
if reason_code > 0:
|
||||||
# error processing
|
# 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
|
def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved
|
||||||
checkWebServer(webServers) #Update list of websites status
|
checkWebServer(webServers) #Update list of websites status
|
||||||
messageStatus = statusCheck(True) #True returns every server and state
|
messageStatus = statusCheck(True) #True returns every server and state
|
||||||
user_topic = msg.topic.split("/")[-1]
|
user_topic = msg.topic.split("/")[-1] #Get the last word from subsription "/tele/out/bart" this get "bart"
|
||||||
args = msg.payload.decode()
|
args = msg.payload.decode()
|
||||||
print(user_topic)
|
keys = list(User.keys()) #Gives list of all keys in User dict
|
||||||
keys = list(User.keys())
|
|
||||||
if not args:
|
if not args:
|
||||||
|
#Empty string
|
||||||
return
|
return
|
||||||
elif args[0] == "m":
|
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:
|
if User[i].name.lower() == user_topic:
|
||||||
print(f"right: {user_topic}: {i}")
|
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[0]].mute = 0
|
||||||
User[keys[1]].mute = 0
|
User[keys[1]].mute = 0
|
||||||
|
#User[i].mute = 0
|
||||||
else:
|
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":
|
elif args[0] == "s":
|
||||||
print("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 = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Creates a mqqt object
|
||||||
Mqtt.on_connect = on_connect #Create callback on connect
|
Mqtt.on_connect = on_connect #Create callback on connect
|
||||||
|
|
@ -175,13 +176,13 @@ def main(): #This is the main loop
|
||||||
while True:
|
while True:
|
||||||
checkWebServer(webServers) #Update list of websites status
|
checkWebServer(webServers) #Update list of websites status
|
||||||
messageStatus = statusCheck(False) #False is only on state change
|
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)
|
Mqtt.publish(send_topic + "broadcast", messageStatus)
|
||||||
sleep(30)
|
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
|
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
|
User[keys[1]].mute = User[keys[1]].mute - 1
|
||||||
print(User[keys[0]].mute)
|
print(User[keys[0]].mute)
|
||||||
print(User[keys[1]].mute)
|
print(User[keys[1]].mute)
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,12 @@ recieve_topic = "telegrambot/out/#"
|
||||||
send_topic_server = "telegrambot/in/server/"
|
send_topic_server = "telegrambot/in/server/"
|
||||||
send_topic_glucose = "telegrambot/in/glucose/"
|
send_topic_glucose = "telegrambot/in/glucose/"
|
||||||
brokercreds = ["hass","Bruk#5"]
|
brokercreds = ["hass","Bruk#5"]
|
||||||
x = "AS"
|
message = " "
|
||||||
flag = " "
|
flag = " "
|
||||||
##### Bot Variabels #####
|
##### Bot Variabels #####
|
||||||
BotToken = '7690417088:AAGcmBegbLHBDXMBKiE6MEgCp3rnLz5vPCE'
|
BotToken = '7690417088:AAGcmBegbLHBDXMBKiE6MEgCp3rnLz5vPCE'
|
||||||
|
|
||||||
class User: # Custom classes for my dictionaries
|
class User: # Custom classes for my dictionary
|
||||||
def __init__(self, name, lastName, userID, mute):
|
def __init__(self, name, lastName, userID, mute):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.lastName = lastName
|
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
|
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)
|
await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.chat.id)
|
||||||
#Mqtt.publish(send_topic,1)
|
#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)
|
args = ' '.join(context.args)
|
||||||
current_user = User[str(update.effective_chat.id)].name
|
current_user = User[str(update.effective_chat.id)].name #Checks wich user sends the message
|
||||||
Mqtt.publish(send_topic_glucose + current_user.lower(), args)
|
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):
|
async def server_command(update: Update, context: CallbackContext):
|
||||||
args = ' '.join(context.args)
|
args = ' '.join(context.args)
|
||||||
current_user = User[str(update.effective_chat.id)].name
|
current_user = User[str(update.effective_chat.id)].name #Checks wich user sends the message
|
||||||
Mqtt.publish(send_topic_server + current_user.lower(), args)
|
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):
|
def on_connect(client, userdata, flags, reason_code, properties):
|
||||||
print(f"Connected with result code {reason_code}")
|
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)
|
Mqtt.subscribe(recieve_topic)
|
||||||
if reason_code > 0:
|
if reason_code > 0:
|
||||||
# error processing
|
# 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
|
def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved
|
||||||
global x
|
global message
|
||||||
global flag
|
global flag
|
||||||
print(msg.topic)
|
message = msg.payload.decode() #Decode message into string
|
||||||
x = msg.payload.decode()
|
flag = msg.topic.split("/")[-1] #Get the last word from subsription "/tele/out/bart" this get "bart"
|
||||||
flag = msg.topic.split("/")[-1]
|
|
||||||
|
|
||||||
async def Cat(context: ContextTypes.DEFAULT_TYPE): #This runs evert second to check for if a mqtt message is recieved
|
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
|
global flag
|
||||||
keys = list(User.keys())
|
keys = list(User.keys()) #Gives list of all keys in User dict
|
||||||
print(flag)
|
print(flag)
|
||||||
if flag == "broadcast":
|
if flag == "broadcast": #If true it sends the message to everyone
|
||||||
await context.bot.send_message(chat_id=keys[0], text=x)
|
await context.bot.send_message(chat_id=keys[0], text=message)
|
||||||
await context.bot.send_message(chat_id=keys[1], text=x)
|
await context.bot.send_message(chat_id=keys[1], text=message)
|
||||||
flag = " "
|
flag = " "
|
||||||
else:
|
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:
|
if User[i].name.lower() == flag:
|
||||||
#await context.bot.send_message(chat_id=keys[0], text=x)
|
#await context.bot.send_message(chat_id=keys[0], text=message)
|
||||||
await context.bot.send_message(chat_id=i, text=x)
|
await context.bot.send_message(chat_id=i, text=message)
|
||||||
flag = " "
|
flag = " "
|
||||||
flag = " "
|
flag = " "
|
||||||
|
|
||||||
Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Create Mqtt object
|
||||||
Mqtt.on_connect = on_connect
|
Mqtt.on_connect = on_connect #Create callback on connect
|
||||||
Mqtt.on_message = on_message
|
Mqtt.on_message = on_message #Create callback on message
|
||||||
|
|
||||||
Mqtt.username_pw_set(brokercreds[0], password=brokercreds[1])
|
Mqtt.username_pw_set(brokercreds[0], password=brokercreds[1]) #Set User and Password
|
||||||
Mqtt.connect(broker_address, broker_port, 60)
|
Mqtt.connect(broker_address, broker_port, 60) #Set Connection settings
|
||||||
Mqtt.loop_start()
|
Mqtt.loop_start() #Start Mqtt
|
||||||
|
|
||||||
def main():#This is the main loop
|
def main():#This is the main loop
|
||||||
application = ApplicationBuilder().token(BotToken).build()
|
application = ApplicationBuilder().token(BotToken).build()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue