Actor

Architecture

_images/picture.jpeg

ActorController

class cbpi.controller.actor_controller.ActorController(cbpi)

Bases: cbpi.controller.crud_controller.CRUDController

The main actor controller

_abc_impl = <_abc_data object>
_init_actor(actor)
_post_add_callback(m)
Parameters:m
Returns:
_post_update_callback(actor)
_pre_delete_callback(actor_id)
Parameters:m
Returns:
_pre_update_callback(actor)
_stop_actor(actor)
call_action(actor_id, data, **kwargs) → None
get_state()
init()

This method initializes all actors during startup. It creates actor instances

Returns:
model

alias of cbpi.database.model.ActorModel

off(actor_id, **kwargs) → None

Method to switch and actor off Supporting Event Topic “actor/+/off”

Parameters:
  • actor_id – the actor actor_id
  • kwargs
on(actor_id, future: _asyncio.Future, power=100, **kwargs) → None

Method to switch an actor on. Supporting Event Topic “actor/+/on”

Parameters:actor_id – the actor id

:param future :param power: as integer value between 1 and 100 :param kwargs: :return:

toggle(actor_id, power=100, **kwargs) → None

Method to toggle an actor on or off Supporting Event Topic “actor/+/toggle”

Parameters:
  • actor_id – the actor actor_id
  • power – the power as integer between 0 and 100
Returns:

CBPiActor

class cbpi.api.CBPiActor(*args, **kwds)

Bases: cbpi.api.extension.CBPiExtension

_abc_impl = <_abc_data object>
init()
off()

Code to switch the actor off

Returns:None
on(power)

Code to switch the actor on. Power is provided as integer value

Parameters:power – power value between 0 and 100
Returns:None
reprJSON()
state()

Return the current actor state

Returns:
stop()

Custom Actor

__init__.py
  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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
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

class CustomActor(CBPiActor):

    # Custom property which can be configured by the user

    def init(self):
        pass

    def on(self, power=0):
        logger.info("ACTOR %s ON" % self.id)
        self.state = True

    def off(self):
        logger.info("ACTOR %s OFF " % self.id)
        self.state = False




class GPIOActor(CBPiActor):

    # Custom property which can be configured by the user

    gpio = Property.Select("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], description="GPIO to which the actor is connected")

    def init(self):
        try:
            GPIO.setup(int(self.gpio), GPIO.OUT)
            GPIO.output(int(self.gpio), 0)
        except Exception as e:
            raise CBPiException("FAILD TO INIT ACTOR")

    def on(self, power=0):

        print("GPIO ON %s" % str(self.gpio))
        GPIO.output(int(self.gpio), 1)
        self.state = True

    def off(self):
        print("GPIO OFF %s" % str(self.gpio))
        GPIO.output(int(self.gpio), 0)
        self.state = False

class GPIORelayBoardActor(CBPiActor):

    # Custom property which can be configured by the user

    gpio = Property.Select("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], description="GPIO to which the actor is connected")

    def init(self):
        try:
            GPIO.setup(int(self.gpio), GPIO.OUT)
            GPIO.output(int(self.gpio), 1)
        except Exception as e:
            raise CBPiException("FAILD TO INIT ACTOR")

    def on(self, power=0):

        print("GPIO ON %s" % str(self.gpio))
        GPIO.output(int(self.gpio), 0)
        self.state = True

    def off(self):
        print("GPIO OFF %s" % str(self.gpio))
        GPIO.output(int(self.gpio), 1)
        self.state = False



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("CustomActor", CustomActor)

config.yaml

1
2
name: DummyActor
version: 4