puzhibing
2025-02-05 74b0e0814e37d640596f44ec86d20fa9ecce9ed6
ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/iotda/utils/produce/IotMessageProduce.java
@@ -1,5 +1,6 @@
package com.ruoyi.integration.iotda.utils.produce;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
@@ -27,17 +28,21 @@
    @Autowired
    private IotBuilder iotBuilder;
    @Autowired
    private IotDAConfig config;
    /**
     * 设备消息下发
     * @param deviceId 设备id
     * @param name 消息名称
     * @param message 消息内容
     * @return
     */
    @PostMapping("/sendMessage")
    public String sendMessage(String deviceId, String name, JSONObject message){
    public String sendMessage(String code, String name, JSONObject message){
        log.info("消息下发至设备:code={},name={},message={}", code, name, message);
        CreateMessageRequest request = new CreateMessageRequest();
        request.withDeviceId(deviceId);
        request.withDeviceId(code);
        DeviceMessageRequest body = new DeviceMessageRequest();
        body.withMessageId(UUID.randomUUID().toString());
        body.withName(name);
@@ -45,19 +50,14 @@
        request.withBody(body);
        try {
            CreateMessageResponse response = iotBuilder.buildIot().createMessage(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            log.info("消息下发至设备结果:{}", JSON.toJSONString(response));
            return JSON.toJSONString(response);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
            log.info("消息下发至设备异常:code---{},msg---{}", code, e.getMessage());
            log.error("消息下发至设备异常:code---{},msg---{}", code, e.getMessage());
            return e.getMessage();
        }
        return null;
    }
}