-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvcheck.py
executable file
·84 lines (79 loc) · 2.74 KB
/
tvcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python3
"""
This is a variation on the script used to catch weird cases
like the alarm being left on all night, and the cron job
turning TVs off, but no alarm to turn it back on when people
are in the building
"""
import paho.mqtt.client as mqtt #import the client1
import time,datetime
import sys
import requests
############
"""
IMPORTANT: TVs REQUIRE IP addresses, not DNS names to be used!
tv-laser.lan.makeitlabs.com has address 10.25.6.28
bkg@cgimisc:~/tvmon$ host tv-shopbot.lan.makeitlabs.com
tv-shopbot.lan.makeitlabs.com has address 10.25.9.124
"""
SHOPBOT="10.25.9.124"
LASER="10.25.6.28"
def on_connect(client, userdata, message,data):
print ("CONNECTED",datetime.datetime.now())
client.subscribe("#")
sys.stdout.flush()
def on_disconnect(client, test,data):
print ("DICONECCTED",data,test,datetime.datetime.now())
client.subscribe("#")
sys.stdout.flush()
def tv_onoff(on=True):
print ("Turn TV ",on,datetime.datetime.now())
if on:
r = requests.post(f"http://{LASER}:8060/keypress/PowerOn","")
r = requests.post(f"http://{SHOPBOT}:8060/keypress/PowerOn","")
else:
r = requests.post(f"http://{LASER}:8060/keypress/PowerOff","")
r = requests.post(f"http://{SHOPBOT}:8060/keypress/PowerOff","")
print ("TV RETUREND",r.status_code,r.reason)
sys.stdout.flush()
def on_message(client, userdata, message):
msg = str(message.payload.decode("utf-8"))
"""
print("message time " ,str(datetime.datetime.now()))
print("message payload " ,msg)
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
"""
if (message.topic == "facility/alarm/system"):
if msg == "armed":
print ("ARMED")
#tv_onoff(False)
elif msg == "disarmed":
print ("DISARMED - Turning TV On")
#sys.exit(0)
tv_onoff(True)
else:
print (f"Misunderstood message \"{msg}\"")
sys.stdout.flush()
########################################
broker_address="mqtt"
print("creating new instance")
client = mqtt.Client("tvcheck") #create new instance
client.on_message=on_message #attach function to callback
client.on_connect=on_connect #attach function to callback
client.on_disconnect=on_disconnect #attach function to callback
print("connecting to broker")
client.connect(broker_address,port=2889) #connect to broker
client.subscribe("#")
#client.loop_start() #start the loop
#while True:
# time.sleep(36000) # wait
#print ("Staring TV on")
#tv_onoff(True)
endtime = datetime.datetime.now()+datetime.timedelta(seconds=10)
while (True):
l=client.loop(timeout=5) #stop the loop
if (datetime.datetime.now() > endtime):
break
print ("EXITED")