|
@@ -1,49 +1,110 @@
|
|
|
-package com.kym.service.awoara.impl;
|
|
|
|
|
|
|
+package com.kym.miniapp.mq;
|
|
|
|
|
|
|
|
-import com.aliyun.iot20180120.Client;
|
|
|
|
|
import com.aliyun.iot20180120.models.*;
|
|
import com.aliyun.iot20180120.models.*;
|
|
|
import com.aliyun.tea.TeaModel;
|
|
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.event.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 com.kym.miniapp.config.AliyunLotConfig;
|
|
|
|
|
+import jakarta.annotation.PostConstruct;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
|
|
+import java.util.Base64;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
|
|
-@Service
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 阿里云物联网平台
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author skyline
|
|
|
|
|
+ */
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
-public class AwoaraServiceImpl implements AwoaraService {
|
|
|
|
|
|
|
+@Service
|
|
|
|
|
+public class AliyunLotClient {
|
|
|
|
|
|
|
|
- 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";
|
|
|
|
|
|
|
+ private static AliyunLotConfig aliyunLotConfig;
|
|
|
|
|
+ private static com.aliyun.iot20180120.Client rRpcClient;
|
|
|
|
|
|
|
|
|
|
+ private static Base64.Encoder encoder = Base64.getEncoder();
|
|
|
|
|
|
|
|
- // ============================================================== 阿里云lot主动请求 ==============================================================
|
|
|
|
|
|
|
+ public AliyunLotClient(AliyunLotConfig aliyunLotConfig) {
|
|
|
|
|
+ this.aliyunLotConfig = aliyunLotConfig;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 初始化IoT(Iot20180120)客户端
|
|
* 初始化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);
|
|
|
|
|
|
|
+ @PostConstruct
|
|
|
|
|
+ public void init() throws Exception {
|
|
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
|
|
|
+ .setRegionId("cn-shanghai")
|
|
|
|
|
+ .setAccessKeyId(aliyunLotConfig.accessKey)
|
|
|
|
|
+ .setAccessKeySecret(aliyunLotConfig.accessSecret);
|
|
|
|
|
+ rRpcClient = new com.aliyun.iot20180120.Client(config);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 调用Iot20180120客户端发送请求
|
|
|
|
|
+ * 2.CreateSubscribeRelation 创建MNS或AMQP服务端订阅
|
|
|
|
|
+ */
|
|
|
|
|
+ public static CreateSubscribeRelationResponseBody createSubscribeRelation(com.aliyun.iot20180120.Client client, String instanceId, String productKey, String type, Boolean deviceDataFlag, Boolean deviceLifeCycleFlag, Boolean deviceStatusChangeFlag, Boolean deviceTagFlag, Boolean deviceTopoLifeCycleFlag, Boolean foundDeviceListFlag, Boolean otaEventFlag, Boolean otaJobFlag, Boolean otaVersionFlag, Boolean thingHistoryFlag, String mnsConfiguration, String consumerGroupIds) throws Exception {
|
|
|
|
|
+ CreateSubscribeRelationRequest request = new CreateSubscribeRelationRequest().setProductKey(productKey).setType(type).setIotInstanceId(instanceId).setThingHistoryFlag(thingHistoryFlag).setOtaVersionFlag(otaVersionFlag).setOtaJobFlag(otaJobFlag).setOtaEventFlag(otaEventFlag).setFoundDeviceListFlag(foundDeviceListFlag).setDeviceTopoLifeCycleFlag(deviceTopoLifeCycleFlag).setDeviceTagFlag(deviceTagFlag).setDeviceStatusChangeFlag(deviceStatusChangeFlag).setDeviceLifeCycleFlag(deviceLifeCycleFlag).setDeviceDataFlag(deviceDataFlag).setMnsConfiguration(mnsConfiguration);
|
|
|
|
|
+ // 创建的AMQP订阅中的消费组ID,Type为AMQP时必填。
|
|
|
|
|
+ if (!com.aliyun.teautil.Common.empty(consumerGroupIds)) {
|
|
|
|
|
+ request.consumerGroupIds = Arrays.stream(consumerGroupIds.split(",", 100)).toList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("-------------------2.CreateSubscribeRelation 创建MNS或AMQP服务端订阅--------------------");
|
|
|
|
|
+ CreateSubscribeRelationResponse response = client.createSubscribeRelation(request);
|
|
|
|
|
+ log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body)));
|
|
|
|
|
+ return response.body;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 调用Iot20180120客户端发送请求
|
|
|
|
|
+ * 3.UpdateSubscribeRelation 修改MNS或AMQP服务端订阅
|
|
|
|
|
+ */
|
|
|
|
|
+ public static UpdateSubscribeRelationResponseBody updateSubscribeRelation(com.aliyun.iot20180120.Client client, String iotInstanceId, String productKey, String type, Boolean deviceDataFlag, Boolean deviceLifeCycleFlag, Boolean deviceStatusChangeFlag, Boolean deviceTagFlag, Boolean deviceTopoLifeCycleFlag, Boolean foundDeviceListFlag, Boolean otaEventFlag, Boolean otaJobFlag, Boolean otaVersionFlag, Boolean thingHistoryFlag, String mnsConfiguration, String consumerGroupIds) throws Exception {
|
|
|
|
|
+ UpdateSubscribeRelationRequest request = new UpdateSubscribeRelationRequest().setProductKey(productKey).setType(type).setIotInstanceId(iotInstanceId).setThingHistoryFlag(thingHistoryFlag).setOtaVersionFlag(otaVersionFlag).setOtaJobFlag(otaJobFlag).setOtaEventFlag(otaEventFlag).setFoundDeviceListFlag(foundDeviceListFlag).setDeviceTopoLifeCycleFlag(deviceTopoLifeCycleFlag).setDeviceTagFlag(deviceTagFlag).setDeviceStatusChangeFlag(deviceStatusChangeFlag).setDeviceLifeCycleFlag(deviceLifeCycleFlag).setDeviceDataFlag(deviceDataFlag).setMnsConfiguration(mnsConfiguration);
|
|
|
|
|
+ // 创建的AMQP订阅中的消费组ID,Type为AMQP时必填。
|
|
|
|
|
+ if (!com.aliyun.teautil.Common.empty(consumerGroupIds)) {
|
|
|
|
|
+ request.consumerGroupIds = Arrays.stream(consumerGroupIds.split(",", 100)).toList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("-------------------3.UpdateSubscribeRelation 修改MNS或AMQP服务端订阅--------------------");
|
|
|
|
|
+ UpdateSubscribeRelationResponse response = client.updateSubscribeRelation(request);
|
|
|
|
|
+ log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body)));
|
|
|
|
|
+ return response.body;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 调用Iot20180120客户端发送请求
|
|
|
|
|
+ * 4.DeleteSubscribeRelation 删除MNS或AMQP服务端订阅
|
|
|
|
|
+ */
|
|
|
|
|
+ public static DeleteSubscribeRelationResponseBody deleteSubscribeRelation(com.aliyun.iot20180120.Client client, String iotInstanceId, String productKey, String type) throws Exception {
|
|
|
|
|
+ DeleteSubscribeRelationRequest request = new DeleteSubscribeRelationRequest().setProductKey(productKey).setType(type).setIotInstanceId(iotInstanceId);
|
|
|
|
|
+ log.info("-------------------4.DeleteSubscribeRelation 删除MNS或AMQP服务端订阅--------------------");
|
|
|
|
|
+ DeleteSubscribeRelationResponse response = client.deleteSubscribeRelation(request);
|
|
|
|
|
+ log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body)));
|
|
|
|
|
+ return response.body;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static RRpcResponse rRpc(String productKey, String deviceName, String requestJson) throws Exception {
|
|
|
|
|
+ var requestBase64Byte = new String(encoder.encode(requestJson.getBytes()));
|
|
|
|
|
+ var request = new RRpcRequest().setIotInstanceId(aliyunLotConfig.getIotInstanceId()).setProductKey(productKey).setDeviceName(deviceName).setRequestBase64Byte(requestBase64Byte);
|
|
|
|
|
+ // 等待设备回复消息的时间,单位是毫秒,取值范围是1,000 ~8,000。
|
|
|
|
|
+ // 校验integer型入参
|
|
|
|
|
+ Integer iTimeout = 8000;
|
|
|
|
|
+ request.setTimeout(iTimeout);
|
|
|
|
|
+ log.info("-------------------1.RRpc:向指定设备发送请求消息,并同步返回响应--------------------");
|
|
|
|
|
+ RRpcResponse response = rRpcClient.rRpc(request);
|
|
|
|
|
+ log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 调用Iot20180120客户端发送请求
|
|
* 调用Iot20180120客户端发送请求
|
|
|
* 1.RRpc:向指定设备发送请求消息,并同步返回响应
|
|
* 1.RRpc:向指定设备发送请求消息,并同步返回响应
|
|
|
*/
|
|
*/
|
|
|
- public static RRpcResponse rRpc(Client client, String iotInstanceId, String productKey, String deviceName, String requestBase64Byte, String timeout, String topic) throws Exception {
|
|
|
|
|
|
|
+ public static RRpcResponse rRpc(com.aliyun.iot20180120.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(topic);
|
|
var request = new RRpcRequest().setIotInstanceId(iotInstanceId).setProductKey(productKey).setDeviceName(deviceName).setRequestBase64Byte(requestBase64Byte).setTopic(topic);
|
|
|
// 等待设备回复消息的时间,单位是毫秒,取值范围是1,000 ~8,000。
|
|
// 等待设备回复消息的时间,单位是毫秒,取值范围是1,000 ~8,000。
|
|
|
// 校验integer型入参
|
|
// 校验integer型入参
|
|
@@ -82,10 +143,6 @@ public class AwoaraServiceImpl implements AwoaraService {
|
|
|
return response;
|
|
return response;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // ============================================================== 阿里云lot主动请求 ==============================================================
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================== 石斑鱼上行消息 ==============================================================
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 调用Iot20180120客户端发送请求
|
|
* 调用Iot20180120客户端发送请求
|
|
|
* 1.QuerySubscribeRelation 查询MNS或AMQP服务端订阅
|
|
* 1.QuerySubscribeRelation 查询MNS或AMQP服务端订阅
|
|
@@ -99,53 +156,6 @@ public class AwoaraServiceImpl implements AwoaraService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 调用Iot20180120客户端发送请求
|
|
|
|
|
- * 2.CreateSubscribeRelation 创建MNS或AMQP服务端订阅
|
|
|
|
|
- */
|
|
|
|
|
- public static CreateSubscribeRelationResponseBody createSubscribeRelation(com.aliyun.iot20180120.Client client, String instanceId, String productKey, String type, Boolean deviceDataFlag, Boolean deviceLifeCycleFlag, Boolean deviceStatusChangeFlag, Boolean deviceTagFlag, Boolean deviceTopoLifeCycleFlag, Boolean foundDeviceListFlag, Boolean otaEventFlag, Boolean otaJobFlag, Boolean otaVersionFlag, Boolean thingHistoryFlag, String mnsConfiguration, String consumerGroupIds) throws Exception {
|
|
|
|
|
- CreateSubscribeRelationRequest request = new CreateSubscribeRelationRequest().setProductKey(productKey).setType(type).setIotInstanceId(instanceId).setThingHistoryFlag(thingHistoryFlag).setOtaVersionFlag(otaVersionFlag).setOtaJobFlag(otaJobFlag).setOtaEventFlag(otaEventFlag).setFoundDeviceListFlag(foundDeviceListFlag).setDeviceTopoLifeCycleFlag(deviceTopoLifeCycleFlag).setDeviceTagFlag(deviceTagFlag).setDeviceStatusChangeFlag(deviceStatusChangeFlag).setDeviceLifeCycleFlag(deviceLifeCycleFlag).setDeviceDataFlag(deviceDataFlag).setMnsConfiguration(mnsConfiguration);
|
|
|
|
|
- // 创建的AMQP订阅中的消费组ID,Type为AMQP时必填。
|
|
|
|
|
- if (!com.aliyun.teautil.Common.empty(consumerGroupIds)) {
|
|
|
|
|
- request.consumerGroupIds = Arrays.stream(consumerGroupIds.split(",", 100)).toList();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- log.info("-------------------2.CreateSubscribeRelation 创建MNS或AMQP服务端订阅--------------------");
|
|
|
|
|
- CreateSubscribeRelationResponse response = client.createSubscribeRelation(request);
|
|
|
|
|
- log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body)));
|
|
|
|
|
- return response.body;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 调用Iot20180120客户端发送请求
|
|
|
|
|
- * 3.UpdateSubscribeRelation 修改MNS或AMQP服务端订阅
|
|
|
|
|
- */
|
|
|
|
|
- public static UpdateSubscribeRelationResponseBody updateSubscribeRelation(com.aliyun.iot20180120.Client client, String iotInstanceId, String productKey, String type, Boolean deviceDataFlag, Boolean deviceLifeCycleFlag, Boolean deviceStatusChangeFlag, Boolean deviceTagFlag, Boolean deviceTopoLifeCycleFlag, Boolean foundDeviceListFlag, Boolean otaEventFlag, Boolean otaJobFlag, Boolean otaVersionFlag, Boolean thingHistoryFlag, String mnsConfiguration, String consumerGroupIds) throws Exception {
|
|
|
|
|
- UpdateSubscribeRelationRequest request = new UpdateSubscribeRelationRequest().setProductKey(productKey).setType(type).setIotInstanceId(iotInstanceId).setThingHistoryFlag(thingHistoryFlag).setOtaVersionFlag(otaVersionFlag).setOtaJobFlag(otaJobFlag).setOtaEventFlag(otaEventFlag).setFoundDeviceListFlag(foundDeviceListFlag).setDeviceTopoLifeCycleFlag(deviceTopoLifeCycleFlag).setDeviceTagFlag(deviceTagFlag).setDeviceStatusChangeFlag(deviceStatusChangeFlag).setDeviceLifeCycleFlag(deviceLifeCycleFlag).setDeviceDataFlag(deviceDataFlag).setMnsConfiguration(mnsConfiguration);
|
|
|
|
|
- // 创建的AMQP订阅中的消费组ID,Type为AMQP时必填。
|
|
|
|
|
- if (!com.aliyun.teautil.Common.empty(consumerGroupIds)) {
|
|
|
|
|
- request.consumerGroupIds = Arrays.stream(consumerGroupIds.split(",", 100)).toList();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- log.info("-------------------3.UpdateSubscribeRelation 修改MNS或AMQP服务端订阅--------------------");
|
|
|
|
|
- UpdateSubscribeRelationResponse response = client.updateSubscribeRelation(request);
|
|
|
|
|
- log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body)));
|
|
|
|
|
- return response.body;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 调用Iot20180120客户端发送请求
|
|
|
|
|
- * 4.DeleteSubscribeRelation 删除MNS或AMQP服务端订阅
|
|
|
|
|
- */
|
|
|
|
|
- public static DeleteSubscribeRelationResponseBody deleteSubscribeRelation(com.aliyun.iot20180120.Client client, String iotInstanceId, String productKey, String type) throws Exception {
|
|
|
|
|
- DeleteSubscribeRelationRequest request = new DeleteSubscribeRelationRequest().setProductKey(productKey).setType(type).setIotInstanceId(iotInstanceId);
|
|
|
|
|
- log.info("-------------------4.DeleteSubscribeRelation 删除MNS或AMQP服务端订阅--------------------");
|
|
|
|
|
- DeleteSubscribeRelationResponse response = client.deleteSubscribeRelation(request);
|
|
|
|
|
- log.info(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body)));
|
|
|
|
|
- return response.body;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
void t1() {
|
|
void t1() {
|
|
|
java.util.List<String> args = java.util.Arrays.asList("");
|
|
java.util.List<String> args = java.util.Arrays.asList("");
|
|
|
// * param.1:IotInstanceId 实例ID
|
|
// * param.1:IotInstanceId 实例ID
|
|
@@ -234,68 +244,4 @@ public class AwoaraServiceImpl implements AwoaraService {
|
|
|
log.info(error.getMessage());
|
|
log.info(error.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // ============================================================== 石斑鱼上行消息 ==============================================================
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================== 石斑鱼自定义API ==============================================================
|
|
|
|
|
- @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;
|
|
|
|
|
- }
|
|
|
|
|
- // ============================================================== 石斑鱼自定义API ==============================================================
|
|
|
|
|
}
|
|
}
|