Exemple #1
0
    def __configure_and_run_slave(config):
        identity = None
        if config.get('identity'):
            identity = ModbusDeviceIdentification()
            identity.VendorName = config['identity'].get('vendorName', '')
            identity.ProductCode = config['identity'].get('productCode', '')
            identity.VendorUrl = config['identity'].get('vendorUrl', '')
            identity.ProductName = config['identity'].get('productName', '')
            identity.ModelName = config['identity'].get('ModelName', '')
            identity.MajorMinorRevision = version.short()

        blocks = {}
        for (key, value) in config.get('values').items():
            values = {}
            converter = BytesModbusDownlinkConverter({})
            for item in value:
                for section in ('attributes', 'timeseries', 'attributeUpdates', 'rpc'):
                    for val in item.get(section, []):
                        function_code = FUNCTION_CODE_WRITE[key][0] if val['objectsCount'] <= 1 else \
                            FUNCTION_CODE_WRITE[key][1]
                        converted_value = converter.convert(
                            {**val,
                             'device': config.get('deviceName', 'Gateway'), 'functionCode': function_code,
                             'byteOrder': config['byteOrder']},
                            {'data': {'params': val['value']}})
                        values[val['address'] + 1] = converted_value

            blocks[FUNCTION_TYPE[key]] = ModbusSparseDataBlock(values)

        context = ModbusServerContext(slaves=ModbusSlaveContext(**blocks), single=True)
        SLAVE_TYPE[config['type']](context, identity=identity,
                                   address=(config.get('host'), config.get('port')) if (
                                           config['type'] == 'tcp' or 'udp') else None,
                                   port=config.get('port') if config['type'] == 'serial' else None,
                                   framer=FRAMER_TYPE[config['method']])
 def __load_converters(self):
     try:
         for device in self.__config["devices"]:
             if self.__config.get("converter") is not None:
                 converter = TBUtility.check_and_import(self._connector_type, self.__config["converter"])(device)
             else:
                 converter = BytesModbusUplinkConverter(device)
             if self.__config.get("downlink_converter") is not None:
                 downlink_converter = TBUtility.check_and_import(self._connector_type, self.__config["downlink_converter"])(device)
             else:
                 downlink_converter = BytesModbusDownlinkConverter(device)
             if device.get('deviceName') not in self.__gateway.get_devices():
                 self.__gateway.add_device(device.get('deviceName'), {"connector": self}, device_type=device.get("deviceType"))
             self.__devices[device["deviceName"]] = {"config": device,
                                                     "converter": converter,
                                                     "downlink_converter": downlink_converter,
                                                     "next_attributes_check": 0,
                                                     "next_timeseries_check": 0,
                                                     "telemetry": {},
                                                     "attributes": {},
                                                     "last_telemetry": {},
                                                     "last_attributes": {}
                                                     }
     except Exception as e:
         log.exception(e)
Exemple #3
0
    def addDevices(self, config, connector):
        token = config['accessToken']
        self._tokens.add(token)
        if token is not None:
            for device in config['devices']:
                if config.get("converter") is not None:
                    converter = TBUtility.check_and_import(
                        'tcp', self._config["converter"])(device)
                else:
                    converter = BytesModbusUplinkConverter(device)
                if config.get("downlink_converter") is not None:
                    downlink_converter = TBUtility.check_and_import(
                        'tcp', self._config["downlink_converter"])(device)
                else:
                    downlink_converter = BytesModbusDownlinkConverter(device)
                if device.get('deviceName') not in self._gateway.get_devices():
                    self._gateway.add_device(
                        device.get('deviceName'), {"connector": connector},
                        device_type=device.get("deviceType"))
                    self._devices[device["deviceName"]] = {
                        "config": device,
                        "token": token,
                        "converter": converter,
                        "downlink_converter": downlink_converter,
                        "next_attributes_check": 0,
                        "next_timeseries_check": 0,
                        "telemetry": {},
                        "attributes": {},
                        "last_telemetry": {},
                        "last_attributes": {}
                    }

                break
Exemple #4
0
    def updateDevices(self, config, connector):
        token = config['accessToken']
        self._tokens.add(token)
        if token is not None:
            # 1. 先删除之前添加的设备,避免存在脏设备。
            pre_gateway_devices = filter(
                lambda x: self._gateway[x]["connector"].get_name() == connector
                .get_name(), self._gateway.get_devices())
            if pre_gateway_devices is not None:
                for pre_device in pre_gateway_devices:
                    self._gateway.del_device(pre_device.get('deviceName'))

            devices = {
                k: v
                for k, v in self._devices.items() if v['token'] == token
            }
            for device in list(devices.keys()):
                del self._devices[device]

            # 2. 新增设备,重置所有的信息
            for device in config['devices']:
                if config.get("converter") is not None:
                    converter = TBUtility.check_and_import(
                        'tcp', self._config["converter"])(device)
                else:
                    converter = BytesModbusUplinkConverter(device)
                if config.get("downlink_converter") is not None:
                    downlink_converter = TBUtility.check_and_import(
                        'tcp', self._config["downlink_converter"])(device)
                else:
                    downlink_converter = BytesModbusDownlinkConverter(device)

                if device.get('deviceName') not in self._gateway.get_devices():
                    self._gateway.add_device(
                        device.get('deviceName'), {"connector": connector},
                        device_type=device.get("deviceType"))
                if device.get('deviceName') not in self._devices:
                    self._devices[device["deviceName"]] = {
                        "config": device,
                        "token": token,
                        "converter": converter,
                        "downlink_converter": downlink_converter,
                        "next_attributes_check": 0,
                        "next_timeseries_check": 0,
                        "telemetry": {},
                        "attributes": {},
                        "last_telemetry": {},
                        "last_attributes": {}
                    }

                break
Exemple #5
0
    def __load_converters(self, connector, gateway):
        try:
            if self.config.get(UPLINK_PREFIX + CONVERTER_PARAMETER) is not None:
                converter = TBModuleLoader.import_module(connector.connector_type,
                                                         self.config[UPLINK_PREFIX + CONVERTER_PARAMETER])(self)
            else:
                converter = BytesModbusUplinkConverter({**self.config, 'deviceName': self.name})

            if self.config.get(DOWNLINK_PREFIX + CONVERTER_PARAMETER) is not None:
                downlink_converter = TBModuleLoader.import_module(connector.connector_type, self.config[
                    DOWNLINK_PREFIX + CONVERTER_PARAMETER])(self)
            else:
                downlink_converter = BytesModbusDownlinkConverter(self.config)

            if self.name not in gateway.get_devices():
                gateway.add_device(self.name, {CONNECTOR_PARAMETER: connector},
                                   device_type=self.config.get(DEVICE_TYPE_PARAMETER))

            self.config[UPLINK_PREFIX + CONVERTER_PARAMETER] = converter
            self.config[DOWNLINK_PREFIX + CONVERTER_PARAMETER] = downlink_converter
        except Exception as e:
            log.exception(e)
 def __load_converters(self):
     try:
         for device in self.__config[CONFIG_DEVICES_SECTION_PARAMETER]:
             if self.__config.get(UPLINK_PREFIX +
                                  CONVERTER_PARAMETER) is not None:
                 converter = TBModuleLoader.import_module(
                     self._connector_type,
                     self.__config[UPLINK_PREFIX +
                                   CONVERTER_PARAMETER])(device)
             else:
                 converter = BytesModbusUplinkConverter(device)
             if self.__config.get(DOWNLINK_PREFIX +
                                  CONVERTER_PARAMETER) is not None:
                 downlink_converter = TBModuleLoader.import_module(
                     self._connector_type,
                     self.__config[DOWNLINK_PREFIX +
                                   CONVERTER_PARAMETER])(device)
             else:
                 downlink_converter = BytesModbusDownlinkConverter(device)
             if device.get(DEVICE_NAME_PARAMETER
                           ) not in self.__gateway.get_devices():
                 self.__gateway.add_device(
                     device.get(DEVICE_NAME_PARAMETER),
                     {CONNECTOR_PARAMETER: self},
                     device_type=device.get(DEVICE_TYPE_PARAMETER))
             self.__devices[device[DEVICE_NAME_PARAMETER]] = {
                 CONFIG_SECTION_PARAMETER: device,
                 UPLINK_PREFIX + CONVERTER_PARAMETER: converter,
                 DOWNLINK_PREFIX + CONVERTER_PARAMETER: downlink_converter,
                 NEXT_PREFIX + ATTRIBUTES_PARAMETER + CHECK_POSTFIX: 0,
                 NEXT_PREFIX + TIMESERIES_PARAMETER + CHECK_POSTFIX: 0,
                 TELEMETRY_PARAMETER: {},
                 ATTRIBUTES_PARAMETER: {},
                 LAST_PREFIX + TELEMETRY_PARAMETER: {},
                 LAST_PREFIX + ATTRIBUTES_PARAMETER: {},
                 CONNECTION_ATTEMPT_PARAMETER: 0
             }
     except Exception as e:
         log.exception(e)