当前位置: 首页 > news >正文

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

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

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

api文档:QueryDevicePropertyData_物联网平台_API文档-阿里云OpenAPI开发者门户

搞了好久才搞出来:

  • 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}

相关文章:

  • 基于反激电路的电池充放电均衡控制
  • 解决linux mobaxterm unable to open connection to comx 串口正常连接,但终端无法输入的问题
  • 机试准备第4天
  • Pytorch使用手册—使用TACOTRON2进行文本到语音转换(专题二十四)
  • Android Studio 新版本Gradle通过JitPack发布Maven仓库示例
  • unity lua属性绑定刷新
  • 【愚公系列】《鸿蒙原生应用开发从零基础到多实战》004-TypeScript 中的泛型
  • 【计算机网络】传输层协议(UDP TCP)
  • 如何用python画一棵分形树
  • java后端开发day23--面向对象进阶(四)--抽象类、接口、内部类
  • Chromium_src源码
  • 【uniapp-Vue3】beforeRegister在注册用户入库前设置初始用户
  • 第四章 数据库安全性
  • 如何配置redis.conf才能远程访问
  • GitHub 语析 - 基于大模型的知识库与知识图谱问答平台
  • AI辅助学习vue第十三章
  • 【SpringBoot】SpringBoot中分页插件(PageHelper)的使用
  • ESP32 S3开发笔记(环境搭建,成功烧录)
  • LDR6500 PD 协议芯片在不同设备中的应用场景
  • Tomcat异常日志中文乱码怎么解决
  • 金华英文网站建设/网站搜索量查询
  • appcms程序怎么做网站/网络推广的基本方法有哪些
  • 为古汉字老人做网站/免费站长统计工具
  • 织梦可以做导航网站/软件培训机构排名
  • 广州做网络服装的网站建设/线下推广渠道和方式
  • 网站备案 费用/百度官网网页版