Added saperate sending and receiving
Je kan nu de server check muten. en ik heb een framework gemaakt voor het versturen naar verschilende personen.
This commit is contained in:
parent
094cb63d2e
commit
608a2c95d2
5 changed files with 75 additions and 23 deletions
1
docs/.~lock.Glucosemeter Project Logboek.docx#
Normal file
1
docs/.~lock.Glucosemeter Project Logboek.docx#
Normal file
|
|
@ -0,0 +1 @@
|
|||
,bart,bart-T420,28.02.2025 12:04,file:///home/bart/.config/libreoffice/4;
|
||||
Binary file not shown.
|
|
@ -7,8 +7,8 @@ from collections import deque
|
|||
##### Mqtt vars #####
|
||||
broker_address = "192.168.1.250"
|
||||
broker_port = 1883
|
||||
recieve_topic = "telegrambot/in"
|
||||
send_topic = "telegrambot/out"
|
||||
recieve_topic = "telegrambot/in/glucose/gj"
|
||||
send_topic = "telegrambot/out/gj"
|
||||
brokercreds = ["hass","Bruk#5"]
|
||||
|
||||
##### Variables #####
|
||||
|
|
@ -145,5 +145,6 @@ Mqtt.connect(broker_address, broker_port, 60) #Set Connection
|
|||
|
||||
Mqtt.loop_start() #Start connecting
|
||||
|
||||
Mqtt.publish(send_topic, "Started Glucose Checker :-)")
|
||||
if __name__ == "__main__":
|
||||
main(url, headers)
|
||||
|
|
|
|||
|
|
@ -8,22 +8,34 @@ from time import sleep
|
|||
##### Mqtt vars #####
|
||||
broker_address = "192.168.1.250"
|
||||
broker_port = 1883
|
||||
recieve_topic = "telegrambot/in"
|
||||
send_topic = "telegrambot/out"
|
||||
recieve_topic = "telegrambot/in/server/#"
|
||||
send_topic = "telegrambot/out/"
|
||||
brokercreds = ["hass","Bruk#5"]
|
||||
|
||||
|
||||
#######################
|
||||
# Setup For Variables #
|
||||
#######################
|
||||
class User: # Custom classes for my dictionaries
|
||||
def __init__(self, name, lastName, userID, mute):
|
||||
self.name = name
|
||||
self.lastName = lastName
|
||||
self.userID = userID
|
||||
self.mute = mute
|
||||
|
||||
User = {
|
||||
"7570193598": User(name="Bart", lastName="Koolen", userID="7570193598", mute=0),
|
||||
"7909366331": User(name="GJ", lastName="Koolen", userID="7909366331", mute=0),}
|
||||
|
||||
mute_time = 86400 #This is in seconds, it defines how long the program /mute command lasts
|
||||
interval_offline_message = 300
|
||||
class Site: #Custom classes for my dictionaries
|
||||
def __init__(self, url, typeVar):
|
||||
self.url = url
|
||||
self.typeVar = typeVar
|
||||
|
||||
webServers = {"Google" : Site(url="https://google.com", typeVar="URL"), #List with all the servers urls and names and types
|
||||
"GameServer" : Site(url="http://192.168.0.251", typeVar="URL"),
|
||||
"GameServer" : Site(url="http://192.168.1.251", typeVar="URL"),
|
||||
"SpiritBody" : Site(url="https://spiritbodyhealing.org", typeVar="URL"),
|
||||
"HeresYourSign": Site(url="https://heresyoursign.nl", typeVar="URL"),
|
||||
#"SMB_server": Site(url="192.168.1.250", typeVar="SMB"),
|
||||
|
|
@ -130,8 +142,24 @@ def on_connect(client, userdata, flags, reason_code, properties): #This runs o
|
|||
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
|
||||
print(messageStatus)
|
||||
Mqtt.publish(send_topic, messageStatus)
|
||||
user_topic = msg.topic.split("/")[-1]
|
||||
args = msg.payload.decode()
|
||||
print(user_topic)
|
||||
keys = list(User.keys())
|
||||
if not args:
|
||||
return
|
||||
elif args[0] == "m":
|
||||
for i in keys:
|
||||
if User[i].name.lower() == user_topic:
|
||||
print(f"right: {user_topic}: {i}")
|
||||
if User[i].mute > 0:
|
||||
User[keys[0]].mute = 0
|
||||
User[keys[1]].mute = 0
|
||||
else:
|
||||
User[i].mute = mute_time//interval_offline_message #This is defined in the setup variables
|
||||
elif args[0] == "s":
|
||||
print("s")
|
||||
Mqtt.publish(send_topic + user_topic , messageStatus)
|
||||
|
||||
Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Creates a mqqt object
|
||||
Mqtt.on_connect = on_connect #Create callback on connect
|
||||
|
|
@ -143,11 +171,21 @@ Mqtt.connect(broker_address, broker_port, 60) #Set Connectio
|
|||
Mqtt.loop_start() #Start connecting
|
||||
|
||||
def main(): #This is the main loop
|
||||
keys = list(User.keys())
|
||||
while True:
|
||||
checkWebServer(webServers) #Update list of websites status
|
||||
messageStatus = statusCheck(False) #False is only on state change
|
||||
Mqtt.publish(send_topic, messageStatus)
|
||||
if User[keys[0]].mute == 0 | User[keys[1]].mute == 0:
|
||||
Mqtt.publish(send_topic + "broadcast", messageStatus)
|
||||
sleep(30)
|
||||
if User[keys[0]].mute > 0:
|
||||
User[keys[0]].mute = User[keys[0]].mute - 1
|
||||
|
||||
if User[keys[1]].mute > 0:
|
||||
User[keys[1]].mute = User[keys[1]].mute - 1
|
||||
print(User[keys[0]].mute)
|
||||
print(User[keys[1]].mute)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -9,12 +9,14 @@ from time import sleep
|
|||
##### Mqtt vars #####
|
||||
broker_address = "192.168.1.250"
|
||||
broker_port = 1883
|
||||
recieve_topic = "telegrambot/out"
|
||||
send_topic = "telegrambot/in"
|
||||
recieve_topic = "telegrambot/out/#"
|
||||
send_topic_server = "telegrambot/in/server/"
|
||||
send_topic_glucose = "telegrambot/in/glucose/"
|
||||
brokercreds = ["hass","Bruk#5"]
|
||||
x = "AS"
|
||||
flag = False
|
||||
flag = " "
|
||||
##### Bot Variabels #####
|
||||
BotToken = '7690417088:AAGcmBegbLHBDXMBKiE6MEgCp3rnLz5vPCE'
|
||||
|
||||
class User: # Custom classes for my dictionaries
|
||||
def __init__(self, name, lastName, userID, mute):
|
||||
|
|
@ -22,7 +24,6 @@ class User: # Custom classes for my dictionaries
|
|||
self.lastName = lastName
|
||||
self.userID = userID
|
||||
self.mute = mute
|
||||
BotToken = '7690417088:AAGcmBegbLHBDXMBKiE6MEgCp3rnLz5vPCE'
|
||||
|
||||
User = {
|
||||
"7570193598": User(name="Bart", lastName="Koolen", userID="7570193598", mute=0),
|
||||
|
|
@ -31,13 +32,15 @@ User = {
|
|||
##### Main #####
|
||||
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)
|
||||
#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
|
||||
args = ' '.join(context.args)
|
||||
print(args)
|
||||
#await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.chat.id)
|
||||
Mqtt.publish(send_topic, args)
|
||||
|
||||
current_user = User[str(update.effective_chat.id)].name
|
||||
Mqtt.publish(send_topic_glucose + current_user.lower(), args)
|
||||
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)
|
||||
|
||||
def on_connect(client, userdata, flags, reason_code, properties):
|
||||
print(f"Connected with result code {reason_code}")
|
||||
|
|
@ -52,17 +55,26 @@ def on_connect(client, userdata, flags, reason_code, properties):
|
|||
def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved
|
||||
global x
|
||||
global flag
|
||||
print(msg.topic)
|
||||
x = msg.payload.decode()
|
||||
flag = True
|
||||
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
|
||||
global x
|
||||
global flag
|
||||
keys = list(User.keys())
|
||||
print(flag)
|
||||
if flag == True:
|
||||
#await context.bot.send_message(chat_id=keys[0], text=x)
|
||||
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)
|
||||
flag = False
|
||||
flag = " "
|
||||
else:
|
||||
for i in keys:
|
||||
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)
|
||||
flag = " "
|
||||
flag = " "
|
||||
|
||||
Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||
Mqtt.on_connect = on_connect
|
||||
|
|
@ -76,13 +88,13 @@ def main():#This is the main loop
|
|||
application = ApplicationBuilder().token(BotToken).build()
|
||||
gluc_handler = CommandHandler('g', gluc_command)
|
||||
subscribe_handler = CommandHandler('subscribe', subscribe)
|
||||
#mute_handler = CommandHandler('mute', mute)
|
||||
server_handler = CommandHandler('s', server_command)
|
||||
job_queue = application.job_queue
|
||||
job_minute = job_queue.run_repeating(Cat , interval=1, first=1)
|
||||
#job_offline = job_queue.run_repeating(callback_offline , interval=interval_offline_message)
|
||||
application.add_handler(gluc_handler)
|
||||
application.add_handler(subscribe_handler)
|
||||
#application.add_handler(mute_handler)
|
||||
application.add_handler(server_handler)
|
||||
application.run_polling()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue