added server component
This commit is contained in:
parent
d6dbeaf9b1
commit
f41de7596e
@ -70,21 +70,6 @@ def subscribe(client: mqtt_client):
|
|||||||
for topic in topics:
|
for topic in topics:
|
||||||
client.subscribe(topic)
|
client.subscribe(topic)
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
|
|
||||||
def publish(client, topic, msg):
|
|
||||||
msg_count = 1
|
|
||||||
while True:
|
|
||||||
sleep(1)
|
|
||||||
result = client.publish(topic, msg)
|
|
||||||
status = result[0]
|
|
||||||
if status == 0:
|
|
||||||
print("Msg sent")
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
print("Failed to send message")
|
|
||||||
msg_count += 1
|
|
||||||
if msg_count > 5:
|
|
||||||
break
|
|
||||||
|
|
||||||
def subscribe_state():
|
def subscribe_state():
|
||||||
client = connect_mqtt()
|
client = connect_mqtt()
|
||||||
@ -121,13 +106,13 @@ def checkMicrophone():
|
|||||||
except Exception as e: print(e)
|
except Exception as e: print(e)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# sub=threading.Thread(target=subscribe_state)
|
sub=threading.Thread(target=subscribe_state)
|
||||||
# sub.start()
|
sub.start()
|
||||||
|
|
||||||
# sleep(5)
|
sleep(5)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# checkMicrophone()
|
checkMicrophone()
|
||||||
checkHeadphones()
|
checkHeadphones()
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
|
83
Audio/audio_automation_server.py
Normal file
83
Audio/audio_automation_server.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
from paho.mqtt import client as mqtt_client
|
||||||
|
import paho.mqtt.publish as publ
|
||||||
|
import random
|
||||||
|
import threading
|
||||||
|
import json
|
||||||
|
import psutil
|
||||||
|
from time import sleep
|
||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
|
||||||
|
FIRST_RECONNECT_DELAY = 1
|
||||||
|
RECONNECT_RATE = 2
|
||||||
|
MAX_RECONNECT_COUNT = 12
|
||||||
|
MAX_RECONNECT_DELAY = 60
|
||||||
|
|
||||||
|
broker = '10.0.69.69'
|
||||||
|
port = 1883
|
||||||
|
topics = ["tele/+/STATE", "stat/+/POWER"]
|
||||||
|
client_id = f'python-mqtt-{random.randint(0, 1000)}'
|
||||||
|
username = 'mosquitto'
|
||||||
|
password = 'Terra_Taonas'
|
||||||
|
|
||||||
|
powerState = {}
|
||||||
|
|
||||||
|
def connect_mqtt():
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
if rc == 0:
|
||||||
|
print("Connected to MQTT Broker!")
|
||||||
|
else:
|
||||||
|
print("Failed to connect, return code %d\n", rc)
|
||||||
|
|
||||||
|
client = mqtt_client.Client(client_id)
|
||||||
|
|
||||||
|
client.username_pw_set(username, password)
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.connect(broker, port)
|
||||||
|
return client
|
||||||
|
|
||||||
|
def subscribe(client: mqtt_client):
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
topic = msg.topic
|
||||||
|
if "tele" in topic:
|
||||||
|
if "STATE" in topic:
|
||||||
|
data = json.loads(msg.payload.decode())
|
||||||
|
device = topic.split('/')[1]
|
||||||
|
powerState[device] = data['POWER']
|
||||||
|
if "stat" in topic:
|
||||||
|
device = topic.split('/')[1]
|
||||||
|
powerState[device] = msg.payload.decode()
|
||||||
|
print(powerState)
|
||||||
|
|
||||||
|
for topic in topics:
|
||||||
|
client.subscribe(topic)
|
||||||
|
client.on_message = on_message
|
||||||
|
|
||||||
|
def subscribe_state():
|
||||||
|
client = connect_mqtt()
|
||||||
|
subscribe(client)
|
||||||
|
client.loop_forever()
|
||||||
|
|
||||||
|
def ping(host):
|
||||||
|
return os.system(f"ping -c 1 {host} > /dev/null") == 0
|
||||||
|
|
||||||
|
def checkPC():
|
||||||
|
t = datetime.datetime.now()
|
||||||
|
h = int(t.strftime("%H"))
|
||||||
|
|
||||||
|
if h < 6 or h >= 23:
|
||||||
|
if not ping("10.69.42.5"):
|
||||||
|
try:
|
||||||
|
if powerState['swhpamp'] == "ON":
|
||||||
|
publ.single("cmnd/swhpamp/POWER", "off", hostname=broker, port=port, auth={'username':username,'password':password})
|
||||||
|
except Exception as e: print(e)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sub=threading.Thread(target=subscribe_state)
|
||||||
|
sub.start()
|
||||||
|
|
||||||
|
sleep(5)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
checkPC()
|
||||||
|
sleep(1)
|
Loading…
x
Reference in New Issue
Block a user