-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshaddow.py
306 lines (257 loc) · 11.7 KB
/
shaddow.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
### shaddow.py python3 compatible fork ###
#
## original script from: https://github.com/pmpkk/openhab-habpanel-theme-matrix/blob/master/shaddow.py
## original script: Copyright (c) 2017 pmpkk under MIT Licence
##
## Author of changes: Copyright (c) 2021 Florian Hotze under MIT License
## Changes:
## - Python 3 compatible
## - using InfluxDB v2's FLUX language instead of the InfluxQL by influxdb_client Python Library
## - using OpenHAB Python Library
## - added current position of moon
##
## Install depedencies with:
## - "sudo -H python3 -m pip install influxdb_client"
## - "sudo -h python3 -m pip install python-openhab"
##
## Options:
## - debug: enabled debug logging
import math
import time
import sys
from datetime import datetime, timedelta, date
from influxdb_client import InfluxDBClient
from openhab import OpenHAB
# Initialize openHAB client
openhab_url="http://localhost:8080/rest"
openhab = OpenHAB(openhab_url)
items = openhab.fetch_all_items()
# Initialize InfluxDB client
influx_url = ''
influx_token = ''
influx_org = ''
influx = InfluxDBClient(url=influx_url, token=influx_token, org=influx_org)
query_api = influx.query_api()
# Shape of the house in a 100 by 100 units square
SHAPE = [{'x': 25.44, 'y': 06.40}, \
{'x': 72.83, 'y': 11.68}, \
{'x': 68.84, 'y': 43.32}, \
{'x': 68.84, 'y': 49.05}, \
{'x': 71.86, 'y': 92.29}, \
{'x': 36.71, 'y': 94.46}, \
{'x': 35.71, 'y': 80.09}, \
{'x': 30.22, 'y': 80.43}, \
{'x': 29.28, 'y': 66.77}, \
{'x': 34.76, 'y': 66.43}, \
{'x': 33.55, 'y': 49.10}, \
{'x': 35.38, 'y': 49.05}, \
{'x': 34.38, 'y': 39.93}, \
{'x': 21.40, 'y':38.48}]
# Item names
ITEMS = {
'SUN_AZIMUTH': 'Sun_Azimuth',
'MOON_AZIMUTH': 'Moon_Azimuth',
'SUN_ELEVATION': 'Sun_Elevation',
'SUNRISE_AZIMUTH': 'Sunrise_Azimuth',
'SUNSET_AZIMUTH': 'Sunset_Azimuth'
}
WIDTH = 100
HEIGHT = 100
PRIMARY_COLOR = '#388e3c' #'#1b3024'
LIGHT_COLOR = '#d32f2f' #'#26bf75'
MOON_COLOR = '#1976d2'
STROKE_WIDTH = '1'
FILENAME = '/etc/openhab/html/shaddow.svg'
HOURS = 1
DEGS = []
class shaddow(object):
"""
Shaddow Object
"""
def __init__(self, debug=False):
self.debug = debug
self.azimuth = float(items.get(ITEMS['SUN_AZIMUTH']).state)
self.moon_azimuth = float(items.get(ITEMS['MOON_AZIMUTH']).state)
self.elevation = float(items.get(ITEMS['SUN_ELEVATION']).state)
self.sunrise_azimuth = float(items.get(ITEMS['SUNRISE_AZIMUTH']).state)
self.sunset_azimuth = float(items.get(ITEMS['SUNSET_AZIMUTH']).state)
if self.debug:
print(f'openHAB: "Sun Azimuth" is: "{self.azimuth}"')
print(f'openHAB: "Moon Azimuth" is: "{self.moon_azimuth}"')
print(f'openHAB: "Sun Elevation" is: "{self.elevation}"')
print(f'openHAB: "Sunrise Azimuth" is: "{self.sunrise_azimuth}"')
print(f'openHAB: "Sunset Azimuth" is: "{self.sunset_azimuth}"')
ts = time.time()
utc_offset = (datetime.fromtimestamp(ts) - datetime.utcfromtimestamp(ts)).total_seconds()/3600
for h in range(0,24,HOURS):
t = datetime.combine(date.today(), datetime.min.time()) + timedelta(hours=-utc_offset+h-24)
querytime = t.strftime('%Y-%m-%dT%H:%M:%S') + 'Z'
query = (f'from(bucket: "openhab")\
|> range(start: {querytime})\
|> filter(fn: (r) => r["_measurement"] == "{ITEMS["SUN_AZIMUTH"]}")\
|> first()\
|> yield(name: "mean")')
result = influx.query_api().query(org=influx_org, query=query)
results = []
for table in result:
for record in table.records:
results.append((record.get_value()))
a = float(record["_value"])
if self.debug: print(f'InfluxDB: "Sun Azimuth" at: "{querytime}"; value: "{a}"')
if (a == None): a = 0
DEGS.extend([a])
def generatePath(self,stroke,fill,points,attrs=None):
p = ''
p = p + '<path stroke="' + stroke + '" stroke-width="' + STROKE_WIDTH + '" fill="' + fill + '" '
if (attrs != None): p = p + ' ' + attrs + ' '
p = p + ' d="'
for point in points:
if (points.index(point) == 0):
p = p + 'M' + str(point['x']) + ' ' + str(point['y'])
else:
p = p + ' L' + str(point['x']) + ' ' + str(point['y'])
p = p + '" />'
return p
def generateArc(self,dist,stroke,start,end,attrs=None):
p = ''
try:
angle = end-start
if (angle<0):
angle = 360 + angle
p = p + '<path d="M' + str(self.degreesToPoint(start,dist)['x']) + ' ' + str(self.degreesToPoint(start,dist)['y']) + ' '
p = p + 'A' + str(dist) + ' ' + str(dist) + ' 0 '
if (angle<180):
p = p + '0 1 '
else:
p = p + '1 1 '
p = p + str(self.degreesToPoint(end,dist)['x']) + ' ' + str(self.degreesToPoint(end,dist)['y']) + '"'
p = p + ' stroke="' + stroke + '"'
if (attrs != None):
p = p + ' ' + attrs + ' '
else:
p = p + ' stroke-width="' + STROKE_WIDTH + '" fill="none" '
p = p + ' />'
except:
p = ''
return p
def degreesToPoint(self,d,r):
coordinates = {'x': 0, 'y': 0}
cx = WIDTH / 2
cy = HEIGHT / 2
d2 = 180 - d
coordinates['x'] = cx + math.sin(math.radians(d2))*r
coordinates['y'] = cy + math.cos(math.radians(d2))*r
return coordinates
def generateSVG(self):
realSun = self.degreesToPoint(self.azimuth, 10000)
if self.debug: print(realSun)
sun = self.degreesToPoint(self.azimuth, WIDTH / 2)
realMoon = self.degreesToPoint(self.moon_azimuth, 10000)
if self.debug: print(realMoon)
moon = self.degreesToPoint(self.moon_azimuth, WIDTH / 2)
minPoint = -1
maxPoint = -1
i = 0
minAngle = 999
maxAngle = -999
for point in SHAPE:
#Angle of close light source
angle = -math.degrees(math.atan2(point['y']-sun['y'],point['x']-sun['x']))
#Angle of distant light source (e.g. sun)
angle = -math.degrees(math.atan2(point['y']-realSun['y'],point['x']-realSun['x']))
distance = math.sqrt(math.pow(sun['y']-point['y'],2) + math.pow(sun['x']-point['x'],2))
if (angle<minAngle):
minAngle = angle
minPoint = i
if (angle>maxAngle):
maxAngle = angle
maxPoint = i
point['angle'] = angle
point['distance'] = distance
if self.debug: print(str(i).ljust(10),":", str(point['x']).ljust(10), str(point['y']).ljust(10), str(round(angle,7)).ljust(10), str(round(distance)).ljust(10))
i = i + 1
if self.debug:
print("Min Point = ",minPoint)
print("Max Point = ",maxPoint)
print("")
i = minPoint
k = 0
side1Distance = 0
side2Distance = 0
side1Done = False
side2Done = False
side1 = []
side2 = []
while True:
if (side1Done == False):
side1Distance = side1Distance + SHAPE[i]['distance']
if(i != minPoint and i != maxPoint): SHAPE[i]['side'] = 1
if (i == maxPoint): side1Done = True
side1.append( { 'x': SHAPE[i]['x'], 'y': SHAPE[i]['y'] } )
if (side1Done == True):
side2Distance = side2Distance + SHAPE[i]['distance']
if(i != minPoint and i != maxPoint): SHAPE[i]['side'] = 2
if (i == minPoint): side2Done = True
side2.append( { 'x': SHAPE[i]['x'], 'y': SHAPE[i]['y'] } )
i = i + 1
if( i > len(SHAPE)-1): i = 0
if (side1Done and side2Done): break
k = k + 1
if (k == 20): break
svg = '<?xml version="1.0" encoding="utf-8"?>'
svg = svg + '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
svg = svg + '<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="-10 -10 120 120" xml:space="preserve">'
minPointShaddowX = SHAPE[minPoint]['x'] + WIDTH * math.cos(math.radians(minAngle))
minPointShaddowY = SHAPE[minPoint]['y'] - HEIGHT * math.sin(math.radians(minAngle))
maxPointShaddowX = SHAPE[maxPoint]['x'] + WIDTH * math.cos(math.radians(maxAngle))
maxPointShaddowY = SHAPE[maxPoint]['y'] - HEIGHT * math.sin(math.radians(maxAngle))
shaddow = [ {'x': maxPointShaddowX, 'y': maxPointShaddowY } ] + \
side2 + \
[ {'x': minPointShaddowX, 'y': minPointShaddowY } ]
svg = svg + '<defs><mask id="shaddowMask">'
svg = svg + ' <rect width="100%" height="100%" fill="black"/>'
svg = svg + ' <circle cx="' + str(WIDTH/2) + '" cy="' + str(HEIGHT/2) + '" r="' + str(WIDTH/2-1) + '" fill="white"/>'
svg = svg + '</mask></defs>'
svg = svg + self.generatePath('none',PRIMARY_COLOR,SHAPE)
shaddow_svg = self.generatePath('none','black',shaddow,'mask="url(#shaddowMask)" fill-opacity="0.5"')
if (self.elevation>0):
svg = svg + self.generatePath(LIGHT_COLOR,'none',side1)
else:
svg = svg + self.generatePath(PRIMARY_COLOR,'none',side1)
if (self.elevation>0): svg = svg + shaddow_svg
svg = svg + self.generateArc(WIDTH/2,PRIMARY_COLOR,self.sunset_azimuth,self.sunrise_azimuth)
svg = svg + self.generateArc(WIDTH/2,LIGHT_COLOR,self.sunrise_azimuth,self.sunset_azimuth)
svg = svg + self.generatePath(LIGHT_COLOR,'none',[self.degreesToPoint(self.sunrise_azimuth,WIDTH/2-2), self.degreesToPoint(self.sunrise_azimuth,WIDTH/2+2)])
svg = svg + self.generatePath(LIGHT_COLOR,'none',[self.degreesToPoint(self.sunset_azimuth,WIDTH/2-2), self.degreesToPoint(self.sunset_azimuth,WIDTH/2+2)])
for i in range(0,len(DEGS)):
if (i == len(DEGS)-1):
j = 0
else:
j = i + 1
if (i % 2 == 0):
svg = svg + self.generateArc(WIDTH/2+8,PRIMARY_COLOR,DEGS[i],DEGS[j],'stroke-width="3" fill="none" stroke-opacity="0.2"')
else:
svg = svg + self.generateArc(WIDTH/2+8,PRIMARY_COLOR,DEGS[i],DEGS[j],'stroke-width="3" fill="none"')
# generate path for the line at midnight
svg = svg + self.generatePath(LIGHT_COLOR,'none',[self.degreesToPoint(DEGS[0],WIDTH/2+5), self.degreesToPoint(DEGS[0],WIDTH/2+11)])
# generate path for the line at lunchtime
svg = svg + self.generatePath(LIGHT_COLOR,'none',[self.degreesToPoint(DEGS[12],WIDTH/2+5), self.degreesToPoint(DEGS[12],WIDTH/2+11)])
# add the sun icon
svg = svg + '<circle cx="' + str(sun['x']) + '" cy="' + str(sun['y']) + '" r="3" stroke="' + LIGHT_COLOR + '" stroke-width="' + STROKE_WIDTH + '" fill="' + LIGHT_COLOR + '" />'
# add the moon icon
svg = svg + '<circle cx="' + str(moon['x']) + '" cy="' + str(moon['y']) + '" r="2" stroke="' + MOON_COLOR + '" stroke-width="' + STROKE_WIDTH + '" fill="' + MOON_COLOR + '" />'
svg = svg + '</svg>'
f = open(FILENAME, 'w')
f.write(svg)
f.close()
def main():
t1 = time.time()
args = sys.argv
debug = False
if(len(args) > 1 and args[1] == "debug"):
debug = True
shaddow(debug).generateSVG()
t2 = time.time()
print("Done in " + str(t2-t1) + " seconds")
if __name__ == '__main__':
main()