阿里云物联网获取设备属性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}

相关推荐
博睿谷IT99_11 分钟前
华为物联网认证:开启万物互联的钥匙
物联网·华为·华为认证·职业规划
糖果罐子♡29 分钟前
MQTT 入门指南(一)
物联网·mqtt·嵌入式·通信协议
小赖同学啊2 小时前
基于区块链的物联网(IoT)安全通信与数据共享的典型实例
物联网·安全·区块链
云资源服务商7 小时前
解锁阿里云日志服务SLS:云时代的日志管理利器
服务器·阿里云·云计算
S,D16 小时前
MCU引脚的漏电流、灌电流、拉电流区别是什么
驱动开发·stm32·单片机·嵌入式硬件·mcu·物联网·硬件工程
从零开始学习人工智能17 小时前
深入解析 OPC UA:工业自动化与物联网的关键技术
运维·物联网·自动化
安 当 加 密1 天前
多云密钥统一管理实战:CKMS对接阿里云/华为云密钥服务
数据库·阿里云·华为云
linweidong1 天前
物联网MQTT协议与实践:从零到精通的硬核指南
物联网·mqtt·websocket·嵌入式·iot·tdengine·工业物联网
门思科技1 天前
设计可靠 LoRaWAN 设备时需要考虑的关键能力
运维·服务器·网络·嵌入式硬件·物联网
MonkeyKing_sunyuhua1 天前
微信小程序能不能获取物联网的上的设备数据
物联网·微信小程序·小程序