Exemplo n.º 1
0
from mycodo.inputs.tsl2591_sensor import InputModule as TSL2591Sensor
from mycodo.inputs.ttn_data_storage import InputModule as TTNDataStorage

input_classes = [
    AM2315Sensor(None, testing=True),
    AtlaspHSensor(None, testing=True),
    AtlasPT1000Sensor(None, testing=True),
    BH1750Sensor(None, testing=True),
    BME280Sensor(None, testing=True),
    BMP180Sensor(None, testing=True),
    BMP280Sensor(None, testing=True),
    ChirpSensor(None, testing=True),
    DHT11Sensor(None, testing=True),
    DHT22Sensor(None, testing=True),
    DS18B20Sensor(None, testing=True),
    HTU21DSensor(None, testing=True),
    INA219xSensor(None, testing=True),
    K30Sensor(None, testing=True),
    LinuxCommand(None, testing=True),
    MHZ16Sensor(None, testing=True),
    MHZ19Sensor(None, testing=True),
    MycodoRam(None, testing=True),
    RaspberryPiCPUTemp(None, testing=True),
    RaspberryPiCPULoad(None, testing=True),
    RaspberryPiFreeSpace(None, testing=True),
    SHT1x7xSensor(None, testing=True),
    SHT2xSensor(None, testing=True),
    SignalPWMInput(None, testing=True),
    SignalRPMInput(None, testing=True),
    TMP006Sensor(None, testing=True),
    TSL2561Sensor(None, testing=True),
Exemplo n.º 2
0
    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.measurement_success = 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.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_relay_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

        # Multiplexer
        self.mux_address_raw = input_dev.multiplexer_address
        self.mux_bus = input_dev.multiplexer_bus
        self.mux_chan = input_dev.multiplexer_channel

        # 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_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_relay_during_measure = input_dev.pre_relay_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.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 multiplexer if enabled
        if self.mux_address_raw:
            from mycodo.devices.tca9548a import TCA9548A
            self.mux_address_string = self.mux_address_raw
            self.mux_address = int(str(self.mux_address_raw), 16)
            self.mux_lock_file = "/var/lock/mycodo_multiplexer_0x{i2c:02X}.pid".format(
                i2c=self.mux_address)
            # Set up lock for pre-output
            self.mux_lock = locket.lock_file(self.mux_lock_file, timeout=120)
            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 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.i2c_address, self.i2c_bus,
                                       self.adc_chan, self.adc_gain)
            elif self.device == 'MCP3008':
                from mycodo.devices.mcp3008 import MCP3008Read
                self.adc = MCP3008Read(self.pin_clock, self.pin_cs,
                                       self.pin_miso, self.pin_mosi,
                                       self.adc_chan, self.adc_volts_max)
            elif self.device == 'MCP342x' and self.location:
                from mycodo.devices.mcp342x import MCP342xRead
                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'] + LIST_DEVICES_ADC:
            self.measure_input = None
        elif self.device == 'MYCODO_RAM':
            from mycodo.inputs.mycodo_ram import MycodoRam
            self.measure_input = MycodoRam()
        elif self.device == 'RPiCPULoad':
            from mycodo.inputs.raspi_cpuload import RaspberryPiCPULoad
            self.measure_input = RaspberryPiCPULoad()
        elif self.device == 'RPi':
            from mycodo.inputs.raspi import RaspberryPiCPUTemp
            self.measure_input = RaspberryPiCPUTemp(
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'RPiFreeSpace':
            from mycodo.inputs.raspi_freespace import RaspberryPiFreeSpace
            self.measure_input = RaspberryPiFreeSpace(self.location)
        elif self.device == 'AM2315':
            from mycodo.inputs.am2315 import AM2315Sensor
            self.measure_input = AM2315Sensor(self.i2c_bus,
                                              power=self.power_output_id)
        elif self.device == 'ATLAS_EC_I2C':
            from mycodo.inputs.atlas_ec import AtlasElectricalConductivitySensor
            self.measure_input = AtlasElectricalConductivitySensor(
                self.interface,
                i2c_address=self.i2c_address,
                i2c_bus=self.i2c_bus,
                sensor_sel=self.input_sel)
        elif self.device == 'ATLAS_EC_UART':
            from mycodo.inputs.atlas_ec import AtlasElectricalConductivitySensor
            self.measure_input = AtlasElectricalConductivitySensor(
                self.interface,
                device_loc=self.device_loc,
                baud_rate=self.baud_rate,
                sensor_sel=self.input_sel)
        elif self.device == 'ATLAS_PH_I2C':
            from mycodo.inputs.atlas_ph import AtlaspHSensor
            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':
            from mycodo.inputs.atlas_ph import AtlaspHSensor
            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':
            from mycodo.inputs.atlas_pt1000 import AtlasPT1000Sensor
            self.measure_input = AtlasPT1000Sensor(
                self.interface,
                i2c_address=self.i2c_address,
                i2c_bus=self.i2c_bus,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'ATLAS_PT1000_UART':
            from mycodo.inputs.atlas_pt1000 import AtlasPT1000Sensor
            self.measure_input = AtlasPT1000Sensor(
                self.interface,
                device_loc=self.device_loc,
                baud_rate=self.baud_rate,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'BH1750':
            from mycodo.inputs.bh1750 import BH1750Sensor
            self.measure_input = BH1750Sensor(self.i2c_address, self.i2c_bus,
                                              self.resolution,
                                              self.sensitivity)
        elif self.device == 'BME280':
            from mycodo.inputs.bme280 import BME280Sensor
            self.measure_input = BME280Sensor(
                self.i2c_address,
                self.i2c_bus,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'BMP180':
            from mycodo.inputs.bmp180 import BMP180Sensor
            self.measure_input = BMP180Sensor(
                self.i2c_bus, convert_to_unit=self.convert_to_unit)
        elif self.device == 'BMP280':
            from mycodo.inputs.bmp280 import BMP280Sensor
            self.measure_input = BMP280Sensor(
                self.i2c_address,
                self.i2c_bus,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'CHIRP':
            from mycodo.inputs.chirp import ChirpSensor
            self.measure_input = ChirpSensor(
                self.i2c_address,
                self.i2c_bus,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'DS18B20':
            from mycodo.inputs.ds18b20 import DS18B20Sensor
            self.measure_input = DS18B20Sensor(
                self.location,
                self.resolution,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'DS18S20':
            from mycodo.inputs.ds18s20 import DS18S20Sensor
            self.measure_input = DS18S20Sensor(
                self.location, convert_to_unit=self.convert_to_unit)
        elif self.device == 'DS1822':
            from mycodo.inputs.ds1822 import DS1822Sensor
            self.measure_input = DS1822Sensor(
                self.location,
                self.resolution,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'DS28EA00':
            from mycodo.inputs.ds28ea00 import DS28EA00Sensor
            self.measure_input = DS28EA00Sensor(
                self.location,
                self.resolution,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'DS1825':
            from mycodo.inputs.ds1825 import DS1825Sensor
            self.measure_input = DS1825Sensor(
                self.location,
                self.resolution,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'DHT11':
            from mycodo.inputs.dht11 import DHT11Sensor
            self.measure_input = DHT11Sensor(
                self.input_id,
                int(self.location),
                power=self.power_output_id,
                convert_to_unit=self.convert_to_unit)
        elif self.device in ['DHT22', 'AM2302']:
            from mycodo.inputs.dht22 import DHT22Sensor
            self.measure_input = DHT22Sensor(
                int(self.location),
                power=self.power_output_id,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'GPIO_STATE':
            from mycodo.inputs.gpio_state import GPIOState
            self.measure_input = GPIOState(int(self.location))
        elif self.device == 'HTU21D':
            from mycodo.inputs.htu21d import HTU21DSensor
            self.measure_input = HTU21DSensor(
                self.i2c_bus, convert_to_unit=self.convert_to_unit)
        elif self.device == 'K30_UART':
            from mycodo.inputs.k30 import K30Sensor
            self.measure_input = K30Sensor(self.device_loc,
                                           baud_rate=self.baud_rate)
        elif self.device == 'MAX31850K':
            from mycodo.inputs.max31850k import MAX31850KSensor
            self.measure_input = MAX31850KSensor(
                self.location, convert_to_unit=self.convert_to_unit)
        elif self.device == 'MAX31855':
            from mycodo.inputs.max31855 import MAX31855Sensor
            self.measure_input = MAX31855Sensor(
                self.pin_clock,
                self.pin_cs,
                self.pin_miso,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'MAX31856':
            from mycodo.inputs.max31856 import MAX31856Sensor
            self.measure_input = MAX31856Sensor(
                self.pin_clock,
                self.pin_cs,
                self.pin_miso,
                self.pin_mosi,
                thermocouple_type=self.thermocouple_type,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'MAX31865':
            from mycodo.inputs.max31865 import MAX31865Sensor
            self.measure_input = MAX31865Sensor(
                self.pin_clock,
                self.pin_cs,
                self.pin_miso,
                self.pin_mosi,
                device=self.thermocouple_type,
                resistor_ref=self.ref_ohm,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'MH_Z16_I2C':
            from mycodo.inputs.mh_z16 import MHZ16Sensor
            self.measure_input = MHZ16Sensor(self.interface,
                                             i2c_address=self.i2c_address,
                                             i2c_bus=self.i2c_bus)
        elif self.device == 'MH_Z16_UART':
            from mycodo.inputs.mh_z16 import MHZ16Sensor
            self.measure_input = MHZ16Sensor(self.interface,
                                             device_loc=self.device_loc,
                                             baud_rate=self.baud_rate)
        elif self.device == 'MH_Z19_UART':
            from mycodo.inputs.mh_z19 import MHZ19Sensor
            self.measure_input = MHZ19Sensor(self.device_loc,
                                             baud_rate=self.baud_rate)
        elif self.device == 'MIFLORA':
            from mycodo.inputs.miflora import MifloraSensor
            self.measure_input = MifloraSensor(
                int(self.location), convert_to_unit=self.convert_to_unit)
        elif self.device == 'SHT1x_7x':
            from mycodo.inputs.sht1x_7x import SHT1x7xSensor
            self.measure_input = SHT1x7xSensor(
                int(self.location),
                self.sht_clock_pin,
                self.sht_voltage,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'SHT2x':
            from mycodo.inputs.sht2x import SHT2xSensor
            self.measure_input = SHT2xSensor(
                self.i2c_address,
                self.i2c_bus,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'SIGNAL_PWM':
            from mycodo.inputs.signal_pwm import SignalPWMInput
            self.measure_input = SignalPWMInput(int(self.location),
                                                self.weighting,
                                                self.sample_time)
        elif self.device == 'SIGNAL_RPM':
            from mycodo.inputs.signal_rpm import SignalRPMInput
            self.measure_input = SignalRPMInput(int(self.location),
                                                self.weighting,
                                                self.rpm_pulses_per_rev,
                                                self.sample_time)
        elif self.device == 'TMP006':
            from mycodo.inputs.tmp006 import TMP006Sensor
            self.measure_input = TMP006Sensor(
                self.i2c_address,
                self.i2c_bus,
                convert_to_unit=self.convert_to_unit)
        elif self.device == 'TSL2561':
            from mycodo.inputs.tsl2561 import TSL2561Sensor
            self.measure_input = TSL2561Sensor(self.i2c_address, self.i2c_bus)
        elif self.device == 'TSL2591':
            from mycodo.inputs.tsl2591_sensor import TSL2591Sensor
            self.measure_input = TSL2591Sensor(self.i2c_address, self.i2c_bus)
        elif self.device == 'LinuxCommand':
            from mycodo.inputs.linux_command import LinuxCommand
            self.measure_input = LinuxCommand(self.cmd_command,
                                              self.cmd_measurement)
        elif self.device == 'SERVER_PING':
            from mycodo.inputs.server_ping import ServerPing
            self.measure_input = ServerPing(self.location, self.times_check,
                                            self.deadline)
        elif self.device == 'SERVER_PORT_OPEN':
            from mycodo.inputs.server_port_open import ServerPortOpen
            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