-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_profile.py
executable file
·226 lines (193 loc) · 8.24 KB
/
write_profile.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
#!/usr/bin/env python3
import collections
import re
import os
import zipfile
import json
import uom
pfx = "write_profile:"
VERSION_FILE = "profile/version.txt"
# define templates for the various sensor nodes we have available. Each
# sensor node will have a pre-defined list of drivers. When we build
# the node definition, we get to pick which drivers we include based on
# the user's configuration.
NODEDEF_TMPL = " <nodeDef id=\"%s\" nodeType=\"139\" nls=\"%s\">\n"
STATUS_TMPL = " <st id=\"%s\" editor=\"%s\" />\n"
# As long as we provide proper dictionary lists for each type of node
# this will generate the node definitions.
#
# Assumes that the NLS exist for the nodes and that the editors exist.
def write_profile(logger, temperature_list, humidity_list, pressure_list,
wind_list, rain_list, light_list, lightning_list):
sd = get_server_data(logger)
if sd is False:
logger.error("Unable to complete without server data...")
return False
logger.info("{0} Writing profile/nodedef/nodedefs.xml".format(pfx))
if not os.path.exists("profile/nodedef"):
try:
os.makedirs("profile/nodedef")
except:
LOGGER.error('unable to create node definition directory.')
nodedef = open("profile/nodedef/nodedefs.xml", "w")
nodedef.write("<nodeDefs>\n")
# First, write the controller node definition
nodedef.write(NODEDEF_TMPL % ('MeteoBridge', 'ctl'))
nodedef.write(" <sts>\n")
nodedef.write(" <st id=\"ST\" editor=\"bool\" />\n")
nodedef.write(" <st id=\"GV0\" editor=\"I_VOLTS\" />\n")
nodedef.write(" </sts>\n")
nodedef.write(" <cmds>\n")
nodedef.write(" <sends />\n")
nodedef.write(" <accepts>\n")
nodedef.write(" <cmd id=\"DISCOVER\" />\n")
nodedef.write(" <cmd id=\"REMOVE_NOTICES_ALL\" />\n")
nodedef.write(" <cmd id=\"UPDATE_PROFILE\" />\n")
nodedef.write(" </accepts>\n")
nodedef.write(" </cmds>\n")
nodedef.write(" </nodeDef>\n\n")
# Need to translate temperature.main into <st id="ST" editor="TEMP_C" />
# and translate temperature.extra1 into <st id="GV5" editor="TEMP_C" />
if (len(temperature_list) > 0):
nodedef.write(NODEDEF_TMPL % ('temperature', '139T'))
nodedef.write(" <sts>\n")
for t in temperature_list:
nodedef.write(STATUS_TMPL % (uom.TEMP_DRVS[t], temperature_list[t]))
nodedef.write(" </sts>\n")
nodedef.write(" </nodeDef>\n")
if (len(humidity_list) > 0):
nodedef.write(NODEDEF_TMPL % ('humidity', '139H'))
nodedef.write(" <sts>\n")
for t in humidity_list:
nodedef.write(STATUS_TMPL % (uom.HUMD_DRVS[t], humidity_list[t]))
nodedef.write(" </sts>\n")
nodedef.write(" </nodeDef>\n")
if (len(pressure_list) > 0):
nodedef.write(NODEDEF_TMPL % ('pressure', '139P'))
nodedef.write(" <sts>\n")
for t in pressure_list:
nodedef.write(STATUS_TMPL % (uom.PRES_DRVS[t], pressure_list[t]))
nodedef.write(" </sts>\n")
nodedef.write(" </nodeDef>\n")
if (len(wind_list) > 0):
nodedef.write(NODEDEF_TMPL % ('wind', '139W'))
nodedef.write(" <sts>\n")
for t in wind_list:
nodedef.write(STATUS_TMPL % (uom.WIND_DRVS[t], wind_list[t]))
nodedef.write(" </sts>\n")
nodedef.write(" </nodeDef>\n")
if (len(rain_list) > 0):
nodedef.write(NODEDEF_TMPL % ('precipitation', '139R'))
nodedef.write(" <sts>\n")
for t in rain_list:
nodedef.write(STATUS_TMPL % (uom.RAIN_DRVS[t], rain_list[t]))
nodedef.write(" </sts>\n")
nodedef.write(" </nodeDef>\n")
if (len(light_list) > 0):
nodedef.write(NODEDEF_TMPL % ('light', '139L'))
nodedef.write(" <sts>\n")
for t in light_list:
nodedef.write(STATUS_TMPL % (uom.LITE_DRVS[t], light_list[t]))
nodedef.write(" </sts>\n")
nodedef.write(" </nodeDef>\n")
if (len(lightning_list) > 0):
nodedef.write(NODEDEF_TMPL % ('lightning', '139S'))
nodedef.write(" <sts>\n")
for t in lightning_list:
nodedef.write(STATUS_TMPL % (uom.LTNG_DRVS[t], lightning_list[t]))
nodedef.write(" </sts>\n")
nodedef.write(" </nodeDef>\n")
nodedef.write("</nodeDefs>")
nodedef.close()
# Update the profile version file with the info from server.json
with open(VERSION_FILE, 'w') as outfile:
outfile.write(sd['profile_version'])
outfile.close()
# Create the zip file that can be uploaded to the ISY
write_profile_zip(logger)
logger.info(pfx + " done.")
def write_profile_zip(logger):
src = 'profile'
abs_src = os.path.abspath(src)
with zipfile.ZipFile('profile.zip', 'w') as zf:
for dirname, subdirs, files in os.walk(src):
# Ignore dirs starint with a dot, stupid .AppleDouble...
if not "/." in dirname:
for filename in files:
if filename.endswith('.xml') or filename.endswith('txt'):
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
logger.info('write_profile_zip: %s as %s' %
(os.path.join(dirname, filename), arcname))
zf.write(absname, arcname)
zf.close()
def get_server_data(logger):
# Read the SERVER info from the json.
try:
with open('server.json') as data:
serverdata = json.load(data)
except Exception as err:
logger.error('get_server_data: failed to read {0}: {1}'.format('server.json',err), exc_info=True)
return False
data.close()
# Get the version info
try:
version = serverdata['credits'][0]['version']
except (KeyError, ValueError):
logger.info('Version not found in server.json.')
version = '0.0.0.0'
# Split version into two floats.
sv = version.split(".");
v1 = 0;
v2 = 0;
if len(sv) == 1:
v1 = int(v1[0])
elif len(sv) > 1:
v1 = float("%s.%s" % (sv[0],str(sv[1])))
if len(sv) == 3:
v2 = int(sv[2])
else:
v2 = float("%s.%s" % (sv[2],str(sv[3])))
serverdata['version'] = version
serverdata['version_major'] = v1
serverdata['version_minor'] = v2
return serverdata
# If we wanted to call this as a stand-alone script to generate the profile
# files, we'd do something like what's below but we'd need some way to
# set the configuration.
if __name__ == "__main__":
import logging,json
logger = logging.getLogger(__name__)
logging.basicConfig(
level=10,
format='%(levelname)s:\t%(name)s\t%(message)s'
)
logger.setLevel(logging.DEBUG)
# Test dictionaries to generate a custom nodedef file.
tl = {'main' : 'I_TEMP_F', 'dewpoint' : 'I_TEMP_F', 'apparent' : 'I_TEMP_F',
'extra3' : 'I_TEMP_F'}
hl = {'main' : 'I_HUMIDITY'}
pl = {'station' : 'I_INHG', 'trend' : 'I_TREND'}
wl = {'windspeed' : 'I_MPH', 'gustspeed' : 'I_MPH', 'winddir' : 'I_DEGREE'}
rl = {'hourly': 'I_MM', 'monthly': 'I_MM', 'yearly': 'I_MM'}
ll = {'uv' : 'I_UV'}
sl = {}
# Only write the profile if the version is updated.
sd = get_server_data(logger)
if sd is not False:
local_version = None
try:
with open(VERSION_FILE,'r') as vfile:
local_version = vfile.readline()
local_version = local_version.rstrip()
vfile.close()
except (FileNotFoundError):
pass
except (Exception) as err:
logger.error('{0} failed to read local version from {1}: {2}'.format(pfx,VERSION_FILE,err), exc_info=True)
if local_version == sd['profile_version']:
#logger.info('{0} Not Generating new profile since local version {1} is the same current {2}'.format(pfx,local_version,sd['profile_version']))
write_profile(logger, tl, hl, pl, wl, rl, ll, sl)
else:
logger.info('{0} Generating new profile since local version {1} is not current {2}'.format(pfx,local_version,sd['profile_version']))
write_profile(logger, tl, hl, pl, wl, rl, ll, sl)