From 4e76b9737e036c18e8a9e840dc443615ffcec348 Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期四, 20 十月 2022 12:20:18 +0800
Subject: [PATCH] 大气农耕
---
ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/BaseInvokeSyncService.java | 177 +++++++++++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 142 insertions(+), 35 deletions(-)
diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/BaseInvokeSyncService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/BaseInvokeSyncService.java
index d1f2407..6e93fc5 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/BaseInvokeSyncService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/BaseInvokeSyncService.java
@@ -13,30 +13,19 @@
import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.topic.IBaseTopic;
import com.sandu.ximon.admin.manager.iot.rrpc.topic.ICustomizeTopic;
+import com.sandu.ximon.admin.manager.iot.rrpc.util.FrameUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
import java.util.List;
+import java.util.Map;
/**
* @author chenjiantian
* @date 2021/12/2 18:21
*/
+@Slf4j
public abstract class BaseInvokeSyncService implements IInvoke, IBaseTopic, ICustomizeTopic {
-
- /**
- * 鑾峰彇浜у搧鍚�
- *
- * @return 浜у搧鍚�
- */
- public abstract String getProductKey();
-
- /**
- * 鍙戦�乺rpc 璇锋眰
- *
- * @param deviceName 璁惧鍚�
- * @param iRequestFrame 缂栫爜甯�
- * @return 杩斿洖鐨勮В鐮佸抚瀹炰綋绫�
- */
- public abstract CommonFrame sendRRPC(String deviceName, IRequestFrame iRequestFrame);
/**
* 鍙戦�乺rpc 璇锋眰
@@ -46,7 +35,69 @@
* @param clz 鍐呴儴甯у搷搴斿疄浣撶被 濡傛灉涓嶆兂鑷繁瑙f瀽 鍙甫涓�
* @return 杩斿洖鐨勮В鐮佸抚瀹炰綋绫� 鍚屾椂鎶婂唴閮ㄥ抚瑙g爜涔嬪悗涔熸惡甯︿笂
*/
- public abstract <T extends BaseResponseInnerFrame<T>> WrapResponseCommonFrame<T> sendRRPC(String deviceName, IRequestFrame iRequestFrame, Class<T> clz);
+ public <T extends BaseResponseInnerFrame<T>> WrapResponseCommonFrame<T> sendRRPC(String deviceName, IRequestFrame iRequestFrame, Class<T> clz) {
+ CommonFrame commonFrame = sendRRPC(deviceName, iRequestFrame);
+ if (commonFrame == null) {
+ return null;
+ }
+ WrapResponseCommonFrame<T> responseCommonFrame = new WrapResponseCommonFrame<>();
+ BeanUtils.copyProperties(commonFrame, responseCommonFrame);
+ try {
+ responseCommonFrame.setResponseInnerFrame(clz.newInstance().transformFrame(responseCommonFrame.getPayload()));
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new BusinessException(String.format("鎵句笉鍒�%s鐨勬瀯閫犲嚱鏁�", clz.getName()));
+ }
+ return responseCommonFrame;
+ }
+
+ @Override
+ public CommonFrame sendRRPC(String deviceName, InvokeParam invokeParam) {
+ InvokeThingServiceResponse.Data data = invokeThing(deviceName, invokeParam, false);
+ if (data == null) {
+ return null;
+ }
+ String result = data.getResult();
+ result = result.replace("\\", "");
+ Map map = JSON.parseObject(result, Map.class);
+ result = (String) map.get("msg");
+ return FrameUtils.transformMessageToFrame(result);
+ }
+
+ @Override
+ public CommonFrame sendRRPC(String deviceName, IRequestFrame iRequestFrame) {
+ InvokeParam param = new InvokeParam();
+ param.setOperate("1001");
+ param.setFrame(iRequestFrame.getEncodeFrame());
+ InvokeThingServiceResponse.Data data = invokeThing(deviceName, param, false);
+ if (data == null) {
+ return null;
+ }
+ String result = data.getResult();
+ result = result.replace("\\", "");
+ Map map = JSON.parseObject(result, Map.class);
+ result = (String) map.get("msg");
+ return FrameUtils.transformMessageToFrame(result);
+ }
+
+ @Override
+ public CommonFrame sendRRPC(String deviceName, IRequestFrame iRequestFrame, boolean resendFlag) {
+ InvokeParam param = new InvokeParam();
+ param.setOperate("1001");
+ param.setFrame(iRequestFrame.getEncodeFrame());
+ InvokeThingServiceResponse.Data data = invokeThing(deviceName, param, true);
+ if (data == null) {
+ return null;
+ }
+ String result = data.getResult();
+ result = result.replace("\\", "");
+ Map map = JSON.parseObject(result, Map.class);
+ result = (String) map.get("msg");
+ return FrameUtils.transformMessageToFrame(result);
+ }
+
+
+
+
/**
* 鍩虹閫氫俊鍚屾璋冪敤
@@ -134,36 +185,92 @@
return null;
}
- @Override
- public InvokeThingServiceResponse.Data invokeThing(String deviceName, InvokeParam param) {
+ /**
+ * 璋冪敤鑷畾涔塼opic
+ * https://help.aliyun.com/document_detail/69584.htm?spm=a2c4g.11186623.0.0.25b33982bSQSom#reference-snk-mrz-wdb
+ *
+ * @param deviceName 璁惧鍚�
+ * @param param 鍙傛暟
+ * @return 璁惧杩斿洖鐨勭粨鏋�
+ */
+ protected InvokeThingServiceResponse.Data invokeThing(String deviceName, InvokeParam param, boolean resendFlag) {
// 濉厖鏈嶅姟璋冪敤鐨勫弬鏁�
InvokeThingServiceRequest request = new InvokeThingServiceRequest();
// 璁惧璇佷功涔媝roductKey
request.setProductKey(getProductKey());
// 璁惧璇佷功涔媎eviceName
request.setDeviceName(deviceName);
+ request.setSysReadTimeout(5000);
// 瑕佽皟鐢ㄧ殑鏈嶅姟鏍囪瘑绗︼紝鍙栧喅浜庢湇鍔$瀹氫箟 鐩墠rrpc鏍囪瘑浠h〃鍚屾璋冪敤
request.setIdentifier("rrpc");
request.setArgs(JSON.toJSONString(param));
// 鑾峰緱鏈嶅姟璋冪敤鍝嶅簲
- InvokeThingServiceResponse response = null;
- try {
- response = getClient().getAcsResponse(request);
- } catch (ClientException e) {
- e.printStackTrace();
+ if (resendFlag) {
+ return getAcsResponse(request, true, 1);
+
+ } else {
+ return getAcsResponse(request, false, null);
+
}
- if (response == null) {
- throw new BusinessException("璋冪敤rrpc澶辫触");
+ }
+
+ /**
+ * 鑾峰緱鏈嶅姟璋冪敤鍝嶅簲
+ *
+ * @param request
+ * @return
+ */
+ private InvokeThingServiceResponse.Data getAcsResponse(InvokeThingServiceRequest request, boolean resendFlag, Integer time) {
+
+ if (resendFlag) {
+ //闇�瑕侀噸鍙�
+ if (time <= 2) {
+ InvokeThingServiceResponse response = null;
+ try {
+ response = getClient().getAcsResponse(request);
+ if (response == null) {
+ log.error("璋冪敤rrpc澶辫触锛屽搷搴斾负绌猴紒");
+ return getAcsResponse(request, true, time + 1);
+ }
+ if (!response.getSuccess()) {
+ log.error("璋冪敤rrpc澶辫触锛�" + response.getErrorMessage());
+ return getAcsResponse(request, true, time + 1);
+ } else {
+ // 鏈嶅姟璋冪敤鎴愬姛锛屼粎浠h〃鍙戦�佹湇鍔℃寚浠ょ殑鎴愬姛锛屼笉浠h〃鎵ц鏈嶅姟鏈韩鏄惁鎴愬姛
+ return response.getData();
+ }
+ } catch (Exception e) {
+ return getAcsResponse(request, true, time + 1);
+// throw new BusinessException("閿欒淇℃伅锛�" + e.getMessage());
+ }
+ } else {
+ throw new BusinessException("纭欢璇锋眰澶辫触锛岃妫�鏌ョ‖浠惰澶囷紒");
+ }
+
+ } else {
+ //涓嶉渶瑕侀噸鍙�
+ InvokeThingServiceResponse response = null;
+ try {
+ response = getClient().getAcsResponse(request);
+ } catch (ClientException e) {
+ e.printStackTrace();
+ throw new BusinessException("纭欢璇锋眰澶辫触锛岃妫�鏌ョ‖浠惰澶囷紒");
+ }
+ if (response == null) {
+ throw new BusinessException("璋冪敤rrpc澶辫触锛屽搷搴斾负绌猴紒");
+ }
+ if (!response.getSuccess()) {
+ throw new BusinessException("璋冪敤rrpc澶辫触锛�" + response.getErrorMessage());
+ }
+ // 鏈嶅姟璋冪敤鎴愬姛锛屼粎浠h〃鍙戦�佹湇鍔℃寚浠ょ殑鎴愬姛锛屼笉浠h〃鎵ц鏈嶅姟鏈韩鏄惁鎴愬姛
+ if (response.getSuccess()) {
+ // 浠呭悓姝ユ湇鍔℃湁result
+ return response.getData();
+ }
+ return null;
}
- if (!response.getSuccess()) {
- throw new BusinessException("璋冪敤rrpc澶辫触锛�" + response.getErrorMessage());
- }
- // 鏈嶅姟璋冪敤鎴愬姛锛屼粎浠h〃鍙戦�佹湇鍔℃寚浠ょ殑鎴愬姛锛屼笉浠h〃鎵ц鏈嶅姟鏈韩鏄惁鎴愬姛
- if (response.getSuccess()) {
- // 浠呭悓姝ユ湇鍔℃湁result
- return response.getData();
- }
- return null;
+
+
}
}
--
Gitblit v1.9.3