forked from openenergymonitor/emonhub
/
emonhub.py
executable file
·332 lines (263 loc) · 12.1 KB
/
emonhub.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
"""
This code is released under the GNU Affero General Public License.
OpenEnergyMonitor project:
http://openenergymonitor.org
"""
import sys
import time
import logging
import logging.handlers
import signal
import argparse
import pprint
import glob, os
import emonhub_setup as ehs
import emonhub_coder as ehc
import emonhub_interfacer as ehi
from interfacers import *
# this namespace and path
namespace = sys.modules[__name__]
path = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
# scan interfacers directory and import all interfacers
for f in glob.glob(path+"/interfacers/*.py"):
name = f.replace(".py","").replace(path+"/interfacers/","")
if name!="__init__":
# print "Loading: "+name
setattr(ehi,name,getattr(getattr(namespace,name),name))
"""class EmonHub
Monitors data inputs through EmonHubInterfacer instances,
and (currently) sends data to
target servers through EmonHubEmoncmsReporter instances.
Controlled by the user via EmonHubSetup
"""
class EmonHub(object):
__version__ = "emonHub emon-pi variant v2.1.1"
def __init__(self, setup):
"""Setup an OpenEnergyMonitor emonHub.
Interface (EmonHubSetup): User interface to the hub.
"""
# Initialize exit request flag
self._exit = False
# Initialize setup and get settings
self._setup = setup
settings = self._setup.settings
# Initialize logging
self._log = logging.getLogger("EmonHub")
self._set_logging_level('INFO', False)
self._log.info("EmonHub %s" % self.__version__)
self._log.info("Opening hub...")
# Initialize Interfacers
self._interfacers = {}
# Update settings
self._update_settings(settings)
def run(self):
"""Launch the hub.
Monitor the interfaces and process data.
Check settings on a regular basis.
"""
# Set signal handler to catch SIGINT and shutdown gracefully
signal.signal(signal.SIGINT, self._sigint_handler)
# Initialise thread restart counters
restart_count={}
for I in self._interfacers.itervalues():
restart_count[I.name]=0
# Until asked to stop
while not self._exit:
# Run setup and update settings if modified
self._setup.run()
if self._setup.check_settings():
self._update_settings(self._setup.settings)
# For all Interfacers
kill_list=[]
for I in self._interfacers.itervalues():
# Check threads are still running
if not I.isAlive():
kill_list.append(I.name) # <-avoid modification of iterable within loop
# Read each interfacers pub channels
for pub_channel in I._settings['pubchannels']:
if pub_channel in I._pub_channels:
if len(I._pub_channels[pub_channel])>0:
# POP cargo item (one at a time)
cargo = I._pub_channels[pub_channel].pop(0)
# Post to each subscriber interface
for sub_interfacer in self._interfacers.itervalues():
# For each subsciber channel
for sub_channel in sub_interfacer._settings['subchannels']:
# If channel names match
if sub_channel==pub_channel:
# init if empty
if not sub_channel in sub_interfacer._sub_channels:
sub_interfacer._sub_channels[sub_channel] = []
# APPEND cargo item
sub_interfacer._sub_channels[sub_channel].append(cargo)
# ->avoid modification of iterable within loop
for name in kill_list:
self._log.warning(name + " thread is dead.")
# The following should trigger a restart ... unless the
# interfacer is also removed from the settings table.
del(self._interfacers[name])
# Trigger restart by calling update settings
self._log.warning("Attempting to restart thread "+name+" (thread has been restarted "+str(restart_count[name])+" times...")
restart_count[name]+=1
self._update_settings(self._setup.settings)
# Sleep until next iteration
time.sleep(0.2)
def close(self):
"""Close hub. Do some cleanup before leaving."""
self._log.info("Exiting hub...")
for I in self._interfacers.itervalues():
I.stop = True
I.join()
self._log.info("Exit completed")
logging.shutdown()
def _sigint_handler(self, signal, frame):
"""Catch SIGINT (Ctrl+C)."""
self._log.debug("SIGINT received.")
# hub should exit at the end of current iteration.
self._exit = True
def _update_settings(self, settings):
"""Check settings and update if needed."""
# EmonHub Logging level
if 'loglevel' in settings['hub']:
self._set_logging_level(settings['hub']['loglevel'])
else:
self._set_logging_level()
# Create a place to hold buffer contents whilst a deletion & rebuild occurs
self.temp_buffer = {}
# Interfacers
for name in self._interfacers.keys():
# Delete interfacers if not listed or have no 'Type' in the settings without further checks
# (This also provides an ability to delete & rebuild by commenting 'Type' in conf)
if not name in settings['interfacers'] or not 'Type' in settings['interfacers'][name]:
pass
else:
try:
# test for 'init_settings' and 'runtime_setting' sections
settings['interfacers'][name]['init_settings']
settings['interfacers'][name]['runtimesettings']
except Exception as e:
# If interfacer's settings are incomplete, continue without updating
self._log.error("Unable to update '" + name + "' configuration: " + str(e))
continue
else:
# check init_settings against the file copy, if they are the same move on to the next
if self._interfacers[name].init_settings == settings['interfacers'][name]['init_settings']:
continue
# Delete interfacers if setting changed or name is unlisted or Type is missing
self._log.info("Deleting interfacer '%s' ", name)
self._interfacers[name].stop = True
del(self._interfacers[name])
for name, I in settings['interfacers'].iteritems():
# If interfacer does not exist, create it
if name not in self._interfacers:
try:
if not 'Type' in I:
continue
self._log.info("Creating " + I['Type'] + " '%s' ", name)
# This gets the class from the 'Type' string
interfacer = getattr(ehi, I['Type'])(name,**I['init_settings'])
interfacer.set(**I['runtimesettings'])
interfacer.init_settings = I['init_settings']
interfacer.start()
except ehi.EmonHubInterfacerInitError as e:
# If interfacer can't be created, log error and skip to next
self._log.error("Failed to create '" + name + "' interfacer: " + str(e))
continue
except Exception as e:
# If interfacer can't be created, log error and skip to next
self._log.error("Unable to create '" + name + "' interfacer: " + str(e))
continue
else:
self._interfacers[name] = interfacer
else:
# Otherwise just update the runtime settings if possible
if 'runtimesettings' in I:
self._interfacers[name].set(**I['runtimesettings'])
if 'nodes' in settings:
ehc.nodelist = settings['nodes']
def _set_logging_level(self, level='WARNING', log=True):
"""Set logging level.
level (string): log level name in
('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
"""
# Ensure "level" is all upper case
level = level.upper()
# Check level argument is valid
try:
loglevel = getattr(logging, level)
except AttributeError:
self._log.error('Logging level %s invalid' % level)
return False
except Exception as e:
self._log.error('Logging level %s ' % str(e))
return False
# Change level if different from current level
if loglevel != self._log.getEffectiveLevel():
self._log.setLevel(level)
if log:
self._log.info('Logging level set to %s' % level)
if __name__ == "__main__":
# Command line arguments parser
parser = argparse.ArgumentParser(description='OpenEnergyMonitor emonHub')
# Configuration file
parser.add_argument("--config-file", action="store",
help='Configuration file', default=sys.path[0]+'/../conf/emonhub.conf')
# Log file
parser.add_argument('--logfile', action='store', type=argparse.FileType('a'),
help='Log file (default: log to Standard error stream STDERR)')
# Show settings
parser.add_argument('--show-settings', action='store_true',
help='show settings and exit (for debugging purposes)')
# Show version
parser.add_argument('--version', action='store_true',
help='display version number and exit')
# Parse arguments
args = parser.parse_args()
# Display version number and exit
if args.version:
print('emonHub %s' % EmonHub.__version__)
sys.exit()
# Logging configuration
logger = logging.getLogger("EmonHub")
if args.logfile is None:
# If no path was specified, everything goes to sys.stderr
loghandler = logging.StreamHandler()
else:
# Otherwise, rotating logging over two 5 MB files
# If logfile is supplied, argparse opens the file in append mode,
# this ensures it is writable
# Close the file for now and get its path
args.logfile.close()
loghandler = logging.handlers.RotatingFileHandler(args.logfile.name,
'a', 5000 * 1024, 1)
# Format log strings
loghandler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)-8s %(threadName)-10s %(message)s'))
logger.addHandler(loghandler)
# Initialize hub setup
try:
setup = ehs.EmonHubFileSetup(args.config_file)
except ehs.EmonHubSetupInitError as e:
logger.critical(e)
sys.exit("Unable to load configuration file: " + args.config_file)
if 'use_syslog' in setup.settings['hub']:
if setup.settings['hub']['use_syslog'] == 'yes':
syslogger = logging.handlers.SysLogHandler(address='/dev/log')
syslogger.setFormatter(logging.Formatter(
'emonHub[%(process)d]: %(levelname)-8s %(threadName)-10s %(message)s'))
logger.addHandler(syslogger)
# If in "Show settings" mode, print settings and exit
if args.show_settings:
setup.check_settings()
pprint.pprint(setup.settings)
# Otherwise, create, run, and close EmonHub instance
else:
try:
hub = EmonHub(setup)
except Exception as e:
sys.exit("Could not start EmonHub: " + str(e))
else:
hub.run()
# When done, close hub
hub.close()