コード例 #1
0
 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)
コード例 #2
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
コード例 #3
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
コード例 #4
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)
コード例 #5
0
 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)
コード例 #6
0
    def test_modbus_getting_values(self):
        test_modbus_config = {
            "attributes": [
                {
                    "string": {
                        "byteOrder": "BIG",
                        "tag": "string",
                        "type": "string",
                        "functionCode": 4,
                        "registerCount": 4
                    }
                },
                {
                    "bits": {
                        "byteOrder": "BIG",
                        "tag": "bits",
                        "type": "bits",
                        "functionCode": 4,
                        "registerCount": 1
                    }
                },
                {
                    "8int": {
                        "byteOrder": "BIG",
                        "tag": "8int",
                        "type": "8int",
                        "functionCode": 4,
                        "registerCount": 1
                    }
                },
                {
                    "16int": {
                        "byteOrder": "BIG",
                        "tag": "16int",
                        "type": "16int",
                        "functionCode": 4,
                        "registerCount": 1
                    }
                },
                {
                    "long": {
                        "byteOrder": "BIG",
                        "tag": "long",
                        "type": "long",
                        "functionCode": 4,
                        "registerCount": 1
                    }
                },
                {
                    "long_with_divider": {
                        "byteOrder": "BIG",
                        "tag": "long",
                        "type": "long",
                        "functionCode": 4,
                        "registerCount": 1,
                        "divider": 10
                    }
                },
                {
                    "32int": {
                        "byteOrder": "BIG",
                        "tag": "32int",
                        "type": "32int",
                        "functionCode": 4,
                        "registerCount": 2
                    }
                },
                {
                    "64int": {
                        "byteOrder": "BIG",
                        "tag": "64int",
                        "type": "64int",
                        "functionCode": 4,
                        "registerCount": 4
                    }
                },
            ],
            "timeseries": [
                {
                    "8uint": {
                        "byteOrder": "BIG",
                        "tag": "8uint",
                        "type": "8uint",
                        "functionCode": 4,
                        "registerCount": 1
                    }
                },
                {
                    "16uint": {
                        "byteOrder": "BIG",
                        "tag": "16uint",
                        "type": "16uint",
                        "functionCode": 4,
                        "registerCount": 2
                    }
                },
                {
                    "32uint": {
                        "byteOrder": "BIG",
                        "tag": "32uint",
                        "type": "32uint",
                        "functionCode": 4,
                        "registerCount": 4
                    }
                },
                {
                    "64uint": {
                        "byteOrder": "BIG",
                        "tag": "64uint",
                        "type": "64uint",
                        "functionCode": 4,
                        "registerCount": 1
                    }
                },
                {
                    "double": {
                        "byteOrder": "BIG",
                        "tag": "double",
                        "type": "double",
                        "functionCode": 4,
                        "registerCount": 2
                    }
                },
                {
                    "16float": {
                        "byteOrder": "BIG",
                        "tag": "16float",
                        "type": "16float",
                        "functionCode": 4,
                        "registerCount": 1
                    }
                },
                {
                    "32float": {
                        "byteOrder": "BIG",
                        "tag": "32float",
                        "type": "32float",
                        "functionCode": 4,
                        "registerCount": 2
                    }
                },
                {
                    "64float": {
                        "byteOrder": "BIG",
                        "tag": "64float",
                        "type": "64float",
                        "functionCode": 4,
                        "registerCount": 4
                    }
                },
            ]
        }
        test_modbus_body_to_convert = {}
        test_modbus_convert_config = {}
        test_modbus_result = {
            'deviceName':
            'Modbus Test',
            'deviceType':
            'default',
            'telemetry': [{
                '8uint': 18
            }, {
                '16uint': 4660
            }, {
                '32uint': 305419896
            }, {
                '64uint': 1311768468603649775
            }, {
                'double': 22.5
            }, {
                '16float': 1.240234375
            }, {
                '32float': 22.34000015258789
            }, {
                '64float': -123.45
            }],
            'attributes': [{
                'string': 'abcdefgh'
            }, {
                'bits': [False, True, False, True, True, False, True, False]
            }, {
                '8int': -18
            }, {
                '16int': -22136
            }, {
                'long': -22136
            }, {
                'long_with_divider': -2213.6
            }, {
                '32int': -4660
            }, {
                '64int': -3735928559
            }]
        }

        builder = BinaryPayloadBuilder(byteorder=Endian.Big)
        builder_registers = {
            "string": (builder.add_string, 'abcdefgh'),
            "bits": (builder.add_bits, [0, 1, 0, 1, 1, 0, 1, 0]),
            "8int": (builder.add_8bit_int, -0x12),
            "16int": (builder.add_16bit_int, -0x5678),
            "long": (builder.add_16bit_int, -0x5678),
            "long_with_divider": (builder.add_16bit_int, -0x5678),
            "32int": (builder.add_32bit_int, -0x1234),
            "64int": (builder.add_64bit_int, -0xDEADBEEF),
            "8uint": (builder.add_8bit_uint, 0x12),
            "16uint": (builder.add_16bit_uint, 0x1234),
            "32uint": (builder.add_32bit_uint, 0x12345678),
            "64uint": (builder.add_64bit_uint, 0x12345678DEADBEEF),
            "double": (builder.add_32bit_float, 22.5),
            "16float": (builder.add_16bit_float, 1.24),
            "32float": (builder.add_32bit_float, 22.34),
            "64float": (builder.add_64bit_float, -123.45),
        }

        class DummyResponse:
            def __init__(self, registers):
                self.registers = registers[:]

        for datatype in test_modbus_config:
            test_modbus_body_to_convert[datatype] = {}
            for tag_dict in test_modbus_config[datatype]:
                for tag in tag_dict:
                    builder_registers[tag][0](builder_registers[tag][1])
                    test_modbus_body_to_convert[datatype].update({
                        tag: {
                            "input_data":
                            DummyResponse(builder.to_registers()),
                            "data_sent": tag_dict[tag]
                        }
                    })
                    builder.reset()

        converter = BytesModbusUplinkConverter({
            "deviceName": "Modbus Test",
            "deviceType": "default",
            "unitId": 1
        })
        result = converter.convert(test_modbus_convert_config,
                                   test_modbus_body_to_convert)
        self.assertDictEqual(result, test_modbus_result)