<commit_before># Copyright (C) 2020 OpenMotics BV
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Sensor BLL
"""
from __future__ import absolute_import

import logging
import time

from peewee import JOIN

from gateway.base_controller import BaseController, SyncStructure
from gateway.dto import MasterSensorDTO, SensorDTO, SensorSourceDTO, \
    SensorStatusDTO
from gateway.events import GatewayEvent
from gateway.hal.master_event import MasterEvent
from gateway.mappers.sensor import SensorMapper
from gateway.models import Plugin, Room, Sensor
from gateway.pubsub import PubSub
from ioc import INJECTED, Inject, Injectable, Singleton

if False:  # MYPY
    from typing import Dict, List, Optional, Set, Tuple
    from gateway.hal.master_controller import MasterController

logger = logging.getLogger(__name__)


@Injectable.named('sensor_controller')
@Singleton
class SensorController(BaseController):
    SYNC_STRUCTURES = [SyncStructure(Sensor, 'sensor')]
    STATUS_EXIPRE = 60.0

    MASTER_TYPES = {MasterEvent.SensorType.TEMPERATURE: Sensor.PhysicalQuantities.TEMPERATURE,
                    MasterEvent.SensorType.HUMIDITY: Sensor.PhysicalQuantities.HUMIDITY,
                    MasterEvent.SensorType.BRIGHTNESS: Sensor.PhysicalQuantities.BRIGHTNESS}

    @Inject
    def __init__(self, master_controller=INJECTED, pubsub=INJECTED):
        # type: (MasterController, PubSub) -> None
        super(SensorController, self).__init__(master_controller, sync_interval=600)
        self._pubsub = pubsub
        self._master_cache = {}  # type: Dict[Tuple[str,int],SensorDTO]
        self._status = {}  # type: Dict[int,SensorStatusDTO]

        self._pubsub.subscribe_master_events(PubSub.MasterTopics.SENSOR, self._handle_master_event)

    def _publish_config(self):
        # type: () -> None
        gateway_event = GatewayEvent(GatewayEvent.Types.CONFIG_CHANGE, {'type': 'sensor'})
        self._pubsub.publish_gateway_event(PubSub.GatewayTopics.CONFIG, gateway_event)

    def _handle_status(self, status_dto):
        # type: (SensorStatusDTO) -> None
        event_data = {'id': status_dto.id,
                      'value': status_dto.value}
        gateway_event = GatewayEvent(GatewayEvent.Types.SENSOR_CHANGE, event_data)
        if not (status_dto == self._status.get(status_dto.id)):
            logger.debug('Sensor value changed %s', gateway_event)
            self._status[status_dto.id] = status_dto
            self._pubsub.publish_gateway_event(PubSub.GatewayTopics.STATE, gateway_event)<commit_msg>increase expire time for sensor statuses<commit_after># Copyright (C) 2020 OpenMotics BV
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Sensor BLL
"""
from __future__ import absolute_import

import logging
import time

from peewee import JOIN

from gateway.base_controller import BaseController, SyncStructure
from gateway.dto import MasterSensorDTO, SensorDTO, SensorSourceDTO, \
    SensorStatusDTO
from gateway.events import GatewayEvent
from gateway.hal.master_event import MasterEvent
from gateway.mappers.sensor import SensorMapper
from gateway.models import Plugin, Room, Sensor
from gateway.pubsub import PubSub
from ioc import INJECTED, Inject, Injectable, Singleton

if False:  # MYPY
    from typing import Dict, List, Optional, Set, Tuple
    from gateway.hal.master_controller import MasterController

logger = logging.getLogger(__name__)


@Injectable.named('sensor_controller')
@Singleton
class SensorController(BaseController):
    SYNC_STRUCTURES = [SyncStructure(Sensor, 'sensor')]
    STATUS_EXIPRE = 360.0

    MASTER_TYPES = {MasterEvent.SensorType.TEMPERATURE: Sensor.PhysicalQuantities.TEMPERATURE,
                    MasterEvent.SensorType.HUMIDITY: Sensor.PhysicalQuantities.HUMIDITY,
                    MasterEvent.SensorType.BRIGHTNESS: Sensor.PhysicalQuantities.BRIGHTNESS}

    @Inject
    def __init__(self, master_controller=INJECTED, pubsub=INJECTED):
        # type: (MasterController, PubSub) -> None
        super(SensorController, self).__init__(master_controller, sync_interval=600)
        self._pubsub = pubsub
        self._master_cache = {}  # type: Dict[Tuple[str,int],SensorDTO]
        self._status = {}  # type: Dict[int,SensorStatusDTO]

        self._pubsub.subscribe_master_events(PubSub.MasterTopics.SENSOR, self._handle_master_event)

    def _publish_config(self):
        # type: () -> None
        gateway_event = GatewayEvent(GatewayEvent.Types.CONFIG_CHANGE, {'type': 'sensor'})
        self._pubsub.publish_gateway_event(PubSub.GatewayTopics.CONFIG, gateway_event)

    def _handle_status(self, status_dto):
        # type: (SensorStatusDTO) -> None
        event_data = {'id': status_dto.id,
                      'value': status_dto.value}
        gateway_event = GatewayEvent(GatewayEvent.Types.SENSOR_CHANGE, event_data)
        if not (status_dto == self._status.get(status_dto.id)):
            logger.debug('Sensor value changed %s', gateway_event)
            self._status[status_dto.id] = status_dto
            self._pubsub.publish_gateway_event(PubSub.GatewayTopics.STATE, gateway_event)
{'fraction_non_alphanumeric': 0.0820659062103929, 'fraction_numerical': 0.003643852978453739, 'mean_word_length': 4.0342902711323765, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '105058', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1836, 'n_words': 514}