| | |
| | | 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 |
| | | @Component |
| | | @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(); |
| | | } |
| | | } |
| | | |
| | | } |