-
Notifications
You must be signed in to change notification settings - Fork 131
/
collect_server_http.py
70 lines (57 loc) · 2.37 KB
/
collect_server_http.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
import re
import cherrypy
from jinja2 import Environment, FileSystemLoader
from coapthon import defines
from coapthon.client.helperclient import HelperClient
from coapthon.resource_directory.databaseManager import DatabaseManager
env = Environment(loader=FileSystemLoader('templates'))
class Root(object):
def __init__(self):
self._rd = (defines.RD_HOST, defines.RD_PORT)
self._temperature_sensors = {}
self._dbManager = DatabaseManager()
self._clientPool = []
self._responses = []
self._read_rd()
@staticmethod
def _read_rd_sensor(client, path):
response = client.get(path)
link_format = response.payload
sensors = []
if response.payload is not None:
while len(link_format) > 0:
pattern = "<([^>]*)>;"
result = re.match(pattern, link_format)
if result is None:
break
path = result.group(1)
sensors.append(path)
link_format = link_format[result.end(1) + 2:]
return sensors
def client_callback_observe(self, response): # pragma: no cover
self._responses.append(response.payload)
def _read_rd(self):
client = HelperClient(self._rd)
path = 'rd-lookup/res?rt=temperature'
self._temperature_sensors["temperature"] = self._read_rd_sensor(client, path)
path = 'rd-lookup/res?rt=humidity'
self._temperature_sensors["humidity"] = self._read_rd_sensor(client, path)
path = 'rd-lookup/res?rt=light'
self._temperature_sensors["light"] = self._read_rd_sensor(client, path)
for k, lst_v in self._temperature_sensors.items():
for v in lst_v:
pattern = "coap://([0-9a-z.]*):([0-9]*)(/[a-zA-Z0-9/]*)"
match = re.match(pattern=pattern, string=v)
if match is not None:
host = match.group(1)
port = int(match.group(2))
path = match.group(3)
client_sensor = HelperClient((host, port))
client_sensor.observe(path, self.client_callback_observe)
self._clientPool.append(client_sensor)
@cherrypy.expose
def index(self):
msg = self._responses.pop(0)
return msg
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/')