From b8ae2abd04473330c7331b364653edbfbd594179 Mon Sep 17 00:00:00 2001 From: zhanghan11 Date: Mon, 8 Apr 2024 16:45:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E8=AD=A6=E5=AD=97=E6=AE=B5=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=B0tags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modbus/IPCDataUploadSyncThread.java | 2 +- .../datasyn/modbus/IPCPlcDataSyncThread.java | 2 +- .../modbus/IPCSensorDataSyncThread.java | 65 +------ .../com/inspur/ipc/utils/IpcConstant.java | 4 + .../java/com/inspur/ipc/utils/IpcUtil.java | 9 +- .../java/com/inspur/ipc/utils/RuleUtils.java | 176 ++++++++++++++++++ 6 files changed, 190 insertions(+), 68 deletions(-) create mode 100644 tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/RuleUtils.java diff --git a/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCDataUploadSyncThread.java b/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCDataUploadSyncThread.java index a1ac760..94cdc81 100644 --- a/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCDataUploadSyncThread.java +++ b/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCDataUploadSyncThread.java @@ -84,7 +84,7 @@ public class IPCDataUploadSyncThread implements Runnable { } msgMap.put("d", dataList); // 进行数据推送 - System.out.println(msgMap); + System.out.println(msgMap.toString()); // System.out.println(changeMapToByte(msgMap)); // System.out.println(host + "----------" + clientId+"----------"+topicu); // MqttClient sampleClient = new MqttClient(host, clientId, new MemoryPersistence()); diff --git a/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCPlcDataSyncThread.java b/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCPlcDataSyncThread.java index a22a93f..bb46e5b 100644 --- a/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCPlcDataSyncThread.java +++ b/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCPlcDataSyncThread.java @@ -106,7 +106,7 @@ public class IPCPlcDataSyncThread implements Runnable { fields.put("insertTime", format); InfluxDBService i = SpringUtils.getBean(InfluxDBService.class); // 根据报警规则校验 - alarmRecordList.addAll(ipcUtil.dealRealTimeData(fields, tags.get("part"), CacheConstants.PLC_MONITOR_PARAMS_KEY)); + alarmRecordList.addAll(ipcUtil.dealRealTimeData(fields, tags, CacheConstants.PLC_MONITOR_PARAMS_KEY)); i.insert(IpcConstant.PLC_MEASUREMENT, tags, fields); } // 批量保存报警数据 diff --git a/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCSensorDataSyncThread.java b/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCSensorDataSyncThread.java index f0732d3..bf60fd3 100644 --- a/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCSensorDataSyncThread.java +++ b/tzipc-server/tzipc-datasyn/src/main/java/com/inspur/datasyn/modbus/IPCSensorDataSyncThread.java @@ -33,33 +33,10 @@ public class IPCSensorDataSyncThread implements Runnable { try { // 获取service IIpcMonitorFieldService iIpcMonitorFieldService = SpringUtils.getBean(IIpcMonitorFieldService.class); - IIpcAlarmRecordService iIpcAlarmRecordService = SpringUtils.getBean(IIpcAlarmRecordService.class); - IpcUtil ipcUtil = SpringUtils.getBean(IpcUtil.class); - /* BatchRead batch = new BatchRead(); - - batch.addLocator(0, BaseLocator.holdingRegister(1, 0, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 进油压力,float - batch.addLocator(1, BaseLocator.holdingRegister(1, 2, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 进油油液温度,float - batch.addLocator(2, BaseLocator.holdingRegister(1, 4, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 回油油液温度,float - batch.addLocator(3, BaseLocator.holdingRegister(1, 6, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 油水监测,float - batch.addLocator(4, BaseLocator.holdingRegister(1, 8, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 油液粘度,float - batch.addLocator(5, BaseLocator.holdingRegister(1, 10, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 力传感器,float - batch.addLocator(6, BaseLocator.holdingRegister(1, 12, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 速度传感器,float - batch.addLocator(7, BaseLocator.holdingRegister(1, 14, DataType.FOUR_BYTE_FLOAT_SWAPPED));// X1轴位置传感器一,float - batch.addLocator(8, BaseLocator.holdingRegister(1, 16, DataType.FOUR_BYTE_FLOAT_SWAPPED));// X2轴位置传感器二,float - batch.addLocator(9, BaseLocator.holdingRegister(1, 18, DataType.FOUR_BYTE_FLOAT_SWAPPED));// Y1轴位置传感器三,float - batch.addLocator(10, BaseLocator.holdingRegister(1, 20, DataType.FOUR_BYTE_FLOAT_SWAPPED));// Y2轴位置传感器四,float - batch.addLocator(11, BaseLocator.holdingRegister(1, 22, DataType.FOUR_BYTE_FLOAT_SWAPPED));// Z轴位置传感器五,float - batch.addLocator(12, BaseLocator.holdingRegister(1, 24, DataType.FOUR_BYTE_FLOAT_SWAPPED));// 油液密度,float - batch.addLocator(13, BaseLocator.holdingRegister(1, 26, DataType.TWO_BYTE_INT_UNSIGNED));// 颗粒度4um,int - batch.addLocator(14, BaseLocator.holdingRegister(1, 27, DataType.TWO_BYTE_INT_UNSIGNED));// 颗粒度6um,int - batch.addLocator(15, BaseLocator.holdingRegister(1, 28, DataType.TWO_BYTE_INT_UNSIGNED));// 颗粒度14um,int - batch.addLocator(16, BaseLocator.holdingRegister(1, 29, DataType.TWO_BYTE_INT_UNSIGNED));// 颗粒度21um,int - batch.setContiguousRequests(false); - BatchResults results = master.send(batch);*/ - - /** - * 温振数据 - */ + // 获取当前时间 + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); + String format = simpleDateFormat.format(new Date()); + // 温振数据 List partList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.MONITOR_PART_KEY); List sensorList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.SENSOR_MONITOR_PARAMS_KEY); List alarmRecordList = new ArrayList<>(); @@ -73,44 +50,10 @@ public class IPCSensorDataSyncThread implements Runnable { // field.getSourceField() fields.put(field.getFieldValue(), Math.random()); } -// //颗粒度4μm -// fields.put("pz4", (float) 0 + Math.random()); -// //颗粒度6μm -// fields.put("pz6", (float) 0 + Math.random()); -// //颗粒度14μm -// fields.put("pz14", (float) 0 + Math.random()); -// //颗粒度21μm -// fields.put("pz21", (float) 0 + Math.random()); -// //油水含量 -// fields.put("owc", (float) 0 + Math.random()); -// //油品粘度 -// fields.put("ov", (float) 0 + Math.random()); -// //回油温度 -// fields.put("rot", (float) 0 + Math.random()); -// //油液密度 -// fields.put("od", (float) 0 + Math.random()); -// //进油压力 -// fields.put("oip", (float) 0 + Math.random()); -// //轴套区温度 -// fields.put("ssat", (float) 0 + Math.random()); -// //振动x轴 -// fields.put("vx", (float) 0 + Math.random()); -// //振动y轴 -// fields.put("vy", (float) 0 + Math.random()); -// //振动z轴 -// fields.put("vz", (float) 0 + Math.random()); - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); - String format = simpleDateFormat.format(new Date()); fields.put("insertTime", format); InfluxDBService i = SpringUtils.getBean(InfluxDBService.class); - // 根据报警规则校验 - alarmRecordList.addAll(ipcUtil.dealRealTimeData(fields,tags.get("part"),CacheConstants.SENSOR_MONITOR_PARAMS_KEY)); i.insert(IpcConstant.SENSOR_MEASUREMENT, tags, fields); } - // 批量保存报警数据 - if(!alarmRecordList.isEmpty()){ - iIpcAlarmRecordService.batchInsertIpcAlarmRecord(alarmRecordList); - } } catch (Exception e) { logger.info("数据处理失败,时间{},{}",new Date(), e.getMessage()); e.printStackTrace(); diff --git a/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcConstant.java b/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcConstant.java index ddaa7ca..5a70426 100644 --- a/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcConstant.java +++ b/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcConstant.java @@ -78,4 +78,8 @@ public class IpcConstant { * 主动上报 flag 字段固定为“rp” */ public static final String MQTT_FLAG = "rp"; + /** + * 数据分隔符 + */ + public static final String DATA_SEPARATOR = ","; } diff --git a/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcUtil.java b/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcUtil.java index c89d6ea..04b640d 100644 --- a/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcUtil.java +++ b/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/IpcUtil.java @@ -24,8 +24,6 @@ public class IpcUtil { @Autowired private IIpcAlarmRulesConfigService iIpcAlarmRulesConfigService; - @Autowired - private IIpcAlarmRecordService iIpcAlarmRecordService; /** @@ -76,21 +74,22 @@ public class IpcUtil { /** * 批量处理实时数据 */ - public List dealRealTimeData(Map map, String tag, String type) { + public List dealRealTimeData(Map map, Map tags, String type) { List returnList = new ArrayList<>(); // 获取判断规则 + String tag = tags.get("part"); List rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tag); for (IpcAlarmRulesConfig rulesConfig : rulesConfigList) { // 判断是否当前参数类型 if (type.equals(rulesConfig.getType())) { if (judgeAlarm(map.get(rulesConfig.getNameKey()), rulesConfig.getReferenceValue1(), rulesConfig.getReferenceValue2(), rulesConfig.getReferenceCon())) { // 报警数据 - map.put("isAlarm", 1); + tags.put("isAlarm", "1"); IpcAlarmRecord ipcAlarmRecord = new IpcAlarmRecord(IdUtils.fastSimpleUUID(),rulesConfig.getPartKey(), rulesConfig.getPart(), rulesConfig.getNameKey(), rulesConfig.getName(), String.valueOf(map.get(rulesConfig.getNameKey())), rulesConfig.getUnit(), rulesConfig.getAlarmLevel(), rulesConfig.getReferenceName(), rulesConfig.getAlarmDetail(),rulesConfig.getId()); returnList.add(ipcAlarmRecord); } else { // 正常数据 - map.put("isAlarm", 0); + tags.put("isAlarm", "0"); } } } diff --git a/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/RuleUtils.java b/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/RuleUtils.java new file mode 100644 index 0000000..c9bab20 --- /dev/null +++ b/tzipc-server/tzipc-system/src/main/java/com/inspur/ipc/utils/RuleUtils.java @@ -0,0 +1,176 @@ +package com.inspur.ipc.utils; + +import com.inspur.common.utils.StringUtils; +import com.inspur.common.utils.uuid.IdUtils; +import com.inspur.ipc.domain.IpcAlarmRecord; +import com.inspur.ipc.domain.IpcAlarmRulesConfig; +import com.inspur.ipc.domain.IpcMonitorField; +import com.inspur.ipc.service.IIpcAlarmRulesConfigService; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.script.*; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class RuleUtils { + @Autowired + private IIpcAlarmRulesConfigService iIpcAlarmRulesConfigService; + + public static void main(String[] args) { + try { + getExtremeValue("","sss",new SimpleBindings(),new SimpleBindings()); + // 创建ScriptEngineManager和ScriptEngine实例 + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("JavaScript"); + // 创建绑定以存储变量值 + SimpleBindings bindings = new SimpleBindings(); + bindings.put("vx", 150); // 假设vx的值为150 + bindings.put("vu", null); // 假设vu的值为10 + bindings.put("vz", 1000.0); // 假设vu的值为10 + // 创建ScriptContext并设置绑定 + ScriptContext context = new SimpleScriptContext(); + context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); + // 要执行的表达式字符串 + String expression = "(100 > vx || 20 > vu && vz > 200 || vx > 10)"; + // 执行表达式并获取结果 + Boolean result = (Boolean) engine.eval(expression, context); + // 输出结果 + System.out.println("Result of expression: " + result); // 输出: true + } catch (ScriptException e) { + e.printStackTrace(); + } + } + + /** + * plc温压数据规则校验 + */ + public List plcDataDeal(Map map, String tag, String type, List paramList) { + List returnList = new ArrayList<>(); + // 获取判断规则 + List rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tag); + try { + // 创建ScriptEngineManager和ScriptEngine实例 + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("JavaScript"); + // 创建绑定以存储变量值 + SimpleBindings bindings = new SimpleBindings(); + for(IpcMonitorField ipcMonitorField:paramList){ + bindings.put(ipcMonitorField.getFieldValue(),map.get(ipcMonitorField.getFieldLabel())); + } + for (IpcAlarmRulesConfig rulesConfig : rulesConfigList) { + // 判断是否当前参数类型 + if (type.equals(rulesConfig.getType())) { + // 创建ScriptContext并设置绑定 + ScriptContext context = new SimpleScriptContext(); + context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); + // 要执行的表达式字符串 + // rulesConfig.getReferenceCon() + String expression = "(100 > vx || 20 > vu && vz > 200 || vx > 10)"; + // 执行表达式并获取结果 + Boolean result = (Boolean) engine.eval(expression, context); + if (result) { + // 报警数据 + map.put("isAlarm", 1); + IpcAlarmRecord ipcAlarmRecord = new IpcAlarmRecord(IdUtils.fastSimpleUUID(), rulesConfig.getPartKey(), rulesConfig.getPart(), rulesConfig.getNameKey(), rulesConfig.getName(), String.valueOf(map.get(rulesConfig.getNameKey())), rulesConfig.getUnit(), rulesConfig.getAlarmLevel(), rulesConfig.getReferenceName(), rulesConfig.getAlarmDetail(), rulesConfig.getId()); + returnList.add(ipcAlarmRecord); + } else { + // 正常数据 + map.put("isAlarm", 0); + } + } + } + } catch (ScriptException e) { + e.printStackTrace(); + } + return returnList; + } + + /** + * sensor温振数据规则校验 + */ + public List sensorDataDeal(Map map, String tag, String type, List paramList) { + List returnList = new ArrayList<>(); + // 获取判断规则 + List rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tag); + try { + // 创建ScriptEngineManager和ScriptEngine实例 + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("JavaScript"); + // 创建绑定以存储变量值 + SimpleBindings maxBindings = new SimpleBindings(); + SimpleBindings minBindings = new SimpleBindings(); + for(IpcMonitorField ipcMonitorField:paramList){ + getExtremeValue(map.get(ipcMonitorField.getFieldLabel()),ipcMonitorField.getFieldValue(),maxBindings,minBindings); + } + for (IpcAlarmRulesConfig rulesConfig : rulesConfigList) { + try { + // 判断是否当前参数类型 + if (type.equals(rulesConfig.getType())) { + // 创建ScriptContext并设置绑定 + ScriptContext contextMax = new SimpleScriptContext(); + contextMax.setBindings(maxBindings, ScriptContext.ENGINE_SCOPE); + ScriptContext contextMin = new SimpleScriptContext(); + contextMin.setBindings(minBindings, ScriptContext.ENGINE_SCOPE); + // 要执行的表达式字符串 + // rulesConfig.getReferenceCon() + String expression = "(100 > vx || 20 > vu && vz > 200 || vx > 10)"; + // 执行表达式并获取结果 + Boolean resultMax = (Boolean) engine.eval(expression, contextMax); + Boolean resultMin = (Boolean) engine.eval(expression, contextMin); + // 最大值或最小值超出范围即报警 + if (resultMax || resultMin) { + // 报警数据 + map.put("isAlarm", 1); + // 记录报警日志 + IpcAlarmRecord ipcAlarmRecord = new IpcAlarmRecord(IdUtils.fastSimpleUUID(), rulesConfig.getPartKey(), rulesConfig.getPart(), rulesConfig.getNameKey(), rulesConfig.getName(), String.valueOf(map.get(rulesConfig.getNameKey())), rulesConfig.getUnit(), rulesConfig.getAlarmLevel(), rulesConfig.getReferenceName(), rulesConfig.getAlarmDetail(), rulesConfig.getId()); + returnList.add(ipcAlarmRecord); + } else { + // 正常数据 + map.put("isAlarm", 0); + } + } + }catch (Exception e){ + // 报警数据 + map.put("isAlarm", 1); + // 记录报警日志 + IpcAlarmRecord ipcAlarmRecord = new IpcAlarmRecord(IdUtils.fastSimpleUUID(), rulesConfig.getPartKey(), rulesConfig.getPart(), rulesConfig.getNameKey(), rulesConfig.getName(), String.valueOf(map.get(rulesConfig.getNameKey())), rulesConfig.getUnit(), rulesConfig.getAlarmLevel(), rulesConfig.getReferenceName(), rulesConfig.getAlarmDetail(), rulesConfig.getId()); + returnList.add(ipcAlarmRecord); + e.printStackTrace(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return returnList; + } + + /** + * 获取最大最小值 极值 + */ + public static void getExtremeValue(Object data,String paramName,SimpleBindings maxBindings,SimpleBindings minBindings){ + if(StringUtils.isNull(data)) { + String[] array = String.valueOf(data).split(IpcConstant.DATA_SEPARATOR); + List list = Arrays.asList(array).stream() + .filter(s -> s != null && !s.trim().isEmpty()) + .collect(Collectors.toList()); + if(!list.isEmpty()) { + BigDecimal max = new BigDecimal(array[0]); + BigDecimal min = new BigDecimal(array[0]); + for (int i = 1; i < array.length; i++) { + if (new BigDecimal(array[i]).compareTo(max) > 0) { + max = new BigDecimal(array[i]); + } + if (new BigDecimal(array[i]).compareTo(min) < 0) { + min = new BigDecimal(array[i]); + } + } + maxBindings.put(paramName, max); + minBindings.put(paramName, min); + } + } + } +}