forked from nesl/SensorActuatorManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
255 lines (216 loc) · 8.62 KB
/
main.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
#!/usr/bin/python
# Author: Mani Srivastava, NESL, UCLA
# Created on: May 22, 2013
#
# Copyright notice in LICENSE file
#
from __future__ import print_function
from pprint import pprint
import argparse
import json
import sys, struct
import httplib
import xml.dom.minidom
import time
import threading, Queue
import os
import socket
import signal
import logging
from datetime import datetime
from pkg.utils.misc import json_convert_unicode_to_string, json_evaluate_expression
log_file = None
DEFAULT_DEBUG_LEVEL = 0
class SensorActuatorManager(object):
def __init__(self, config_file_name):
# read config file
with open(config_file_name) as config_file:
try:
self.config = json_evaluate_expression(json_convert_unicode_to_string(json.load(config_file)))
except ValueError as e:
print(e)
logging.error("Malformed JSON in config file")
exit(1)
except:
logging.error("Malformed JSON in config file")
exit(1)
logging.debug(self.config)
# following is a dict with keys as device types and values as handles of loaded module code
self.device_modules={}
# following is a dict with keys as device ids and values as dicts with keys type, params, and handle of object
self.devices={}
# following is a dict with keys as service types and values as handles of loaded module code
self.service_modules={}
# following is a dict with keys as service ids and values as dicts with keys type, params, and handle of object
self.services={}
# following is a list of tuples (queue handle, id of device, id of service)
self.queues = []
if (not 'devices' in self.config):
logging.error("Bad config file: no devices section")
exit(1)
if (not 'services' in self.config):
logging.error("Bad config file: no services section")
exit(1)
if (type(self.config['devices'])!=list):
self.config['devices'] = [self.config['devices']]
if (type(self.config['services'])!=list):
self.config['services'] = [self.config['services']]
for s in self.config['services']:
if (type(s)!=dict):
logging.error("Malformed service specification - must be a dict")
exit(1)
if (('enable' in s) and (s['enable']!="True" and s['enable']!=True)):
continue
if (not 'id' in s):
logging.error("anonymous service with no id")
exit(1)
if (s['id'] in self.services):
logging.error("duplicate service id "+s['id'])
exit(1)
self.services[s['id']] = {}
self.services[s['id']]['type'] = s['type']
if ('params' in s):
if (type(s['params'])!=dict):
logging.error(" service "+s['id']+" has malformed parameters")
exit(1)
self.services[s['id']]['params'] = s['params']
else:
self.services[s['id']]['params'] = {}
# for each device in the config file
for d in self.config['devices']:
if (type(d)!=dict):
logging.error("Malformed device specification - must be a dict")
exit(1)
if (('enable' in d) and (d['enable']!="True" and d['enable']!=True)):
continue
# check that the device has an id
if (not 'id' in d):
logging.error("anonymous device with no id")
exit(1)
# check that device id is unique
if (d['id'] in self.devices):
logging.error("duplicate device id "+d['id'])
exit(1)
# check that device has at least some services
if (not 'services' in d):
logging.error("device with no service")
exit(1)
# check that the device has a type
if (not 'type' in d):
logging.error(" device "+d['id']+" has no type specified")
exit(1)
# check that parameter list, if present, is properly formed
if ('params' in d):
if (type(d['params'])!=dict):
logging.error(" device "+d['id']+" has malformed parameters")
exit(1)
# check that there is at least one service specified for this device
if (type(d['services'])!=list):
d['services'] = [d['services']]
if (len(d['services'])<1):
logging.error("must specify at least one service for device "+d['id'])
exit(1)
# import the device
try:
self.device_modules[d['type']] = __import__('pkg.devices.'+d['type'], globals(), locals(), [d['type']], -1)
logging.debug("Loaded code for device type "+d['type'])
except ImportError as e:
logging.exception("Unable to load device %s due to %s."%(d['type'],e))
raise ImportError
# now make an object of this device instance
try:
device_constructor = getattr(self.device_modules[d['type']], d['type'])
logging.debug("Created object for device "+d['type']+":"+d['id'])
except:
logging.exception("ERROR: could not load class for device "+d['type'])
exit(1)
device_object = device_constructor(d['id'],d.get('params'))
self.devices[d['id']] = {}
self.devices[d['id']]['type'] = d['type']
self.devices[d['id']]['services'] = d['services']
self.devices[d['id']]['params'] = d['params']
self.devices[d['id']]['handle'] = device_object
for s in d['services']:
if ((type(s)!=dict) or (not 'id' in s)):
logging.error("Malformed service specification for device "+d['id'])
if (('enable' in s) and (s['enable']!="True" and s['enable']!=True)):
continue
if (not s['id'] in self.services):
logging.error("Service "+s['id']+" in device "+d['id']+" has no available configuration")
exit(1)
s['type'] = self.services[s['id']]['type']
if (s['type'] not in self.service_modules):
try:
self.service_modules[s['type']] = __import__('pkg.services.'+s['type'], globals(), locals(), [s['type']], -1)
logging.debug("Loaded code for service type "+s['type'])
except ImportError as e:
logging.error("Unable to load service "+s['type'])
exit(1)
if (not 'handle' in self.services[s['id']]):
try:
service_constructor = getattr(self.service_modules[s['type']], s['type'])
logging.debug("Created object for service "+s['type']+":"+s['id'])
except:
logging.error("could not load class for service "+d['type'])
exit(1)
service_object = service_constructor(s['id'],self.services[s['id']]['params'])
self.services[s['id']]['handle'] = service_object
else:
service_object = self.services[s['id']]['handle']
# create a queue connecting d to s
q = Queue.Queue()
self.queues.append((q,d['id'],s['id']))
# store a pointer to the queue in the device object
device_object.attach_queue(q)
#print(d)
# store a pointer to the queue in the service object together with parameters
# specific to the device-service connection and also a pointer to the device
service_object.attach_queue(q,s.get('params'),device_object)
# clean up unused services
services_to_delete=[]
for s in self.services:
if (not 'handle' in self.services[s]):
services_to_delete.append(s)
for s in services_to_delete:
logging.debug("Removing "+self.services[s]['type']+":"+s)
del self.services[s]
for d in self.devices:
services_to_retain=[]
for s in self.devices[d]['services']:
if (('enable' in s) and (s['enable']!="True")):
continue
else:
services_to_retain.append(s)
self.devices[d]['services']=services_to_retain
def start(self):
# start all the service threads
for s in self.services:
self.services[s]['handle'].daemon=True
self.services[s]['handle'].start()
# start all the device threads
for d in self.devices:
self.devices[d]['handle'].daemon=True
self.devices[d]['handle'].start()
if __name__ == "__main__":
# read sensor configuration file
parser = argparse.ArgumentParser()
parser.add_argument("config", nargs='?', help="Configuration File, default: config.json", default="config.json")
parser.add_argument("-l", "--loglevel", help="Logging Level, default: WARNING", default="WARNING")
parser.add_argument("-i", "--include", help="List of devices to include, default: all", nargs='*')
#parser.add_argument("-l", "--logfilename", help="Log file, default: stdout", type=argparse.FileType('a', 0), default="-")
current_time = datetime.now().strftime("%Y%m%d")
default_logfilename = "sam_logs/log_%s.txt"%(current_time)
parser.add_argument("-f", "--logfilename", help="Log file, default: logs/log_YYYYmmdd", default=default_logfilename)
args = parser.parse_args()
numeric_level = getattr(logging, args.loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % args.loglevel)
d = os.path.dirname(args.logfilename)
if not os.path.exists(os.path.dirname(args.logfilename)):
os.makedirs(d)
logging.basicConfig(filename=args.logfilename,level=numeric_level,format='%(asctime)s %(levelname)s:%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.info("STARTING SAM")
main = SensorActuatorManager(args.config)
main.start()
while True:
pass