craftbeerpi4-pione/core/controller/sensor_controller.py

50 lines
1.5 KiB
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
import logging
2018-11-16 20:35:59 +01:00
import os
2018-11-01 19:50:04 +01:00
from logging.handlers import TimedRotatingFileHandler
2018-11-18 15:40:10 +01:00
from core.job.aiohttp import get_scheduler_from_app
2018-11-01 19:50:04 +01:00
from core.api.decorator import background_task
from core.controller.crud_controller import CRUDController
from core.database.model import SensorModel
from core.http_endpoints.http_api import HttpAPI
class SensorController(CRUDController, HttpAPI):
model = SensorModel
2018-11-01 21:25:42 +01:00
def __init__(self, cbpi):
self.cbpi = cbpi
self.cbpi.register(self, "/sensor")
2018-11-01 19:50:04 +01:00
self.service = self
2018-11-18 15:40:10 +01:00
self.types = {}
2018-11-01 19:50:04 +01:00
2018-11-18 23:09:17 +01:00
self.sensors = {}
2018-11-16 20:35:59 +01:00
2018-11-01 19:50:04 +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:
'''
await super(SensorController, self).init()
print("INIT SENSOR")
for name, clazz in self.types.items():
print("Type", name)
for id, value in self.cache.items():
if value.type in self.types:
cfg = value.config.copy()
cfg.update(dict(cbpi=self.cbpi, id=id, name=value.name))
clazz = self.types[value.type]["class"];
self.cache[id].instance = clazz(**cfg)
scheduler = get_scheduler_from_app(self.cbpi.app)
self.cache[id].instance.job = await scheduler.spawn(self.cache[id].instance.run(self.cbpi), value.name, "sensor")
print("------------")
2018-11-04 01:55:54 +01:00
2018-11-18 23:09:17 +01:00
async def get_value(self, id):
return self.cache[id].instance.value