actor added

This commit is contained in:
Manuel Fritsch 2021-02-02 00:07:58 +01:00
parent 5f846ee2ad
commit b3af77fdb8
10 changed files with 145 additions and 80 deletions

View file

@ -90,7 +90,8 @@ class BasicController:
type = item["type"] type = item["type"]
clazz = self.types[type]["class"] 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() await item["instance"].start()
item["instance"].task = self._loop.create_task(item["instance"].run()) item["instance"].task = self._loop.create_task(item["instance"].run())
logging.info("{} started {}".format(self.name, id)) logging.info("{} started {}".format(self.name, id))
@ -122,9 +123,9 @@ class BasicController:
async def update(self, id, data) -> dict: async def update(self, id, data) -> dict:
logging.info("{} Get Update".format(self.name)) logging.info("{} Get Update".format(self.name))
await self.stop(id) await self.stop(id)
self.data = list(map(lambda old: {**old, **data} if old["id"] == id else old, self.data))
if self.autostart is True: if self.autostart is True:
await self.start(id) await self.start(id)
self.data = list(map(lambda old: {**old, **data} if old["id"] == id else old, self.data))
await self.save() await self.save()
return self.find_by_id(id) return self.find_by_id(id)

View file

@ -3,25 +3,10 @@ from unittest.mock import MagicMock, patch
from cbpi.api import * from cbpi.api import *
logger = logging.getLogger(__name__) 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([]) @parameters([])
class CustomActor(CBPiActor): class DummyActor(CBPiActor):
my_name = "" my_name = ""
# Custom property which can be configured by the user # Custom property which can be configured by the user
@ -31,17 +16,16 @@ class CustomActor(CBPiActor):
self.my_name = kwargs.get("name") self.my_name = kwargs.get("name")
pass pass
def init(self): async def start(self):
print("INIT") await super().start()
self.state = False
pass
async def on(self, power=0): async def on(self, power=0):
logger.info("ACTOR 1111 %s ON" % self.id) logger.info("ACTOR %s ON " % self.id)
self.state = True self.state = True
async def off(self): async def off(self):
logger.info("ACTOR %s OFF " % self.id) logger.info("ACTOR %s OFF " % self.id)
self.state = False self.state = False
def get_state(self): def get_state(self):
@ -61,4 +45,4 @@ def setup(cbpi):
:return: :return:
''' '''
cbpi.plugin.register("CustomActor", CustomActor) cbpi.plugin.register("DummyActor", DummyActor)

View file

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

View file

@ -0,0 +1,3 @@
name: GPIOActor
version: 4
active: true

View file

@ -8,4 +8,8 @@ python3 setup.py sdist
twine upload dist/* twine upload dist/*
# Checkout Pull Request
git fetch origin pull/ID/head:BRANCHNAME
git checkout BRANCHNAME

View file

@ -1,57 +1,24 @@
{ {
"data": [ "data": [
{ {
"id": "8BLRqagLicCdEBDdc77Sgr", "id": "YwGzXvWMpmbLb6XobesL8n",
"name": "Heater", "name": "111",
"props": { "props": {
"Param3": 2, "GPIO": 4,
"Param4": "", "Inverted": "Yes"
"Param5": "8BLRqagLicCdEBDdc77Sgr"
}, },
"state": false, "state": false,
"type": "CustomActor" "type": "GPIOActor"
}, },
{ {
"id": "Aifjxmw4QdPfU3XbR6iyis", "id": "EsmZwWi9Qp3bzmXqq7N3Ly",
"name": "Pump1", "name": "HALO",
"props": {}, "props": {
"state": true, "Frequency": "20",
"type": "CustomActor" "GPIO": 5
}, },
{
"id": "HX2bKdobuANehPggYcynnj",
"name": "Pump2",
"props": {},
"state": false, "state": false,
"type": "CustomActor" "type": "GPIOPWMActor"
},
{
"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"
} }
] ]
} }

View file

@ -7,9 +7,7 @@
"name": "MashTun", "name": "MashTun",
"props": {}, "props": {},
"sensor": "8ohkXvFA9UrkHLsxQL38wu", "sensor": "8ohkXvFA9UrkHLsxQL38wu",
"state": { "state": {},
"running": false
},
"target_temp": 52, "target_temp": 52,
"type": "CustomKettleLogic" "type": "CustomKettleLogic"
}, },
@ -36,9 +34,7 @@
"name": "Boil", "name": "Boil",
"props": {}, "props": {},
"sensor": "", "sensor": "",
"state": { "state": {},
"running": false
},
"target_temp": 55, "target_temp": 55,
"type": "CustomKettleLogic" "type": "CustomKettleLogic"
} }

View file

@ -5,7 +5,7 @@
"name": "Sensor1", "name": "Sensor1",
"props": {}, "props": {},
"state": { "state": {
"value": 0 "value": 35
}, },
"type": "CustomSensor" "type": "CustomSensor"
} }

View file

@ -13,4 +13,4 @@ pyfiglet==0.8.post1
pandas==1.1.5 pandas==1.1.5
shortuuid==1.0.1 shortuuid==1.0.1
tabulate==0.8.7 tabulate==0.8.7
cbpi4-ui==0.0.2 cbpi4-ui

View file

@ -31,7 +31,7 @@ setup(name='cbpi',
'click==7.1.2', 'click==7.1.2',
'shortuuid==1.0.1', 'shortuuid==1.0.1',
'tabulate==0.8.7', 'tabulate==0.8.7',
'cbpi4-ui==0.0.3', 'cbpi4-ui',
], ],
dependency_links=[ dependency_links=[
'https://testpypi.python.org/pypi' 'https://testpypi.python.org/pypi'