阿里云物联网获取设备属性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
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文件:
java 复制代码
<?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格式,想要哪些字段自行解析

python 复制代码
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}

相关推荐
国际云,接待18 小时前
腾讯云国际版服务器从注册到使用的完整流程指南
运维·服务器·阿里云·架构·云计算·腾讯云·csdn开发云
薛慕昭18 小时前
《从硬件到云端:STC8H ADC数据采集与华为物联网平台对接全解析》
服务器·物联网
zskj_zhyl18 小时前
银发科技:AI健康小屋如何破解老龄化困局
人工智能·科技·物联网
Blossom.11819 小时前
量子计算在密码学中的应用与挑战:重塑信息安全的未来
人工智能·深度学习·物联网·算法·密码学·量子计算·量子安全
龙大大L1 天前
第五章:5.3 ESP32物联网应用:阿里云IoT平台与腾讯云IoT平台的数据上传与远程控制
java·物联网·struts·esp32
移远通信1 天前
QuecPython+GNSS:实现快速定位
物联网·gnss·quecpython
国际云,接待2 天前
[特殊字符]服务器性能优化:从硬件到AI的全栈调优指南
运维·服务器·人工智能·阿里云·性能优化·架构·云计算
小叮当⇔2 天前
IOT项目——物联网 GPS
物联网
程序边界2 天前
DeepSeek在物联网设备中的应用:通过轻量化模型实现本地化数据分析
物联网·struts·数据分析
网易独家音乐人Mike Zhou2 天前
【Linux应用】交叉编译环境配置,以及最简单粗暴的环境移植(直接从目标板上复制)
linux·stm32·mcu·物联网·嵌入式·iot