2019-01-01 15:35:35 +01:00
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
import logging
|
|
|
|
from cbpi.controller.crud_controller import CRUDController
|
|
|
|
from cbpi.database.model import SensorModel
|
|
|
|
from cbpi.job.aiohttp import get_scheduler_from_app
|
2019-01-02 00:48:36 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
|
|
|
|
|
2019-01-02 21:20:44 +01:00
|
|
|
class SensorController(CRUDController):
|
2018-11-01 19:50:04 +01:00
|
|
|
|
|
|
|
model = SensorModel
|
|
|
|
|
2018-11-01 21:25:42 +01:00
|
|
|
def __init__(self, cbpi):
|
|
|
|
self.cbpi = cbpi
|
2019-01-02 21:20:44 +01:00
|
|
|
self.cbpi.register(self)
|
2018-11-01 19:50:04 +01:00
|
|
|
self.service = self
|
2018-11-18 15:40:10 +01:00
|
|
|
self.types = {}
|
2019-01-01 15:35:35 +01:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2018-11-18 23:09:17 +01:00
|
|
|
self.sensors = {}
|
2018-11-16 20:35:59 +01:00
|
|
|
|
2018-11-18 15:40:10 +01:00
|
|
|
async def init(self):
|
|
|
|
'''
|
|
|
|
This method initializes all actors during startup. It creates actor instances
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
|
2018-11-18 15:40:10 +01:00
|
|
|
await super(SensorController, self).init()
|
2019-01-02 00:48:36 +01:00
|
|
|
for id, value in self.cache.items():
|
|
|
|
await self.init_sensor(value)
|
2018-12-29 00:27:19 +01:00
|
|
|
|
2019-01-04 09:29:09 +01:00
|
|
|
def get_state(self):
|
|
|
|
return dict(items=self.cache,types=self.types)
|
|
|
|
|
2019-01-02 00:48:36 +01:00
|
|
|
async def init_sensor(self, sensor):
|
|
|
|
if sensor.type in self.types:
|
|
|
|
cfg = sensor.config.copy()
|
|
|
|
cfg.update(dict(cbpi=self.cbpi, id=sensor.id, name=sensor.name))
|
|
|
|
clazz = self.types[sensor.type]["class"];
|
|
|
|
self.cache[sensor.id].instance = clazz(**cfg)
|
|
|
|
self.cache[sensor.id].instance.init()
|
|
|
|
scheduler = get_scheduler_from_app(self.cbpi.app)
|
|
|
|
self.cache[sensor.id].instance.job = await scheduler.spawn(self.cache[sensor.id].instance.run(self.cbpi), sensor.name, "sensor")
|
|
|
|
else:
|
|
|
|
self.logger.error("Sensor type '%s' not found (Available Sensor Types: %s)" % (sensor.type, ', '.join(self.types.keys())))
|
2019-01-01 15:35:35 +01:00
|
|
|
|
2019-01-05 22:44:54 +01:00
|
|
|
|
|
|
|
|
2019-01-02 00:48:36 +01:00
|
|
|
async def stop_sensor(self, sensor):
|
|
|
|
print("STOP", sensor.id)
|
|
|
|
sensor.instance.stop()
|
|
|
|
await self.cbpi.bus.fire(topic="sensor/%s/stopped" % sensor.id, id=sensor.id)
|
2018-11-18 15:40:10 +01:00
|
|
|
|
2019-01-02 00:48:36 +01:00
|
|
|
|
|
|
|
async def get_value(self, sensor_id):
|
|
|
|
sensor_id = int(sensor_id)
|
|
|
|
return self.cache[sensor_id].instance.value
|
|
|
|
|
|
|
|
async def _post_add_callback(self, m):
|
|
|
|
'''
|
|
|
|
|
|
|
|
:param m:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
await self.init_sensor(m)
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def _pre_delete_callback(self, sensor_id):
|
|
|
|
if int(sensor_id) not in self.cache:
|
|
|
|
return
|
|
|
|
if self.cache[int(sensor_id)].instance is not None:
|
|
|
|
await self.stop_sensor(self.cache[int(sensor_id)])
|
|
|
|
|
|
|
|
async def _pre_update_callback(self, sensor):
|
|
|
|
|
|
|
|
if sensor.instance is not None:
|
|
|
|
await self.stop_sensor(sensor)
|
|
|
|
|
|
|
|
async def _post_update_callback(self, sensor):
|
|
|
|
self.init_sensor(sensor)
|