diff --git a/cbpi/controller/basic_controller.py b/cbpi/controller/basic_controller.py index aedd77e..9df4915 100644 --- a/cbpi/controller/basic_controller.py +++ b/cbpi/controller/basic_controller.py @@ -90,7 +90,8 @@ class BasicController: type = item["type"] clazz = self.types[type]["class"] - item["instance"] = clazz(self.cbpi, item["id"], {}) + print("#####",item) + item["instance"] = clazz(self.cbpi, item["id"], item["props"]) await item["instance"].start() item["instance"].task = self._loop.create_task(item["instance"].run()) logging.info("{} started {}".format(self.name, id)) @@ -122,9 +123,9 @@ class BasicController: async def update(self, id, data) -> dict: logging.info("{} Get Update".format(self.name)) await self.stop(id) - if self.autostart is True: - await self.start(id) self.data = list(map(lambda old: {**old, **data} if old["id"] == id else old, self.data)) + if self.autostart is True: + await self.start(id) await self.save() return self.find_by_id(id) diff --git a/cbpi/extension/dummyactor/__init__.py b/cbpi/extension/dummyactor/__init__.py index a999f04..d27d216 100644 --- a/cbpi/extension/dummyactor/__init__.py +++ b/cbpi/extension/dummyactor/__init__.py @@ -3,25 +3,10 @@ from unittest.mock import MagicMock, patch from cbpi.api import * - logger = logging.getLogger(__name__) -try: - import RPi.GPIO as GPIO -except Exception: - logger.error("Failed to load RPi.GPIO. Using Mock") - MockRPi = MagicMock() - modules = { - "RPi": MockRPi, - "RPi.GPIO": MockRPi.GPIO - } - patcher = patch.dict("sys.modules", modules) - patcher.start() - import RPi.GPIO as GPIO - - @parameters([]) -class CustomActor(CBPiActor): +class DummyActor(CBPiActor): my_name = "" # Custom property which can be configured by the user @@ -30,18 +15,17 @@ class CustomActor(CBPiActor): print("ACTION !", kwargs) self.my_name = kwargs.get("name") pass - - def init(self): - print("INIT") - self.state = False - pass + + async def start(self): + await super().start() async def on(self, power=0): - logger.info("ACTOR 1111 %s ON" % self.id) + logger.info("ACTOR %s ON " % self.id) self.state = True async def off(self): logger.info("ACTOR %s OFF " % self.id) + self.state = False def get_state(self): @@ -61,4 +45,4 @@ def setup(cbpi): :return: ''' - cbpi.plugin.register("CustomActor", CustomActor) + cbpi.plugin.register("DummyActor", DummyActor) diff --git a/cbpi/extension/gpioactor/__init__.py b/cbpi/extension/gpioactor/__init__.py new file mode 100644 index 0000000..50a15ae --- /dev/null +++ b/cbpi/extension/gpioactor/__init__.py @@ -0,0 +1,110 @@ +import logging +from unittest.mock import MagicMock, patch + +from cbpi.api import * + + +logger = logging.getLogger(__name__) + +try: + import RPi.GPIO as GPIO +except Exception: + logger.error("Failed to load RPi.GPIO. Using Mock") + MockRPi = MagicMock() + modules = { + "RPi": MockRPi, + "RPi.GPIO": MockRPi.GPIO + } + patcher = patch.dict("sys.modules", modules) + patcher.start() + import RPi.GPIO as GPIO + + +@parameters([Property.Select(label="GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]), Property.Select(label="Inverted", options=["Yes", "No"])]) +class GPIOActor(CBPiActor): + + + def get_GPIO_state(self, state): + # ON + if state == 1: + return 0 if self.inverted == False else 1 + # OFF + if state == 0: + return 1 if self.inverted == False else 0 + + async def start(self): + await super().start() + self.gpio = self.props.get("GPIO") + self.inverted = True if self.props.get("Inverted", "No") == "Yes" else False + GPIO.setup(self.gpio, GPIO.OUT) + GPIO.output(self.gpio, self.get_GPIO_state(0)) + self.state = False + pass + + async def on(self, power=0): + logger.info("ACTOR %s ON - GPIO %s " % (self.id, self.gpio)) + GPIO.output(self.gpio, self.get_GPIO_state(1)) + self.state = True + + async def off(self): + logger.info("ACTOR %s OFF - GPIO %s " % (self.id, self.gpio)) + GPIO.output(self.gpio, self.get_GPIO_state(0)) + self.state = False + + def get_state(self): + + return self.state + + async def run(self): + pass + +@parameters([Property.Select(label="GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]), Property.Number("Frequency", configurable=True)]) +class GPIOPWMActor(CBPiActor): + + # Custom property which can be configured by the user + @action("test", parameters={}) + async def power(self, **kwargs): + self.p.ChangeDutyCycle(1) + + + async def start(self): + await super().start() + self.gpio = self.props.get("GPIO") + self.frequency = self.props.get("Frequency") + GPIO.setup(self.gpio, GPIO.OUT) + GPIO.output(self.gpio, 0) + self.state = False + pass + + async def on(self, power=0): + logger.info("PWM ACTOR %s ON - GPIO %s " % (self.id, self.gpio)) + try: + self.p = GPIO.PWM(int(self.gpio), float(self.frequency)) + self.p.start(1) + except: + pass + self.state = True + + async def off(self): + logger.info("PWM ACTOR %s OFF - GPIO %s " % (self.id, self.gpio)) + self.p.stop() + self.state = False + + def get_state(self): + return self.state + + async def run(self): + pass + +def setup(cbpi): + + ''' + This method is called by the server during startup + Here you need to register your plugins at the server + + :param cbpi: the cbpi core + :return: + ''' + + cbpi.plugin.register("GPIOActor", GPIOActor) + cbpi.plugin.register("GPIOPWMActor", GPIOPWMActor) diff --git a/cbpi/extension/gpioactor/config.yaml b/cbpi/extension/gpioactor/config.yaml new file mode 100644 index 0000000..c410d4d --- /dev/null +++ b/cbpi/extension/gpioactor/config.yaml @@ -0,0 +1,3 @@ +name: GPIOActor +version: 4 +active: true \ No newline at end of file diff --git a/cheat_sheet.txt b/cheat_sheet.txt index 7a1664e..53cea8a 100644 --- a/cheat_sheet.txt +++ b/cheat_sheet.txt @@ -8,4 +8,8 @@ python3 setup.py sdist twine upload dist/* +# Checkout Pull Request +git fetch origin pull/ID/head:BRANCHNAME +git checkout BRANCHNAME + diff --git a/config/actor.json b/config/actor.json index c2aad86..7e3bd1d 100644 --- a/config/actor.json +++ b/config/actor.json @@ -1,57 +1,24 @@ { "data": [ { - "id": "8BLRqagLicCdEBDdc77Sgr", - "name": "Heater", + "id": "YwGzXvWMpmbLb6XobesL8n", + "name": "111", "props": { - "Param3": 2, - "Param4": "", - "Param5": "8BLRqagLicCdEBDdc77Sgr" + "GPIO": 4, + "Inverted": "Yes" }, "state": false, - "type": "CustomActor" + "type": "GPIOActor" }, { - "id": "Aifjxmw4QdPfU3XbR6iyis", - "name": "Pump1", - "props": {}, - "state": true, - "type": "CustomActor" - }, - { - "id": "HX2bKdobuANehPggYcynnj", - "name": "Pump2", - "props": {}, + "id": "EsmZwWi9Qp3bzmXqq7N3Ly", + "name": "HALO", + "props": { + "Frequency": "20", + "GPIO": 5 + }, "state": false, - "type": "CustomActor" - }, - { - "id": "NjammuygecdvMpoGYc3rXt", - "name": "Heater Boil", - "props": {}, - "state": false, - "type": "CustomActor" - }, - { - "id": "j4PnSfuWRhgZDgrQScLN7e", - "name": "Vent1", - "props": {}, - "state": true, - "type": "CustomActor" - }, - { - "id": "ZGJqoybWv3eWrEeGJLopFs", - "name": "Water In", - "props": {}, - "state": false, - "type": "CustomActor" - }, - { - "id": "NfYJEWbTXPUSUQzS83dfAn", - "name": "Vent Out", - "props": {}, - "state": false, - "type": "CustomActor" + "type": "GPIOPWMActor" } ] } \ No newline at end of file diff --git a/config/kettle.json b/config/kettle.json index ff907ca..74b96b1 100644 --- a/config/kettle.json +++ b/config/kettle.json @@ -7,9 +7,7 @@ "name": "MashTun", "props": {}, "sensor": "8ohkXvFA9UrkHLsxQL38wu", - "state": { - "running": false - }, + "state": {}, "target_temp": 52, "type": "CustomKettleLogic" }, @@ -36,9 +34,7 @@ "name": "Boil", "props": {}, "sensor": "", - "state": { - "running": false - }, + "state": {}, "target_temp": 55, "type": "CustomKettleLogic" } diff --git a/config/sensor.json b/config/sensor.json index ffd1236..a2b8d06 100644 --- a/config/sensor.json +++ b/config/sensor.json @@ -5,7 +5,7 @@ "name": "Sensor1", "props": {}, "state": { - "value": 0 + "value": 35 }, "type": "CustomSensor" } diff --git a/requirements.txt b/requirements.txt index 15480d0..dc05279 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,4 @@ pyfiglet==0.8.post1 pandas==1.1.5 shortuuid==1.0.1 tabulate==0.8.7 -cbpi4-ui==0.0.2 \ No newline at end of file +cbpi4-ui \ No newline at end of file diff --git a/setup.py b/setup.py index 40968aa..99d8219 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ setup(name='cbpi', 'click==7.1.2', 'shortuuid==1.0.1', 'tabulate==0.8.7', - 'cbpi4-ui==0.0.3', + 'cbpi4-ui', ], dependency_links=[ 'https://testpypi.python.org/pypi'