def publish_cov(self, point_store: PointStoreModel, device: DeviceModel = None, network: NetworkModel = None, force_clear: bool = False): if point_store is None: raise Exception('Point.publish_cov point_store cannot be None') if device is None: device = DeviceModel.find_by_uuid(self.device_uuid) if network is None: network = NetworkModel.find_by_uuid(device.network_uuid) if device is None or network is None: raise Exception( f'Cannot find network or device for point {self.uuid}') priority = self._get_highest_priority_field() if self.history_enable \ and (self.history_type == HistoryType.COV or self.history_type == HistoryType.COV_AND_INTERVAL) \ and network.history_enable \ and device.history_enable: PointStoreHistoryModel.create_history(point_store) db.session.commit() if not self.disable_mqtt: from src.services.mqtt_client import MqttClient MqttClient.publish_point_cov(Drivers.GENERIC.name, network, device, self, point_store, force_clear, priority)
def _update_networks_tags(self): logger.info(f"Storing network_tags_list...") network_tags_list: List[tuple] = [] for network in NetworkModel.find_all(): if network.tags: network_tags: dict = json.loads(network.tags) # insert tags from network object for network_tag in network_tags.keys(): network_tags_list.append( (network.uuid, network_tag, network_tags[network_tag])) if len(network_tags_list): logger.debug(f"Data: {network_tags_list}") query_network_tag = f'INSERT INTO {self.__networks_tags_table_name} ' \ f'(network_uuid, tag_name, tag_value) ' \ f'VALUES %s ON CONFLICT (network_uuid, tag_name) ' \ f'DO UPDATE SET tag_value = excluded.tag_value' with self.__client: with self.__client.cursor() as curs: try: if len(network_tags_list): # Remove comma (,) from ('<uuid>',) in_uuid: str = rreplace( str(tuple(i[0] for i in network_tags_list)), ",)", ")", 1) in_tags_list: str = rreplace( str( tuple((i[0], i[1]) for i in network_tags_list)), ",)", ")", 1) query_delete_network_tag = f'DELETE FROM {self.__networks_tags_table_name} ' \ f'WHERE network_uuid IN {in_uuid} ' \ f'AND (network_uuid, tag_name) NOT IN {in_tags_list}' curs.execute(query_delete_network_tag) execute_values(curs, query_network_tag, network_tags_list, page_size=PAGE_SIZE) except psycopg2.Error as e: logger.error(str(e)) logger.info( f'Stored/updated {len(network_tags_list)} rows on {self.__networks_tags_table_name} table' ) else: logger.info( f"Nothing to store on {self.__networks_tags_table_name}")
def __check_and_clear_cov_point(self, topic: List[str], message: MQTTMessage): point_name: str = topic[-1] point_uuid: str = topic[-2] device_name: str = topic[-3] device_uuid: str = topic[-4] network_name: str = topic[-5] network_uuid: str = topic[-6] driver: str = topic[-7] if driver == Drivers.GENERIC.name: point_by_uuid: PointModel = PointModel.find_by_uuid(point_uuid) if point_by_uuid is None or \ PointModel.find_by_name(network_name, device_name, point_name) is None or \ DeviceModel.find_by_uuid(device_uuid) is None or \ NetworkModel.find_by_uuid(network_uuid) is None: logger.warning(f'No point with topic: {message.topic}') self.__clear_mqtt_retain_value(message) elif point_by_uuid and point_by_uuid.disable_mqtt: logger.warning( f'Flag disable_mqtt is true for point.uuid={point_uuid}') self.__clear_mqtt_retain_value(message)
def _update_networks(self): logger.info(f"Storing networks_list...") networks_list: List[tuple] = [] for network in NetworkModel.find_all(): networks_list.append( (network.uuid, network.name, network.enable, network.fault, network.history_enable, Drivers.GENERIC.name, network.created_on, network.updated_on, self.__device_info.global_uuid)) if len(networks_list): logger.debug(f"Data: {networks_list}") query_network = f'INSERT INTO {self.__networks_table_name} ' \ f'(uuid, name, enable, fault, history_enable, driver, created_on, updated_on, ' \ f'wires_plat_global_uuid) ' \ f'VALUES %s ON CONFLICT (uuid) ' \ f'DO UPDATE SET ' \ f'name = excluded.name, ' \ f'enable = excluded.enable, ' \ f'fault = excluded.fault, ' \ f'history_enable = excluded.history_enable, ' \ f'driver = excluded.driver, ' \ f'created_on = excluded.created_on, ' \ f'updated_on = excluded.updated_on, ' \ f'wires_plat_global_uuid = excluded.wires_plat_global_uuid' with self.__client: with self.__client.cursor() as curs: try: execute_values(curs, query_network, networks_list, page_size=PAGE_SIZE) except psycopg2.Error as e: logger.error(str(e)) logger.info( f'Stored/updated {len(networks_list)} rows on {self.__networks_table_name} table' ) else: logger.info(f"Nothing to store on {self.__networks_table_name}")
def get(cls): return network_marshaller(NetworkModel.find_all(), request.args)
def add_network(data): uuid: str = shortuuid.uuid() network: NetworkModel = NetworkModel(uuid=uuid, **data) network.save_to_db() return network
def get_network(cls, **kwargs) -> NetworkModel: return NetworkModel.find_by_name(kwargs.get('name'))
def get_network(cls, **kwargs) -> NetworkModel: return NetworkModel.find_by_uuid(kwargs.get('uuid'))