-
Notifications
You must be signed in to change notification settings - Fork 1
/
sony-pjtalk2mqtt.py
46 lines (36 loc) · 1.1 KB
/
sony-pjtalk2mqtt.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
import paho.mqtt.client as mqtt
import pysdcp
import time
lastState = ""
projector = pysdcp.Projector('beamer.rzl')
def on_connect(client, userdata, flags, rc):
print ("connected")
client.subscribe("/service/beamer/command")
def on_message(client, userdata, msg):
payload = msg.payload
print(payload)
global lastState
lastState = ""
if(payload == b'ON' or payload == b'1'):
projector.set_power(True)
elif (payload == b'OFF' or payload == b'0'):
projector.set_power(False)
else:
client.publish("/service/beamer/error", payload="unknown command. please pass 0, 1, ON or OFF", qos=0)
client = mqtt.Client()
client.will_set('/service/beamer/state', 'offline', 0, True)
client.on_connect = on_connect
client.on_message = on_message
client.connect_async("127.0.0.1")
client.loop_start()
while True:
state = 'unknown'
try:
state = projector.get_power_string()
except:
pass
print (state)
if(state != lastState):
client.publish("/service/beamer/state", payload=state, qos=0, retain=True)
lastState = state
time.sleep(1)