-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.py
108 lines (97 loc) · 3.91 KB
/
webapp.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
#!/usr/bin/env python3
import os
import sys
import time
import yaml
import threading
import multiprocessing
import cherrypy
import sensors
class SensorServ:
def __init__(self):
self.sensor_data = {'basic':{},'advanced':{}}
self.load_config()
self.queue = multiprocessing.Queue()
self.collector_queue = multiprocessing.Queue()
self.lock = multiprocessing.Lock()
self.start_collector()
def load_config(self):
f = open(os.path.join(os.path.dirname(__file__), 'config.yml'), 'r')
self.config = yaml.load(f,Loader=yaml.FullLoader)
f.close()
def start_collector(self):
self.sensor_monitor_thread = threading.Thread(target=self.sensor_monitor)
self.collector_thread = threading.Thread(target=self.collector)
not_sensors = ['modules','os', 'glob']
self.active_sensors = []
sensors_list = dir(sensors)
sensors_list.reverse()
for sensor in sensors_list:
if not (sensor.startswith('_') or sensor in not_sensors):
if sensor in self.config['devices']:
self.active_sensors.append(sensor)
self.sensor_monitor_thread.start()
self.collector_thread.start()
def sensor_monitor(self):
threads = {}
while True:
for sensor in self.active_sensors:
if not sensor in threads:
sensor_module = getattr(sensors,sensor)
if sensor in self.config['device_config']:
config = self.config['device_config'][sensor]
else:
config = {}
threads[sensor] = {}
threads[sensor]['class'] = getattr(sensor_module,sensor)(self.queue,config)
threads[sensor]['thread'] = threading.Thread(target=threads[sensor]['class'].poll)
threads[sensor]['thread'].start()
# print(sensor)
# time.sleep(1)
else:
if threads[sensor]['thread'].is_alive()==False and threads[sensor]['class'].auto_restart==True:
threads[sensor]['thread'].join(1)
del threads[sensor]
time.sleep(1)
def collector(self):
basic_collector_data = {}
collector_data = {}
while True:
next_poll_time = time.time()+self.config['collector_poll_time']
while self.queue.qsize()>0:
(sensor_name,data) = self.queue.get()
collector_data[sensor_name] = data
# if self.collector_queue.qsize()>=1:
# self.collector_queue.get(0.05)
# self.collector_queue.put(collector_data)
for sensor in self.active_sensors[::-1]:
if sensor in collector_data:
basic_collector_data.update(collector_data[sensor])
self.lock.acquire()
self.sensor_data['basic'] = basic_collector_data
self.sensor_data['advanced'] = collector_data
self.lock.release()
wait_time = next_poll_time-time.time()
if wait_time>0:
time.sleep(wait_time)
@cherrypy.expose
@cherrypy.tools.json_out()
def get_stats(self):
# if self.collector_queue.qsize()==1:
# self.sensor_data = self.collector_queue.get_nowait()
self.lock.acquire()
out = dict(self.sensor_data['basic'])
self.lock.release()
return(out)
@cherrypy.expose
@cherrypy.tools.json_out()
def get_adv_stats(self):
# if self.collector_queue.qsize()==1:
# self.sensor_data = self.collector_queue.get_nowait()
self.lock.acquire()
out = dict(self.sensor_data['advanced'])
self.lock.release()
return(out)
if __name__ == '__main__':
cherrypy.server.socket_host = '0.0.0.0'
cherrypy.quickstart(SensorServ())