阿里雲學習之IOT物聯網套件(客戶端與服務端的後臺數據傳輸)

設備端代碼(mqttClient):https://help.aliyun.com/document_detail/42648.html?spm=5176.doc30579.6.569.ZEgA1ghtml

服務端代碼(aliyun-java-sdk-iot):https://help.aliyun.com/document_detail/30579.html?spm=5176.doc42693.6.597.WhnZQajava

消息代碼(aliyun-sdk-mns-samples):https://help.aliyun.com/document_detail/27508.html?spm=5176.doc27509.6.648.9hGlrbide

 

https://help.aliyun.com/document_detail/42693.html?spm=5176.doc50633.6.573.wlKHyi學習

https://help.aliyun.com/document_detail/27509.html?spm=5176.product27412.6.652.uIO9HWthis

1、設備端發送消息到服務端流程阿里雲

  一、終端發送消息到物聯網套件spa

    SimpleClient4IOT/connectMqtt:sampleClient.publish(pubTopic,message);調試

     String content = "客戶端發送消息到服務端";
        MqttMessage message = new MqttMessage(content.getBytes("utf-8"));
        message.setQos(0);//消息qos 0:最多一次,1:至少一次
        sampleClient.publish(pubTopic, message);//發送數據到某個topic
        System.out.println("發送消息成功");

 

  二、物聯網套件發送消息到消息服務code

    經過上一篇 阿里雲學習之IOT物聯網套件(配置篇),講解物聯網套件和消息服務的具體配置流程和走向htm

    規則引擎——>主題——>訂閱——>隊列

  三、消息服務發送消息到服務端,服務端接收消息

    ComsumerDemo/main:queue.popMessage();

        MNSClient client = account.getMNSClient(); //this client need only initialize once
            CloudQueue queue = client.getQueueRef("aliyun-iot-UGlr70uPYVM");// replace with your queue name
            System.out.println("開始調試...");
            for (int i = 0; i < 20; i++)
            {
                Message popMsg = queue.popMessage();
                System.out.println("消息爲空...");
                if (popMsg != null){
                    System.out.println("message handle: " + popMsg.getReceiptHandle());
                    System.out.println("message body: " + popMsg.getMessageBodyAsString());
                    System.out.println("message id: " + popMsg.getMessageId());
                    System.out.println("message dequeue count:" + popMsg.getDequeueCount());
                    //<<to add your special logic.>>
                    
                    //remember to  delete message when consume message successfully.
                    queue.deleteMessage(popMsg.getReceiptHandle());
                    System.out.println("delete message successfully.\n");
                }
            }

 

2、服務端發送消息到設備端流程

  一、服務端發送消息到物聯網套件

     String accessKey = "帳號key";
        String accessSecret = "帳號Secret";
        try {
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客戶端
        
        PubRequest request = new PubRequest();
        request.setProductKey("產品key");
        request.setMessageContent(Base64.encodeBase64String("消息服務".getBytes()));
        request.setTopicFullName("topic");// /UGlr70uPYVM/bulb/get
     request.setQos(0); //目前支持QoS0和QoS1  PubResponse response; try { response = client.getAcsResponse(request); System.out.println(response.getSuccess()); System.out.println(response.getErrorMessage()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); }

 

    二、設備端接收消息

        sampleClient.setCallback(new MqttCallback() {
            @Override
            /*public void connectionLost(Throwable cause) {
                LogUtil.print("鏈接失敗,緣由:" + cause);
                cause.printStackTrace();
            }*/

            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                LogUtil.print("接收到消息,來至Topic [" + topic + "] , 內容是:["
                    + new String(message.getPayload(), "UTF-8") + "],  ");
            }

            @Override
            /*public void deliveryComplete(IMqttDeliveryToken token) {
                //若是是QoS0的消息,token.resp是沒有回覆的
                LogUtil.print("消息發送成功! " + ((token == null || token.getResponse() == null) ? "null"
                    : token.getResponse().getKey()));
            }*/
        });
相關文章
相關標籤/搜索