diff --git a/.gitignore b/.gitignore index 7d835d2..f9a9199 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,7 @@ dist cbpi.egg-info logs venv -cbpi/extension/ui \ No newline at end of file +cbpi/extension/ui +node_modules +.DS_STORE +.vscode \ No newline at end of file diff --git a/cbpi/api/sensor.py b/cbpi/api/sensor.py index de0568c..2e25f2b 100644 --- a/cbpi/api/sensor.py +++ b/cbpi/api/sensor.py @@ -35,6 +35,12 @@ class CBPiSensor(metaclass=ABCMeta): def get_unit(self): pass + def push_update(self, value): + try: + self.cbpi.ws.send(dict(topic="sensorstate", id=self.id, value=value)) + except: + logging.error("Faild to push sensor update") + async def start(self): self.running = True diff --git a/cbpi/controller/actor_controller.py b/cbpi/controller/actor_controller.py index 485902d..e2ab188 100644 --- a/cbpi/controller/actor_controller.py +++ b/cbpi/controller/actor_controller.py @@ -4,9 +4,8 @@ from tabulate import tabulate class ActorController(BasicController): def __init__(self, cbpi): - - super(ActorController, self).__init__(cbpi, "actor.json") + self.update_key = "actorupdate" async def on(self, id): try: diff --git a/cbpi/controller/basic_controller.py b/cbpi/controller/basic_controller.py index a4f375b..658a326 100644 --- a/cbpi/controller/basic_controller.py +++ b/cbpi/controller/basic_controller.py @@ -11,6 +11,7 @@ from tabulate import tabulate class BasicController: def __init__(self, cbpi, file): + self.update_key = "" self.name = self.__class__.__name__ self.cbpi = cbpi self.cbpi.register(self) @@ -46,7 +47,7 @@ class BasicController: await self.push_udpate() async def push_udpate(self): - await self.cbpi.bus.fire("sensor/update", data=list(map(lambda x: self.create_dict(x), self.data))) + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda x: self.create_dict(x), self.data)))) def create_dict(self, data): return dict(name=data.get("name"), id=data.get("id"), type=data.get("type"), status=data.get("status"),props=data.get("props", [])) diff --git a/cbpi/controller/dashboard_controller.py b/cbpi/controller/dashboard_controller.py index 032fc22..299d335 100644 --- a/cbpi/controller/dashboard_controller.py +++ b/cbpi/controller/dashboard_controller.py @@ -30,8 +30,3 @@ class DashboardController(): async def delete_content(self, dashboard_id): if os.path.exists('./config/dashboard/cbpi_dashboard_%s.json' % dashboard_id): os.remove('./config/dashboard/cbpi_dashboard_%s.json' % dashboard_id) - - - async def delete_dashboard(self, dashboard_id): - await DashboardContentModel.delete_by_dashboard_id(dashboard_id) - await self.model.delete(dashboard_id) \ No newline at end of file diff --git a/cbpi/controller/kettle_controller.py b/cbpi/controller/kettle_controller.py index eeda472..5ddaf27 100644 --- a/cbpi/controller/kettle_controller.py +++ b/cbpi/controller/kettle_controller.py @@ -5,6 +5,7 @@ class KettleController(BasicController): def __init__(self, cbpi): super(KettleController, self).__init__(cbpi, "kettle.json") + self.update_key = "kettleupdate" self.autostart = False async def on(self, id): diff --git a/cbpi/controller/sensor_controller.py b/cbpi/controller/sensor_controller.py index a542d70..a988976 100644 --- a/cbpi/controller/sensor_controller.py +++ b/cbpi/controller/sensor_controller.py @@ -1,5 +1,18 @@ from cbpi.controller.basic_controller import BasicController +import logging class SensorController(BasicController): def __init__(self, cbpi): super(SensorController, self).__init__(cbpi, "sensor.json") + self.update_key = "sensorupdate" + + def create_dict(self, data): + try: + instance = data.get("instance") + state = state=instance.get_state() + except Exception as e: + logging.error("Faild to crate actor dict {} ".format(e)) + state = dict() + + return dict(name=data.get("name"), id=data.get("id"), type=data.get("type"), state=state,props=data.get("props", [])) + \ No newline at end of file diff --git a/cbpi/extension/dummysensor/__init__.py b/cbpi/extension/dummysensor/__init__.py index fdeb922..65ae6b2 100644 --- a/cbpi/extension/dummysensor/__init__.py +++ b/cbpi/extension/dummysensor/__init__.py @@ -2,7 +2,7 @@ import asyncio import random import re - +import random from aiohttp import web from cbpi.api import * @@ -14,6 +14,11 @@ from cbpi.api import * Property.Actor(label="Param5")]) class CustomSensor(CBPiSensor): + def __init__(self, cbpi, id, props): + super(CustomSensor, self).__init__(cbpi, id, props) + self.value = 0 + + @action(key="Test", parameters=[]) async def action1(self, **kwargs): print("ACTION!", kwargs) @@ -30,8 +35,13 @@ class CustomSensor(CBPiSensor): while self.running is True: print("HALLO") + self.value = random.randint(0,50) + self.push_update(self.value) await asyncio.sleep(1) + def get_state(self): + return dict(value=self.value) + def setup(cbpi): diff --git a/cbpi/sample.py b/cbpi/sample.py deleted file mode 100644 index 2e4f383..0000000 --- a/cbpi/sample.py +++ /dev/null @@ -1,13 +0,0 @@ -from aiohttp import web - -async def handle(request): - name = request.match_info.get('name', "Anonymous") - text = "Hello, " + name - return web.Response(text=text) - -app = web.Application() -app.add_routes([web.get('/', handle), - web.get('/{name}', handle)]) - -if __name__ == '__main__': - web.run_app(app, port="8000") \ No newline at end of file diff --git a/cbpi/websocket.py b/cbpi/websocket.py index 032bb83..482a90d 100644 --- a/cbpi/websocket.py +++ b/cbpi/websocket.py @@ -41,7 +41,6 @@ class CBPiWebSocket: ws = web.WebSocketResponse() await ws.prepare(request) - print(ws) self._clients.add(ws) try: peername = request.transport.get_extra_info('peername') diff --git a/config/actor.json b/config/actor.json index 59aa613..af1f306 100644 --- a/config/actor.json +++ b/config/actor.json @@ -2,7 +2,7 @@ "data": [ { "id": "8BLRqagLicCdEBDdc77Sgr", - "name": "Test", + "name": "Heater", "props": { "Param3": 2, "Param4": "", @@ -10,6 +10,20 @@ }, "state": false, "type": "CustomActor" + }, + { + "id": "Aifjxmw4QdPfU3XbR6iyis", + "name": "Pump1", + "props": {}, + "state": false, + "type": "CustomActor" + }, + { + "id": "HX2bKdobuANehPggYcynnj", + "name": "Pump2", + "props": {}, + "state": false, + "type": "CustomActor" } ] } \ No newline at end of file diff --git a/config/dashboard/cbpi_dashboard_1.json b/config/dashboard/cbpi_dashboard_1.json index 31fd9de..647b400 100644 --- a/config/dashboard/cbpi_dashboard_1.json +++ b/config/dashboard/cbpi_dashboard_1.json @@ -81,7 +81,7 @@ "id": "4d2c8dfe-61a9-433d-83a8-72f74d17e7e5", "name": "Sensor Data", "props": { - "sensor": 1, + "sensor": "8ohkXvFA9UrkHLsxQL38wu", "unit": "\u00b0" }, "type": "Sensor", @@ -92,7 +92,7 @@ "id": "13a6b89d-50c7-4efb-b940-ec174e522314", "name": "Sensor Data", "props": { - "sensor": 1, + "sensor": "8ohkXvFA9UrkHLsxQL38wu", "unit": "\u00b0" }, "type": "Sensor", @@ -103,7 +103,7 @@ "id": "8d171952-791d-4f72-bfc9-dac8714b839f", "name": "Sensor Data", "props": { - "sensor": 1, + "sensor": "8ohkXvFA9UrkHLsxQL38wu", "unit": "\u00b0" }, "type": "Sensor", @@ -186,11 +186,11 @@ "id": "3ec3e5d8-f82e-40c1-8c41-cb8286659d3b", "name": "Led", "props": { - "actor": 1 + "actor": "8BLRqagLicCdEBDdc77Sgr" }, "type": "Led", - "x": 245, - "y": 220 + "x": 240, + "y": 210 }, { "id": "2e325539-6ed9-4e0d-b1dc-de860c47a1be", @@ -199,18 +199,8 @@ "actor": "8BLRqagLicCdEBDdc77Sgr" }, "type": "ActorButton", - "x": 210, - "y": 380 - }, - { - "id": "78b85989-c1bc-47e3-ad7b-0defeabb9bdc", - "name": "Pump", - "props": { - "actor": "8BLRqagLicCdEBDdc77Sgr" - }, - "type": "ActorButton", - "x": 305, - "y": 380 + "x": 120, + "y": 255 }, { "id": "3c3f81d0-cdfd-4521-a2fe-2f039f17b583", @@ -245,25 +235,15 @@ "x": 615, "y": 280 }, - { - "id": "3a9e422f-8d55-4360-8f16-807f9a657988", - "name": "Steps", - "props": { - "width": "400" - }, - "type": "Steps", - "x": 20, - "y": 430 - }, { "id": "3be00e94-4e06-4a6b-9b8d-c832be73386a", "name": "Led", "props": { - "actor": 1 + "actor": "Aifjxmw4QdPfU3XbR6iyis" }, "type": "Led", - "x": 440, - "y": 215 + "x": 435, + "y": 210 }, { "id": "d896b230-8dab-4c33-b73a-1dd74e6de906", @@ -274,10 +254,34 @@ "type": "Led", "x": 625, "y": 215 + }, + { + "id": "adfe673c-1778-4980-b751-5c613c5c5b76", + "name": "Pump1", + "props": { + "actor": "Aifjxmw4QdPfU3XbR6iyis" + }, + "type": "ActorButton", + "x": 360, + "y": 360 + }, + { + "id": "0e1ea214-9ae0-47c2-902d-cae0947ba8a1", + "name": "Pump2", + "props": { + "actor": "HX2bKdobuANehPggYcynnj" + }, + "type": "ActorButton", + "x": 810, + "y": 320 } ], "pathes": [ { + "condition": [ + "8BLRqagLicCdEBDdc77Sgr", + "Aifjxmw4QdPfU3XbR6iyis" + ], "coordinates": [ [ 305, @@ -291,6 +295,9 @@ "id": "49e7684e-21a3-4e0b-8e94-60f95abee80f" }, { + "condition": [ + "8BLRqagLicCdEBDdc77Sgr" + ], "coordinates": [ [ 400, @@ -304,6 +311,9 @@ "id": "5ba909c1-49a9-46e5-a6d0-1d0350c37aa4" }, { + "condition": [ + "Aifjxmw4QdPfU3XbR6iyis" + ], "coordinates": [ [ 255, @@ -329,14 +339,21 @@ "id": "aed2d4d3-b99e-4af5-b8cf-d92d47721be4" }, { + "condition": [ + "HX2bKdobuANehPggYcynnj" + ], "coordinates": [ [ 685, 275 ], [ - 805, + 795, 275 + ], + [ + 795, + 375 ] ], "id": "176fed29-56c2-4534-9cab-8c328d0e138c" diff --git a/config/kettle.json b/config/kettle.json index 0b462d7..dcbb3ff 100644 --- a/config/kettle.json +++ b/config/kettle.json @@ -4,7 +4,7 @@ "agitator": "", "heater": "8BLRqagLicCdEBDdc77Sgr", "id": "oHxKz3z5RjbsxfSz6KUgov", - "name": "Test", + "name": "Test1111111", "props": {}, "sensor": "", "state": {}, @@ -40,6 +40,28 @@ "state": {}, "target_temp": null, "type": "CustomKettleLogic" + }, + { + "agitator": "", + "heater": "", + "id": "ZfF2N2UnEHtgExNgZJyF5i", + "name": "Test", + "props": {}, + "sensor": "", + "state": {}, + "target_temp": null, + "type": "CustomKettleLogic" + }, + { + "agitator": "", + "heater": "8BLRqagLicCdEBDdc77Sgr", + "id": "oTivUB7LueLeUWoZAnLhwp", + "name": "", + "props": {}, + "sensor": "", + "state": {}, + "target_temp": null, + "type": "" } ] } \ No newline at end of file diff --git a/config/sensor.json b/config/sensor.json index 6346c5f..dc4fcb2 100644 --- a/config/sensor.json +++ b/config/sensor.json @@ -1,5 +1,13 @@ { "data": [ - + { + "id": "8ohkXvFA9UrkHLsxQL38wu", + "name": "Test1112222", + "props": {}, + "state": { + "value": 0 + }, + "type": "CustomSensor" + } ] } \ No newline at end of file diff --git a/config/step_data.json b/config/step_data.json index ed352dd..0eaecf6 100644 --- a/config/step_data.json +++ b/config/step_data.json @@ -3,6 +3,18 @@ "name": "WOOHOo" }, "profile": [ - + { + "id": "MSXuATqL56EAeCXrg3XLuY", + "name": "Test", + "props": { + "Param1": 123, + "Param2": "HALLO", + "Param3": 1, + "count": 6, + "wohoo": 0 + }, + "status": "D", + "type": "CustomStep2" + } ] } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index d96f579..3ee8248 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,6 @@ aiojobs>=0.2.2 aiosqlite>=0.7.0 cryptography>=2.3.1 voluptuous>=0.11.5 -pyfiglet>=0.7.6 \ No newline at end of file +pyfiglet>=0.7.6 +shortuuid==1.0.1 +tabulate==0.8.7 \ No newline at end of file