def __init__(self, ready, input_id): threading.Thread.__init__(self) self.logger = logging.getLogger( "mycodo.input_{id}".format(id=input_id.split('-')[0])) self.stop_iteration_counter = 0 self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.lock = {} self.measurement = None self.measurement_success = False self.control = DaemonControl() self.pause_loop = False self.verify_pause_loop = True self.sample_rate = db_retrieve_table_daemon( Misc, entry='first').sample_rate_controller_input self.input_id = input_id input_dev = db_retrieve_table_daemon( Input, unique_id=self.input_id) self.input_dev = input_dev self.input_name = input_dev.name self.unique_id = input_dev.unique_id self.i2c_bus = input_dev.i2c_bus self.location = input_dev.location self.power_output_id = input_dev.power_output_id self.measurements = input_dev.measurements self.convert_to_unit = input_dev.convert_to_unit self.device = input_dev.device self.interface = input_dev.interface self.device_loc = input_dev.device_loc self.baud_rate = input_dev.baud_rate self.period = input_dev.period self.resolution = input_dev.resolution self.sensitivity = input_dev.sensitivity self.cmd_command = input_dev.cmd_command self.cmd_measurement = input_dev.cmd_measurement self.cmd_measurement_units = input_dev.cmd_measurement_units self.thermocouple_type = input_dev.thermocouple_type self.ref_ohm = input_dev.ref_ohm # Serial self.pin_clock = input_dev.pin_clock self.pin_cs = input_dev.pin_cs self.pin_mosi = input_dev.pin_mosi self.pin_miso = input_dev.pin_miso # ADC self.adc_measure = input_dev.adc_measure self.adc_volts_min = input_dev.adc_volts_min self.adc_volts_max = input_dev.adc_volts_max self.adc_units_min = input_dev.adc_units_min self.adc_units_max = input_dev.adc_units_max self.adc_inverse_unit_scale = input_dev.adc_inverse_unit_scale # Edge detection self.switch_edge = input_dev.switch_edge self.switch_bouncetime = input_dev.switch_bouncetime self.switch_reset_period = input_dev.switch_reset_period # PWM and RPM options self.weighting = input_dev.weighting self.rpm_pulses_per_rev = input_dev.rpm_pulses_per_rev self.sample_time = input_dev.sample_time # Server options self.port = input_dev.port self.times_check = input_dev.times_check self.deadline = input_dev.deadline # Output that will activate prior to input read self.pre_output_id = input_dev.pre_output_id self.pre_output_duration = input_dev.pre_output_duration self.pre_output_during_measure = input_dev.pre_output_during_measure self.pre_output_setup = False self.next_measurement = time.time() self.get_new_measurement = False self.trigger_cond = False self.measurement_acquired = False self.pre_output_activated = False self.pre_output_locked = False self.pre_output_timer = time.time() # Check if Pre-Output ID actually exists output = db_retrieve_table_daemon(Output, entry='all') for each_output in output: if each_output.unique_id == self.pre_output_id and self.pre_output_duration: self.pre_output_setup = True smtp = db_retrieve_table_daemon(SMTP, entry='first') self.smtp_max_count = smtp.hourly_max self.email_count = 0 self.allowed_to_send_notice = True # Set up input lock self.input_lock = None self.lock_file = '/var/lock/input_pre_output_{id}'.format(id=self.pre_output_id) # Convert string I2C address to base-16 int if self.device in LIST_DEVICES_I2C: self.i2c_address = int(str(self.location), 16) # Set up edge detection of a GPIO pin if self.device == 'EDGE': if self.switch_edge == 'rising': self.switch_edge_gpio = GPIO.RISING elif self.switch_edge == 'falling': self.switch_edge_gpio = GPIO.FALLING else: self.switch_edge_gpio = GPIO.BOTH # Set up analog-to-digital converter if self.device in LIST_DEVICES_ADC: if self.device in ['ADS1x15', 'MCP342x']: self.adc_lock_file = "/var/lock/mycodo_adc_bus{bus}_0x{i2c:02X}.pid".format( bus=self.i2c_bus, i2c=self.i2c_address) elif self.device == 'MCP3008': self.adc_lock_file = "/var/lock/mycodo_adc_uart-{clock}-{cs}-{miso}-{mosi}".format( clock=self.pin_clock, cs=self.pin_cs, miso=self.pin_miso, mosi=self.pin_mosi) if self.device == 'ADS1x15' and self.location: from mycodo.devices.ads1x15 import ADS1x15Read self.adc = ADS1x15Read(self.input_dev) elif self.device == 'MCP3008': from mycodo.devices.mcp3008 import MCP3008Read self.adc = MCP3008Read(self.input_dev) elif self.device == 'MCP342x' and self.location: from mycodo.devices.mcp342x import MCP342xRead self.adc = MCP342xRead(self.input_dev) else: self.adc = None self.device_recognized = True # Set up inputs or devices if self.device in ['EDGE'] + LIST_DEVICES_ADC: self.measure_input = None elif self.device == 'MYCODO_RAM': from mycodo.inputs.mycodo_ram import MycodoRam self.measure_input = MycodoRam(self.input_dev) elif self.device == 'RPiCPULoad': from mycodo.inputs.raspi_cpuload import RaspberryPiCPULoad self.measure_input = RaspberryPiCPULoad(self.input_dev) elif self.device == 'RPi': from mycodo.inputs.raspi import RaspberryPiCPUTemp self.measure_input = RaspberryPiCPUTemp(self.input_dev) elif self.device == 'RPiFreeSpace': from mycodo.inputs.raspi_freespace import RaspberryPiFreeSpace self.measure_input = RaspberryPiFreeSpace(self.input_dev) elif self.device == 'AM2315': from mycodo.inputs.am2315 import AM2315Sensor self.measure_input = AM2315Sensor(self.input_dev) elif self.device == 'ATLAS_EC_I2C': from mycodo.inputs.atlas_ec import AtlasElectricalConductivitySensor self.measure_input = AtlasElectricalConductivitySensor(self.input_dev) elif self.device == 'ATLAS_EC_UART': from mycodo.inputs.atlas_ec import AtlasElectricalConductivitySensor self.measure_input = AtlasElectricalConductivitySensor(self.input_dev) elif self.device == 'ATLAS_PH_I2C': from mycodo.inputs.atlas_ph import AtlaspHSensor self.measure_input = AtlaspHSensor(self.input_dev) elif self.device == 'ATLAS_PH_UART': from mycodo.inputs.atlas_ph import AtlaspHSensor self.measure_input = AtlaspHSensor(self.input_dev) elif self.device == 'ATLAS_PT1000_I2C': from mycodo.inputs.atlas_pt1000 import AtlasPT1000Sensor self.measure_input = AtlasPT1000Sensor(self.input_dev) elif self.device == 'ATLAS_PT1000_UART': from mycodo.inputs.atlas_pt1000 import AtlasPT1000Sensor self.measure_input = AtlasPT1000Sensor(self.input_dev) elif self.device == 'BH1750': from mycodo.inputs.bh1750 import BH1750Sensor self.measure_input = BH1750Sensor(self.input_dev) elif self.device == 'BME280': from mycodo.inputs.bme280 import BME280Sensor self.measure_input = BME280Sensor(self.input_dev) elif self.device == 'BMP180': from mycodo.inputs.bmp180 import BMP180Sensor self.measure_input = BMP180Sensor(self.input_dev) elif self.device == 'BMP280': from mycodo.inputs.bmp280 import BMP280Sensor self.measure_input = BMP280Sensor(self.input_dev) elif self.device == 'CCS811': from mycodo.inputs.ccs811 import CCS811Sensor self.measure_input = CCS811Sensor(self.input_dev) elif self.device == 'CHIRP': from mycodo.inputs.chirp import ChirpSensor self.measure_input = ChirpSensor(self.input_dev) elif self.device == 'COZIR_CO2': from mycodo.inputs.cozir_co2 import COZIRSensor self.measure_input = COZIRSensor(self.input_dev) elif self.device == 'DHT11': from mycodo.inputs.dht11 import DHT11Sensor self.measure_input = DHT11Sensor(self.input_dev) elif self.device in ['DHT22', 'AM2302']: from mycodo.inputs.dht22 import DHT22Sensor self.measure_input = DHT22Sensor(self.input_dev) elif self.device == 'DS18B20': from mycodo.inputs.ds18b20 import DS18B20Sensor self.measure_input = DS18B20Sensor(self.input_dev) elif self.device == 'DS18S20': from mycodo.inputs.ds18s20 import DS18S20Sensor self.measure_input = DS18S20Sensor(self.input_dev) elif self.device == 'DS1822': from mycodo.inputs.ds1822 import DS1822Sensor self.measure_input = DS1822Sensor(self.input_dev) elif self.device == 'DS28EA00': from mycodo.inputs.ds28ea00 import DS28EA00Sensor self.measure_input = DS28EA00Sensor(self.input_dev) elif self.device == 'DS1825': from mycodo.inputs.ds1825 import DS1825Sensor self.measure_input = DS1825Sensor(self.input_dev) elif self.device == 'GPIO_STATE': from mycodo.inputs.gpio_state import GPIOState self.measure_input = GPIOState(self.input_dev) elif self.device == 'HDC1000': from mycodo.inputs.hdc1000 import HDC1000Sensor self.measure_input = HDC1000Sensor(self.input_dev) elif self.device == 'HTU21D': from mycodo.inputs.htu21d import HTU21DSensor self.measure_input = HTU21DSensor(self.input_dev) elif self.device == 'K30_UART': from mycodo.inputs.k30 import K30Sensor self.measure_input = K30Sensor(self.input_dev) elif self.device == 'LinuxCommand': from mycodo.inputs.linux_command import LinuxCommand self.measure_input = LinuxCommand(self.input_dev) elif self.device == 'MAX31850K': from mycodo.inputs.max31850k import MAX31850KSensor self.measure_input = MAX31850KSensor(self.input_dev) elif self.device == 'MAX31855': from mycodo.inputs.max31855 import MAX31855Sensor self.measure_input = MAX31855Sensor(self.input_dev) elif self.device == 'MAX31856': from mycodo.inputs.max31856 import MAX31856Sensor self.measure_input = MAX31856Sensor(self.input_dev) elif self.device == 'MAX31865': from mycodo.inputs.max31865 import MAX31865Sensor self.measure_input = MAX31865Sensor(self.input_dev) elif self.device == 'MH_Z16_I2C': from mycodo.inputs.mh_z16 import MHZ16Sensor self.measure_input = MHZ16Sensor(self.input_dev) elif self.device == 'MH_Z16_UART': from mycodo.inputs.mh_z16 import MHZ16Sensor self.measure_input = MHZ16Sensor(self.input_dev) elif self.device == 'MH_Z19_UART': from mycodo.inputs.mh_z19 import MHZ19Sensor self.measure_input = MHZ19Sensor(self.input_dev) elif self.device == 'MIFLORA': from mycodo.inputs.miflora_sensor import MifloraSensor self.measure_input = MifloraSensor(self.input_dev) elif self.device == 'SERVER_PING': from mycodo.inputs.server_ping import ServerPing self.measure_input = ServerPing(self.input_dev) elif self.device == 'SERVER_PORT_OPEN': from mycodo.inputs.server_port_open import ServerPortOpen self.measure_input = ServerPortOpen(self.input_dev) elif self.device == 'SHT1x_7x': from mycodo.inputs.sht1x_7x import SHT1x7xSensor self.measure_input = SHT1x7xSensor(self.input_dev) elif self.device == 'SHT2x': from mycodo.inputs.sht2x import SHT2xSensor self.measure_input = SHT2xSensor(self.input_dev) elif self.device == 'SIGNAL_PWM': from mycodo.inputs.signal_pwm import SignalPWMInput self.measure_input = SignalPWMInput(self.input_dev) elif self.device == 'SIGNAL_RPM': from mycodo.inputs.signal_rpm import SignalRPMInput self.measure_input = SignalRPMInput(self.input_dev) elif self.device == 'TMP006': from mycodo.inputs.tmp006 import TMP006Sensor self.measure_input = TMP006Sensor(self.input_dev) elif self.device == 'TSL2561': from mycodo.inputs.tsl2561 import TSL2561Sensor self.measure_input = TSL2561Sensor(self.input_dev) elif self.device == 'TSL2591': from mycodo.inputs.tsl2591_sensor import TSL2591Sensor self.measure_input = TSL2591Sensor(self.input_dev) else: self.device_recognized = False self.logger.debug("Device '{device}' not recognized".format( device=self.device)) raise Exception("'{device}' is not a valid device type.".format( device=self.device)) self.edge_reset_timer = time.time() self.input_timer = time.time() self.running = False self.lastUpdate = None
def __init__(self, ready, input_id): threading.Thread.__init__(self) self.logger = logging.getLogger( "mycodo.input_{id}".format(id=input_id)) self.stop_iteration_counter = 0 self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.lock = {} self.measurement = None self.updateSuccess = False self.input_id = input_id self.control = DaemonControl() self.pause_loop = False self.verify_pause_loop = True input_dev = db_retrieve_table_daemon(Input, device_id=self.input_id) self.input_sel = input_dev self.id = input_dev.id self.unique_id = input_dev.unique_id self.i2c_bus = input_dev.i2c_bus self.location = input_dev.location self.power_output_id = input_dev.power_relay_id self.measurements = input_dev.measurements self.device = input_dev.device self.interface = input_dev.interface self.device_loc = input_dev.device_loc self.baud_rate = input_dev.baud_rate self.period = input_dev.period self.resolution = input_dev.resolution self.sensitivity = input_dev.sensitivity self.cmd_command = input_dev.cmd_command self.cmd_measurement = input_dev.cmd_measurement self.cmd_measurement_units = input_dev.cmd_measurement_units self.mux_address_raw = input_dev.multiplexer_address self.mux_bus = input_dev.multiplexer_bus self.mux_chan = input_dev.multiplexer_channel self.adc_chan = input_dev.adc_channel self.adc_gain = input_dev.adc_gain self.adc_resolution = input_dev.adc_resolution self.adc_measure = input_dev.adc_measure self.adc_measure_units = input_dev.adc_measure_units self.adc_volts_min = input_dev.adc_volts_min self.adc_volts_max = input_dev.adc_volts_max self.adc_units_min = input_dev.adc_units_min self.adc_units_max = input_dev.adc_units_max self.adc_inverse_unit_scale = input_dev.adc_inverse_unit_scale self.sht_clock_pin = input_dev.sht_clock_pin self.sht_voltage = input_dev.sht_voltage # Edge detection self.switch_edge = input_dev.switch_edge self.switch_bouncetime = input_dev.switch_bouncetime self.switch_reset_period = input_dev.switch_reset_period # PWM and RPM options self.weighting = input_dev.weighting self.rpm_pulses_per_rev = input_dev.rpm_pulses_per_rev self.sample_time = input_dev.sample_time # Server options self.port = input_dev.port self.times_check = input_dev.times_check self.deadline = input_dev.deadline # Output that will activate prior to input read self.pre_output_id = input_dev.pre_relay_id self.pre_output_duration = input_dev.pre_relay_duration self.pre_output_setup = False self.next_measurement = time.time() self.get_new_measurement = False self.trigger_cond = False self.measurement_acquired = False self.pre_output_activated = False self.pre_output_timer = time.time() output = db_retrieve_table_daemon(Output, entry='all') for each_output in output: # Check if output ID actually exists if each_output.id == self.pre_output_id and self.pre_output_duration: self.pre_output_setup = True smtp = db_retrieve_table_daemon(SMTP, entry='first') self.smtp_max_count = smtp.hourly_max self.email_count = 0 self.allowed_to_send_notice = True # Convert string I2C address to base-16 int if self.device in LIST_DEVICES_I2C: self.i2c_address = int(str(self.location), 16) # Set up multiplexer if enabled if self.device in LIST_DEVICES_I2C and self.mux_address_raw: self.mux_address_string = self.mux_address_raw self.mux_address = int(str(self.mux_address_raw), 16) self.mux_lock = "/var/lock/mycodo_multiplexer_0x{i2c:02X}.pid".format( i2c=self.mux_address) self.mux_lock = fasteners.InterProcessLock(self.mux_lock) self.mux_lock_acquired = False self.multiplexer = TCA9548A(self.mux_bus, self.mux_address) else: self.multiplexer = None # Set up edge detection of a GPIO pin if self.device == 'EDGE': if self.switch_edge == 'rising': self.switch_edge_gpio = GPIO.RISING elif self.switch_edge == 'falling': self.switch_edge_gpio = GPIO.FALLING else: self.switch_edge_gpio = GPIO.BOTH # Lock multiplexer, if it's enabled if self.multiplexer: self.lock_multiplexer() # Set up analog-to-digital converter if self.device in ['ADS1x15', 'MCP342x'] and self.location: self.adc_lock_file = "/var/lock/mycodo_adc_bus{bus}_0x{i2c:02X}.pid".format( bus=self.i2c_bus, i2c=self.i2c_address) if self.device == 'ADS1x15': self.adc = ADS1x15Read(self.i2c_address, self.i2c_bus, self.adc_chan, self.adc_gain) elif self.device == 'MCP342x': self.adc = MCP342xRead(self.i2c_address, self.i2c_bus, self.adc_chan, self.adc_gain, self.adc_resolution) else: self.adc = None self.device_recognized = True # Set up inputs or devices if self.device in ['EDGE', 'ADS1x15', 'MCP342x']: self.measure_input = None elif self.device == 'MYCODO_RAM': self.measure_input = MycodoRam() elif self.device == 'RPiCPULoad': self.measure_input = RaspberryPiCPULoad() elif self.device == 'RPi': self.measure_input = RaspberryPiCPUTemp() elif self.device == 'RPiFreeSpace': self.measure_input = RaspberryPiFreeSpace(self.location) elif self.device == 'AM2302': self.measure_input = DHT22Sensor(int(self.location)) elif self.device == 'AM2315': self.measure_input = AM2315Sensor(self.i2c_bus, power=self.power_output_id) elif self.device == 'ATLAS_PH_I2C': self.measure_input = AtlaspHSensor(self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus, sensor_sel=self.input_sel) elif self.device == 'ATLAS_PH_UART': self.measure_input = AtlaspHSensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate, sensor_sel=self.input_sel) elif self.device == 'ATLAS_PT1000_I2C': self.measure_input = AtlasPT1000Sensor( self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus) elif self.device == 'ATLAS_PT1000_UART': self.measure_input = AtlasPT1000Sensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate) elif self.device == 'BH1750': self.measure_input = BH1750Sensor(self.i2c_address, self.i2c_bus, self.resolution, self.sensitivity) elif self.device == 'BME280': self.measure_input = BME280Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'BMP180': self.measure_input = BMP180Sensor(self.i2c_bus) elif self.device == 'BMP280': self.measure_input = BMP280Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'CHIRP': self.measure_input = ChirpSensor(self.i2c_address, self.i2c_bus) elif self.device == 'DS18B20': self.measure_input = DS18B20Sensor(self.location) elif self.device == 'DHT11': self.measure_input = DHT11Sensor(self.input_id, int(self.location), power=self.power_output_id) elif self.device == 'DHT22': self.measure_input = DHT22Sensor(int(self.location), power=self.power_output_id) elif self.device == 'GPIO_STATE': self.measure_input = GPIOState(int(self.location)) elif self.device == 'HTU21D': self.measure_input = HTU21DSensor(self.i2c_bus) elif self.device == 'K30_UART': self.measure_input = K30Sensor(self.device_loc, baud_rate=self.baud_rate) elif self.device == 'MH_Z16_I2C': self.measure_input = MHZ16Sensor(self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus) elif self.device == 'MH_Z16_UART': self.measure_input = MHZ16Sensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate) elif self.device == 'MH_Z19_UART': self.measure_input = MHZ19Sensor(self.device_loc, baud_rate=self.baud_rate) elif self.device == 'SHT1x_7x': self.measure_input = SHT1x7xSensor(int(self.location), self.sht_clock_pin, self.sht_voltage) elif self.device == 'SHT2x': self.measure_input = SHT2xSensor(self.i2c_address, self.i2c_bus) elif self.device == 'SIGNAL_PWM': self.measure_input = SignalPWMInput(int(self.location), self.weighting, self.sample_time) elif self.device == 'SIGNAL_RPM': self.measure_input = SignalRPMInput(int(self.location), self.weighting, self.rpm_pulses_per_rev, self.sample_time) elif self.device == 'TMP006': self.measure_input = TMP006Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'TSL2561': self.measure_input = TSL2561Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'TSL2591': self.measure_input = TSL2591Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'LinuxCommand': self.measure_input = LinuxCommand(self.cmd_command, self.cmd_measurement) elif self.device == 'SERVER_PING': self.measure_input = ServerPing(self.location, self.times_check, self.deadline) elif self.device == 'SERVER_PORT_OPEN': self.measure_input = ServerPortOpen(self.location, self.port) else: self.device_recognized = False self.logger.debug( "Device '{device}' not recognized".format(device=self.device)) raise Exception("'{device}' is not a valid device type.".format( device=self.device)) if self.multiplexer: self.unlock_multiplexer() self.edge_reset_timer = time.time() self.input_timer = time.time() self.running = False self.lastUpdate = None