-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.py
161 lines (117 loc) · 3.76 KB
/
service.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
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
149
150
151
152
153
154
155
156
157
158
159
160
import os
import sys
import threading
import xbmc
import redshift
import utils
from globs import Action
from globs import fifo
from globs import addon
rs = redshift.Redshift()
class Receiver():
_active = threading.Event()
_exit = threading.Event()
_exited = threading.Event()
fifo = None
callback = None
def __init__(self, fifo_path, on_receive=None):
self.fifo = fifo_path
self.callback = on_receive
def _runner(self):
if not self.callback or not hasattr(self.callback, '__call__'):
return
if os.path.exists(self.fifo):
os.remove(self.fifo)
try:
os.mkfifo(self.fifo)
except:
utils.log('Unable to create fifo', lvl=xbmc.LOGERROR)
return
self._active.set()
self._exited.clear()
while not self._exit.is_set():
with open(self.fifo) as fifo:
line = fifo.read()
if line == 'exit':
break
self.callback(line)
try:
os.remove(self.fifo)
except:
utils.log('Failed unlinking fifo', lvl=xbmc.LOGERROR)
self._active.clear()
self._exited.set()
def send(self, msg):
with open(self.fifo, 'a') as fifo:
fifo.write(str(msg))
def start(self):
t = threading.Thread(target=self._runner)
t.daemon = True
t.start()
return self._active.wait(1)
def stop(self):
self._exit.set()
self._exit.wait()
self.send('exit')
self._exited.wait()
def do(action):
try:
action = int(action)
except:
utils.log('Unsupported action:', action)
return
if action == Action.ON:
if rs.is_running:
utils.log('Was on already!')
utils.notify(utils.translate(30013))
return
utils.log('Turning on!')
utils.notify(utils.translate(30012))
# Reconfigure for setting changes to take effect
configure_redshift(rs)
rs.start()
elif action == Action.OFF:
if not rs.is_running:
utils.log('Was off already!')
utils.notify(utils.translate(30015))
return
utils.log('Turning off!')
utils.notify(utils.translate(30014))
rs.stop()
rs.reset()
elif action == Action.TOGGLE:
if not rs.is_running:
do(Action.ON)
else:
do(Action.OFF)
def configure_redshift(rs):
rs.latitude = float(addon.getSetting('latitude'))
rs.longitude = float(addon.getSetting('longitude'))
rs.transition = addon.getSetting('transition') == 'true'
rs.temperature_day = addon.getSetting('day.temperature')
rs.temperature_night = addon.getSetting('night.temperature')
rs.brightness_day = float(addon.getSetting('day.brightness'))
rs.brightness_night = float(addon.getSetting('night.brightness'))
rs.gamma_day_r = float(addon.getSetting('day.gamma.r'))
rs.gamma_day_g = float(addon.getSetting('day.gamma.g'))
rs.gamma_day_b = float(addon.getSetting('day.gamma.b'))
#rs.gamma_night_r = float(addon.getSetting('night.gamma.r'))
#rs.gamma_night_g = float(addon.getSetting('night.gamma.g'))
#rs.gamma_night_b = float(addon.getSetting('night.gamma.b'))
if __name__ == '__main__':
recv = Receiver(fifo, do)
if not recv.start():
utils.log('Unable to start receiver pipe, exiting!')
sys.exit(1)
configure_redshift(rs)
if not rs.start():
utils.notify(utils.translate(30019))
recv.stop()
sys.exit(1)
monitor = xbmc.Monitor()
while not monitor.abortRequested():
if monitor.waitForAbort(60):
break
recv.stop()
rs.stop()
rs.reset()