This commit is contained in:
Tony 2024-04-08 17:04:04 +08:00
commit 48c2c26aec
6 changed files with 190 additions and 68 deletions

View File

@ -84,7 +84,7 @@ public class IPCDataUploadSyncThread implements Runnable {
} }
msgMap.put("d", dataList); msgMap.put("d", dataList);
// 进行数据推送 // 进行数据推送
System.out.println(msgMap); System.out.println(msgMap.toString());
// System.out.println(changeMapToByte(msgMap)); // System.out.println(changeMapToByte(msgMap));
// System.out.println(host + "----------" + clientId+"----------"+topicu); // System.out.println(host + "----------" + clientId+"----------"+topicu);
// MqttClient sampleClient = new MqttClient(host, clientId, new MemoryPersistence()); // MqttClient sampleClient = new MqttClient(host, clientId, new MemoryPersistence());

View File

@ -106,7 +106,7 @@ public class IPCPlcDataSyncThread implements Runnable {
fields.put("insertTime", format); fields.put("insertTime", format);
InfluxDBService i = SpringUtils.getBean(InfluxDBService.class); 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); i.insert(IpcConstant.PLC_MEASUREMENT, tags, fields);
} }
// 批量保存报警数据 // 批量保存报警数据

View File

@ -33,33 +33,10 @@ public class IPCSensorDataSyncThread implements Runnable {
try { try {
// 获取service // 获取service
IIpcMonitorFieldService iIpcMonitorFieldService = SpringUtils.getBean(IIpcMonitorFieldService.class); IIpcMonitorFieldService iIpcMonitorFieldService = SpringUtils.getBean(IIpcMonitorFieldService.class);
IIpcAlarmRecordService iIpcAlarmRecordService = SpringUtils.getBean(IIpcAlarmRecordService.class); // 获取当前时间
IpcUtil ipcUtil = SpringUtils.getBean(IpcUtil.class); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
/* BatchRead<Integer> batch = new BatchRead<Integer>(); String format = simpleDateFormat.format(new Date());
// 温振数据
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<Integer> results = master.send(batch);*/
/**
* 温振数据
*/
List<IpcMonitorField> partList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.MONITOR_PART_KEY); List<IpcMonitorField> partList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.MONITOR_PART_KEY);
List<IpcMonitorField> sensorList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.SENSOR_MONITOR_PARAMS_KEY); List<IpcMonitorField> sensorList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.SENSOR_MONITOR_PARAMS_KEY);
List<IpcAlarmRecord> alarmRecordList = new ArrayList<>(); List<IpcAlarmRecord> alarmRecordList = new ArrayList<>();
@ -73,44 +50,10 @@ public class IPCSensorDataSyncThread implements Runnable {
// field.getSourceField() // field.getSourceField()
fields.put(field.getFieldValue(), Math.random()); 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); fields.put("insertTime", format);
InfluxDBService i = SpringUtils.getBean(InfluxDBService.class); 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); i.insert(IpcConstant.SENSOR_MEASUREMENT, tags, fields);
} }
// 批量保存报警数据
if(!alarmRecordList.isEmpty()){
iIpcAlarmRecordService.batchInsertIpcAlarmRecord(alarmRecordList);
}
} catch (Exception e) { } catch (Exception e) {
logger.info("数据处理失败,时间{}{}",new Date(), e.getMessage()); logger.info("数据处理失败,时间{}{}",new Date(), e.getMessage());
e.printStackTrace(); e.printStackTrace();

View File

@ -78,4 +78,8 @@ public class IpcConstant {
* 主动上报 flag 字段固定为rp * 主动上报 flag 字段固定为rp
*/ */
public static final String MQTT_FLAG = "rp"; public static final String MQTT_FLAG = "rp";
/**
* 数据分隔符
*/
public static final String DATA_SEPARATOR = ",";
} }

View File

@ -24,8 +24,6 @@ public class IpcUtil {
@Autowired @Autowired
private IIpcAlarmRulesConfigService iIpcAlarmRulesConfigService; private IIpcAlarmRulesConfigService iIpcAlarmRulesConfigService;
@Autowired
private IIpcAlarmRecordService iIpcAlarmRecordService;
/** /**
@ -76,21 +74,22 @@ public class IpcUtil {
/** /**
* 批量处理实时数据 * 批量处理实时数据
*/ */
public List<IpcAlarmRecord> dealRealTimeData(Map<String, Object> map, String tag, String type) { public List<IpcAlarmRecord> dealRealTimeData(Map<String, Object> map, Map<String, String> tags, String type) {
List<IpcAlarmRecord> returnList = new ArrayList<>(); List<IpcAlarmRecord> returnList = new ArrayList<>();
// 获取判断规则 // 获取判断规则
String tag = tags.get("part");
List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tag); List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tag);
for (IpcAlarmRulesConfig rulesConfig : rulesConfigList) { for (IpcAlarmRulesConfig rulesConfig : rulesConfigList) {
// 判断是否当前参数类型 // 判断是否当前参数类型
if (type.equals(rulesConfig.getType())) { if (type.equals(rulesConfig.getType())) {
if (judgeAlarm(map.get(rulesConfig.getNameKey()), rulesConfig.getReferenceValue1(), rulesConfig.getReferenceValue2(), rulesConfig.getReferenceCon())) { 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()); 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); returnList.add(ipcAlarmRecord);
} else { } else {
// 正常数据 // 正常数据
map.put("isAlarm", 0); tags.put("isAlarm", "0");
} }
} }
} }

View File

@ -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<IpcAlarmRecord> plcDataDeal(Map<String, Object> map, String tag, String type, List<IpcMonitorField> paramList) {
List<IpcAlarmRecord> returnList = new ArrayList<>();
// 获取判断规则
List<IpcAlarmRulesConfig> 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<IpcAlarmRecord> sensorDataDeal(Map<String, Object> map, String tag, String type, List<IpcMonitorField> paramList) {
List<IpcAlarmRecord> returnList = new ArrayList<>();
// 获取判断规则
List<IpcAlarmRulesConfig> 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<String> 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);
}
}
}
}