def __init__(self, readings_conn): # get to the ip address of the single base unit try: ip_addr = SysWideConfig.prop_val('deployment', 'wbu_ip') except: Logger.err( f"Failed to determine the ip addr of the base unit. Aborting." ) sys.exit() try: port = SysWideConfig.prop_val('deployment', 'wbu_port') except: Logger.err( f"Failed to determine the port of the base unit. Aborting.") sys.exit() # make the base unit connection object. the connection handle will reside with it. self.wbu_conn = WBUConn(wbu_ip=ip_addr, wbu_port=port) self.readings_conn = readings_conn # prep the sensors self.sensors = [] self._prep_sensors()
def connect(self): if self.wbu_conn_handle is not None: try: self.wbu_conn_handle = self.wbu_conn.open() return True except: Logger.err(f"failed to open the connection") return False
def __init__(self, wbu_ip, wbu_port): self.wbu_ip = wbu_ip self.wbu_conn_handle = None # the handle made within the connection upon making the connection try: self.wbu_conn = ModbusClient(host=wbu_ip, port=502) except Exception as e: Logger.err(f"unable to make the connection to WBU: {e}") sys.exit()
def connect(self): # make the connection to the db try: self.handle = sqlite3.connect(self.db_file) except sqlite3.Error as e: Logger.err( f'Failed to make the connection to the SQLite3 db: {e}. Aborting.' ) sys.exit()
def __init__(self, sleep_secs): # make the connection to the sqlite3 db try: readings_db_path = SysWideConfig.prop_val('readings_db', 'sqlite_path') except: Logger.err(f"Failed to determine the path of the readings db. Aborting.") sys.exit() self.readings_conn = SQLiteConn(db_path=readings_db_path) self.acquirer = Acquirer(readings_conn=self.readings_conn) self.outputter = Outputter(sensors=self.acquirer.sensors, readings_conn=self.readings_conn) self.sleep_secs = sleep_secs
def acquire_readings(self): # acquire and record (locally) readings from this sensor no_of_regs_to_read = 1 # what does the wbu say is the time of the last reading of ? try: self.time_of_last_update = self.wbu_conn.wbu_conn_handle.read_input_registers(self.reg_last_updated, no_of_regs_to_read) except Exception as e: Logger.err(f"Unable to access Modbus {e}") status = False # record the time of latest acquisition from the wbu self.time_last_acquired = datetime.now() if self.time_of_last_update is None: # TODO: this is just strange. self.co2_reading = 99 else: self.co2_reading = self.wbu_conn.wbu_conn_handle.read_input_registers(self.reg_co2, no_of_regs_to_read) self._assess_sensor_status()
def _prep_sensors(self): # prep the sensors from the sensors config # get hold of the locations info from the global config if 'locations' in SysWideConfig.loaded_config: locations_config = SysWideConfig.loaded_config('locations') else: Logger.err( f"Failed to find the locations within the site configuration. Aborting." ) sys.exit() Logger.info(f"Creating and adding the sensors") for location, config in locations_config: # exclude inactive sensors state = config['state'] if not state: continue # TODO: add try-excepts are all of these calls as we cannot assume that the config is correct. sensor_id = config['sensor_id'] reg_temp = config['reg_temp'] reg_humidity = config['reg_humidity'] reg_co2 = config['reg_co2'] reg_last_updated = config['reg_last_updated'] # make and record the sensor sensor = Sensor(wbu_conn=self.wbu_conn, location=location, sensor_id=sensor_id, reg_temp=reg_temp, reg_humidity=reg_humidity, reg_co2=reg_co2, reg_last_updated=reg_last_updated) self.sensors.append(sensor)
def sensor_status(self, value): if value in (Sensor.SENSOR_OK, Sensor.SENSOR_NOK): self._sensor_status = value else: Logger.err(f"Passed an unexpected value of sensor status: {value}. Aborting.") sys.exit()