示例#1
0
def decode(msg, hex=False):
    """msg: payload as one of hex string, list, or bytearray"""
    bytes_ = bytearray(binascii.a2b_hex(msg) if hex else msg)
    version = bytes_[0]
    if version != PROTOCOL_VERSION:
        raise ValueError(
            "protocol version {} doesn't match v2".format(version))

    devid = struct.unpack('>H', bytes_[1:3])[0]
    bin_flags = bin(struct.unpack('>H', bytes_[3:5])[0])
    flags = bin_flags[2:].zfill(struct.calcsize('>H') * 8)[::-1]

    words = [
        struct.unpack('>H', bytes_[i:i + 2])[0]
        for i in range(5, len(bytes_), 2)
    ]

    cur = 0
    result = {'Device ID': devid, 'Protocol version': version}
    for flag, sensor in zip(flags, SENSORS):
        if flag != '1':
            continue

        x = words[cur:cur + sensor["length"]]
        cur += sensor["length"]
        for value in sensor['values']:
            if 'convert' not in value:
                continue

            result[value['name']] = {
                'value': value['convert'](x),
                'unit': value.get('unit', None)
            }

    return result
示例#2
0
def decode(msg, EUI, hex=False):
    """msg: payload as one of hex string, list, or bytearray"""

    bytes_ = bytearray(binascii.a2b_hex(msg) if hex else msg)
    version = bytes_[0]

    if version != PROTOCOL_VERSION:
        raise ValueError(
            "protocol version {} doesn't match v2".format(version))

    devid = struct.unpack(">H", bytes_[1:3])[0]
    bin_flags = bin(struct.unpack(">H", bytes_[3:5])[0])
    flags = bin_flags[2:].zfill(struct.calcsize(">H") * 8)[::-1]

    words = [
        struct.unpack(">H", bytes_[i:i + 2])[0]
        for i in range(5, len(bytes_), 2)
    ]

    cur = 0
    result = {"Device ID": devid, "Protocol version": version}

    SENSORS = get_SENSOR(EUI)

    for flag, sensor in zip(flags, SENSORS):
        if flag != "1":
            continue

        x = words[cur:cur + sensor["length"]]
        cur += sensor["length"]
        for value in sensor["values"]:
            if "convert" not in value:
                continue

            result[value["name"]] = {
                "value": value["convert"](x),
                "unit": value.get("unit", None)
            }

    return (result)
def decode(msg, hex=False):
    """
    msg: payload as one of hex string, list, or bytearray

    return:

    {
     "Device ID": 4838,
     "Protocol version": 2,
     "Dielectric permittivity": {
      "value": 1.0392113047231324,
      "unit": null
     },
     "Volumetric water content": {
      "value": 0.002387260000000002,
      "unit": "m\u00b3\u22c5m\u207b\u00b3"
     },
     "Soil temperature": {
      "value": 20.6,
      "unit": "\u00b0C"
     },
     "Electrical conductivity": {
      "value": 0,
      "unit": "\u00b5S\u22c5cm\u207b\u00b9"
     },
     "Battery voltage": {
      "value": 3.037,
      "unit": "V"
     }
    }
    """
    bytes_ = bytearray(binascii.a2b_hex(msg) if hex else msg)
    version = bytes_[0]
    if version != PROTOCOL_VERSION:
        raise ValueError(
            "protocol version {} doesn't match v2".format(version))

    devid = struct.unpack('>H', bytes_[1:3])[0]
    bin_flags = bin(struct.unpack('>H', bytes_[3:5])[0])
    flags = bin_flags[2:].zfill(struct.calcsize('>H') * 8)[::-1]

    words = [
        struct.unpack('>H', bytes_[i:i + 2])[0]
        for i in range(5, len(bytes_), 2)
    ]

    cur = 0
    result = {'Device ID': devid, 'Protocol version': version}
    for flag, sensor in zip(flags, SENSORS):
        if flag != '1':
            continue

        x = words[cur:cur + sensor["length"]]
        cur += sensor["length"]
        for value in sensor['values']:
            if 'convert' not in value:
                continue

            result[value['name']] = {
                'value': value['convert'](x),
                'unit': value.get('unit', None)
            }

    return result