阿里云物联网管理SDK使用示例

简介: 目前阿里云的绝大部分服务都提供了管理资源的Rest API,针对相应的API也做了SDK的封装,且版本也在不断的迭代更新。管理API SDK的统一GitHub地址链接。关于IoT SDK的使用,之前官方也给出了示例参考,但是该示例使用的SDK版本为5.0.0,且这个版本的SDK中没有DeleteProductRequest类的实现,下面演示使用目前最新版本:6.5.0关于产品的创建及删除操作,其它操作类似。

概述

目前阿里云的绝大部分服务都提供了管理资源的Rest API,针对相应的API也做了SDK的封装,且版本也在不断的迭代更新。管理API SDK的统一GitHub地址链接。关于IoT SDK的使用,之前官方也给出了示例参考,但是该示例使用的SDK版本为5.0.0,且这个版本的SDK中没有DeleteProductRequest类的实现,下面演示使用目前最新版本:6.5.0关于产品的创建及删除操作,新建Topic间的消息路由关系、查询向指定Topic订阅消息的目标Topic操作,其它操作类似。

pom依赖

        <!--核心依赖-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.5.0</version>
        </dependency>

        <!--管理API SDK调用问题-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-iot</artifactId>
            <version>6.5.0</version>
        </dependency>

Java示例代码

import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.RpcAcsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.CreateProductRequest;
import com.aliyuncs.iot.model.v20180120.CreateProductResponse;
import com.aliyuncs.iot.model.v20180120.DeleteProductRequest;
import com.aliyuncs.iot.model.v20180120.DeleteProductResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class IoTDemo {

    public static void main(String[] args) {

        String productName = "iottestdemo123456";
        String productKey = "******";
        Integer nodeType = 0;
//      createProductTest(productName,nodeType);
        deleteProductTest(productKey);
        System.out.println("删除成功");
    }


    /**
     * 创建产品
     * @param productName 产品名称
     * @param nodeType 产品描述
     * @return 产品的PK
     */
    public static String createProductTest(String productName, Integer nodeType) {
        CreateProductRequest request = new CreateProductRequest();
        request.setProductName(productName);

        request.setNodeType(nodeType);//节点的类型,0标识设备,1标识网关
        CreateProductResponse response = (CreateProductResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("创建产品成功!productKey:" + response.getProductKey());
            return response.getProductKey();
        } else {
            System.out.println("创建产品失败!requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return null;
    }

    /**
     * 删除产品
     * @param productKey 产品key
     * @return 产品的PK,可以在管理门户获取
     */
    public static Boolean deleteProductTest(String productKey) {
        DeleteProductRequest request = new DeleteProductRequest();
        request.setProductKey(productKey);
        DeleteProductResponse response = (DeleteProductResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("删除产品成功!productKey:" + response.getSuccess());
            return response.getSuccess();
        } else {
            System.out.println("创建产品失败!requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return false;
    }
    
    public static AcsResponse executeTest1(RpcAcsRequest request) {

        DefaultAcsClient client = null;
        String regionId = "cn-shanghai";
        String accessKeyID = "********";
        String accessKeySecret = "********";
        String productCode = "Iot";
        String domain = "iot.cn-shanghai.aliyuncs.com";
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(regionId, regionId, productCode, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        // 初始化client
        client = new DefaultAcsClient(profile);

        System.out.println("client 初始化完成!");

        AcsResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (Exception e) {
            System.out.println("执行失败:e:" + e.getMessage());
        }
        return response;
    }

    /**
     * 新建Topic间的消息路由关系
     */
    public static void CreateTopicRouteTable() {
        CreateTopicRouteTableRequest request = new CreateTopicRouteTableRequest();
        request.setSrcTopic("/a1D8NwXCe56/device1/user/TopicToTopicDemo"); // 源Topic,即被订阅的Topic。
        List<String> dstTopics = new ArrayList<String>();
        dstTopics.add("/a1D8NwXCe56/ceshi/user/TopicToTopicDemo"); // 目标Topic列表,即从SrcTopic订阅消息的Topic列表。
        request.setDstTopics(dstTopics);
        CreateTopicRouteTableResponse response = (CreateTopicRouteTableResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("CreateTopicRouteTable 成功:" + response.getSuccess());
        } else {
            System.out.println("CreateTopicRouteTable, requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
    }

    /**
     *  查询向指定Topic订阅消息的目标Topic
     */
    public static void QueryTopicRouteTable() {
        QueryTopicRouteTableRequest request = new QueryTopicRouteTableRequest();
        request.setActionName("QueryTopicRouteTable");
        request.setTopic("/a1D8NwXCe56/device1/user/TopicToTopicDemo"); // 要查询的源Topic,即发布消息的Topic。
        QueryTopicRouteTableResponse response = (QueryTopicRouteTableResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("QueryTopicRouteTable 成功:" + response.getSuccess());
            System.out.println(response.getDstTopics());
        } else {
            System.out.println("CreateTopicRouteTable, requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
    }
}

注意

当前阶段国内只有上海地区支持IoT服务,用户只需要根据需求替换AK及IoT的信息即可。删除产品的前提是产品中没有设备,管理门户目前删除产品需要短信验证码,直接使用SDK调用不需要验证码。

参考链接

IoT SDK Demo

API列表

相关实践学习
阿里云AIoT物联网开发实战
本课程将由物联网专家带你熟悉阿里云AIoT物联网领域全套云产品,7天轻松搭建基于Arduino的端到端物联网场景应用。 开始学习前,请先开通下方两个云产品,让学习更流畅: IoT物联网平台:https://iot.console.aliyun.com/ LinkWAN物联网络管理平台:https://linkwan.console.aliyun.com/service-open
相关文章
|
机器学习/深度学习 算法 数据可视化
复杂网络-常用绘图软件和库
复杂网络-常用绘图软件和库
1192 0
复杂网络-常用绘图软件和库
|
人工智能 自然语言处理 供应链
为什么一定要做Agent智能体?
作者通过深入分析、理解、归纳,最后解答了“为什么一定要做Agent”这个问题。
2610 41
为什么一定要做Agent智能体?
|
12月前
|
Prometheus 监控 Cloud Native
打造你的专属Linux监控仪表板(小白也能轻松上手的系统可视化教程)
本教程教你使用Prometheus + Grafana从零搭建Linux监控仪表板,实时可视化CPU、内存、磁盘等系统性能指标。无需经验,手把手教学,助你快速掌握服务器状态监控,提升运维效率,适合所有Linux用户。
打造你的专属Linux监控仪表板(小白也能轻松上手的系统可视化教程)
|
5月前
|
数据采集 人工智能 运维
打破 Coding Agent 黑盒困境:阿里云LoongSuite AI Agent 观测审计数据采集实践教程
随着AI Agent在研发、办公、企业业务领域规模化落地,Claude Code、OpenClaw、Cursor、Hermes Agent等各类智能体已经成为开发者和企业团队的常规生产力工具。从本地代码助手、个人办公智能体,到基于LangChain、Dify等框架搭建的复杂业务Agent,不同形态的AI应用渗透到工作的各个环节。但在大规模使用后,传统运维模式的短板彻底暴露:Agent内部的推理逻辑、工具调用流程、资源消耗完全处于“黑盒”状态,运维人员无法直观掌握运行细节。
601 0
|
9月前
|
人工智能 前端开发 开发者
拒绝夸大!AI编程工具真实使用体验(附案例)
开源、轻量、易部署的AI编程助手,支持Docker一键安装(1核2GB即可),适配Python/Vue/React等主流技术栈。本文以开发者视角分享其真实使用体验:含Excel批量分析、Vue3→React组件转译两大实操案例,并客观剖析优缺点,干货满满,无广告。
拒绝夸大!AI编程工具真实使用体验(附案例)
|
11月前
|
存储 数据管理 关系型数据库
数据库分库分表
分库分表旨在减轻单库单表压力,提升查询性能。垂直切分按业务或字段拆分,降低耦合、减少IO;水平切分按数据逻辑分布存储,减小单表数据量,实现分布式部署。垂直适用于业务清晰的系统,水平适合单表数据量大场景,各有优劣,需结合实际选择。
|
XML 人工智能 文字识别
Mobile-Agent:通过视觉感知实现自动化手机操作,支持多应用跨平台
Mobile-Agent 是一款基于多模态大语言模型的智能代理,能够通过视觉感知自主完成复杂的移动设备操作任务,支持跨应用操作和纯视觉解决方案。
7780 10
Mobile-Agent:通过视觉感知实现自动化手机操作,支持多应用跨平台
|
消息中间件 资源调度 关系型数据库
如何在Flink on YARN环境中配置Debezium CDC 3.0,以实现实时捕获数据库变更事件并将其传输到Flink进行处理
本文介绍了如何在Flink on YARN环境中配置Debezium CDC 3.0,以实现实时捕获数据库变更事件并将其传输到Flink进行处理。主要内容包括安装Debezium、配置Kafka Connect、创建Flink任务以及启动任务的具体步骤,为构建实时数据管道提供了详细指导。
1049 9
|
tengine 运维 Kubernetes
阿里巴巴重磅开源云原生网关: Higress
11月5日,2022 杭州 · 云栖大会-云原生峰会现场,阿里巴巴研究员、阿里云智能云原生应用平台总经理丁宇宣布:云原生网关 Higress 正式开源,Higress 是一款标准化、高集成、易扩展、热更新的云原生网关。
1507 118
阿里巴巴重磅开源云原生网关: Higress

相关产品

  • 物联网平台