2018-12-29 00:27:19 +01:00
|
|
|
import json
|
|
|
|
import logging
|
2018-12-13 21:45:33 +01:00
|
|
|
from asyncio import Future
|
2018-12-15 00:01:37 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
from aiohttp import web
|
2018-12-29 00:27:19 +01:00
|
|
|
from cbpi_api import *
|
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-12-29 00:27:19 +01:00
|
|
|
from utils.encoder import ComplexEncoder
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2018-12-31 00:22:00 +01:00
|
|
|
auth = True
|
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-12-31 00:22:00 +01:00
|
|
|
@request_mapping(path="/{id:\d+}/on", auth_required=auth)
|
2018-11-01 21:25:42 +01:00
|
|
|
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'])
|
2018-12-29 00:27:19 +01:00
|
|
|
result = await self.cbpi.bus.fire(topic="actor/%s/switch/on" % id, id=id, power=99)
|
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
|
|
|
|
for key, value in result.results.items():
|
2018-12-29 00:27:19 +01:00
|
|
|
pass
|
2018-11-16 20:35:59 +01:00
|
|
|
return web.Response(status=204)
|
|
|
|
|
2018-11-01 21:25:42 +01:00
|
|
|
|
2018-12-31 00:22:00 +01:00
|
|
|
@request_mapping(path="/{id:\d+}/off", auth_required=auth)
|
2018-11-16 20:35:59 +01:00
|
|
|
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'])
|
2018-12-13 21:45:33 +01:00
|
|
|
await 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-12-31 00:22:00 +01:00
|
|
|
@request_mapping(path="/{id:\d+}/toggle", auth_required=auth)
|
2018-11-16 20:35:59 +01:00
|
|
|
async def http_toggle(self, request) -> web.Response:
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
id = int(request.match_info['id'])
|
2018-12-29 00:27:19 +01:00
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
await self.cbpi.bus.fire(topic="actor/%s/toggle" % id, id=id)
|
2018-11-16 20:35:59 +01:00
|
|
|
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;
|
2018-12-29 00:27:19 +01:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2018-11-01 19:50:04 +01:00
|
|
|
self.cbpi.register(self, "/actor")
|
|
|
|
self.types = {}
|
|
|
|
self.actors = {}
|
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
def info(self):
|
|
|
|
|
|
|
|
return json.dumps(dict(name="ActorController", types=self.types), cls=ComplexEncoder)
|
|
|
|
|
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
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-18 15:40:10 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
for id, value in self.cache.items():
|
2018-12-31 00:22:00 +01:00
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
await self._init_actor(value)
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
async def _init_actor(self, actor):
|
2019-01-01 15:35:35 +01:00
|
|
|
print("INIT ACXTOR")
|
2018-12-13 21:45:33 +01:00
|
|
|
if actor.type in self.types:
|
2019-01-01 15:35:35 +01:00
|
|
|
print("INIT ONE ACTOT")
|
2018-12-13 21:45:33 +01:00
|
|
|
cfg = actor.config.copy()
|
|
|
|
cfg.update(dict(cbpi=self.cbpi, id=id, name=actor.name))
|
|
|
|
clazz = self.types[actor.type]["class"];
|
|
|
|
self.cache[actor.id].instance = clazz(**cfg)
|
|
|
|
self.cache[actor.id].instance.init()
|
|
|
|
await self.cbpi.bus.fire(topic="actor/%s/initialized" % actor.id, id=actor.id)
|
2018-12-31 00:22:00 +01:00
|
|
|
else:
|
2019-01-01 15:35:35 +01:00
|
|
|
print("NOT FOUND")
|
2018-12-31 00:22:00 +01:00
|
|
|
self.logger.error("Actor type '%s' not found (Available Actor Types: %s)" % (actor.type, ', '.join(self.types.keys())))
|
|
|
|
|
2018-11-18 15:40:10 +01:00
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
async def _stop_actor(self, actor):
|
|
|
|
actor.instance.stop()
|
|
|
|
await self.cbpi.bus.fire(topic="actor/%s/stopped" % actor.id, id=actor.id)
|
2018-11-29 21:59:08 +01:00
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
|
2018-11-04 00:47:26 +01:00
|
|
|
|
2018-12-03 22:16:03 +01:00
|
|
|
@on_event(topic="actor/+/switch/on")
|
2018-12-13 21:45:33 +01:00
|
|
|
async def on(self, id , future: Future, power=100, **kwargs) -> None:
|
2018-11-18 15:40:10 +01:00
|
|
|
'''
|
|
|
|
Method to switch an actor on.
|
|
|
|
Supporting Event Topic "actor/+/on"
|
|
|
|
|
2018-11-29 21:59:08 +01:00
|
|
|
:param actor_id: the actor id
|
2018-11-18 15:40:10 +01:00
|
|
|
:param power: as integer value between 1 and 100
|
|
|
|
:param kwargs:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
id = int(id)
|
|
|
|
if id in self.cache:
|
2018-12-29 00:27:19 +01:00
|
|
|
self.logger.debug("ON %s" % id)
|
|
|
|
actor = self.cache[id].instance
|
2018-12-13 21:45:33 +01:00
|
|
|
await self.cbpi.bus.fire("actor/%s/on/ok" % id)
|
2018-11-16 20:35:59 +01:00
|
|
|
actor.on(power)
|
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
future.set_result("OK")
|
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
@on_event(topic="actor/+/toggle")
|
2018-12-13 21:45:33 +01:00
|
|
|
async def toggle(self, id, power=100, **kwargs) -> None:
|
2018-11-18 15:40:10 +01:00
|
|
|
'''
|
|
|
|
Method to toggle an actor on or off
|
|
|
|
Supporting Event Topic "actor/+/toggle"
|
|
|
|
|
2018-12-08 14:21:00 +01:00
|
|
|
:param id: the actor id
|
|
|
|
:param power: the power as interger between 0 and 100
|
2018-11-18 15:40:10 +01:00
|
|
|
:return:
|
|
|
|
'''
|
2018-11-16 20:35:59 +01:00
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
self.logger.debug("TOGGLE %s" % id)
|
2018-11-16 20:35:59 +01:00
|
|
|
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")
|
2018-12-13 21:45:33 +01:00
|
|
|
async def off(self, id, **kwargs) -> None:
|
2018-11-01 21:25:42 +01:00
|
|
|
"""
|
2018-11-18 15:40:10 +01:00
|
|
|
|
|
|
|
Method to switch and actor off
|
|
|
|
Supporting Event Topic "actor/+/off"
|
|
|
|
|
2018-12-08 14:21:00 +01:00
|
|
|
:param id: the actor id
|
2018-11-01 21:25:42 +01:00
|
|
|
:param kwargs:
|
|
|
|
"""
|
2018-11-16 20:35:59 +01:00
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
self.logger.debug("OFF %s" % id)
|
2018-11-16 20:35:59 +01:00
|
|
|
id = int(id)
|
|
|
|
|
|
|
|
if id in self.cache:
|
|
|
|
actor = self.cache[id].instance
|
|
|
|
actor.off()
|
2018-12-13 21:45:33 +01:00
|
|
|
|
|
|
|
async def _post_add_callback(self, m):
|
|
|
|
'''
|
|
|
|
|
|
|
|
:param m:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
await self._init_actor(m)
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def _pre_delete_callback(self, actor_id):
|
|
|
|
if int(actor_id) not in self.cache:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.cache[int(actor_id)].instance is not None:
|
|
|
|
await self._stop_actor(self.cache[int(actor_id)])
|
|
|
|
|
|
|
|
async def _pre_update_callback(self, actor):
|
|
|
|
|
|
|
|
if actor.instance is not None:
|
|
|
|
await self._stop_actor(actor)
|
|
|
|
|
|
|
|
async def _post_update_callback(self, actor):
|
|
|
|
self._init_actor(actor)
|