|
@@ -0,0 +1,158 @@
|
|
|
|
|
+package com.kym.service.awoara.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.aliyun.iot20180120.Client;
|
|
|
|
|
+import com.aliyun.iot20180120.models.*;
|
|
|
|
|
+import com.aliyun.tea.TeaModel;
|
|
|
|
|
+import com.kym.service.awoara.AwoaraService;
|
|
|
|
|
+import com.kym.service.awoara.entity.ApiList;
|
|
|
|
|
+import com.kym.service.awoara.entity.Config;
|
|
|
|
|
+import com.kym.service.awoara.entity.MethodHelp;
|
|
|
|
|
+import com.kym.service.awoara.entity.OrderInfo;
|
|
|
|
|
+import com.kym.service.awoara.entity.response.AwoaraResponse;
|
|
|
|
|
+import com.kym.service.awoara.entity.response.CreateOrder;
|
|
|
|
|
+import com.kym.service.awoara.entity.response.HardwareInfo;
|
|
|
|
|
+import com.kym.service.awoara.entity.response.State;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class AwoaraServiceImpl implements AwoaraService {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String ENDPOINT = "iot.cn-shanghai.aliyuncs.com";
|
|
|
|
|
+ private static final String IOT_INSTANCE_ID = "iot.cn-shanghai.aliyuncs.com";
|
|
|
|
|
+ private static final String GROUP_ID = "iot.cn-shanghai.aliyuncs.com";
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 初始化IoT(Iot20180120)客户端
|
|
|
|
|
+ */
|
|
|
|
|
+ public static com.aliyun.iot20180120.Client initialization() throws Exception {
|
|
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
|
|
|
+ .setRegionId("cn-shanghai")
|
|
|
|
|
+ .setAccessKeyId(System.getenv("ACCESS_KEY_ID"))
|
|
|
|
|
+ .setAccessKeySecret(System.getenv("ACCESS_KEY_SECRET"));
|
|
|
|
|
+ return new com.aliyun.iot20180120.Client(config);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 调用Iot20180120客户端发送请求
|
|
|
|
|
+ * 1.RRpc:向指定设备发送请求消息,并同步返回响应
|
|
|
|
|
+ */
|
|
|
|
|
+ public static RRpcResponse rRpc(Client client, String iotInstanceId, String productKey, String deviceName, String requestBase64Byte, String timeout, String topic) throws Exception {
|
|
|
|
|
+ var request = new RRpcRequest()
|
|
|
|
|
+ .setIotInstanceId(iotInstanceId)
|
|
|
|
|
+ .setProductKey(productKey)
|
|
|
|
|
+ .setDeviceName(deviceName)
|
|
|
|
|
+ .setRequestBase64Byte(requestBase64Byte)
|
|
|
|
|
+ .setTopic(timeout);
|
|
|
|
|
+ // 等待设备回复消息的时间,单位是毫秒,取值范围是1,000 ~8,000。
|
|
|
|
|
+ // 校验integer型入参
|
|
|
|
|
+ Integer iTimeout = Integer.parseInt(topic);
|
|
|
|
|
+ request.setTimeout(iTimeout);
|
|
|
|
|
+ log.info("-------------------1.RRpc:向指定设备发送请求消息,并同步返回响应--------------------");
|
|
|
|
|
+ RRpcResponse response = client.rRpc(request);
|
|
|
|
|
+ log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 调用Iot20180120客户端发送请求
|
|
|
|
|
+ * 2.PubBroadcast 向指定产品所有设备,或向订阅了指定Topic的所有设备发布广播消息
|
|
|
|
|
+ */
|
|
|
|
|
+ public static PubBroadcastResponse pubBroadcast(com.aliyun.iot20180120.Client client, String iotInstanceId, String productKey, String messageContent, String topic) throws Exception {
|
|
|
|
|
+ PubBroadcastRequest request = new PubBroadcastRequest()
|
|
|
|
|
+ .setIotInstanceId(iotInstanceId)
|
|
|
|
|
+ .setProductKey(productKey)
|
|
|
|
|
+ .setMessageContent(messageContent)
|
|
|
|
|
+ .setTopicFullName(topic);
|
|
|
|
|
+ log.info("-------------------2.PubBroadcast 向指定产品所有设备,或向订阅了指定Topic的所有设备发布广播消息--------------------");
|
|
|
|
|
+ PubBroadcastResponse response = client.pubBroadcast(request);
|
|
|
|
|
+ log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 调用Iot20180120客户端发送请求
|
|
|
|
|
+ * 3.SubscribeTopic 为指定设备订阅Topic
|
|
|
|
|
+ */
|
|
|
|
|
+ public static SubscribeTopicResponse subscribeTopic(com.aliyun.iot20180120.Client client, String iotInstanceId, String productKey, String deviceName, String topicStr) throws Exception {
|
|
|
|
|
+ // 要订阅的Topic,最多订阅10个Topic。
|
|
|
|
|
+ // Topic的操作权限必须为订阅或发布和订阅。
|
|
|
|
|
+ List<String> arrTopic = Arrays.stream(topicStr.split(",", 10)).toList();
|
|
|
|
|
+ SubscribeTopicRequest request = new SubscribeTopicRequest()
|
|
|
|
|
+ .setIotInstanceId(iotInstanceId)
|
|
|
|
|
+ .setProductKey(productKey)
|
|
|
|
|
+ .setDeviceName(deviceName)
|
|
|
|
|
+ .setTopic(arrTopic);
|
|
|
|
|
+ log.info("-------------------3.SubscribeTopic 为指定设备订阅Topic--------------------");
|
|
|
|
|
+ SubscribeTopicResponse response = client.subscribeTopic(request);
|
|
|
|
|
+ log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<ApiList> help() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<MethodHelp> methodHelp(String method) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<?> showMsgbox(Map<String, Object> params) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<?> hideMsgbox() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<?> reboot() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<State> queryState() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<HardwareInfo> queryHardwareInfo() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<CreateOrder> createOrder() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<?> closeOrder(Map<String, Object> params) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<OrderInfo> queryOrder(String orderId) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<Config> readConfig() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AwoaraResponse<?> writeConfig(Config config) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|