-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquaredDimmingService.py
More file actions
66 lines (59 loc) · 1.86 KB
/
squaredDimmingService.py
File metadata and controls
66 lines (59 loc) · 1.86 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
#!/usr/bin/env python
from jsonsocket import Client
from datetime import datetime
from time import sleep
host = '127.0.0.1'
port = 5204
dimmingStartHour = 23
dimmingEndHour = 16
dimmedBrightness = 0
nonDimmedBrightness = 1
client = Client()
inDimmingMode = False
def setBrightness(newBrightness):
print("setting Brightness to %s" % newBrightness)
client.connect(host, port)
client.send(
{
"method": "setBrightness",
"params": {"brightness": newBrightness}
}
)
client.send(
{
"method": "loadModel"
}
)
response = client.recv()
currentBrightness = response['params']['brightness']
if not(int(currentBrightness * 100) == int(newBrightness * 100)):
raise Exception("Failed setting brightness, sent %s but got %s" % (currentBrightness, response['brightness']))
sleep(0.5)
client.close()
while True:
t = datetime.now()
shouldBeInDimmingMode = not (dimmingStartHour > t.hour >= dimmingEndHour) # assumes the dimming hours span midnight
print(f'inDimmingMode {inDimmingMode} shouldBeInDimmingMode {shouldBeInDimmingMode}')
if inDimmingMode and not shouldBeInDimmingMode:
try:
setBrightness(nonDimmedBrightness)
inDimmingMode = False
print("Success")
except Exception as e:
print("Error changing brightness")
if hasattr(e, 'message'):
print(e.message)
else:
print(e)
if not inDimmingMode and shouldBeInDimmingMode:
try:
setBrightness(dimmedBrightness)
inDimmingMode = True
print("Success")
except Exception as e:
print("Error changing brightness")
if hasattr(e, 'message'):
print(e.message)
else:
print(e)
sleep(60)