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
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue