This repository was archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlirc_watcher.py
More file actions
148 lines (120 loc) · 4.15 KB
/
Copy pathlirc_watcher.py
File metadata and controls
148 lines (120 loc) · 4.15 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
import os
import paho.mqtt.client as paho
from threading import Timer
import sys
import socket
import fcntl
import errno
from time import sleep
LONG_PRESS = os.getenv('LONG_PRESS', 12)
READ_TIMEOUT = os.getenv('READ_TIMEOUT', 0.2)
PAYLOAD_LONG_PRESS = os.getenv('PAYLOAD_LONG_PRESS', 'long')
PAYLOAD_SHORT_CLICK = os.getenv('PAYLOAD_SHORT_CLICK', 'short')
MQTT_BROKER = os.getenv('MQTT_BROKER', 'localhost')
MQTT_USER = os.getenv('MQTT_USER', None)
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', None)
MQTT_QOS = os.getenv('MQTT_QOS', 1)
MQTT_PORT = os.getenv('MQTT_PORT', 1883)
MQTT_ID = os.getenv('MQTT_ID', 'lirc-watcher')
MQTT_PREFIX = os.getenv('MQTT_PREFIX', 'lirc')
MQTT_SEND_TOPIC = '%s/send/#' % MQTT_PREFIX
MQTT_STATUS_TOPIC = '%s/alive' % MQTT_PREFIX
MQTT_PAYLOAD_ONLINE = '1'
MQTT_PAYLOAD_OFFLINE = '0'
print("LIRC watcher started")
def on_mqtt_connect(mqtt, userdata, flags, rc):
if rc == 0:
print('MQTT connected')
mqtt.publish(MQTT_STATUS_TOPIC, payload=MQTT_PAYLOAD_ONLINE,
qos=MQTT_QOS, retain=True)
mqtt.subscribe(MQTT_SEND_TOPIC)
else:
print('MQTT connect failed:', rc)
def on_mqtt_message(client, userdata, msg):
try:
(remote, key) = str(msg.topic).split("/")[-2:]
try:
repeat = int(msg.payload)
except:
repeat = 1
command = "SEND_ONCE %s %s %d\n" % (remote, key, repeat)
print(command)
sock.sendall(command.encode("utf-8"))
except:
print(str(msg.topic))
prev_data = None
timer = None
mqtt = paho.Client(MQTT_ID)
mqtt.on_connect = on_mqtt_connect
mqtt.on_message = on_mqtt_message
mqtt.will_set(MQTT_STATUS_TOPIC, payload=MQTT_PAYLOAD_OFFLINE,
qos=MQTT_QOS, retain=True)
mqtt.username_pw_set(MQTT_USER, MQTT_PASSWORD)
mqtt.connect(MQTT_BROKER, MQTT_PORT)
mqtt.loop_start()
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect("/var/run/lirc/lircd")
fcntl.fcntl(sock, fcntl.F_SETFL, os.O_NONBLOCK)
def send_code(priority_data=None):
global prev_data, mqtt, timer
if timer is not None and timer.is_alive():
timer.cancel()
timer = None
if priority_data is not None or prev_data is not None:
to_send = priority_data if priority_data is not None else prev_data
to_send = to_send.split()
key_name = to_send[2]
remote = to_send[3]
counter = int(to_send[1], 16)
if(counter >= LONG_PRESS):
payload = PAYLOAD_LONG_PRESS
else:
payload = PAYLOAD_SHORT_CLICK
topic = "%s/%s/%s" % (MQTT_PREFIX, remote, key_name)
print("Sending message: '%s' to topic: '%s'" %
(payload, topic))
mqtt.publish(topic, payload=payload, qos=MQTT_QOS)
if priority_data is None:
prev_data = None
try:
while True:
"""
Check for new data
"""
try:
new_data = sock.recv(128)
new_data = new_data.strip()
except socket.error as e:
err = e.args[0]
"""
Check for "real" error
"""
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
sleep(0.01)
continue
else:
print(e)
mqtt.disconnect()
mqtt.loop_stop()
sys.exit(1)
else:
if new_data:
new_data = new_data.decode("utf-8")
print("new_data: ", new_data)
counter_str = new_data.split()
#ignore the status return from lirc sends
if counter_str[0] != "BEGIN":
"""
If we received new_data and prev_data was not sent
"""
if prev_data is not None and int(counter_str[1], 16) == 0:
send_code(prev_data)
prev_data = new_data
if timer is not None and timer.is_alive():
timer.cancel()
timer = Timer(READ_TIMEOUT, send_code)
timer.start()
except KeyboardInterrupt:
mqtt.disconnect()
mqtt.loop_stop()