homecontrol: add more ws features

- sending commands
- requesting status
- remove Node_WS.name
This commit is contained in:
Konstantin Koslowski 2020-03-01 22:23:58 +01:00
parent 7e6ee25a6d
commit 577dd4f78b
9 changed files with 26 additions and 44 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@ __pycache__
*.swp *.swp
db.sqlite db.sqlite
poetry.lock poetry.lock
homecontrol.egg-info

View file

@ -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

View file

@ -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

View file

@ -1 +0,0 @@

View file

@ -1,3 +0,0 @@
[console_scripts]
homecontrol = homecontrol.homecontrol:main

View file

@ -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

View file

@ -1 +0,0 @@
homecontrol

View file

@ -20,7 +20,7 @@ PORT_WS = 8100
HOST_WS = "0.0.0.0" HOST_WS = "0.0.0.0"
PORT_RT = 8200 PORT_RT = 8200
HOST_RT = "0.0.0.0" HOST_RT = "0.0.0.0"
# TIMEOUT_CLIENT = 10 TIMEOUT_STATUS = 10
core = None core = None
# sensors # sensors
@ -139,7 +139,9 @@ class Core:
actorId = content.get("id") actorId = content.get("id")
command = content.get("command") command = content.get("command")
data = content.get("data") 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: except Exception as ex:
self.logger.error(f"Exception Type:{type(ex).__name__}, args: {ex.args}") self.logger.error(f"Exception Type:{type(ex).__name__}, args: {ex.args}")
abort(400) abort(400)
@ -228,12 +230,12 @@ class Core:
async def status_thread(self): async def status_thread(self):
while True: while True:
for n in self.cws.nodes: 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() status = self.cws.nodes[n].get_status()
if status and len(status) > 0: if status and len(status) > 0:
self.actor_add_level(n, status["level"], status["ts"]) 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): def actor_add_level(self, actorId, level, ts):
@ -248,8 +250,17 @@ class Core:
self.db['actor_levels'].insert(q) self.db['actor_levels'].insert(q)
except Exception as ex: except Exception as ex:
self.logger.error(f"Exception Type:{type(ex).__name__}, args: {ex.args}") 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): def actor_add_queue(self, actorId, command, data):
try: try:

View file

@ -5,11 +5,10 @@ import time
import websockets import websockets
class Node_WS: class Node_WS:
def __init__(self, debug, nodeid, nodetype, name, maxlevel, ws): def __init__(self, debug, nodeid, nodetype, maxlevel, ws):
self.debug = debug self.debug = debug
self.nodeid = nodeid self.nodeid = nodeid
self.nodetype = nodetype self.nodetype = nodetype
self.name = name
self.maxlevel = maxlevel self.maxlevel = maxlevel
self.ws = ws self.ws = ws
self.status = {} self.status = {}
@ -26,7 +25,6 @@ class Node_WS:
ret = {} ret = {}
ret["nodeid"] = self.nodeid ret["nodeid"] = self.nodeid
ret["nodetype"] = self.nodetype ret["nodetype"] = self.nodetype
ret["name"] = self.name
ret["maxlevel"] = self.maxlevel ret["maxlevel"] = self.maxlevel
return ret return ret
@ -62,21 +60,19 @@ class Core_WS:
ret = self.nodes[nodeid].get_info() ret = self.nodes[nodeid].get_info()
return ret 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): async def node_register(self, nodeid, nodetype, maxlevel, websocket):
self.nodes[nodeid] = Node_WS(self.debug, nodeid, nodetype, name, maxlevel, websocket) self.nodes[nodeid] = Node_WS(self.debug, nodeid, nodetype, maxlevel, websocket)
self.logger.info(f"node registered: {nodeid} :: {name}") self.logger.info(f"node registered: {nodeid}")
async def node_unregister(self, nodeid): async def node_unregister(self, nodeid):
if nodeid and nodeid in self.nodes: if nodeid and nodeid in self.nodes:
self.nodes.pop(nodeid) self.nodes.pop(nodeid)
self.logger.info(f"node unregistered: {nodeid.split('-')[0]}") 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: if nodeid in self.nodes:
m = {"command": command, "args": args} m = {"command": command, "data": data}
message = json.dumps(m) message = json.dumps(m)
self.logger.debug(f"> {nodeid} {command}") self.logger.debug(f"> {nodeid} {command}")
if self.nodes[nodeid].ws.open: if self.nodes[nodeid].ws.open:
@ -84,6 +80,8 @@ class Core_WS:
else: else:
self.logger.warning(f"send_command({nodeid}): ws not open") self.logger.warning(f"send_command({nodeid}): ws not open")
self.node_unregister(nodeid) self.node_unregister(nodeid)
else:
self.logger.warning(f"send_command({nodeid}) unknown")
async def handler(self, websocket, path): async def handler(self, websocket, path):
nodeid = None nodeid = None
@ -99,9 +97,8 @@ class Core_WS:
if cmd == "register": if cmd == "register":
if not nodeid in self.nodes: if not nodeid in self.nodes:
nodetype = m.get("nodetype") nodetype = m.get("nodetype")
name = m.get("name")
maxlevel = m.get("maxlevel") maxlevel = m.get("maxlevel")
await self.node_register(nodeid, nodetype, name, maxlevel, await self.node_register(nodeid, nodetype, maxlevel,
websocket) websocket)
ret = {"command": "registered"} ret = {"command": "registered"}
await websocket.send(json.dumps(ret)) await websocket.send(json.dumps(ret))