Beispiel #1
0
def RESTRemote(config, debug):
    logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s')
    logger.setLevel('DEBUG')
    logger.info('Starting with config file %s', config.name)
    configData = yaml.load(config)
    sys.path.append(configData['driversPath'])
    for deviceName, deviceData in configData['devices'].items():
        if deviceData.get('enable', True):
            driverName = deviceData['driver']
            logger.info('Loading device %s using driver %s', deviceName,
                        driverName)
            driver = importlib.import_module('drivers.' + driverName)
            deviceData.update(configData['drivers'][driverName])
            utils.flatten_commands(deviceData)
            devices[deviceName] = getattr(
                driver, deviceData.get('moduleName',
                                       driverName.capitalize()))(deviceData,
                                                                 logger)
            devices[deviceName].start()

    app.run(host=configData.get('bindHost', '0.0.0.0'),
            port=configData.get('port', 5000),
            debug=debug)
Beispiel #2
0
def RESTRemote(config, serverconfig, debug):
    logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s')
    logger.setLevel('DEBUG')
    logger.info('Starting with config file %s', config.name)
    configData = yaml.safe_load(config)
    configData.update(yaml.safe_load(serverconfig))
    sys.path.append(configData['driversPath'])

    drivers = {}
    devicesConfig = configData.get('devices', {})
    for driverName, driverData in configData['drivers'].items():
        module = importlib.import_module('drivers.' + driverName)
        driver = getattr(module,
                         driverData.get('moduleName', driverName.capitalize()))
        drivers[driverName] = driver
        deviceData = driver.discoverDevices(logger)
        if deviceData is not None:
            devicesConfig.update(deviceData)

    configData['devices'] = devicesConfig
    for deviceName, deviceData in configData['devices'].items():
        if deviceData.get('enable', True):
            driverName = deviceData['driver']
            logger.info('Loading device %s using driver %s', deviceName,
                        driverName)
            driverData = configData['drivers'][driverName]
            deviceData.update(driverData)
            deviceData.get('values', {}).update(
                configData.get('driverConfig', {}).get(driverName,
                                                       {}).get('values', {}))
            utils.flatten_commands(deviceData)
            devices[deviceName] = drivers[driverName](deviceData, logger)
            devices[deviceName].start()

    app.run(host=configData.get('bindHost', '0.0.0.0'),
            port=configData.get('port', 5000),
            debug=debug)
Beispiel #3
0
def ISYExport(config, destination, output, input, host, temp):
    configData = yaml.load(config)
    configData['host'] = host

    if input:
        print("Extracting existing resources from", input.name)
        with ZipFile(input) as inputFile:
            inputFile.extractall(destination)

    global curResourceID
    resources = {}
    commands = {}
    maxResourceID = 0
    outputFileName = os.path.join(destination, ISY_CONF_FOLDER, ISY_NET_FOLDER,
        'RES.CFG')
    try:
        with open(outputFileName, 'r') as resourceFile:
            resourceTree = ET.parse(resourceFile).getroot()
            for resource in resourceTree:
                curResourceID = 0
                for id in resource.iter('id'):
                    curResourceID = int(id.text)
                    if maxResourceID < curResourceID:
                        maxResourceID = curResourceID
                for name in resource.iter('name'):
                    resources[name.text] = resource
    except:
        resourceTree = ET.Element('NetConfig')

    print('Found', maxResourceID, 'existing resources')
    curResourceID = maxResourceID + 1

    print("Exporting ISY network resources from", config.name, "to", destination)

    for deviceName, deviceData in configData['devices'].items():
        deviceData.update(configData['drivers'][deviceData['driver']])
        paramParser = ParamParser(deviceData)
        utils.flatten_commands(deviceData)
        for commandName, commandData in deviceData['commands'].items():
            if not commandData.get('result'):
                simpleCommand = True
                resourceName = deviceName + '.' + commandName
                command = deviceName + '/' + commandName
                if commandData.get('acceptsNumber'):
                    print('Create state variable for', resourceName)
                    resourceID = addResource(configData, resources, resourceTree,
                        resourceName)
                    commands[resourceID] = command + STATE_VAR_MESSAGE
                    simpleCommand = False

                if 'value_set' in commandData:
                    for value in paramParser.value_sets[commandData['value_set']].keys():
                        resourceID = addResource(configData, resources,
                            resourceTree, resourceName + '/' + value)
                        commands[resourceID] = command + '/' + value
                    simpleCommand = False

                if simpleCommand:
                    resourceID = addResource(configData, resources, resourceTree,
                        resourceName)
                    commands[resourceID] = command

    outputFileName = os.path.join(destination, ISY_CONF_FOLDER, ISY_NET_FOLDER,
        'RES.CFG')
    if not os.path.exists(os.path.dirname(outputFileName)):
        try:
            os.makedirs(os.path.dirname(outputFileName))
        except OSError as error:
            if error.errno != errno.EEXIST:
                raise

    with ZipFile(os.path.join(destination, output), 'w') as outputFile:

        with open(outputFileName, 'w') as output:
            output.write(ET.tostring(resourceTree).decode())

        # Add main resources file to zip
        outputFile.write(outputFileName,
            os.path.relpath(outputFileName, destination))

        # Add RES files to zip
        for resourceID in range(1, curResourceID):
            command = commands.get(resourceID)
            outputFileName = os.path.join(destination, ISY_CONF_FOLDER,
                ISY_NET_FOLDER, str(resourceID) + '.RES')
            if command:
                with open(outputFileName, 'w') as output:
                    output.write(REQUEST.format(DEFAULT_METHOD, command,
                        configData['host'], configData['port']))

            outputFile.write(outputFileName,
                os.path.relpath(outputFileName, destination))

    if not temp:
        shutil.rmtree(os.path.join(destination, ISY_CONF_FOLDER))
Beispiel #4
0
 def test_prefix(self):
     config = UtilsTester.config['prefix']
     utils.flatten_commands(config)
     self.assertTrue('g1_command1' in config['commands'])
     self.assertTrue('g1_command2' in config['commands'])
Beispiel #5
0
 def test_suffix(self):
     config = UtilsTester.config['suffix']
     utils.flatten_commands(config)
     self.assertTrue('command1_g1' in config['commands'])
     self.assertTrue('command2_g1' in config['commands'])
Beispiel #6
0
 def test_no_main(self):
     config = UtilsTester.config['no_main']
     utils.flatten_commands(config)
     self.assertEqual(len(list(config['commands'].keys())), 1)
Beispiel #7
0
 def test_sub_group(self):
     config = UtilsTester.config['sub_group']
     utils.flatten_commands(config)
     self.assertEqual(len(list(config['commands'].keys())), 3)
Beispiel #8
0
 def test_simple(self):
     config = UtilsTester.config['simple']
     utils.flatten_commands(config)
     self.assertEqual(len(list(config['commands'].keys())), 2)