luodangjia
2024-08-23 7085ba24d8decee4b13c86a55b93a3bcc4ea0d03
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
package com.ruoyi.integration.iotda.utils.produce;
 
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;
 
/**
 * iotda消息发送
 */
@Slf4j
@Component
public class IotMessageProduce {
 
    @Autowired
    private IotBuilder iotBuilder;
 
    /**
     * 设备消息下发
     * @param deviceId 设备id
     * @param name 消息名称
     * @param message 消息内容
     * @return
     */
    public String sendMessage(String deviceId,String name,String message){
        CreateMessageRequest request = new CreateMessageRequest();
        request.withDeviceId(deviceId);
        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);
            System.out.println(response.toString());
        } catch (ConnectionException 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());
        }
        return null;
    }
 
}