2019-11-03 01:36:23 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-11-03 13:57:14 +01:00
|
|
|
import queue
|
2019-11-03 01:36:23 +01:00
|
|
|
import time
|
|
|
|
|
|
|
|
class Client:
|
2019-11-03 13:57:14 +01:00
|
|
|
def __init__(self, id_c, type_c="default"):
|
|
|
|
self.type_c = type_c
|
|
|
|
self.id_c = id_c
|
|
|
|
self.ts = time.time()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.ts = time.time()
|
2019-11-03 01:36:23 +01:00
|
|
|
|
|
|
|
def get_info(self):
|
2019-11-03 16:39:08 +01:00
|
|
|
return {"id": self.id_c, "type": self.type_c, "ts": self.ts}
|
2019-11-03 01:36:23 +01:00
|
|
|
|
|
|
|
class Sensor(Client):
|
2019-11-03 13:57:14 +01:00
|
|
|
def __init__(self, id_s, type_s, num_values):
|
2019-11-03 01:36:23 +01:00
|
|
|
self.num_values = num_values
|
|
|
|
self.values = {}
|
2019-11-03 13:57:14 +01:00
|
|
|
super().__init__(id_s, type_s)
|
2019-11-03 01:36:23 +01:00
|
|
|
|
2019-11-05 01:18:25 +01:00
|
|
|
def get_values(self, single=False):
|
|
|
|
ret = {}
|
|
|
|
ret["id"] = self.id_c
|
|
|
|
ret["type"] = self.type_c
|
|
|
|
if single:
|
|
|
|
ret["values"] = self.values[next(iter(self.values))]
|
|
|
|
else:
|
|
|
|
ret["values"] = self.values
|
|
|
|
return ret
|
2019-11-03 01:36:23 +01:00
|
|
|
|
|
|
|
def add_value(self, value):
|
|
|
|
ts = time.time()
|
|
|
|
self.values[ts] = value
|
|
|
|
|
|
|
|
while len(self.values) > self.num_values:
|
|
|
|
self.values.pop(next(iter(self.values)))
|
|
|
|
|
|
|
|
return len(self.values)
|
|
|
|
|
|
|
|
class Actor(Client):
|
2019-11-03 13:57:14 +01:00
|
|
|
def __init__(self, id_a, type_a, levels):
|
|
|
|
self.type_a = type_a
|
2019-11-03 01:36:23 +01:00
|
|
|
self.levels = levels
|
2019-11-03 13:57:14 +01:00
|
|
|
self.queue = queue.Queue()
|
|
|
|
super().__init__(id_a, type_a)
|
2019-11-03 01:36:23 +01:00
|
|
|
|
|
|
|
|
2019-11-03 13:57:14 +01:00
|
|
|
def get_info(self):
|
2019-11-05 01:18:25 +01:00
|
|
|
return {"id": self.id_c, "type": self.type_a, "ts": self.ts,
|
2019-11-03 16:39:08 +01:00
|
|
|
"levels": self.levels, "level": self.level}
|
2019-11-03 13:57:14 +01:00
|
|
|
|
|
|
|
def add_queue(self, command, data):
|
2019-11-05 01:18:25 +01:00
|
|
|
# print("add_queue: {\"command\": %s, \"data\": %s}" % (command, data))
|
2019-11-03 13:57:14 +01:00
|
|
|
self.queue.put({"command": command, "data": data})
|
|
|
|
|
|
|
|
def get_queue(self):
|
|
|
|
ret = {}
|
|
|
|
if not self.queue.empty():
|
|
|
|
ret = self.queue.get()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_level(self):
|
2019-11-03 01:36:23 +01:00
|
|
|
return(self.level)
|