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"]
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)
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)
self.data = list(map(lambda old: {**old, **data} if old["id"] == id else old, self.data))
await self.save()
return self.find_by_id(id)

View file

@ -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
@ -31,17 +16,16 @@ class CustomActor(CBPiActor):
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)

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/*
# Checkout Pull Request
git fetch origin pull/ID/head:BRANCHNAME
git checkout BRANCHNAME

View file

@ -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": "EsmZwWi9Qp3bzmXqq7N3Ly",
"name": "HALO",
"props": {
"Frequency": "20",
"GPIO": 5
},
{
"id": "HX2bKdobuANehPggYcynnj",
"name": "Pump2",
"props": {},
"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"
}
]
}

View file

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

View file

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

View file

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

View file

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