-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtbbuttonsd.py
67 lines (52 loc) · 1.75 KB
/
tbbuttonsd.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
from __future__ import print_function
import subprocess, threading, signal, json, os, shlex, Queue
from tingbot.platform_specific.tingbot import register_button_callback
button_states = ['up', 'up', 'up', 'up']
actions = {}
queue = Queue.Queue()
default_config = [
{
'combo': ['up', 'down', 'down', 'up'],
'command': 'tbopen /apps/home',
}
]
def button_callback(button_index, state):
button_states[button_index] = state
queue.put(tuple(button_states))
def process_combo_events():
while True:
combo = queue.get()
action = actions.get(combo)
if action:
print('Combo detected:', combo)
print('Action:', action)
subprocess.call(shlex.split(action))
queue.task_done()
def load_json(filename):
try:
with open(filename, 'r') as fp:
return json.load(fp)
except ValueError:
raise ValueError('Failed to load %s because it\'s not a valid JSON file' % filename)
def load_config():
if os.path.exists('tbbuttonsd.conf'):
config = load_json('tbbuttonsd.conf')
elif os.path.exists('/etc/tbbuttonsd.conf'):
config = load_json('/etc/tbbuttonsd.conf')
else:
config = default_config
for action in config:
actions[tuple(action["combo"])] = action["command"]
def main():
load_config()
register_button_callback(button_callback)
# got to keep the main thread free to respond ot signals e.g. SIGTERM
# Event.wait() will block signals until set so we push this to its own
# thread.
respond_thread = threading.Thread(target=process_combo_events)
respond_thread.daemon = True
respond_thread.start()
while True:
signal.pause()
if __name__ == '__main__':
main()