homecontrol/Client.py

60 lines
1.5 KiB
Python
Raw Normal View History

#!/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: %s, type: %s, ts: %s" % (self.id_c, self.type_c, 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):
return self.values
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.id_a = id_a
self.type_a = type_a
self.levels = levels
self.queue = queue.Queue()
super().__init__(id_a, type_a)
def get_info(self):
return "id: %s, type: %s, ts: %s, levels: 0x%x, level: 0x%06x" % (
self.id_a, self.type_a, self.ts, self.levels, 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)