def create_devices(Config): for section in Config.sections(): # split section name ITEM123 or ITEM_123 or ITEM-123 into device=ITEM and circuit=123 res = re.search('^([^_-]+)[_-]+(.+)$', section) if not res: res = re.search('^(.*\D)(\d+)$', section) if not res: continue devclass = res.group(1) # devclass = re.sub('[_-]*$','',devclass) # circuit = int(res.group(2)) circuit = res.group(2) # print "%s %s %s" % (section,devclass, circuit) try: if devclass == 'OWBUS': import owclient bus = Config.get(section, "owbus") interval = Config.getfloat(section, "interval") scan_interval = Config.getfloat(section, "scan_interval") #### prepare 1wire process ##### (using thread affects timing!) resultPipe = multiprocessing.Pipe() taskPipe = multiprocessing.Pipe() owbus = owclient.OwBusDriver(circuit, taskPipe, resultPipe, bus=bus, interval=interval, scan_interval=scan_interval) Devices.register_device(OWBUS, owbus) elif devclass == 'SENSOR' or devclass == '1WDEVICE': #permanent thermometer bus = Config.get(section, "bus") owbus = Devices.by_int(OWBUS, bus) typ = Config.get(section, "type") address = Config.get(section, "address") interval = Config.getintdef(section, "interval", 15) sensor = owclient.MySensorFabric(address, typ, owbus, interval=interval, circuit=circuit, is_static=True) Devices.register_device(SENSOR, sensor) elif devclass == '1WRELAY': # Relays on DS2404 sensor = Config.get(section, "sensor") sensor = Devices.by_int(SENSOR, sensor) pin = Config.getint(section, "pin") r = unipig.DS2408_relay(circuit, sensor, pin) Devices.register_device(RELAY, r) elif devclass == '1WINPUT': # Inputs on DS2404 sensor = Config.get(section, "sensor") sensor = Devices.by_int(SENSOR, sensor) pin = Config.getint(section, "pin") i = unipig.DS2408_input(circuit, sensor, pin) Devices.register_device(INPUT, i) elif devclass == 'I2CBUS': # I2C bus on /dev/i2c-1 via pigpio daemon busid = Config.getint(section, "busid") i2cbus = I2cBus(circuit=circuit, host='localhost', busid=busid) Devices.register_device(I2CBUS, i2cbus) elif devclass == 'MCP': # MCP on I2c i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) bus = Devices.by_int(I2CBUS, i2cbus) mcp = unipig.UnipiMcp(bus, circuit, address=address) Devices.register_device(MCP, mcp) elif devclass == 'RELAY': # Relays on MCP mcp = Config.get(section, "mcp") mcp = Devices.by_int(MCP, mcp) pin = Config.getint(section, "pin") r = unipig.Relay(circuit, mcp, pin) Devices.register_device(RELAY, r) elif devclass == 'GPIOBUS': # access to GPIO via pigpio daemon bus = GpioBus(circuit=circuit, host='localhost') Devices.register_device(GPIOBUS, bus) elif devclass == 'PCA9685': #PCA9685 on I2C i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) frequency = Config.getintdef(section, "frequency", 400) bus = Devices.by_int(I2CBUS, i2cbus) pca = unipig.UnipiPCA9685(bus, int(circuit), address=address, frequency=frequency) Devices.register_device(PCA9685, pca) elif devclass in ('AO', 'ANALOGOUTPUT'): try: #analog output on PCA9685 pca = Config.get(section, "pca") channel = Config.getint(section, "channel") #value = Config.getfloatdef(section, "value", 0) driver = Devices.by_int(PCA9685, pca) ao = unipig.AnalogOutputPCA(circuit, driver, channel) except: # analog output (PWM) on GPIO via pigpio daemon gpiobus = Config.get(section, "gpiobus") bus = Devices.by_int(GPIOBUS, gpiobus) frequency = Config.getintdef(section, "frequency", 100) value = Config.getfloatdef(section, "value", 0) ao = unipig.AnalogOutputGPIO(bus, circuit, frequency=frequency, value=value) Devices.register_device(AO, ao) elif devclass in ('DI', 'INPUT'): # digital inputs on GPIO via pigpio daemon gpiobus = Config.get(section, "gpiobus") bus = Devices.by_int(GPIOBUS, gpiobus) pin = Config.getint(section, "pin") debounce = Config.getintdef(section, "debounce", 0) counter_mode = Config.getstringdef(section, "counter_mode", "disabled") inp = unipig.Input(bus, circuit, pin, debounce=debounce, counter_mode=counter_mode) Devices.register_device(INPUT, inp) elif devclass in ('EPROM', 'EE'): i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) size = Config.getintdef(section, "size", 256) bus = Devices.by_int(I2CBUS, i2cbus) ee = unipig.Eprom(bus, circuit, size=size, address=address) Devices.register_device(EE, ee) elif devclass in ('AICHIP', ): i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) bus = Devices.by_int(I2CBUS, i2cbus) mcai = unipig.UnipiMCP342x(bus, circuit, address=address) Devices.register_device(ADCHIP, mcai) elif devclass in ('AI', 'ANALOGINPUT'): chip = Config.get(section, "chip") channel = Config.getint(section, "channel") interval = Config.getfloatdef(section, "interval", 0) bits = Config.getintdef(section, "bits", 14) gain = Config.getintdef(section, "gain", 1) if circuit in ('1', '2'): correction = Config.getfloatdef( section, "correction", globals['devices']['ai'][circuit]) else: correction = Config.getfloatdef(section, "correction", 5.564920867) #print correction mcai = Devices.by_int(ADCHIP, chip) try: corr_rom = Config.get(section, "corr_rom") eeprom = Devices.by_int(EE, corr_rom) corr_addr = hexint(Config.get(section, "corr_addr")) ai = unipig.AnalogInput(circuit, mcai, channel, bits=bits, gain=gain, continuous=False, interval=interval, correction=correction, rom=eeprom, corr_addr=corr_addr) except: ai = unipig.AnalogInput(circuit, mcai, channel, bits=bits, gain=gain, continuous=False, interval=interval, correction=correction) Devices.register_device(AI, ai) elif devclass == 'NEURON': from neuron import Neuron modbus_server = Config.getstringdef(section, "modbus_server", "127.0.0.1") modbus_port = Config.getintdef(section, "modbus_port", 502) scanfreq = Config.getfloatdef(section, "scan_frequency", 1) scan_enabled = Config.getbooldef(section, "scan_enabled", True) neuron = Neuron(circuit, modbus_server, modbus_port, scanfreq, scan_enabled) Devices.register_device(NEURON, neuron) elif devclass == 'UNIPI2': '''from spiarm import ArmSpiAsync, ArmUart # UNIPI2 on SPI bus busdevice = Config.get(section, "busdevice") gpioint = Config.getint(section, "gpioint") arm = ArmSpiAsync(circuit, busdevice, gpioint) Devices.register_device(UNIPI2, arm) if True: import unipi2 for i in range(arm.nDO): _r = unipi2.Relay("%s%02d" % (circuit,i+1),arm,i, 1, 0x1<<i) Devices.register_device(RELAY, _r) for i in range(arm.nDI): _inp = unipi2.Input("%s%02d" % (circuit,i+1),arm, 0, 0x1<<i, regdebounce=1006+i, regcounter=arm.counter_reg+(2*i)) Devices.register_device(INPUT, _inp) for i in range(arm.nAO): _ao = unipi2.AnalogOutput("%s%02d" % (circuit,i+1),arm, 2+i) Devices.register_device(AO, _ao) for i in range(arm.nAI): correction = 1.0 if i==0 else 1.0/3 _ai = unipi2.AnalogInput("%s%02d" % (circuit,i+1),arm, 3+i, correction = correction) Devices.register_device(AI, _ai) arm.scanning = True ''' elif devclass == 'UART': # UART on UNIPI2 ''' unipi2 = Config.get(section, "unipi2") unipi2 = Devices.by_int(UNIPI2, unipi2) uart = ArmUart(circuit,unipi2) Devices.register_device(UART, uart) ''' except Exception, E: logger.debug("Error in config section %s - %s", section, str(E))
def create_devices(Config, hw_dict): dev_counter = 0 Config.hw_dict = hw_dict for section in Config.sections(): # split section name ITEM123 or ITEM_123 or ITEM-123 into device=ITEM and circuit=123 res = re.search('^([^_-]+)[_-]+(.+)$', section) if not res: res = re.search('^(.*\D)(\d+)$', section) if not res: continue devclass = res.group(1) circuit = res.group(2) try: if devclass == 'OWBUS': import owclient bus = Config.get(section, "owbus") interval = Config.getfloat(section, "interval") scan_interval = Config.getfloat(section, "scan_interval") #### prepare 1wire process ##### (using thread affects timing!) resultPipe = multiprocessing.Pipe() taskPipe = multiprocessing.Pipe() bus_driver = owclient.OwBusDriver(circuit, taskPipe, resultPipe, bus=bus, interval=interval, scan_interval=scan_interval) owbus = OWBusDevice(bus_driver, dev_id=0) Devices.register_device(OWBUS, owbus) elif devclass == 'SENSOR' or devclass == '1WDEVICE': #permanent thermometer bus = Config.get(section, "bus") owbus = (Devices.by_int(OWBUS, bus)).bus_driver ow_type = Config.get(section, "type") address = Config.get(section, "address") interval = Config.getintdef(section, "interval", 15) sensor = owclient.MySensorFabric(address, ow_type, owbus, interval=interval, circuit=circuit, is_static=True) if ow_type in ["DS2408", "DS2406", "DS2404", "DS2413"]: sensor = OWSensorDevice(sensor, dev_id=0) Devices.register_device(SENSOR, sensor) elif devclass == '1WRELAY': # Relays on DS2404 sensor = Config.get(section, "sensor") sensor = (Devices.by_int(SENSOR, sensor)).sensor_dev pin = Config.getint(section, "pin") r = unipig.DS2408_relay(circuit, sensor, pin, dev_id=0) Devices.register_device(RELAY, r) elif devclass == '1WINPUT': # Inputs on DS2404 sensor = Config.get(section, "sensor") sensor = (Devices.by_int(SENSOR, sensor)).sensor_dev pin = Config.getint(section, "pin") i = unipig.DS2408_input(circuit, sensor, pin, dev_id=0) Devices.register_device(INPUT, i) elif devclass == 'I2CBUS': # I2C bus on /dev/i2c-1 via pigpio daemon busid = Config.getint(section, "busid") bus_driver = I2cBus(circuit=circuit, host='localhost', busid=busid) i2cbus = I2CBusDevice(bus_driver, 0) Devices.register_device(I2CBUS, i2cbus) elif devclass == 'MCP': # MCP on I2c i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) bus = (Devices.by_int(I2CBUS, i2cbus)).bus_driver mcp = unipig.UnipiMcp(bus, circuit, address=address, dev_id=0) Devices.register_device(MCP, mcp) elif devclass == 'RELAY': # Relays on MCP mcp = Config.get(section, "mcp") mcp = Devices.by_int(MCP, mcp) pin = Config.getint(section, "pin") r = unipig.Relay(circuit, mcp, pin, dev_id=0) Devices.register_device(RELAY, r) elif devclass == 'GPIOBUS': # access to GPIO via pigpio daemon bus_driver = GpioBus(circuit=circuit, host='localhost') gpio_bus = GPIOBusDevice(bus_driver, 0) Devices.register_device(GPIOBUS, gpio_bus) elif devclass == 'PCA9685': #PCA9685 on I2C i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) frequency = Config.getintdef(section, "frequency", 400) bus = (Devices.by_int(I2CBUS, i2cbus)).bus_driver pca = unipig.UnipiPCA9685(bus, int(circuit), address=address, frequency=frequency, dev_id=0) Devices.register_device(PCA9685, pca) elif devclass in ('AO', 'ANALOGOUTPUT'): try: #analog output on PCA9685 pca = Config.get(section, "pca") channel = Config.getint(section, "channel") #value = Config.getfloatdef(section, "value", 0) driver = Devices.by_int(PCA9685, pca) ao = unipig.AnalogOutputPCA(circuit, driver, channel, dev_id=0) except: # analog output (PWM) on GPIO via pigpio daemon gpiobus = Config.get(section, "gpiobus") bus = (Devices.by_int(GPIOBUS, gpiobus)).bus_driver frequency = Config.getintdef(section, "frequency", 100) value = Config.getfloatdef(section, "value", 0) ao = unipig.AnalogOutputGPIO(bus, circuit, frequency=frequency, value=value, dev_id=0) Devices.register_device(AO, ao) elif devclass in ('DI', 'INPUT'): # digital inputs on GPIO via pigpio daemon gpiobus = Config.get(section, "gpiobus") bus = (Devices.by_int(GPIOBUS, gpiobus)).bus_driver pin = Config.getint(section, "pin") debounce = Config.getintdef(section, "debounce", 0) counter_mode = Config.getstringdef(section, "counter_mode", "disabled") inp = unipig.Input(bus, circuit, pin, debounce=debounce, counter_mode=counter_mode, dev_id=0) Devices.register_device(INPUT, inp) elif devclass in ('EPROM', 'EE'): i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) size = Config.getintdef(section, "size", 256) bus = (Devices.by_int(I2CBUS, i2cbus)).bus_driver ee = unipig.Eprom(bus, circuit, size=size, address=address, dev_id=0) Devices.register_device(EE, ee) elif devclass in ('AICHIP', ): i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) bus = (Devices.by_int(I2CBUS, i2cbus)).bus_driver mcai = unipig.UnipiMCP342x(bus, circuit, address=address, dev_id=0) Devices.register_device(ADCHIP, mcai) elif devclass in ('AI', 'ANALOGINPUT'): chip = Config.get(section, "chip") channel = Config.getint(section, "channel") interval = Config.getfloatdef(section, "interval", 0) bits = Config.getintdef(section, "bits", 14) gain = Config.getintdef(section, "gain", 1) if circuit in ('1', '2'): correction = Config.getfloatdef( section, "correction", up_globals['devices']['ai'][circuit]) else: correction = Config.getfloatdef(section, "correction", 5.564920867) mcai = Devices.by_int(ADCHIP, chip) try: corr_rom = Config.get(section, "corr_rom") eeprom = Devices.by_int(EE, corr_rom) corr_addr = hexint(Config.get(section, "corr_addr")) ai = unipig.AnalogInput(circuit, mcai, channel, bits=bits, gain=gain, continuous=False, interval=interval, correction=correction, rom=eeprom, corr_addr=corr_addr, dev_id=0) except: ai = unipig.AnalogInput(circuit, mcai, channel, bits=bits, gain=gain, continuous=False, interval=interval, correction=correction, dev_id=0) Devices.register_device(AI, ai) elif devclass == 'NEURON': from neuron import Neuron dev_counter += 1 modbus_server = Config.getstringdef(section, "modbus_server", "127.0.0.1") modbus_port = Config.getintdef(section, "modbus_port", 502) scanfreq = Config.getfloatdef(section, "scan_frequency", 1) scan_enabled = Config.getbooldef(section, "scan_enabled", True) allow_register_access = Config.getbooldef( section, "allow_register_access", False) circuit = Config.getintdef(section, "global_id", 2) neuron = Neuron(circuit, Config, modbus_server, modbus_port, scanfreq, scan_enabled, hw_dict, direct_access=allow_register_access, dev_id=dev_counter) Devices.register_device(NEURON, neuron) elif devclass == 'EXTENSION': from neuron import UartNeuron dev_counter += 1 modbus_uart_port = Config.getstringdef(section, "modbus_uart_port", "/dev/ttyNS0") scanfreq = Config.getfloatdef(section, "scan_frequency", 10) scan_enabled = Config.getbooldef(section, "scan_enabled", True) uart_baud_rate = Config.getintdef(section, "baud_rate", 19200) uart_parity = Config.getstringdef(section, "parity", 'N') uart_stopbits = Config.getintdef(section, "stop_bits", 1) uart_address = Config.getintdef(section, "address", 1) device_name = Config.getstringdef(section, "device_name", "unspecified") allow_register_access = Config.getbooldef( section, "allow_register_access", False) neuron_uart_circuit = Config.getstringdef( section, "neuron_uart_circuit", "None") circuit = Config.getintdef(section, "global_id", 2) neuron = UartNeuron(circuit, Config, modbus_uart_port, scanfreq, scan_enabled, hw_dict, baud_rate=uart_baud_rate, parity=uart_parity, stopbits=uart_stopbits, device_name=device_name, uart_address=uart_address, direct_access=allow_register_access, dev_id=dev_counter, neuron_uart_circuit=neuron_uart_circuit) Devices.register_device(NEURON, neuron) except Exception, E: logger.exception("Error in config section %s - %s", section, str(E))
def create_devices(Config): for section in Config.sections(): # split section name ITEM123 or ITEM_123 or ITEM-123 into device=ITEM and circuit=123 res = re.search('^([^_-]+)[_-]+(.+)$', section) if not res: res = re.search('^(.*\D)(\d+)$', section) if not res: continue devclass = res.group(1) # devclass = re.sub('[_-]*$','',devclass) # circuit = int(res.group(2)) circuit = res.group(2) #print "%s %s %s" % (section,devclass, circuit) try: if devclass == 'OWBUS': bus = Config.get(section, "owbus") interval = Config.getfloat(section, "interval") scan_interval = Config.getfloat(section, "scan_interval") #### prepare 1wire process ##### (using thread affects timing!) resultPipe = multiprocessing.Pipe() taskPipe = multiprocessing.Pipe() owbus = owclient.OwBusDriver(circuit, taskPipe, resultPipe, bus=bus, interval=interval, scan_interval=scan_interval) Devices.register_device(OWBUS, owbus) elif devclass == 'SENSOR': #permanent thermometer bus = Config.get(section, "bus") owbus = Devices.by_int(OWBUS, bus) typ = Config.get(section, "type") address = Config.get(section, "address") interval = Config.getfloat(section, "interval") therm1 = owclient.MySensorFabric(address, typ, owbus, interval=1, circuit=circuit) Devices.register_device(SENSOR, therm1) elif devclass == 'I2CBUS': # I2C bus on /dev/i2c-1 via pigpio daemon busid = Config.getint(section, "busid") i2cbus = I2cBus(circuit=circuit, host='localhost', busid=busid) Devices.register_device(I2CBUS, i2cbus) elif devclass == 'MCP': # MCP on I2c i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) bus = Devices.by_int(I2CBUS, i2cbus) mcp = unipig.UnipiMcp(bus, circuit, address=address) Devices.register_device(MCP, mcp) elif devclass == 'RELAY': # Relays on MCP mcp = Config.get(section, "mcp") mcp = Devices.by_int(MCP, mcp) pin = Config.getint(section, "pin") r = unipig.Relay(circuit, mcp, pin) Devices.register_device(RELAY, r) elif devclass == 'GPIOBUS': # access to GPIO via pigpio daemon bus = GpioBus(circuit=circuit, host='localhost') Devices.register_device(GPIOBUS, bus) elif devclass == 'PCA9685': #PCA9685 on I2C i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) frequency = getintdef(Config, section, "frequency", 400) bus = Devices.by_int(I2CBUS, i2cbus) pca = unipig.UnipiPCA9685(bus, int(circuit), address=address, frequency=frequency) Devices.register_device(PCA9685, pca) elif devclass in ('AO', 'ANALOGOUTPUT'): try: #analog output on PCA9685 pca = Config.get(section, "pca") channel = Config.getint(section, "channel") #value = getfloatdef(Config, section, "value", 0) driver = Devices.by_int(PCA9685, pca) ao = unipig.AnalogOutputPCA(circuit, driver, channel) except: # analog output (PWM) on GPIO via pigpio daemon gpiobus = Config.get(section, "gpiobus") bus = Devices.by_int(GPIOBUS, gpiobus) frequency = getintdef(Config, section, "frequency", 100) value = getfloatdef(Config, section, "value", 0) ao = unipig.AnalogOutputGPIO(bus, circuit, frequency=frequency, value=value) Devices.register_device(AO, ao) elif devclass in ('INPUT', 'DI'): # digital inputs on GPIO via pigpio daemon gpiobus = Config.get(section, "gpiobus") bus = Devices.by_int(GPIOBUS, gpiobus) pin = Config.getint(section, "pin") debounce = getintdef(Config, section, "debounce", 0) inp = unipig.Input(bus, circuit, pin, debounce=debounce) Devices.register_device(INPUT, inp) elif devclass in ('EPROM', 'EE'): i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) size = getintdef(Config, section, "size", 256) bus = Devices.by_int(I2CBUS, i2cbus) ee = unipig.Eprom(bus, circuit, size=size, address=address) Devices.register_device(EE, ee) elif devclass in ('AICHIP', ): i2cbus = Config.get(section, "i2cbus") address = hexint(Config.get(section, "address")) bus = Devices.by_int(I2CBUS, i2cbus) mcai = unipig.UnipiMCP342x(bus, circuit, address=address) Devices.register_device(ADCHIP, mcai) elif devclass in ('AI', 'ANALOGINPUT'): chip = Config.get(section, "chip") channel = Config.getint(section, "channel") interval = getfloatdef(Config, section, "interval", 0) bits = getintdef(Config, section, "bits", 14) gain = getintdef(Config, section, "gain", 1) correction = getfloatdef(Config, section, "correction", 5.564920867) corr_rom = Config.get(section, "corr_rom") eeprom = Devices.by_int(EE, corr_rom) corr_addr = hexint(Config.get(section, "corr_addr")) mcai = Devices.by_int(ADCHIP, chip) ai = unipig.AnalogInput(circuit, mcai, channel, bits=bits, gain=gain, continuous=False, interval=interval, correction=correction, rom=eeprom, corr_addr=corr_addr) Devices.register_device(AI, ai) except Exception, E: print("Error in config section %s - %s" % (section, str(E)))