diff --git a/.gitignore b/.gitignore index 7c32132..37c0291 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ __pycache__ *.swp db.sqlite poetry.lock +homecontrol.egg-info diff --git a/homecontrol.egg-info/PKG-INFO b/homecontrol.egg-info/PKG-INFO deleted file mode 100644 index fd55f91..0000000 --- a/homecontrol.egg-info/PKG-INFO +++ /dev/null @@ -1,11 +0,0 @@ -Metadata-Version: 1.2 -Name: homecontrol -Version: 0.1.0 -Summary: central application to connect iot devices -Home-page: UNKNOWN -Author: Konstantin Koslowski -Author-email: konstantin.koslowski@gmail.com -License: UNKNOWN -Description: UNKNOWN -Platform: UNKNOWN -Requires-Python: >=3.8,<4.0 diff --git a/homecontrol.egg-info/SOURCES.txt b/homecontrol.egg-info/SOURCES.txt deleted file mode 100644 index 9635c5b..0000000 --- a/homecontrol.egg-info/SOURCES.txt +++ /dev/null @@ -1,8 +0,0 @@ -setup.py -homecontrol/homecontrol.py -homecontrol.egg-info/PKG-INFO -homecontrol.egg-info/SOURCES.txt -homecontrol.egg-info/dependency_links.txt -homecontrol.egg-info/entry_points.txt -homecontrol.egg-info/requires.txt -homecontrol.egg-info/top_level.txt \ No newline at end of file diff --git a/homecontrol.egg-info/dependency_links.txt b/homecontrol.egg-info/dependency_links.txt deleted file mode 100644 index 8b13789..0000000 --- a/homecontrol.egg-info/dependency_links.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/homecontrol.egg-info/entry_points.txt b/homecontrol.egg-info/entry_points.txt deleted file mode 100644 index bbdbc60..0000000 --- a/homecontrol.egg-info/entry_points.txt +++ /dev/null @@ -1,3 +0,0 @@ -[console_scripts] -homecontrol = homecontrol.homecontrol:main - diff --git a/homecontrol.egg-info/requires.txt b/homecontrol.egg-info/requires.txt deleted file mode 100644 index 96ec3f6..0000000 --- a/homecontrol.egg-info/requires.txt +++ /dev/null @@ -1,3 +0,0 @@ -dataset<2.0.0,>=1.2.2 -flask<2.0.0,>=1.1.1 -requests<3.0.0,>=2.23.0 diff --git a/homecontrol.egg-info/top_level.txt b/homecontrol.egg-info/top_level.txt deleted file mode 100644 index 3022831..0000000 --- a/homecontrol.egg-info/top_level.txt +++ /dev/null @@ -1 +0,0 @@ -homecontrol diff --git a/homecontrol/core.py b/homecontrol/core.py index 4af8bd1..ed971d1 100755 --- a/homecontrol/core.py +++ b/homecontrol/core.py @@ -20,7 +20,7 @@ PORT_WS = 8100 HOST_WS = "0.0.0.0" PORT_RT = 8200 HOST_RT = "0.0.0.0" -# TIMEOUT_CLIENT = 10 +TIMEOUT_STATUS = 10 core = None # sensors @@ -139,7 +139,9 @@ class Core: actorId = content.get("id") command = content.get("command") data = content.get("data") - self.actor_add_queue(actorId, command, data) + loop = asyncio.new_event_loop() + loop.run_until_complete(self.cws.node_send_command(actorId, command, data)) + # self.actor_add_queue(actorId, command, data) except Exception as ex: self.logger.error(f"Exception Type:{type(ex).__name__}, args: {ex.args}") abort(400) @@ -228,12 +230,12 @@ class Core: async def status_thread(self): while True: for n in self.cws.nodes: - await self.cws.node_get_status(n) + await self.cws.node_send_command(n, "get_status", {}) status = self.cws.nodes[n].get_status() if status and len(status) > 0: self.actor_add_level(n, status["level"], status["ts"]) - await asyncio.sleep(1) + await asyncio.sleep(TIMEOUT_STATUS) def actor_add_level(self, actorId, level, ts): @@ -248,8 +250,17 @@ class Core: self.db['actor_levels'].insert(q) except Exception as ex: self.logger.error(f"Exception Type:{type(ex).__name__}, args: {ex.args}") + finally: + return ret + + + async def actor_send_command(self, actorId, command, data): + try: + if self.db["actors"].find_one(actorId=actorId): + await self.cws.node_send_command(n, command, data) + except Exception as ex: + self.logger.error(f"Exception Type:{type(ex).__name__}, args: {ex.args}") - return ret def actor_add_queue(self, actorId, command, data): try: diff --git a/homecontrol/core_ws.py b/homecontrol/core_ws.py index f8591cd..0d63558 100644 --- a/homecontrol/core_ws.py +++ b/homecontrol/core_ws.py @@ -5,11 +5,10 @@ import time import websockets class Node_WS: - def __init__(self, debug, nodeid, nodetype, name, maxlevel, ws): + def __init__(self, debug, nodeid, nodetype, maxlevel, ws): self.debug = debug self.nodeid = nodeid self.nodetype = nodetype - self.name = name self.maxlevel = maxlevel self.ws = ws self.status = {} @@ -26,7 +25,6 @@ class Node_WS: ret = {} ret["nodeid"] = self.nodeid ret["nodetype"] = self.nodetype - ret["name"] = self.name ret["maxlevel"] = self.maxlevel return ret @@ -62,21 +60,19 @@ class Core_WS: ret = self.nodes[nodeid].get_info() return ret - async def node_get_status(self, nodeid): - await self.node_send_command("get_status", nodeid, {}) - async def node_register(self, nodeid, nodetype, name, maxlevel, websocket): - self.nodes[nodeid] = Node_WS(self.debug, nodeid, nodetype, name, maxlevel, websocket) - self.logger.info(f"node registered: {nodeid} :: {name}") + async def node_register(self, nodeid, nodetype, maxlevel, websocket): + self.nodes[nodeid] = Node_WS(self.debug, nodeid, nodetype, maxlevel, websocket) + self.logger.info(f"node registered: {nodeid}") async def node_unregister(self, nodeid): if nodeid and nodeid in self.nodes: self.nodes.pop(nodeid) self.logger.info(f"node unregistered: {nodeid.split('-')[0]}") - async def node_send_command(self, command, nodeid, args): + async def node_send_command(self, nodeid, command, data): if nodeid in self.nodes: - m = {"command": command, "args": args} + m = {"command": command, "data": data} message = json.dumps(m) self.logger.debug(f"> {nodeid} {command}") if self.nodes[nodeid].ws.open: @@ -84,6 +80,8 @@ class Core_WS: else: self.logger.warning(f"send_command({nodeid}): ws not open") self.node_unregister(nodeid) + else: + self.logger.warning(f"send_command({nodeid}) unknown") async def handler(self, websocket, path): nodeid = None @@ -99,9 +97,8 @@ class Core_WS: if cmd == "register": if not nodeid in self.nodes: nodetype = m.get("nodetype") - name = m.get("name") maxlevel = m.get("maxlevel") - await self.node_register(nodeid, nodetype, name, maxlevel, + await self.node_register(nodeid, nodetype, maxlevel, websocket) ret = {"command": "registered"} await websocket.send(json.dumps(ret))