2020 SpringBoot 连接腾讯云物联网平台,建项目、产品、设备

目录

1.Maven pom引入腾讯云SDK

2. 配置application.yml

3.创建TencentConfig 类

4.封装一个 TencentApi 类

5.新建Test 测试类


 

1.Maven pom引入腾讯云SDK

  
            com.tencentcloudapi
            tencentcloud-sdk-java
            
            
            3.1.88
        

2. 配置application.yml

tencent:
    secretId: xxxxx   //申请secretId
    secretKey: xxxxx //申请secretKey
    endpoint: iotexplorer.tencentcloudapi.com
    region: ap-shanghai

3.创建TencentConfig 类

package com.example.demo.tencent;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/07/10/14:08
 * @Description:
 */

@Component
@PropertySource("classpath:application.yml")
@ConfigurationProperties(prefix = "tencent")
@Data
public class TencentConfig {


    @Value("${secretId}")
    private String secretId;

    @Value("${secretKey}")
    private String secretKey;

    @Value("${endpoint}")
    private String endpoint;

    @Value("${region}")
    private String region;



}

4.封装一个 TencentApi 类

package com.example.demo.tencent;

import com.example.demo.tencent.TencentConfig;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.iotexplorer.v20190423.IotexplorerClient;
import com.tencentcloudapi.iotexplorer.v20190423.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/07/10/14:20
 * @Description:
 */
@Component
public class TencentApi {

    @Autowired
    private TencentConfig tencentConfig;


    /**
     * 创建项目
     * @param params
     * @return
     */
    public  String  createProject(String params){
        String json = null;
        CreateProjectRequest request = CreateStudioProductRequest.fromJsonString(params,CreateProjectRequest.class);
        CreateProjectResponse resp = null;
        try {
            resp = getClient().CreateProject(request);
            json = CreateStudioProductResponse.toJsonString(resp);
            System.out.println(json);
            return json;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 创建产品
     * @param params
     * @return
     */
    public  String  createProduct(String params){
        String json = null;
        CreateStudioProductRequest req = CreateStudioProductRequest.fromJsonString(params, CreateStudioProductRequest.class);
        CreateStudioProductResponse resp = null;
        try {
            resp = getClient().CreateStudioProduct(req);
            json = CreateStudioProductResponse.toJsonString(resp);
            System.out.println(json);
            return json;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建产品
     * @param params
     * @return
     */
    public  String  createDevice(String params){
        String json = null;
        CreateDeviceRequest request = CreateDeviceRequest.fromJsonString(params,CreateDeviceRequest.class);
        CreateDeviceResponse resp = null;
        try {
            resp = getClient().CreateDevice(request);
            json = CreateDeviceResponse.toJsonString(resp);
            System.out.println(json);
            return json;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return json;
    }



    /**
     * 请求client
     * @return
     */
    private IotexplorerClient getClient(){
        Credential cred = new Credential(tencentConfig.getSecretId(), tencentConfig.getSecretKey());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(tencentConfig.getEndpoint());
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
        clientProfile.setDebug(true);
        IotexplorerClient client = new IotexplorerClient(cred, tencentConfig.getRegion(), clientProfile);
        return client;
    }

}

 

5.新建Test 测试类

package com.example.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.tencent.TencentApi;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/07/10/14:30
 * @Description:
 */

@SpringBootTest
public class TencentTest {

    @Autowired
    private TencentApi tencentApi;


    /**
     * 产品名称,名称不能和已经存在的产品名称重复。命名规则:[a-zA-Z0-9:_-]{1,32}
     */
    @Test
    public void createDevice() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductId","OKYYTEY8LY");
        jsonObject.put("DeviceName","device_001");
        jsonObject.put("DevAddr","");
        jsonObject.put("AppKey","");
        jsonObject.put("DevEUI","");
        jsonObject.put("AppSKey","");
        jsonObject.put("NwkSKey","");

        tencentApi.createDevice(jsonObject.toJSONString());

    }





    /**
     * 产品名称,名称不能和已经存在的产品名称重复。命名规则:[a-zA-Z0-9:_-]{1,32}
     */
    @Test
    public void createProduct() {

         JSONObject jsonObject = new JSONObject();
         jsonObject.put("ProductName","test_002");
         jsonObject.put("CategoryId","1");
        jsonObject.put("ProductType","0");
        jsonObject.put("EncryptionType","2");
        jsonObject.put("NetType","wifi");
        jsonObject.put("DataProtocol","1");
        jsonObject.put("ProductDesc","新增test产品1112312312313");
        jsonObject.put("ProjectId","prj-7nl2uzyj");

         tencentApi.createProduct(jsonObject.toJSONString());

    }



    /**
     * 创建项目
     * {
     *     "Project":{
     *         "ProjectId":"prj-i35uryl5",
     *         "ProjectName":"测试啊112",
     *         "ProjectDesc":"测试啊测试啊测试啊测试啊",
     *         "CreateTime":1594366403,
     *         "UpdateTime":1594366403
     *     },
     *     "RequestId":"dc7ce4eb-d66d-4cb9-ad88-301904dd9ec5"
     * }
     */
    @Test
    public void crateProject() {

        String params = "{"ProjectName":"测试啊112","ProjectDesc":"测试啊测试啊测试啊测试啊"}";
        tencentApi.createProject(params);

    }

}

 

 

 

 

 

本文章来源于互联网,如有侵权,请联系删除!原文地址:2020 SpringBoot 连接腾讯云物联网平台,建项目、产品、设备

相关推荐: 医疗物联网平台架构图

物联网,简称IoT,是未来智慧医疗的核心。按照面向服务的体系架构,医疗物联网被分为四层结构:感知层、网络层、平台层和应用层。 感知层,又被称为传感层,是物联网的基本特征,由包括RFID以及各种智能传感器在内的传感器硬件以及相应的数据感知/采集协议构成。它的作用…