阿里云物联网获取设备属性api接口:QueryDevicePropertyData

阿里云物联网接口:QueryDevicePropertyData
 说明:调用该接口查询指定设备或数字孪生节点,在指定时间段内,单个属性的数据

比如提取上传到物联网的温度数据

api文档:QueryDevicePropertyData_物联网平台_API文档-阿里云OpenAPI开发者门户QueryDevicePropertyData调用该接口查询指定设备或数字孪生节点,在指定时间段内,单个属性的数据。是物联网平台的API文档,是阿里云OpenAPI开发者门户提供的OpenAPI开发手册,包括云产品的OpenAPI、出入参、错误码、变更历史。https://next.api.aliyun.com/document/Iot/2018-01-20/QueryDevicePropertyData

阿里云物联网平台云端SDK下载:

物联网平台SDK下载地址和示例_物联网平台(IoT)-阿里云帮助中心物联网平台云端SDK支持Java、Python、PHP、Node.js、.NET、Go和Swift语言,用于调用云端API,以实现物联网平台的云端能力,如产品管理、设备管理、Topic管理、数据流转规则管理、消息通信等。本文介绍云端SDK的下载地址及使用示例。https://help.aliyun.com/zh/iot/developer-reference/download-iot-platform-sdks-2

搞了好久才搞出来:

  • 1、java实现:

  • QueryDeviceTempData.java

  • package com.example.iot;
    
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.iot.model.v20180120.QueryDevicePropertyDataRequest;
    import com.aliyuncs.iot.model.v20180120.QueryDevicePropertyDataResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    public class QueryDeviceTempData {
    
        public static void main(String[] args) {
            // 阿里云访问密钥
            String accessKeyId = "**";
            String accessKeySecret = "**";
            String regionId = "cn-shanghai";
    
            // 设备信息
            String productKey = "**";
            String deviceName = "**";
    
            // 初始化客户端
            IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);
    
            // 创建请求
            QueryDevicePropertyDataRequest request = new QueryDevicePropertyDataRequest();
            request.setProductKey(productKey);
            request.setDeviceName(deviceName);
            request.setIdentifier("indate"); // 温度属性标识符
            request.setStartTime(System.currentTimeMillis() - 3600 * 1000); // 查询过去一小时的数据
            request.setEndTime(System.currentTimeMillis());
            request.setAsc(1); // 按时间升序排列
            request.setPageSize(10); // 每页返回的数据条数
    
            try {
                // 发送请求并获取响应
                QueryDevicePropertyDataResponse response = client.getAcsResponse(request);
                System.out.println("Response: " + response);
    
                // 解析响应
                if (response != null && response.getData() != null) {
                    for (QueryDevicePropertyDataResponse.Data.PropertyInfo propertyInfo : response.getData().getList()) {
                        System.out.println("Time: " + propertyInfo.getTime() + ", Value: " + propertyInfo.getValue());
                    }
                }
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

  • pom.xml文件:
  • <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>QueryDevicePropertyData</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>21</maven.compiler.source>
            <maven.compiler.target>21</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <dependencies>
            <!-- 阿里云核心库 -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.5.3</version>
            </dependency>
            <!-- 阿里云物联网平台SDK -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-iot</artifactId>
                <version>7.4.0</version>
            </dependency>
        </dependencies>
    
    </project>
  • java项目结构
  • java输出:
  • Response: com.aliyuncs.iot.model.v20180120.QueryDevicePropertyDataResponse@38d8f54a
    Time: 1740729954244, Value: 18.0
    Time: 1740730254422, Value: 18.0
    Time: 1740730554592, Value: 18.0
    Time: 1740730854785, Value: 18.0
    Time: 1740731154962, Value: 17.0
     

    2、python实现

    输出json格式,想要哪些字段自行解析

    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException
    from aliyunsdkcore.acs_exception.exceptions import ServerException
    from aliyunsdkiot.request.v20180120.QueryDevicePropertyDataRequest import QueryDevicePropertyDataRequest
    
    # 设置你的AccessKey ID和AccessKey Secret
    access_key_id = '**'
    access_key_secret = '**'
    region_id = 'cn-shanghai'  # 根据你的实际情况选择region
    
    # 创建AcsClient实例
    client = AcsClient(access_key_id, access_key_secret, region_id)
    
    # 创建QueryDevicePropertyDataRequest实例
    request = QueryDevicePropertyDataRequest()
    request.set_accept_format('json')
    
    # 设置请求参数
    request.set_ProductKey('**')  # 产品Key
    request.set_DeviceName('**')  # 设备名称
    request.set_Identifier('temp')  # 属性标识符
    request.set_StartTime(1740346648574)  # 开始时间,Unix时间戳,单位毫秒
    request.set_EndTime(1740357451888)  # 结束时间,Unix时间戳,单位毫秒
    request.set_PageSize(10)  # 每页记录数
    request.set_Asc(0)  # 0表示降序,1表示升序
    
    try:
        # 发送请求并获取响应
        response = client.do_action_with_exception(request)
        print(response.decode('utf-8'))
    except ClientException as e:
        print(e.get_error_code())
        print(e.get_error_msg())
    except ServerException as e:
        print(e.get_error_code())
        print(e.get_error_msg())

    python程序输出:{"RequestId":"036BBE41-52E9-5C5F-9312-D561C0D82282","Data":{"NextValid":false,"NextTime":1740355512752,"List":{"PropertyInfo":[{"Value":"-2.0","Time":1740357315548},{"Value":"-2.0","Time":1740357015021},{"Value":"-2.0","Time":1740356714858},{"Value":"-5.0","Time":1740356414687},{"Value":"-5.0","Time":1740356113089},{"Value":"-5.0","Time":1740355812915},{"Value":"-5.0","Time":1740355512754}]}},"Code":"","Success":true}

    作者:mangge08

    物联沃分享整理
    物联沃-IOTWORD物联网 » 阿里云物联网获取设备属性api接口:QueryDevicePropertyData

    发表回复