基于spring boot远程信息下发(将远程网页的数据下发到阿里云设备的topic,通过三元组连接到该topic的终端能够直接得到消息)

阿里云教程,物联网javaSDK

1.pom文件引入物联网平台的 java SDK

阿里云 IoT java SDK:

<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-iot</artifactId>
    <version>7.8.0</version>
</dependency>

阿里云 java SDK公共包:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>3.5.1</version>
</dependency>

2.远程信息发送方法

    public void sendTopicTODevice() {
    
    //初始化SDK===================================================================
        String accessKey = "your accessKey";
        String accessSecret = "your accessSecret";
        String regionId = "cn-shanghai";
        String uid = "your uid";
        String deviceName = "your deviceName ";
        String productKey = "your productKey ";

        try {
            DefaultProfile.addEndpoint(
                    "cn-shanghai",
                    regionId,
                    "Iot",
                    "iot.cn-shanghai.aliyuncs.com");
        }
        catch (com.aliyuncs.exceptions.ClientException e)
        {
            e.printStackTrace();
        }

        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端


//调用物联网平台的API(以Pub方式发布消息到topic--即绑定相应topic的设备)====================
        PubRequest request = new PubRequest();
        request.setProductKey(productKey);
        request.setMessageContent(Base64.encodeBase64String("哈罗,我的".getBytes()));
        request.setTopicFullName("你的设备下topic路径");
        request.setQos(0); //目前支持QoS0和QoS1
        try
        {
            PubResponse response = client.getAcsResponse(request);
            System.out.println(response.getSuccess());
            System.out.println(response.getErrorMessage());
        }
        catch (com.aliyuncs.exceptions.ServerException e)
        {
            e.printStackTrace();
        }
        catch (com.aliyuncs.exceptions.ClientException e)
        {
            e.printStackTrace();
        }
    }

以上代码直接放在spring boot的test类能直接发送消息。

3. lamp_control网页代码片段

<script>
    layui.use(['form', 'layedit', 'laydate','layer'], function(){
        $(document).on('click','#open_btn',function() {
            layer.msg("打开")
            openControlMessage(); //发送控制消息到队列
        });
    });


    function openControlMessage(){
        $.ajax({
            url:"/lamp/open_message",
            type: "GET",

        });
    }
</script>

4.对应Controller代码片段

@Controller
@RequestMapping("/lamp")
public class LampController {

    @Resource
    private IControlMessageProducer controlMessageProducer;

    @RequestMapping("/lamp_control")
    public String lampControl(){

        return "lamp_control";
    }

    @RequestMapping("/open_message")
    public String openControl(){

        controlMessageProducer.sendTopicTODevice(); //调用发送消息的方法

        return "lamp_control";
//        return null;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值