2018-11-04 00:47:26 +01:00
import logging
2019-01-04 09:29:09 +01:00
from unittest . mock import MagicMock , patch
2018-11-01 19:50:04 +01:00
2019-01-05 20:43:48 +01:00
from cbpi . api import *
2019-01-04 09:29:09 +01:00
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
2018-11-01 19:50:04 +01:00
2021-01-22 23:25:20 +01:00
class CustomActor ( CBPiActor2 ) :
my_name = " "
2018-11-01 19:50:04 +01:00
2018-12-08 14:21:00 +01:00
# Custom property which can be configured by the user
2019-01-24 21:27:55 +01:00
@action ( " test " , parameters = { } )
2021-01-22 23:25:20 +01:00
async def action1 ( self , * * kwargs ) :
print ( " ACTION ! " , kwargs )
self . my_name = kwargs . get ( " name " )
2019-07-27 21:08:19 +02:00
2019-01-24 21:27:55 +01:00
pass
2021-01-22 23:25:20 +01:00
2018-12-13 21:45:33 +01:00
def init ( self ) :
2021-01-22 23:25:20 +01:00
print ( " INIT " )
2019-01-08 23:31:39 +01:00
self . state = False
2018-12-29 00:27:19 +01:00
pass
2018-12-13 21:45:33 +01:00
2021-01-22 23:25:20 +01:00
async def on ( self , power = 0 ) :
logger . info ( " ACTOR 1111 %s ON " % self . id )
2019-01-04 09:29:09 +01:00
self . state = True
2018-12-13 21:45:33 +01:00
2021-01-22 23:25:20 +01:00
async def off ( self ) :
2019-01-04 09:29:09 +01:00
logger . info ( " ACTOR %s OFF " % self . id )
self . state = False
2018-11-01 19:50:04 +01:00
2019-01-08 23:31:39 +01:00
def get_state ( self ) :
2021-01-22 23:25:20 +01:00
2019-01-08 23:31:39 +01:00
return self . state
2021-01-22 23:25:20 +01:00
async def run ( self ) :
pass
2018-11-01 19:50:04 +01:00
2018-12-29 00:27:19 +01:00
2019-01-04 09:29:09 +01:00
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
2018-12-08 14:21:00 +01:00
2019-01-04 09:29:09 +01:00
def off ( self ) :
print ( " GPIO OFF %s " % str ( self . gpio ) )
GPIO . output ( int ( self . gpio ) , 0 )
2018-11-16 20:35:59 +01:00
self . state = False
2018-11-01 19:50:04 +01:00
2019-01-04 09:29:09 +01:00
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 " )
2018-12-29 00:27:19 +01:00
2019-01-04 09:29:09 +01:00
def on ( self , power = 0 ) :
2018-12-03 22:16:03 +01:00
2019-01-04 09:29:09 +01:00
print ( " GPIO ON %s " % str ( self . gpio ) )
GPIO . output ( int ( self . gpio ) , 0 )
2018-11-16 20:35:59 +01:00
self . state = True
2019-01-04 09:29:09 +01:00
def off ( self ) :
print ( " GPIO OFF %s " % str ( self . gpio ) )
GPIO . output ( int ( self . gpio ) , 1 )
self . state = False
2018-11-16 20:35:59 +01:00
2018-11-01 19:50:04 +01:00
def setup ( cbpi ) :
2018-11-16 21:42:59 +01:00
'''
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 )