def test_05_sensor(self): print('') hw_components = fill_db_hw_comp(self._sqlite_path) brokers = fill_db_broker(self._sqlite_path) self.assertIsNotNone(brokers) sensors = fill_db_sensor(self._sqlite_path) self.assertIsNotNone(sensors) self.assertIsInstance(sensors, list) self.assertEqual(len(sensors), 4) with wp_repository.SQLiteRepository( iot_repository_sensor.IotSensorConfig, self._sqlite_path) as repository: # SELECT BY KEY for sensor in sensors: s_res_1 = repository.select_by_key(sensor) self.assertIsNotNone(s_res_1) self.assertIsInstance(s_res_1, iot_repository_sensor.IotSensorConfig) self.assertEqual(str(s_res_1), str(sensor)) # SELECT BY device_id for hw_device in hw_components: num_ref = 0 for sensor in sensors: if sensor.device_id == hw_device.device_id: num_ref += 1 s_res_2 = repository.select_where([("device_id", "=", hw_device.device_id)]) self.assertIsNotNone(s_res_2) self.assertIsInstance(s_res_2, list) self.assertEqual(len(s_res_2), num_ref)
def test_03_assignment(self): print('') host_1 = fill_db_host(self._sqlite_path) self.assertIsNotNone(host_1) hw_comp_list = fill_db_hw_comp(self._sqlite_path) self.assertIsNotNone(hw_comp_list) self.assertIsInstance(hw_comp_list, list) self.assertEqual(len(hw_comp_list), 3) ass_list = fill_db_host_assign(self._sqlite_path) self.assertIsNotNone(ass_list) self.assertIsInstance(ass_list, list) self.assertEqual(len(ass_list), 3) with wp_repository.SQLiteRepository( iot_repository_host.IotHostAssignedComponent, self._sqlite_path) as repository: # SELECT BY KEY for ass_comp in ass_list: ass_res_1 = repository.select_by_key(ass_comp) self.assertIsNotNone(ass_res_1) self.assertIsInstance( ass_res_1, iot_repository_host.IotHostAssignedComponent) self.assertEqual(str(ass_res_1), str(ass_comp))
def fill_db_hw_comp(sqlite_path: str) -> list: comp_list = [] hw_rows = [[ 'hw_comp_01', 'DigitalInput', 'ADS1115', 'I2C', 0, 0x40, 60, 'broker.1', 'data/hw', None, None, 'broker.1', 'health/hw', datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'hw_comp_02', 'DigitalOutput', 'MCP23017', 'I2C', 0, 0x60, 10, None, None, 'broker.2', 'input/hw', 'broker.1', 'health/hw', datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'hw_comp_03', 'DigitalInput', 'ADS1115', 'I2C', 0, 0x41, 60, 'broker.1', 'data/hw', None, None, 'broker.1', 'health/hw', datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] with wp_repository.SQLiteRepository( iot_repository_hardware.IotHardwareConfig, sqlite_path) as repository: for hw_row in hw_rows: hw_comp = iot_repository_hardware.IotHardwareConfig() hw_comp.load_row(hw_row) repository.insert(hw_comp) comp_list.append(hw_comp) return comp_list
def test02_hardware(self): print('') hw_comp_list = fill_db_hw_comp(self._sqlite_path) id_list = [] for hw_comp in hw_comp_list: id_list.append(hw_comp.device_id) self.assertIsNotNone(hw_comp_list) self.assertIsInstance(hw_comp_list, list) self.assertEqual(len(hw_comp_list), 3) with wp_repository.SQLiteRepository( iot_repository_hardware.IotHardwareConfig, self._sqlite_path) as repository: # SELECT ALL print('IotHardwareConfig: SELECT ALL') hw_res_1 = repository.select_all() self.assertIsNotNone(hw_res_1) self.assertIsInstance(hw_res_1, list) res_id_list = [] for res in hw_res_1: self.assertIsInstance( res, iot_repository_hardware.IotHardwareConfig) res_id_list.append(res.device_id) self._compare_lists(id_list, res_id_list) # SELECT BY KEY print('IotHardwareConfig: SELECT BY KEY') for hw_comp in hw_comp_list: hw_res_2 = repository.select_by_key(hw_comp) self.assertIsNotNone(hw_res_2) self.assertIsInstance( hw_res_2, iot_repository_hardware.IotHardwareConfig) self.assertEqual(str(hw_comp), str(hw_res_2)) # SELECT WHERE print('IotHardwareConfig: SELECT WHERE') hw_res_3 = repository.select_where([("device_type", "like", "%INPUT%")]) self.assertIsNotNone(hw_res_3) self.assertEqual(len(hw_res_3), 2) for res in hw_res_3: self.assertIsInstance( res, iot_repository_hardware.IotHardwareConfig) self.assertTrue(res.device_id in ['hw_comp_01', 'hw_comp_03']) hw_res_3 = repository.select_where([("model", "=", "ADS1115")]) self.assertIsNotNone(hw_res_3) self.assertEqual(len(hw_res_3), 2) for res in hw_res_3: self.assertIsInstance( res, iot_repository_hardware.IotHardwareConfig) self.assertTrue(res.device_id in ['hw_comp_01', 'hw_comp_03']) hw_res_3 = repository.select_where([("if_type", "!=", "I2C")]) self.assertIsNotNone(hw_res_3) self.assertEqual(len(hw_res_3), 0) hw_res_3 = repository.select_where([("polling_interval", "<", 30)]) self.assertIsNotNone(hw_res_3) self.assertEqual(len(hw_res_3), 1) self.assertIsInstance(hw_res_3[0], iot_repository_hardware.IotHardwareConfig) self.assertEqual(hw_res_3[0].device_id, 'hw_comp_02')
def _build_sensor_config(self, broker: iot_repository.IotMqttBroker) -> list: """ Builds the list of configuration dictionaries of sensors using the given MQTT broker for message subscription/publishing. Parameters: broker : iot_repository.IotMqttBroker Broker for which the configuration dictionary shall be created. Returns: list : Configuration dictionaries for the sensors using the MQTT broker. """ sensor_config = [] with wp_repository.SQLiteRepository(iot_repository.IotSensor, self._sqlite_file_name) as repo: sensor_list = repo.select_where([("broker_id", "=", broker.broker_id)]) for sensor in sensor_list: config = { 'sensor_id': sensor.sensor_id, 'sensor_type': sensor.sensor_type, 'hardware': { 'id': sensor.hardware_id, 'channel': sensor.hw_channel }, 'topics': { 'input_prefix': sensor.topic_input, 'data_prefix': sensor.topic_data, 'health_prefix': sensor.topic_health }, 'polling_interval': sensor.polling_interval, 'change_date': sensor.store_date_str } sensor_config.append(config) return sensor_config
def fill_db_sensor(sqlite_path: str) -> list: sensor_list = [] sensor_rows = [[ 'sensor.01', 'KYES516', 'hw_comp_01', 0, 60, 'broker_01', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'sensor.02', 'KYES516', 'hw_comp_01', 1, 60, 'broker_01', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'sensor.03', 'KYES516', 'hw_comp_01', 2, 60, 'broker_01', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'sensor.04', 'KYES516', 'hw_comp_03', 0, 60, 'broker_02', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] with wp_repository.SQLiteRepository(iot_repository_sensor.IotSensorConfig, sqlite_path) as repository: for row in sensor_rows: sensor = iot_repository_sensor.IotSensorConfig() sensor.load_row(row) repository.insert(sensor) sensor_list.append(sensor) return sensor_list
def fill_db_host(sqlite_path: str) -> iot_repository_host.IotHostConfig: hconf = iot_repository_host.IotHostConfig() hconf.host_id = HOST_ID hconf.host_ip = socket.gethostbyname(socket.gethostname()) with wp_repository.SQLiteRepository(iot_repository_host.IotHostConfig, sqlite_path) as repository: repository.insert(hconf) return hconf
def _create_db_items(self, item_type: type, item_rows: list) -> list: """ Inserts items into the database. """ res_list = [] with wp_repository.SQLiteRepository(item_type, self._config_db_path) as repository: for row in item_rows: db_item = item_type() db_item.load_row(row) repository.insert(db_item) res_list.append(db_item) return res_list
def _build_hardware_config(self, broker: iot_repository.IotMqttBroker) -> list: """ Builds the list of configuration dictionaries of hardware elements using the given MQTT broker for message subscription/publishing. Parameters: broker : iot_repository.IotMqttBroker Broker for which the configuration dictionary shall be created. Returns: list : Configuration dictionaries for the hardware elements using the MQTT broker. """ hw_config = [] with wp_repository.SQLiteRepository(iot_repository.IotHardwareComponent, self._sqlite_file_name) as repo: hw_components = repo.select_where([("broker_id", "=", broker.broker_id)]) for hw_component in hw_components: with wp_repository.SQLiteRepository(iot_repository.IotSensor, self._sqlite_file_name) as repo: assigned_sensors = repo.select_where([("hardware_id", "=", hw_component.hardware_id)]) channels = [] for sensor in assigned_sensors: channels.append(sensor.hw_channel) hw_component_config = { 'device_id': hw_component.hardware_id, 'device_type': hw_component.hardware_type, 'interface:': { 'if_type': hw_component.if_type, 'i2c': { 'bus_id': hw_component.i2c_bus_id, 'bus_address': hw_component.i2c_bus_address } }, 'active_ports': channels, 'topics': { 'data_prefix': hw_component.topic_data, 'health_prefix': hw_component.topic_health }, 'polling_interval': hw_component.polling_interval, 'change_date': hw_component.store_date_str } hw_config.append(hw_component_config) return hw_config
def __init__(self, sqlite_file_name: str): """ Constructor. Parameters: sqlite_file_name : str Full path name to the SQLite database file. """ self._sqlite_file_name = sqlite_file_name self._broker_config_list = [] with wp_repository.SQLiteRepository(iot_repository.IotMqttBroker, self._sqlite_file_name) as repo: broker_list = repo.select_all() for broker in broker_list: self._broker_config_list.append(self._build_config(broker))
def message(self, msg: wp_queueing.QueueMessage) -> None: """ Function called by the MQTT broker session handler when a message is received. Parameters: msg : wp_queueing.QueueMessage Message received from the MQTT broker. """ msg_base = iot_stat_msg.IotRecorderMsg(msg) msg_payload = msg.msg_payload try: if 'class' in msg_payload: msg_base.msg_class = msg_payload['class'] if msg_payload['class'] == 'InputProbe': conv_msg = iot_msg_input.InputProbe() conv_msg.from_dict(msg_payload) rec_msg = iot_stat_msg.IotRecorderInputProbe(msg_base.msg_id, conv_msg) elif msg_payload['class'] == 'InputHealth': conv_msg = iot_msg_input.InputHealth() conv_msg.from_dict(msg_payload) rec_msg = iot_stat_msg.IotRecorderInputHealth(msg_base.msg_id, conv_msg) elif msg_payload['class'] == 'SensorMsmt': conv_msg = iot_msg_sensor.SensorMsmt() conv_msg.from_dict(msg_payload) rec_msg = iot_stat_msg.IotRecorderSensorMsg(msg_base.msg_id, conv_msg) else: rec_msg = iot_stat_msg.IotRecorderGenericMsg(msg_base.msg_id, msg_payload) else: rec_msg = iot_stat_msg.IotRecorderGenericMsg(msg_base.msg_id, msg_payload) except TypeError: rec_msg = iot_stat_msg.IotRecorderGenericMsg(msg_base.msg_id, msg_payload) except ValueError: rec_msg = iot_stat_msg.IotRecorderGenericMsg(msg_base.msg_id, msg_payload) with wp_repository.SQLiteRepository(iot_stat_msg.IotRecorderMsg, self._sqlite_db_path) as repository: repository.insert(msg_base) with wp_repository.SQLiteRepository(type(rec_msg), self._sqlite_db_path) as repository: repository.insert(rec_msg)
def test_2_input_probe(self): rec_msg = iot_stat_msg.IotRecorderMsg() with wp_repository.SQLiteRepository(iot_stat_msg.IotRecorderMsg, self._stat_db_target) as msg_repo: with wp_repository.SQLiteRepository( iot_stat_msg.IotRecorderInputProbe, self._stat_db_target) as prb_repo: for hour in range(self._probe_hour[0], self._probe_hour[1]): for min in range(0, 60, self._probe_min_step): for device in self._devices: for channel in device['channels']: sec = self._random.randint(0, 4) msec = self._random.randint(0, 999999) _msg_id = str(uuid.uuid4()) _msg_timestamp = f'{self._probe_date.strftime("%Y-%m-%d")} {hour:02d}:{min:02d}:{sec:02d}.{msec}' _msg_topic = f"data/device/{device['device_id']}/{channel}" sec = self._random.randint(sec, sec + 4) _store_timestamp = f'{self._probe_date.strftime("%Y-%m-%d")} {hour:02d}:{min:02d}:{sec:02d}' rec_msg.load_row([ _msg_id, _msg_topic, _msg_timestamp, 'InputProbe', _store_timestamp ]) probe = iot_msg_input.InputProbe( device['device_type'], device['device_id'], _msg_timestamp, channel) probe.value = self._random.randint( 8000 + (12 - hour) * 51, 20000 + (12 - hour) * 83) probe.voltage = (3.3 / 29999) * float( probe.value) prb_msg = iot_stat_msg.IotRecorderInputProbe( _msg_id, probe) msg_repo.insert(rec_msg) prb_repo.insert(prb_msg)
def test01_host(self): print('') host_1 = fill_db_host(self._sqlite_path) print('>>> HOST: ', str(host_1)) self.assertIsNotNone(host_1) with wp_repository.SQLiteRepository(iot_repository_host.IotHostConfig, self._sqlite_path) as repository: host_2 = repository.select_by_key(host_1) hosts_1 = repository.select_where([("host_id", "=", HOST_ID)]) hosts_2 = repository.select_where([("host_ip", "=", host_1.host_ip) ]) # Test host_2 self.assertIsNotNone(host_2) self.assertEqual(str(host_1), str(host_2)) self.assertIsNotNone(hosts_1) self.assertIsNotNone(hosts_2)
def fill_db_broker(sqlite_path: str) -> list: broker_list = [] broker_rows = [[ 'broker_01', '192.168.1.249', 1883, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'broker_02', '192.168.1.250', 1883, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] with wp_repository.SQLiteRepository( iot_repository_broker.IotMqttBrokerConfig, sqlite_path) as repository: for row in broker_rows: broker = iot_repository_broker.IotMqttBrokerConfig() broker.load_row(row) repository.insert(broker) broker_list.append(broker) return broker_list
def test_04_broker(self): print('') brokers = fill_db_broker(self._sqlite_path) self.assertIsNotNone(brokers) self.assertEqual(len(brokers), 2) with wp_repository.SQLiteRepository( iot_repository_broker.IotMqttBrokerConfig, self._sqlite_path) as repository: # SELECT BY KEY for broker in brokers: brk_res_1 = repository.select_by_key(broker) self.assertIsNotNone(brk_res_1) self.assertIsInstance( brk_res_1, iot_repository_broker.IotMqttBrokerConfig) self.assertEqual(str(brk_res_1), str(broker)) # SELECT WHERE (broker_port) brk_res_2 = repository.select_where([("broker_port", "=", 1883)]) self.assertIsNotNone(brk_res_2) self.assertIsInstance(brk_res_2, list) self.assertEqual(len(brk_res_2), 2) for brk_res in brk_res_2: self.assertIsInstance( brk_res, iot_repository_broker.IotMqttBrokerConfig) self.assertTrue(brk_res.broker_id in [brokers[0].broker_id, brokers[1].broker_id]) # SELECT WHERE (broker_host) brk_res_3 = repository.select_where([("broker_host", "=", brokers[1].broker_host)]) self.assertIsNotNone(brk_res_3) self.assertIsInstance(brk_res_3, list) self.assertEqual(len(brk_res_3), 1) brk_res = brk_res_3[0] self.assertIsInstance(brk_res, iot_repository_broker.IotMqttBrokerConfig) self.assertEqual(str(brk_res), str(brokers[1]))
def fill_db_host_assign(sqlite_path: str) -> list: assign_list = [] assign_rows = [[ 'pi1.lcl', 'hw_comp_01', 1, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'pi1.lcl', 'hw_comp_02', 1, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'pi1.lcl', 'hw_comp_03', 1, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] with wp_repository.SQLiteRepository( iot_repository_host.IotHostAssignedComponent, sqlite_path) as repository: for assign_row in assign_rows: a_row = iot_repository_host.IotHostAssignedComponent() a_row.load_row(assign_row) repository.insert(a_row) assign_list.append(a_row) return assign_list
def _fill_db(self) -> None: # Create HOST entry self._host = iot_repository_host.IotHostConfig() self._host.load_row([ "host.01", HOST_IP, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]) with wp_repository.SQLiteRepository(iot_repository_host.IotHostConfig, self._sqlite_path) as repository: res = repository.insert(self._host) self.assertEqual(res, 1) # Create hardware components hw_rows = [[ 'hw_comp_01', 'DigitalInput', 'ADS1115', 'I2C', 0, 0x40, 60, 'broker.1', 'data/hw', None, None, 'broker.1', 'health/hw', datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'hw_comp_02', 'DigitalOutput', 'MCP23017', 'I2C', 0, 0x60, 10, None, None, 'broker.2', 'input/hw', 'broker.1', 'health/hw', datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'hw_comp_03', 'DigitalInput', 'ADS1115', 'I2C', 0, 0x41, 60, 'broker.1', 'data/hw', None, None, 'broker.1', 'health/hw', datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] self._hw_components = [] with wp_repository.SQLiteRepository( iot_repository_hardware.IotHardwareConfig, self._sqlite_path) as repository: for row in hw_rows: hw_component = iot_repository_hardware.IotHardwareConfig() hw_component.load_row(row) res = repository.insert(hw_component) self.assertEqual(res, 1) self._hw_components.append(hw_component) # Create host assignments assign_rows = [[ 'host.01', 'hw_comp_01', 1, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'host.01', 'hw_comp_02', 2, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'host.01', 'hw_comp_03', 1, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] self._host_components = [] with wp_repository.SQLiteRepository( iot_repository_host.IotHostAssignedComponent, self._sqlite_path) as repository: for row in assign_rows: host_comp = iot_repository_host.IotHostAssignedComponent() host_comp.load_row(row) res = repository.insert(host_comp) self.assertEqual(res, 1) self._host_components.append(host_comp) # Create brokers self._brokers = [] brk_rows = [[ 'broker.1', '192.168.1.250', 1883, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'broker.2', '192.168.1.250', 1883, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] with wp_repository.SQLiteRepository( iot_repository_broker.IotMqttBrokerConfig, self._sqlite_path) as repository: for row in brk_rows: broker = iot_repository_broker.IotMqttBrokerConfig() broker.load_row(row) res = repository.insert(broker) self.assertEqual(res, 1) self._brokers.append(broker) # Create sensors self._sensors = [] sensor_rows = [[ 'sensor.01', 'KYES516', 'hw_comp_01', 0, 60, 'broker.1', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'sensor.02', 'KYES516', 'hw_comp_01', 1, 60, 'broker.1', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'sensor.03', 'KYES516', 'hw_comp_01', 2, 60, 'broker.1', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ], [ 'sensor.04', 'KYES516', 'hw_comp_03', 0, 60, 'broker.2', 'data/sensor', None, None, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") ]] with wp_repository.SQLiteRepository( iot_repository_sensor.IotSensorConfig, self._sqlite_path) as repository: for row in sensor_rows: sensor = iot_repository_sensor.IotSensorConfig() sensor.load_row(row) res = repository.insert(sensor) self.assertEqual(res, 1) self._sensors.append(sensor)