homecontrol: add more ws features
- sending commands - requesting status - remove Node_WS.name
This commit is contained in:
parent
7e6ee25a6d
commit
577dd4f78b
9 changed files with 26 additions and 44 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,3 +6,4 @@ __pycache__
|
|||
*.swp
|
||||
db.sqlite
|
||||
poetry.lock
|
||||
homecontrol.egg-info
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -1 +0,0 @@
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
[console_scripts]
|
||||
homecontrol = homecontrol.homecontrol:main
|
||||
|
|
@ -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
|
|
@ -1 +0,0 @@
|
|||
homecontrol
|
|
@ -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:
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue