阿里云物联网平台数据解析(python)

阿里云物联网平台数据解析(python)

DTU上传设备返回报文消息,通过数据解析后显示各功能数值,这里以智能电表DLT645规约为例进行解析
因为是做光伏的,所以对电表的需求比较多,之前查找了好多文章,下面的代码算是可以使用了,后面再把所有的都写出来,写的不好,供大家参考吧
这个代码参考的这
[https://help.aliyun.com/document_detail/147611.html?spm=a2c4g.11186623.2.20.f1d12d75dwRcFQ#concept-2371165]

# coding=UTF-8
import struct
import common_util
import math


ALINK_PROP_REPORT_METHOD = 'thing.event.property.post'  # 物联网平台Topic,设备上传属性数据到云端。

def raw_data_to_protocol(bytes):
    
    uint8Array = []
    for byteValue in bytes:
        uint8Array.append(byteValue & 0xff)
#首先对收到的消息进行定位,这里定位报文帧头68    
    wz=dingwei(uint8Array)   
#算是通用
    jsonMap = {}
    jsonMap['method'] = ALINK_PROP_REPORT_METHOD
    jsonMap['version'] = '1.0'
    jsonMap['id'] = str(bytes_to_int(uint8Array[1:5]))
    params = {}
#这里首先判断收到的报文功能码是不是电压的,其它的一样
#如果是电压的则进行解析,这个算法比较粗糙但是能用,把每个字节高位16进和地位16进分别结算出来最后相加,有啥好的方法希望共享一下
    if (uint8Array[wz+10]==0x33)&(uint8Array[int(wz)+11]==0x34)&(uint8Array[int(wz)+12]==0x34)&(uint8Array[int(wz)+13]==0x35):
        params['Pav'] = ((uint8Array[wz+14]&0xF0)-48)/16+((uint8Array[wz+14]&0x0F)-3)*0.1+((uint8Array[wz+15]&0xF0)-48)/16*100+((uint8Array[wz+15]&0x0F)-3)*10
    jsonMap['params'] = params
    return jsonMap



def protocol_to_raw_data(json):
    method = json.get('method', None)
    id = json.get('id', None)
    version = json.get('version', None)
    payload_array = []

    return payload_array

# 不下发指令所以一下部分功能省略
def transform_payload(topic, bytes):
    uint8Array = []
    for byteValue in bytes:
        uint8Array.append(byteValue & 0xff)

    jsonMap = {}
    if SELF_DEFINE_TOPIC_ERROR_FLAG in topic:
        jsonMap['topic'] = topic
        jsonMap['errorCode'] = bytes_to_int(uint8Array[0:1])

    elif SELF_DEFINE_TOPIC_UPDATE_FLAG in topic:
        jsonMap['topic'] = topic
        jsonMap['prop_int16'] = bytes_to_int(uint8Array[5:7])
        jsonMap['prop_bool'] = bytes_to_int(uint8Array[7: 8])
        jsonMap['prop_float'] = bytes_to_int(uint8Array[8:])

    return jsonMap
    
#定位首个数据帧头68H位置
def dingwei(uint8Array):
    i=0
    for i in range(len(uint8Array)):
        if uint8Array[i]==104:
            break
        i=i+1
    return i
    

# byte转成int。
def bytes_to_int(bytes):
    data = ['%02X' % i for i in bytes]
    return int(''.join(data), 16)


# byte转成float,不带精度。
def bytes_to_float(bytes):
    data = []
    for i in bytes:
        t_value = '%02X' % i
        if t_value % 2 != 0:
            t_value += 0
        data.append(t_value)
    hex_str = ''.join(data)
    return struct.unpack('!f', hex_str.decode('hex'))[0]


# 8位整形转成byte数组。
def int_8_to_byte(value):
    t_value = '%02X' % value
    if len(t_value) % 2 != 0:
        t_value += '0'

    return hex_string_to_byte_array(t_value)


# 32位整形转成byte数组。
def int_32_to_byte(value):
    t_value = '%08X' % value
    if len(t_value) % 2 != 0:
        t_value += '0'

    return hex_string_to_byte_array(t_value)


# 16位整形转成byte数组。
def int_16_to_byte(value):
    t_value = '%04X' % value
    if len(t_value) % 2 != 0:
        t_value += '0'

    return hex_string_to_byte_array(t_value)


# float转成整形数组。
def float_to_byte(param):
    return hex_string_to_byte_array(struct.pack(">f", param).encode('hex'))


# 16进制字符串转成byte数组。
def hex_string_to_byte_array(str_value):
    if len(str_value) % 2 != 0:
        return None

    cycle = len(str_value) / 2

    pos = 0
    result = []
    for i in range(0, cycle, 1):
        temp_str_value = str_value[pos:pos + 2]
        temp_int_value = int(temp_str_value, base=16)

        result.append(temp_int_value)
        pos += 2
    return result

**

下面是解析之后的数据

阿里云物联网平台数据解析(python)_第1张图片
阿里云物联网平台数据解析(python)_第2张图片
阿里云物联网平台数据解析(python)_第3张图片

你可能感兴趣的:(阿里云物联网平台数据解析(python))