Pu Zhibing
8 天以前 7a4f9541331bef779a506b38a27ed5c3373c0bec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.CreateMessageRequest;
import com.huaweicloud.sdk.iotda.v5.model.CreateMessageResponse;
import com.huaweicloud.sdk.iotda.v5.model.DeviceMessageRequest;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.integration.iotda.builder.IotBuilder;
import com.ruoyi.integration.iotda.config.IotDAConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * iotda消息发送
 */
@Slf4j
@RestController
public class IotMessageProduce {
 
    @Autowired
    private IotBuilder iotBuilder;
    
    @Autowired
    private IotDAConfig config;
    
 
    /**
     * 设备消息下发
     * @param name 消息名称
     * @param message 消息内容
     * @return
     */
    @PostMapping("/sendMessage")
    public String sendMessage(String code, String name, JSONObject message){
        log.info("消息下发至设备:code={},name={},message={}", code, name, message);
        CreateMessageRequest request = new CreateMessageRequest();
        request.withDeviceId(code);
        DeviceMessageRequest body = new DeviceMessageRequest();
        body.withMessageId(UUID.randomUUID().toString());
        body.withName(name);
        body.withMessage(message);
        request.withBody(body);
        try {
            CreateMessageResponse response = iotBuilder.buildIot().createMessage(request);
            log.info("消息下发至设备结果:{}", JSON.toJSONString(response));
            return JSON.toJSONString(response);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("消息下发至设备异常:code---{},msg---{}", code, e.getMessage());
            log.error("消息下发至设备异常:code---{},msg---{}", code, e.getMessage());
            return e.getMessage();
        }
    }
 
}