def dict_from_payload()

in transform_binary_payload/src-payload-decoders/python/dragino_lbt1.py [0:0]


def dict_from_payload(base64_input: str, fport: int = None):
    """ Decodes a base64-encoded binary payload into JSON.
            Parameters 
            ----------
            base64_input : str
                Base64-encoded binary payload
            fport: int
                FPort as provided in the metadata. Please note the fport is optional and can have value "None", if not provided by the LNS or invoking function. 

                If  fport is None and binary decoder can not proceed because of that, it should should raise an exception.

            Returns
            -------
            JSON object with key/value pairs of decoded attributes

        """
    decoded = base64.b64decode(base64_input)
    logger.debug(f"Input hex is {decoded.hex()}")

    battery_value = (decoded[0] << 8 | decoded[1]) / 1000
    step_count = ((decoded[2] & 0x0F) << 16) | (decoded[3] << 8) | (decoded[4])
    mode = decoded[5]

    uuid = 0
    if mode == 3:
        uuid = decoded[6:18]
        major = decoded[18:22]
        minor = decoded[22:26]
        power = decoded[26:28]
        rssi = decoded[28:32]

        result = {
            "battery_value": battery_value,
            "step_count": step_count,
            "mode": mode,
            "uuid": uuid.decode(),
            "major": int(to_ascii(major.hex()), 16),
            "minor": int(to_ascii(minor.hex()), 16),
            "power": int(to_ascii(power.hex()), 16)-256,
            "rssi": int(to_ascii(rssi.hex()), 16)
        }
    elif mode == 2:
        uuid = decoded[6:6+32]
        addr = decoded[38:39+12]
        result = {
            "battery_value": battery_value,
            "step_count": step_count,
            "mode": mode,
            "uuid": uuid.decode(),
            "addr": addr.decode()
        }
    elif mode == 1:
        uuid = decoded[6:11]
        result = {
            "battery_value": battery_value,
            "step_count": step_count,
            "mode": mode,
            "uuid": uuid.decode(),
        }
    else:
        result = {
            "battery_value": battery_value,
            "step_count": step_count,
            "mode": mode,
        }

    return result