-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_handler.py
83 lines (63 loc) · 2.07 KB
/
event_handler.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
"""
Handle the events captured by the main module.
"""
from evdev import categorize, ecodes
import json
from iptv import Iptv
class EventHandler(object):
"""
Handles the captured events.
"""
def __init__(self):
# Open settings
with open("settings.json") as settings_file:
self.settings = json.load(settings_file)
self.iptv = Iptv()
def handle(self, event):
"""
Do something meaningful with the provided event.
"""
if not self.filter_event(event):
return None
key = ecodes.KEY[event.code][4:]
try:
# Call a 'set channel number' function
self.iptv.set_channel(int(key))
return None
except ValueError:
pass
if key == self.settings["start_tv_key"]:
self.iptv.switch_on()
elif key == self.settings["stop_tv_key"]:
self.iptv.switch_off()
elif key == self.settings["channel_down"]:
self.iptv.previous_channel()
elif key == self.settings["channel_up"]:
self.iptv.next_channel()
elif key == self.settings["volume_down"]:
self.iptv.volume_down()
elif key == self.settings["volume_up"]:
self.iptv.volume_up()
elif key == self.settings["volume_mute"]:
self.iptv.volume_mute()
elif key == self.settings["stream_pause"]:
self.iptv.stream_pause()
def filter_event(self, event):
"""
Filter out all unneeded events.
"""
# Filter out if not a key event.
if event.type != ecodes.EV_KEY:
return False
# Filter out if a key 'down' event.
if event.value != 0:
return False
# Filter out if a key is not found in settings.json
if ecodes.KEY[event.code][4:] not in self.settings["accepted_keys"]:
return False
return True
def output(self, event):
"""
Print the event to stdout in a readable format.
"""
print categorize(event)