craftbeerpi4-pione/core/controller/actor_controller.py

127 lines
3.4 KiB
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
from aiohttp import web
2018-11-16 20:35:59 +01:00
from core.api.actor import CBPiActor
2018-11-04 01:55:54 +01:00
from core.api.decorator import on_event, request_mapping
2018-11-01 19:50:04 +01:00
from core.controller.crud_controller import CRUDController
from core.database.model import ActorModel
from core.http_endpoints.http_api import HttpAPI
2018-11-16 20:35:59 +01:00
from core.utils import parse_props
2018-11-01 19:50:04 +01:00
2018-11-01 21:25:42 +01:00
class ActorHttp(HttpAPI):
2018-11-01 19:50:04 +01:00
2018-11-01 21:25:42 +01:00
@request_mapping(path="/{id}/on", auth_required=False)
async def http_on(self, request) -> web.Response:
"""
2018-11-16 20:35:59 +01:00
:param request:
:return:
"""
id = int(request.match_info['id'])
self.cbpi.bus.fire(topic="actor/%s/on" % id, id=id, power=99)
return web.Response(status=204)
2018-11-01 21:25:42 +01:00
2018-11-16 20:35:59 +01:00
@request_mapping(path="/{id}/off", auth_required=False)
async def http_off(self, request) -> web.Response:
"""
2018-11-01 21:25:42 +01:00
:param request:
:return:
"""
2018-11-16 20:35:59 +01:00
id = int(request.match_info['id'])
self.cbpi.bus.fire(topic="actor/%s/off" % id, id=id)
2018-11-01 21:25:42 +01:00
return web.Response(status=204)
2018-11-01 19:50:04 +01:00
2018-11-16 20:35:59 +01:00
@request_mapping(path="/{id}/toggle", auth_required=False)
async def http_toggle(self, request) -> web.Response:
"""
:param request:
:return:
"""
id = int(request.match_info['id'])
print("ID", id)
self.cbpi.bus.fire(topic="actor/%s/toggle" % id, id=id)
return web.Response(status=204)
class ActorController(ActorHttp, CRUDController):
2018-11-01 19:50:04 +01:00
2018-11-16 20:35:59 +01:00
'''
The main actor controller
'''
2018-11-01 21:25:42 +01:00
model = ActorModel
2018-11-01 19:50:04 +01:00
def __init__(self, cbpi):
super(ActorController, self).__init__(cbpi)
self.cbpi = cbpi
self.state = False;
self.cbpi.register(self, "/actor")
self.types = {}
self.actors = {}
2018-11-16 20:35:59 +01:00
def register(self, name, clazz) -> None:
'''
Register a new actor type
:param name: actor name
:param clazz: actor class
:return: None
'''
print("REGISTER", name)
if issubclass(clazz, CBPiActor):
print("ITS AN ACTOR")
2018-11-01 19:50:04 +01:00
2018-11-16 20:35:59 +01:00
parse_props(clazz)
self.types[name] = clazz
async def init(self):
'''
This method initializes all actors during startup. It creates actor instances
:return:
'''
2018-11-01 19:50:04 +01:00
await super(ActorController, self).init()
2018-11-16 20:35:59 +01:00
print("INIT ACTOR")
2018-11-01 19:50:04 +01:00
for name, clazz in self.types.items():
print("Type", name)
for id, value in self.cache.items():
if value.type in self.types:
2018-11-16 20:35:59 +01:00
clazz = self.types[value.type]["class"];
print(self.cache[id])
self.cache[id].instance = clazz(self.cbpi)
2018-11-04 00:47:26 +01:00
2018-11-01 19:50:04 +01:00
@on_event(topic="actor/+/on")
2018-11-01 21:25:42 +01:00
def on(self, id, power=100, **kwargs) -> None:
2018-11-16 20:35:59 +01:00
print(id)
id = int(id)
if id in self.cache:
print("POWER ON")
actor = self.cache[id].instance
actor.on(power)
@on_event(topic="actor/+/toggle")
def toggle(self, id, power=100, **kwargs) -> None:
id = int(id)
if id in self.cache:
actor = self.cache[id].instance
if actor.state is True:
actor.off()
else:
actor.on()
2018-11-01 19:50:04 +01:00
2018-11-01 21:25:42 +01:00
@on_event(topic="actor/+/off")
def off(self, id, **kwargs) -> None:
"""
:param id:
:param kwargs:
"""
2018-11-16 20:35:59 +01:00
id = int(id)
if id in self.cache:
actor = self.cache[id].instance
actor.off()