def device_handler(config, devices, descriptions, available_devices, units_used): logging.info('Device handler started. Entering service loop ...') while True: time.sleep(60) while config['message'] != '': logging.info('Device handler blocked (' + config['message'] + '). Waiting ...') if config['message'] == 'device': logging.warning('Device is blocking itself. Removing block.') config['message'] = '' time.sleep(10) config['message'] = 'device' for product, device in devices: if product == 'product': logging.debug('search for device: ' + device + ' ' + devices[product, device]) args = prepare_arguments('openhab', device, devices[product, device]) logging.debug(args) keep_going = True result = OHcommons.search(args, devices, config, keep_going) if result != None: if device not in available_devices: descriptions[device] = result available_devices.append(device) logging.info('New device ' + device + ' added to list of available devices.') config['message'] = 'newdevice' else: if device in available_devices: available_devices.remove(device) logging.info( 'Device ' + device + ' now offline. Deleting from list of available devices.' ) if device in units_used.keys(): for unit in units_used[device]: try: logging.info('Delete subscription for ' + device + ' and event url ' + unit[1] + '.') del unit[1:] except: logging.warning('Subscription for ' + device + ' was already deleted.') del descriptions[device] logging.info('Service loop done. Available devices: ' + str(available_devices) + '.') if config['message'] == 'device': config['message'] = '' return
def search(config, devices): 'search all products in config given and return descriptions' available_devices = [] descriptions = {} for product, device in devices: if product == 'product': logging.debug('search for device: ' + device + ' ' + devices[product, device]) args = prepare_arguments('openhab', device, devices[product, device]) logging.debug(args) keep_going = True result = OHcommons.search(args, devices, config, keep_going) if result != None: descriptions[device] = result available_devices.append(device) logging.info('Devices found: ' + str(available_devices) + '.') return descriptions, available_devices
logger = logging.getLogger() logger.setLevel(numeric_level) logger.addHandler(log_handler) logging.debug('logging started') elif args.log == ["systemd"]: from systemd.journal import JournalHandler logger = logging.getLogger() logger.setLevel(numeric_level) logger.addHandler(JournalHandler(SYSLOG_IDENTIFIER='OHsentinel')) logging.debug('logging started') logging.debug('Used configuration: ' + str(config)) logging.debug('Known devices: ' + str(devices)) return args, config, devices args, config, devices = init() logging.debug(args) if args.mode == 'search': OHcommons.search(args, devices, config) elif args.mode == 'command': if args.unit[0] == 'Custom': "Process command in custom unit" OHcustoms.command(args, devices, config) else: "Process standard command" OHcommons.command(args, devices, config) elif args.mode == 'explore': OHcommons.explore(args, devices, config) elif args.mode == 'remote': OHcommons.remote(args, devices, config)