From 560303799978bd141dc1e9553b7607012591fb42 Mon Sep 17 00:00:00 2001
From: 严智鑫 <test>
Date: 星期二, 17 六月 2025 17:02:53 +0800
Subject: [PATCH] 1.json文件优化 增加配置项:【1.触发点;2.逻辑线程时间间隔】 2.优化处理代码 【1.去除多余日志输出点;2.两个设备json文件进行根据codeId进行关联;3.调整代码先后顺序】 3.json文件路径提出jar包外,可在设备表上配置上配置路径。因原方式修改地址json 后需重新打包,实际调试中传递jar包比较浪费时间
---
ShangHaiMesParent/moduleService/plcConnectModule/src/main/java/com/mes/connect/IndustrialInterface/Api.java | 142 ++++++++++++++++++++++++++++++++--------------
1 files changed, 98 insertions(+), 44 deletions(-)
diff --git a/ShangHaiMesParent/moduleService/plcConnectModule/src/main/java/com/mes/connect/IndustrialInterface/Api.java b/ShangHaiMesParent/moduleService/plcConnectModule/src/main/java/com/mes/connect/IndustrialInterface/Api.java
index 609a592..61a9109 100644
--- a/ShangHaiMesParent/moduleService/plcConnectModule/src/main/java/com/mes/connect/IndustrialInterface/Api.java
+++ b/ShangHaiMesParent/moduleService/plcConnectModule/src/main/java/com/mes/connect/IndustrialInterface/Api.java
@@ -2,6 +2,11 @@
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.mes.connect.entity.ApiConfig;
+import com.mes.connect.entity.LogicItem;
+import com.mes.connect.entity.PlcParameters;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -19,7 +24,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
+@Slf4j
@Service
public class Api implements ApiService {
@@ -37,6 +44,47 @@
this.restTemplate = restTemplate;
this.jdbcTemplate = jdbcTemplate;
}
+
+ /**
+ * 鍙戦�佽皟鐢ㄦ帴鍙h姹� 锛屾牴鎹�昏緫閰嶇疆璋冪敤
+ *
+ * @param apiConfig 閫昏緫閰嶇疆鍙傛暟
+ * @param plcParameters plc鍙傛暟
+ * @return 鍝嶅簲鍐呭鎸夎鍒嗗壊鐨勬暟缁�
+ */
+ public List<String> callApi(ApiConfig apiConfig, PlcParameters plcParameters){
+ try{
+ List<String> result=new ArrayList<>();
+ String connectType=apiConfig.getType();
+ String connectAddress=apiConfig.getAddress();
+ Map<String,Object> map=new HashMap<String,Object>();
+ map.put("apiConfig",apiConfig);
+ map.put("plcParameter",plcParameters);
+ switch (connectType) {
+ case "Http":
+ result= this.httpApi(connectAddress,map);
+ break;
+ case "View": // 瑙嗗浘/琛�
+ result= this.viewApi(connectAddress,map);
+ break;
+ case "Procedure": // 瀛樺偍杩囩▼
+ result= this.procedureAPI(connectAddress,map);
+ break;
+ default:
+ log.warn("涓嶆敮鎸佺殑杩炴帴绫诲瀷: {}", connectType);
+ return null; // 涓嶆敮鎸佺殑鏂瑰紡
+ }
+ return result;
+ }catch (Exception e){
+ log.error("璋冪敤鎺ュ彛澶辫触: {}", e.getMessage(), e);
+ }
+ return null;
+ }
+
+
+
+
+
/**
* 鍙戦�丠TTP璇锋眰锛屾敮鎸丟ET鍜孭OST鏂规硶
*
@@ -52,8 +100,11 @@
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
// 澶勭悊鍝嶅簲
String responseBody;
- String method=data.get("method").toString();
- data.remove("method");
+ ObjectMapper mapper = new ObjectMapper();
+ Map<String, Object> apiConfig= mapper.convertValue(data.get("apiConfig"), Map.class);
+ Map<String, Object> parameters= mapper.convertValue(apiConfig.get("parameters"), Map.class);
+ String method=parameters.get("method").toString();
+ data.remove("parameters");
if ("GET".equals(method)) {
// GET璇锋眰锛氬皢鍙傛暟娣诲姞鍒癠RL鏌ヨ鍙傛暟涓�
if (data != null) {
@@ -97,12 +148,14 @@
}
@DS("mes_machine")
@Override
- public List<String> viewApi(String viewName, Map<String, Object> params) {
+ public List<String> viewApi(String viewName, Map<String, Object> parameters) {
// 楠岃瘉瑙嗗浘鍚嶆槸鍚﹀悎娉曪紝闃叉SQL娉ㄥ叆
if (!isValidViewName(viewName)) {
throw new IllegalArgumentException("鏃犳晥鐨勮鍥惧悕绉�");
}
-
+ ObjectMapper mapper = new ObjectMapper();
+ Map<String, Object> apiConfig= mapper.convertValue(parameters.get("apiConfig"), Map.class);
+ Map<String, Object> params= mapper.convertValue(apiConfig.get("parameters"), Map.class);
// 浣跨敤棰勭紪璇戣鍙ユ瀯寤烘煡璇�
StringBuilder sql = new StringBuilder("SELECT * FROM " + viewName);
MapSqlParameterSource paramSource = new MapSqlParameterSource();
@@ -130,66 +183,67 @@
}
@DS("jiumumes")
@Override
- public List<String> procedureAPI(String procedureName, Map<String, Object> params,Map<String, Object> outParams) {
+ public List<String> procedureAPI(String procedureName, Map<String, Object> params) {
try {
if (!isValidProcedureName(procedureName)) {
throw new IllegalArgumentException("鏃犳晥鐨勫瓨鍌ㄨ繃绋嬪悕绉�");
}
-
+ ObjectMapper mapper = new ObjectMapper();
+ Map<String, Object> apiConfig= mapper.convertValue(params.get("apiConfig"), Map.class);
+ Map<String, Object> parameters=mapper.convertValue(apiConfig.get("parameters"), Map.class);
+ Map<String, Object> inParams= mapper.convertValue(parameters.get("inParams"), Map.class);
+ Map<String, Object> outParams= mapper.convertValue(parameters.get("outParams"), Map.class);
+ // 鍒涘缓鏂扮殑 Map 骞跺悎骞�
+ Map<String, Object> mergedMap = new HashMap<>(inParams);
+ mergedMap.putAll(outParams);
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName(procedureName)
.withoutProcedureColumnMetaDataAccess();
-
- if (params != null) {
- for (Map.Entry<String, Object> entry : params.entrySet()) {
- // 纭畾鍙傛暟绫诲瀷
- int sqlType = getSqlType(entry.getValue());
-
- // 妫�鏌ユ槸鍚︿负杈撳嚭鍙傛暟
- if (outParams != null && outParams.containsKey(entry.getKey())) {
- Object outParamInfo = outParams.get(entry.getKey());
- int outSqlType;
-
- // 浠庤緭鍑哄弬鏁颁俊鎭腑鑾峰彇SQL绫诲瀷
- if (outParamInfo instanceof Integer) {
- outSqlType = (Integer) outParamInfo;
- } else if (outParamInfo instanceof Map) {
- // 鍋囪Map涓寘鍚�"sqlType"閿�
- Map<String, Object> outParamMap = (Map<String, Object>) outParamInfo;
- outSqlType = (Integer) outParamMap.getOrDefault("sqlType", sqlType);
- } else {
- // 榛樿浣跨敤杈撳叆鍙傛暟鐨凷QL绫诲瀷
- outSqlType = sqlType;
- }
-
- // 浣跨敤鎸囧畾鐨凷QL绫诲瀷浣滀负杈撳嚭鍙傛暟
- jdbcCall.declareParameters(
- new SqlOutParameter(entry.getKey(), outSqlType)
- );
- } else {
- // 浣滀负杈撳叆鍙傛暟
- jdbcCall.declareParameters(
- new SqlParameter(entry.getKey(), sqlType)
- );
- }
- }
+ for (Map.Entry<String, Object> entry : inParams.entrySet()) {
+ int sqlType = getSqlType(entry.getValue());
+ // 浣滀负杈撳叆鍙傛暟
+ jdbcCall.declareParameters(
+ new SqlParameter(entry.getKey(), sqlType)
+ );
}
+ for (Map.Entry<String, Object> entry : outParams.entrySet()) {
+ int outSqlType=12;
+// int sqlType = getSqlType(entry.getValue());
+// Object outParamInfo = outParams.get(entry.getKey());
+// // 浠庤緭鍑哄弬鏁颁俊鎭腑鑾峰彇SQL绫诲瀷
+// if (outParamInfo instanceof Integer) {
+// outSqlType = (Integer) outParamInfo;
+// } else if (outParamInfo instanceof Map) {
+// // 鍋囪Map涓寘鍚�"sqlType"閿�
+// Map<String, Object> outParamMap = (Map<String, Object>) outParamInfo;
+// outSqlType = (Integer) outParamMap.getOrDefault("sqlType", sqlType);
+// } else {
+// // 榛樿浣跨敤杈撳叆鍙傛暟鐨凷QL绫诲瀷
+// outSqlType = sqlType;
+// }
+ // 浣跨敤鎸囧畾鐨凷QL绫诲瀷浣滀负杈撳嚭鍙傛暟
+ jdbcCall.declareParameters(
+ new SqlOutParameter(entry.getKey(), outSqlType)
+ );
+ }
// 鎵ц瀛樺偍杩囩▼骞惰幏鍙栫粨鏋�
- Map<String, Object> result = jdbcCall.execute(params);
+ Map<String, Object> result = jdbcCall.execute(mergedMap);
// 澶勭悊杈撳嚭鍙傛暟
if (outParams != null) {
for (String paramName : outParams.keySet()) {
if (result.containsKey(paramName)) {
// 灏嗚緭鍑哄弬鏁扮殑鍊兼斁鍥炲師鍙傛暟Map涓�
- params.put(paramName, result.get(paramName));
+ outParams.put(paramName, result.get(paramName));
}
}
}
-
+ List<String> outParamsValues = outParams.values().stream()
+ .map(value -> value != null ? value.toString() : "null")
+ .collect(Collectors.toList());
// 杩斿洖缁撴灉淇℃伅
- return null;
+ return outParamsValues;
} catch (Exception e) {
return null;
}
--
Gitblit v1.8.0