homecontrol/Client.py

65 lines
1.7 KiB
Python

#!/usr/bin/env python3
import queue
import time
class Client:
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()
def get_info(self):
return {"id": self.id_c, "type": self.type_c, "ts": self.ts}
class Sensor(Client):
def __init__(self, id_s, type_s, num_values):
self.num_values = num_values
self.values = {}
super().__init__(id_s, type_s)
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
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):
def __init__(self, id_a, type_a, levels):
self.type_a = type_a
self.levels = levels
self.queue = queue.Queue()
super().__init__(id_a, type_a)
def get_info(self):
return {"id": self.id_c, "type": self.type_a, "ts": self.ts,
"levels": self.levels, "level": self.level}
def add_queue(self, command, data):
# print("add_queue: {\"command\": %s, \"data\": %s}" % (command, data))
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):
return(self.level)