|
@@ -0,0 +1,290 @@
|
|
|
|
|
+package com.ufo.project.gatewayapp.util;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.expression.ExpressionParser;
|
|
|
|
|
+import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
|
|
|
+import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
|
|
|
+
|
|
|
|
|
+import com.ufo.base.common.exception.ServiceException;
|
|
|
|
|
+import com.ufo.base.common.utils.StringUtils;
|
|
|
|
|
+import com.ufo.base.common.utils.spring.SpringUtils;
|
|
|
|
|
+import com.ufo.module.system.domain.SysDictData;
|
|
|
|
|
+import com.ufo.module.system.service.ISysDictTypeService;
|
|
|
|
|
+import com.ufo.project.gatewayapp.domain.ChannelStatus;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.http.Header;
|
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 网关服务(GatewayApp)调用工具类
|
|
|
|
|
+ *
|
|
|
|
|
+ * 读取字典 gateway_app_config 中的JSON配置, 调用网关服务提供的通道状态查询/通道操作接口.
|
|
|
|
|
+ *
|
|
|
|
|
+ * 配置示例:
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * {
|
|
|
|
|
+ * "baseUrl": "http://xxx", // 网关服务url前缀
|
|
|
|
|
+ * "pointStateUrl": "/biz/point-state/real", // 状态查询接口
|
|
|
|
|
+ * "channelStateExpressions": "#iValue != null and #iValue == 1", // 通道状态判断表达式(SpEL, 变量 #iValue)
|
|
|
|
|
+ * "channelOperationUrl": "/biz/channel/control" // 通道操作接口
|
|
|
|
|
+ * }
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author ufo
|
|
|
|
|
+ * @date 2026-09-18
|
|
|
|
|
+ */
|
|
|
|
|
+public final class GatewayAppUtils
|
|
|
|
|
+{
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(GatewayAppUtils.class);
|
|
|
|
|
+
|
|
|
|
|
+ /** 网关服务配置项字典类型 */
|
|
|
|
|
+ public static final String DICT_TYPE_GATEWAY_APP_CONFIG = "gateway_app_config";
|
|
|
|
|
+
|
|
|
|
|
+ /** 通道操作命令: 停止 */
|
|
|
|
|
+ public static final int COMMAND_STOP = 0;
|
|
|
|
|
+
|
|
|
|
|
+ /** 通道操作命令: 启动和重启 */
|
|
|
|
|
+ public static final int COMMAND_START = 1;
|
|
|
|
|
+
|
|
|
|
|
+ /** 状态查询测点类型(通道) */
|
|
|
|
|
+ private static final String POINT_TYPE_CHANNEL = "CH";
|
|
|
|
|
+
|
|
|
|
|
+ /** 状态查询设备ID固定值 */
|
|
|
|
|
+ private static final long QUERY_DEVICE_ID = -1L;
|
|
|
|
|
+
|
|
|
|
|
+ /** 请求超时时间(毫秒) */
|
|
|
|
|
+ private static final int REQUEST_TIMEOUT_MILLIS = 10000;
|
|
|
|
|
+
|
|
|
|
|
+ /** 通道状态判断默认表达式 */
|
|
|
|
|
+ private static final String DEFAULT_STATE_EXPRESSION = "#iValue != null and #iValue == 1";
|
|
|
|
|
+
|
|
|
|
|
+ /** SpEL 解析器 */
|
|
|
|
|
+ private static final ExpressionParser SPEL_PARSER = new SpelExpressionParser();
|
|
|
|
|
+
|
|
|
|
|
+ private GatewayAppUtils()
|
|
|
|
|
+ {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量查询通道运行状态
|
|
|
|
|
+ *
|
|
|
|
|
+ * 调用网关状态查询接口, 逐条按配置表达式判断 item.iValue:
|
|
|
|
|
+ * true -> 运行中(1), false -> 未运行(0); 调用失败时全部返回未知(-1).
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param channelIds 通道ID列表
|
|
|
|
|
+ * @return 通道状态列表
|
|
|
|
|
+ */
|
|
|
|
|
+ public static List<ChannelStatus> queryChannelStatus(List<Long> channelIds)
|
|
|
|
|
+ {
|
|
|
|
|
+ List<ChannelStatus> statusList = new ArrayList<>();
|
|
|
|
|
+ if (channelIds == null || channelIds.isEmpty())
|
|
|
|
|
+ {
|
|
|
|
|
+ return statusList;
|
|
|
|
|
+ }
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ GatewayAppConfig config = loadConfig();
|
|
|
|
|
+ List<Map<String, Object>> body = new ArrayList<>();
|
|
|
|
|
+ for (Long channelId : channelIds)
|
|
|
|
|
+ {
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("channelId", channelId);
|
|
|
|
|
+ item.put("deviceId", QUERY_DEVICE_ID);
|
|
|
|
|
+ item.put("pointType", POINT_TYPE_CHANNEL);
|
|
|
|
|
+ body.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("[GatewayAppUtils] 查询通道状态, baseUrl={}, pointStateUrl={}, body={}",
|
|
|
|
|
+ config.getBaseUrl(), config.getPointStateUrl(), JSON.toJSONString(body));
|
|
|
|
|
+ String responseText = sendRequest(config.getBaseUrl(), config.getPointStateUrl(), JSON.toJSONString(body));
|
|
|
|
|
+ log.info("[GatewayAppUtils] 查询通道状态, responseText={}", responseText);
|
|
|
|
|
+ JSONArray array = JSON.parseArray(responseText);
|
|
|
|
|
+ if (array == null)
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new ServiceException("网关服务状态查询返回数据为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (int i = 0; i < array.size(); i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ JSONObject item = array.getJSONObject(i);
|
|
|
|
|
+ Integer ivalue = item.containsKey("ivalue") ? item.getInteger("ivalue") : null;
|
|
|
|
|
+ int state = matchState(config.getChannelStateExpressions(), ivalue)
|
|
|
|
|
+ ? ChannelStatus.STATE_RUNNING : ChannelStatus.STATE_STOPPED;
|
|
|
|
|
+ statusList.add(new ChannelStatus(item.getLong("channelId"), state));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("[GatewayAppUtils] 查询通道状态失败, channelIds={}", channelIds, e);
|
|
|
|
|
+ statusList.clear();
|
|
|
|
|
+ for (Long channelId : channelIds)
|
|
|
|
|
+ {
|
|
|
|
|
+ statusList.add(new ChannelStatus(channelId, ChannelStatus.STATE_UNKNOWN));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return statusList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通道操作(停止/启动和重启)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param channelId 通道ID
|
|
|
|
|
+ * @param command 操作命令 0:停止, 1:启动和重启
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void operationChannel(Long channelId, int command)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ GatewayAppConfig config = loadConfig();
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("id", channelId);
|
|
|
|
|
+ item.put("command", command);
|
|
|
|
|
+ log.info("[GatewayAppUtils] 通道操作, baseUrl={}, channelOperationUrl={}, body={}",
|
|
|
|
|
+ config.getBaseUrl(), config.getChannelOperationUrl(), JSON.toJSONString(item));
|
|
|
|
|
+ String responseText = sendRequest(config.getBaseUrl(), config.getChannelOperationUrl(), JSON.toJSONString(item));
|
|
|
|
|
+ log.info("[GatewayAppUtils] 通道操作, responseText={}", responseText);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("[GatewayAppUtils] 通道操作失败, channelId={}, command={}", channelId, command, e);
|
|
|
|
|
+ throw new ServiceException("调用网关远程服务失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * POST JSON 请求网关服务, 返回响应体 data (字符串)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param baseUrl 网关服务url前缀
|
|
|
|
|
+ * @param apiUrl 接口路径
|
|
|
|
|
+ * @param bodyStr 请求体JSON字符串
|
|
|
|
|
+ * @return response.data (JSON字符串)
|
|
|
|
|
+ */
|
|
|
|
|
+ private static String sendRequest(String baseUrl, String apiUrl, String bodyStr)
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = baseUrl + apiUrl;
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
|
|
+ .header(Header.CONTENT_TYPE, "application/json; charset=UTF-8")
|
|
|
|
|
+ .body(bodyStr)
|
|
|
|
|
+ .timeout(REQUEST_TIMEOUT_MILLIS)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ if (response.getStatus() != 200)
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new ServiceException("网关服务HTTP状态异常: " + response.getStatus() + ", url=" + url);
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject json = JSON.parseObject(response.body());
|
|
|
|
|
+ if (json == null)
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new ServiceException("网关服务响应解析失败, url=" + url);
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer code = json.getInteger("code");
|
|
|
|
|
+ if (code == null || code != 200)
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new ServiceException("网关服务返回异常: " + json.getString("msg") + ", url=" + url);
|
|
|
|
|
+ }
|
|
|
|
|
+ Object data = json.get("data");
|
|
|
|
|
+ return data == null ? null : data.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 按配置的SpEL表达式判断测点值是否代表运行中, 表达式变量 #iValue 为测点当前值
|
|
|
|
|
+ */
|
|
|
|
|
+ private static boolean matchState(String expression, Integer iValue)
|
|
|
|
|
+ {
|
|
|
|
|
+ String expr = StringUtils.isBlank(expression) ? DEFAULT_STATE_EXPRESSION : expression;
|
|
|
|
|
+ StandardEvaluationContext context = new StandardEvaluationContext();
|
|
|
|
|
+ context.setVariable("ivalue", iValue);
|
|
|
|
|
+ Boolean hit = SPEL_PARSER.parseExpression(expr).getValue(context, Boolean.class);
|
|
|
|
|
+ log.debug("[GatewayAppUtils] 判断通道状态, expression={}, ivalue={}, result={}", expr, iValue, hit);
|
|
|
|
|
+ return Boolean.TRUE.equals(hit);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 读取字典 gateway_app_config 并解析为配置对象
|
|
|
|
|
+ */
|
|
|
|
|
+ private static GatewayAppConfig loadConfig()
|
|
|
|
|
+ {
|
|
|
|
|
+ List<SysDictData> dictList = SpringUtils.getBean(ISysDictTypeService.class)
|
|
|
|
|
+ .selectDictDataByTypeWithoutCache(DICT_TYPE_GATEWAY_APP_CONFIG);
|
|
|
|
|
+ if (dictList == null || dictList.isEmpty())
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new ServiceException("未配置字典项: " + DICT_TYPE_GATEWAY_APP_CONFIG);
|
|
|
|
|
+ }
|
|
|
|
|
+ String dictValue = dictList.get(0).getDictValue();
|
|
|
|
|
+ if (StringUtils.isBlank(dictValue))
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new ServiceException("字典项值为空: " + DICT_TYPE_GATEWAY_APP_CONFIG);
|
|
|
|
|
+ }
|
|
|
|
|
+ GatewayAppConfig config = JSON.parseObject(dictValue, GatewayAppConfig.class);
|
|
|
|
|
+ if (config == null || StringUtils.isBlank(config.getBaseUrl()))
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new ServiceException("字典项缺少baseUrl配置: " + DICT_TYPE_GATEWAY_APP_CONFIG);
|
|
|
|
|
+ }
|
|
|
|
|
+ return config;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 字典 gateway_app_config 的网关服务配置
|
|
|
|
|
+ */
|
|
|
|
|
+ public static class GatewayAppConfig
|
|
|
|
|
+ {
|
|
|
|
|
+ /** 网关服务url前缀 */
|
|
|
|
|
+ private String baseUrl;
|
|
|
|
|
+
|
|
|
|
|
+ /** 状态查询接口 */
|
|
|
|
|
+ private String pointStateUrl;
|
|
|
|
|
+
|
|
|
|
|
+ /** 通道状态判断表达式(SpEL) */
|
|
|
|
|
+ private String channelStateExpressions;
|
|
|
|
|
+
|
|
|
|
|
+ /** 通道操作接口 */
|
|
|
|
|
+ private String channelOperationUrl;
|
|
|
|
|
+
|
|
|
|
|
+ public String getBaseUrl()
|
|
|
|
|
+ {
|
|
|
|
|
+ return baseUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setBaseUrl(String baseUrl)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.baseUrl = baseUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String getPointStateUrl()
|
|
|
|
|
+ {
|
|
|
|
|
+ return pointStateUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setPointStateUrl(String pointStateUrl)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.pointStateUrl = pointStateUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String getChannelStateExpressions()
|
|
|
|
|
+ {
|
|
|
|
|
+ return channelStateExpressions;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setChannelStateExpressions(String channelStateExpressions)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.channelStateExpressions = channelStateExpressions;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String getChannelOperationUrl()
|
|
|
|
|
+ {
|
|
|
|
|
+ return channelOperationUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setChannelOperationUrl(String channelOperationUrl)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.channelOperationUrl = channelOperationUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|