Exemple #1
0
 def sync(self):
     logger.info('PostgreSQL sync has is been called')
     self.__device_info: Union[DeviceInfoModel, None] = get_device_info()
     if not self.__device_info:
         logger.error('Please add device-info on Rubix Service')
         return
     self._sync()
Exemple #2
0
 def start(self,
           config: MqttSetting,
           subscribe_topics: List[str] = None,
           callback: Callable = lambda: None):
     self.__config = config
     self.__device_info: Union[DeviceInfoModel, None] = get_device_info()
     if not self.__device_info:
         logger.error('Please add device-info on Rubix Service')
         return
     subscribe_topics: List[str] = []
     topic: str = self.__make_topic(
         (self.get_schedule_value_topic_prefix(), '#'))
     subscribe_topics.append(topic)
     if self.config.listen:
         # Resubscribe logic is not necessary here, these topics are for this app and will clear out when we start
         topic: str = self.__make_topic(
             (self.get_listener_topic_prefix(), '#'))
         subscribe_topics.append(topic)
     if self.config.publish_value:
         topic: str = self.__make_topic(
             (self.get_value_topic_prefix(), '#'))
         subscribe_topics.append(topic)
         FlaskThread(target=self.__resubscribe_value_topic,
                     args=(topic, )).start()
     logger.info(f'Listening at: {subscribe_topics}')
     super().start(config, subscribe_topics, callback)
Exemple #3
0
 def prefix_topic(cls) -> str:
     device_info: Union[DeviceInfoModel, None] = get_device_info()
     if not device_info:
         logger.error('Please add device-info on Rubix Service')
         return ''
     return MqttClient.SEPARATOR.join(
         (device_info.client_id, device_info.client_name,
          device_info.site_id, device_info.site_name, device_info.device_id,
          device_info.device_name))
Exemple #4
0
    def poll_devices(self):
        """
        We don't need to sleep the response itself has sleep of bridge timeout seconds
        """
        RemoteDeviceRegistry().sem.acquire()
        active_slave_devices: Dict[
            str,
            Dict] = api_to_slaves_broadcast_topic_mapper('/api/wires/plat',
                                                         timeout=10).content
        RemoteDeviceRegistry().sem.release()
        for global_uuid in active_slave_devices:
            active_slave_device = active_slave_devices[global_uuid]
            device_info_model: DeviceInfoModel = get_device_info()
            self.__temp_devices[global_uuid] = {
                **active_slave_device, 'is_master':
                global_uuid == device_info_model.global_uuid,
                'count':
                self.__temp_devices.get(global_uuid, {}).get('count', 0) + 1
            }
        available_inserted_devices_global_uuids: List[str] = []
        slaves: Dict[str, Dict] = SlavesBase.get_slaves_by_app_setting(
            self.__app_setting)[0]

        for global_uuid in slaves:
            self.__total_count[global_uuid] = self.__total_count.get(
                global_uuid, 0) + 1
            if global_uuid not in active_slave_devices:
                self.__failure_count[global_uuid] = self.__failure_count.get(
                    global_uuid, 0) + 1

        global_uuids: List[str] = list(self.__temp_devices.keys())
        for global_uuid in global_uuids:
            if global_uuid not in active_slave_devices:
                temp_device = self.__temp_devices[global_uuid]
                logger.warning(
                    f'Deleting global_uuid={global_uuid}, device_name={temp_device.get("device_name")}, '
                    f'count={temp_device.get("count")}')
                del self.__temp_devices[global_uuid]

        devices: Dict[str, Dict] = {}
        for global_uuid in active_slave_devices:
            temp_device: dict = self.__temp_devices[global_uuid]
            if temp_device.get(
                    'count'
            ) >= MIN_LOOP_TO_SHOW_ONLINE or self.failure_count.get(
                    global_uuid, 0) == 0:
                devices[global_uuid] = temp_device
                if global_uuid in slaves:
                    available_inserted_devices_global_uuids.append(global_uuid)

        logger.info(
            f'Available devices count: {len(available_inserted_devices_global_uuids)}'
        )
        self.__devices = devices
        self.__available_inserted_devices_global_uuids = available_inserted_devices_global_uuids
Exemple #5
0
 def get(cls):
     device_info: Union[DeviceInfoModel, None] = get_device_info()
     if not device_info:
         logger.error('Please add device_info on Rubix Service')
         return
     uuid = get_authorized_user_uuid()
     user = UserModel.find_by_uuid(uuid)
     output: dict = {}
     for site in user.sites:
         output[site.site_uuid] = {
             'layout_topic':
             f'{device_info.global_uuid}/{site.site_uuid}/layout',
             'alert_topic':
             f'{device_info.global_uuid}/{site.site_uuid}/alerts'
         }
     return output
Exemple #6
0
def upgrade_001():
    device_info = json.loads(read_file('/data/rubix-registry/wires-plat.json') or "{}")
    if device_info and not get_device_info():
        put_device_info(DeviceInfoModel(**device_info))
Exemple #7
0
import json
import os
from typing import Callable, List, Union

from registry.models.model_device_info import DeviceInfoModel
from registry.resources.resource_device_info import get_device_info
from rubix_mqtt.setting import BaseSetting

from mrb.setting import MqttSetting
from mrb.utils.singleton import Singleton

device_info_model: Union[DeviceInfoModel, None] = get_device_info()


class MqttRestBridge(metaclass=Singleton):
    def __init__(self,
                 port: int = 8080,
                 mqtt_setting: MqttSetting = MqttSetting(),
                 callback: Callable = None):
        self.__port: int = port
        self.__mqtt_setting: MqttSetting = mqtt_setting
        self.__mqtt_setting.retain = False
        self.__data_dir = None
        self.__callback: Callable = callback

    @property
    def mqtt_setting(self) -> MqttSetting:
        return self.__mqtt_setting

    @property
    def global_uuid(self) -> str:
Exemple #8
0
 def store_device_info(cls, device_info: dict, data: dict):
     data['updated_on'] = datetime.utcnow().isoformat()
     put_device_info(DeviceInfoModel(**{**device_info, **data}))
     return get_device_info()
Exemple #9
0
 def make_topic(cls) -> str:
     device_info: Union[DeviceInfoModel, None] = get_device_info()
     return f'{device_info.global_uuid if device_info else ""}/+/alerts'