craftbeerpi4-pione/core/controller/actor_controller.py

67 lines
1.8 KiB
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
from aiohttp import web
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
2018-11-04 00:47:26 +01:00
from core.controller.plugin_controller import PluginController
2018-11-01 19:50:04 +01:00
from core.database.model import ActorModel
from core.http_endpoints.http_api import HttpAPI
2018-11-01 21:25:42 +01:00
class ActorHttp(HttpAPI):
2018-11-04 00:47:26 +01:00
count = 0
2018-11-01 21:25:42 +01:00
@request_mapping(path="/hallo", auth_required=False)
async def hello_world(self, request) -> web.Response:
2018-11-04 00:47:26 +01:00
self.count = self.count + 1
return web.Response(status=200, text=str(self.count))
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:
"""
:param request:
:return:
"""
self.cbpi.bus.fire(event="actor/1/on", id=1, power=99)
return web.Response(status=204)
2018-11-01 19:50:04 +01:00
2018-11-04 00:47:26 +01:00
class ActorController(ActorHttp, CRUDController, PluginController):
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 = {}
async def init(self):
await super(ActorController, self).init()
for name, clazz in self.types.items():
print("Type", name)
for id, value in self.cache.items():
if value.type in self.types:
clazz = self.types[value.type];
2018-11-04 01:55:54 +01:00
self.actors[id] = 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-01 19:50:04 +01:00
print("ON-------------", id, power)
if id in self.actors:
i = self.actors[id]
i.on(power)
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-04 00:47:26 +01:00
pass