forked from vinnyspb/nexa-controller-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
60 lines (49 loc) · 2.07 KB
/
controller.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
import sched
import time
import os
from datetime import datetime
from switch_nexa import NexaSwitcher
from time_controller import TimeController
from presence_controller import PresenceController
from kodi_play_pause import KodiPlayPause
from controller_config import Config
from datadog_stat import DataDogStat
def dispatch_all_controllers(sc, controllers, global_status):
try:
new_status = True
for controller in controllers:
if not controller.dispatch():
new_status = False
break
if global_status is None or global_status != new_status:
kodi = None
if Config.KODI_ADDR is not None and len(Config.KODI_ADDR) > 0:
kodi = KodiPlayPause()
kodi.pause()
time.sleep(1)
print str(datetime.now()) + " Changing status from " + str(global_status) + " to " + str(new_status)
os.nice(+40)
switcher = NexaSwitcher(Config.RASPBERRY_PI_DATA_PIN, Config.TRANSMITTER_CODE)
switcher.switch(new_status)
os.nice(-40)
if kodi is not None:
kodi.play()
global_status = new_status
if Config.DATADOG_API_KEY is not None and len(Config.DATADOG_API_KEY) > 0:
datadog = DataDogStat()
datadog.post_status(new_status)
except Exception as e:
print "Exception: " + str(e)
sc.enter(60, 1, dispatch_all_controllers, (sc, controllers, global_status))
global_status = None
controllers = [TimeController(),
PresenceController(Config.ROUTER_HOST,
Config.ROUTER_URI,
Config.ROUTER_USERNAME,
Config.ROUTER_PASSWORD,
Config.MONITORED_MAC_ADRESSES,
Config.SEARCH_PREFIX_FOR_MAC,
Config.SEARCH_SUFFIX_FOR_MAC)]
s = sched.scheduler(time.time, time.sleep)
s.enter(1, 1, dispatch_all_controllers, (s, controllers, global_status))
s.run()