diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java index ede97b4..dbcca22 100644 --- a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/controller/HwMonitorPlatformController.java @@ -484,8 +484,15 @@ public class HwMonitorPlatformController extends BaseController { * */ @PostMapping("/selectDeviceByName") public AjaxResult selectDeviceByName(@RequestBody Map map){ - List deviceList = hwDeviceService.selectMonitorUnitAndDeviceByName(map); - return success(deviceList); + + try { + List deviceList = hwDeviceService.selectMonitorUnitAndDeviceByName(map); + return success(deviceList); + }catch (Exception ex){ + return error(String.format("获取设备信息异常:%e",ex.getMessage())); + } + + } diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java index bdf0fbb..f7aaafe 100644 --- a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/service/impl/DataProcessServiceImpl.java @@ -27,10 +27,12 @@ import com.ruoyi.dataprocess.service.IDataProcessService; import com.ruoyi.tdengine.api.RemoteTdEngineService; import com.ruoyi.tdengine.api.domain.TdField; import com.ruoyi.tdengine.api.domain.TdHistorySelectDto; +import com.ruoyi.tdengine.api.domain.TdReturnDataVo; import com.ruoyi.tdengine.api.domain.TdTableVo; import com.sun.org.apache.xpath.internal.operations.Bool; import darabonba.core.client.ClientOverrideConfiguration; import org.apache.commons.codec.binary.Base64; +import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -44,13 +46,11 @@ import javax.script.ScriptException; import java.io.IOException; import java.math.BigDecimal; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; /** * @Description: 数据处理业务类 @@ -220,7 +220,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData final R insertResult = this.remoteTdEngineService.insertTable(tdTableVo , SecurityConstants.INNER); if (insertResult.getCode() == ResultEnums.SUCCESS.getCode()) { - logger.info("Insert data result: {}", insertResult.getMsg()); + logger.info("Insert data result: {}", insertResult.getCode()); } else { logger.error("Insert data Exception: {},data:{}", insertResult.getMsg(), jsonData); } @@ -242,6 +242,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData return paramArr.size(); } + private static String getImageFileName(String imagePath, String imageDomain, String imagePrefix, String valueStr, Long deviceId, String extension) { try { String imageFileName = ImageUtils.convertBase64ToImage(imagePath, @@ -363,87 +364,147 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData */ private void checkAlarm(Long deviceId, List alarmFields, Long tenantId, Long sceneId, Long monitorUnitId, String topic, String subDeviceCode) { - HwAlarmRule queryAlarmRule = new HwAlarmRule(); - queryAlarmRule.setRuleDeviceId(deviceId); - queryAlarmRule.setRuleType(HwDictConstants.ALARM_RULE_RULE_TYPE_DEVICE); - queryAlarmRule.setAlarmRuleStatus(HwDictConstants.ALARM_RULE_STATUS_ENABLE); - List alarmRules = hwAlarmRuleMapper.selectHwAlarmRulesWithLink(queryAlarmRule); - - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine engine = manager.getEngineByName("js"); - - if (alarmRules != null) { - Date currentDate = new Date(); - for (HwAlarmRule alarmRule : alarmRules) { - String triggerExpression = alarmRule.getTriggerExpression() - .replaceAll("and", "&&") - .replaceAll("or", "||"); - AtomicBoolean isAlarmed = new AtomicBoolean(false); - List alarmDetails = new ArrayList(); - - for (TdField alarmField : alarmFields) { - String fieldName = alarmField.getFieldName(); - Object filedValue = alarmField.getFieldValue(); - if (triggerExpression.contains("{" + fieldName + "}")) { - isAlarmed.set(true); - triggerExpression = triggerExpression.replaceAll("\\{" + fieldName + "\\}", String.valueOf(filedValue)); - HwAlarmDetail alarmDetail = new HwAlarmDetail(); - alarmDetail.setDeviceId(deviceId); - alarmDetail.setFunctionIdentifier(fieldName); - alarmDetail.setFunctionValue(String.valueOf(filedValue)); - alarmDetail.setMonitorTime(currentDate); - alarmDetails.add(alarmDetail); - } - } - if (isAlarmed.get() && !triggerExpression.contains("{") - && RegexUtils.findSymbolInText(triggerExpression).size() > 0 - && RegexUtils.findNumberInText(triggerExpression).size() > 0) { + try { + HwAlarmRule queryAlarmRule = new HwAlarmRule(); + queryAlarmRule.setRuleDeviceId(deviceId); + queryAlarmRule.setRuleType(HwDictConstants.ALARM_RULE_RULE_TYPE_DEVICE); + queryAlarmRule.setAlarmRuleStatus(HwDictConstants.ALARM_RULE_STATUS_ENABLE); + List alarmRules = hwAlarmRuleMapper.selectHwAlarmRulesWithLink(queryAlarmRule); + + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("js"); + + if (alarmRules != null) { + Date currentDate = new Date(); + for (HwAlarmRule alarmRule : alarmRules) { + String triggerExpression = alarmRule.getTriggerExpression() + .replaceAll("and", "&&") + .replaceAll("or", "||"); + AtomicBoolean isAlarmed = new AtomicBoolean(false); + List alarmDetails = new ArrayList(); + + for (TdField alarmField : alarmFields) { + String fieldName = alarmField.getFieldName(); + Object filedValue = alarmField.getFieldValue(); + if (triggerExpression.contains("{" + fieldName + "}")) { + isAlarmed.set(true); - Boolean triggerExpressionBool = null; - try { - triggerExpressionBool = (Boolean) engine.eval(triggerExpression); - } catch (ScriptException e) { - throw new RuntimeException(e); - } - if (triggerExpressionBool) { - HwAlarmInfo alarmInfo = new HwAlarmInfo(); - alarmInfo.setAlarmInfoType(HwDictConstants.ALARM_INFO_TYPE_DEVICE); - alarmInfo.setAlarmReleatedId(alarmRule.getAlarmRuleId()); - alarmInfo.setDeviceId(deviceId); - alarmInfo.setMonitorUnitId(monitorUnitId); - alarmInfo.setTenantId(tenantId); - alarmInfo.setSceneId(sceneId); - alarmInfo.setAlarmLevelId(alarmRule.getAlarmLevelId()); - alarmInfo.setAlarmTypeId(alarmRule.getAlarmTypeId()); - alarmInfo.setHwAlarmDetailList(alarmDetails); - alarmInfo.setHandleStatus(HwDictConstants.ALARM_HANDLE_STATUS_NO); - alarmInfo.setCreateTime(currentDate); - alarmInfo.setAlarmTime(currentDate); - hwAlarmInfoMapper.insertHwAlarmInfo(alarmInfo); - this.insertHwAlarmDetail(alarmInfo); - this.handleAlarmLink(alarmRule, topic, subDeviceCode); - - try { - HwDevice hwDevice = hwDeviceMapper.GetDeviceById(deviceId); - if(hwDevice != null){ - AlarmMsgTemplateParam alarmMsgTemplateParam = new AlarmMsgTemplateParam(); - alarmMsgTemplateParam.setWarning(alarmRule.getAlarmRuleName()); - alarmMsgTemplateParam.setParentname(hwDevice.getMonitorUnitName()); - alarmMsgTemplateParam.setSensorID(deviceId.toString()); - SendAlarmInfoToSms(alarmRule.getPhoneNumbers(),alarmMsgTemplateParam); + /** + * Add By 获取五分钟内的数据 WenJY 2024-07-03 14:13:21 + */ + if(alarmRule.getTriggerTimeFrame() != null && alarmRule.getTriggerTimeFrame() > 0){ + double difValue = FilterDeviceValue(deviceId, fieldName, alarmRule.getTriggerTimeFrame()); + if(difValue > 0){ + filedValue = difValue; + } } - } catch (ExecutionException | InterruptedException e) { - throw new RuntimeException(e.getMessage()); + /** End **/ + + triggerExpression = triggerExpression.replaceAll("\\{" + fieldName + "\\}", String.valueOf(filedValue)); + HwAlarmDetail alarmDetail = new HwAlarmDetail(); + alarmDetail.setDeviceId(deviceId); + alarmDetail.setFunctionIdentifier(fieldName); + alarmDetail.setFunctionValue(String.valueOf(filedValue)); + alarmDetail.setMonitorTime(currentDate); + alarmDetails.add(alarmDetail); + } + } - break; + if (isAlarmed.get() && !triggerExpression.contains("{") + && RegexUtils.findSymbolInText(triggerExpression).size() > 0 + && RegexUtils.findNumberInText(triggerExpression).size() > 0) { + + Boolean triggerExpressionBool = (Boolean) engine.eval(triggerExpression); + if (triggerExpressionBool) { + HwAlarmInfo alarmInfo = new HwAlarmInfo(); + alarmInfo.setAlarmInfoType(HwDictConstants.ALARM_INFO_TYPE_DEVICE); + alarmInfo.setAlarmReleatedId(alarmRule.getAlarmRuleId()); + alarmInfo.setDeviceId(deviceId); + alarmInfo.setMonitorUnitId(monitorUnitId); + alarmInfo.setTenantId(tenantId); + alarmInfo.setSceneId(sceneId); + alarmInfo.setAlarmLevelId(alarmRule.getAlarmLevelId()); + alarmInfo.setAlarmTypeId(alarmRule.getAlarmTypeId()); + alarmInfo.setHwAlarmDetailList(alarmDetails); + alarmInfo.setHandleStatus(HwDictConstants.ALARM_HANDLE_STATUS_NO); + alarmInfo.setCreateTime(currentDate); + alarmInfo.setAlarmTime(currentDate); + hwAlarmInfoMapper.insertHwAlarmInfo(alarmInfo); + this.insertHwAlarmDetail(alarmInfo); + this.handleAlarmLink(alarmRule, topic, subDeviceCode); + /** + * Add By WenJY 下发报警信息 + */ + if(StringUtils.isNotEmpty(alarmRule.getPhoneNumbers())){ + SendAlarmInfoBySms(deviceId,alarmRule); + }else { + logger.warn("设备:{};报警信息发送失败,联系方式为空",deviceId); + } + break; + } } } } + }catch (Exception e){ + logger.error("设备:{};报警逻辑处理异常:{}",deviceId,e.getMessage()); } } + /** + * 过滤指定时间段内的数据,返回最大最小的差值,过滤0 + * @param deviceId + * @param fieldName + * @param triggerTimeFrame + * @return 差值 + */ + private double FilterDeviceValue(Long deviceId,String fieldName,long triggerTimeFrame){ + double differenceValue = 0.00; + String databaseName = TdEngineConstants.getDatabaseName(); + String tableName = TdEngineConstants.DEFAULT_TABLE_NAME_PREFIX + deviceId; + long currentTimeMillis = System.currentTimeMillis(); + long fiveMinutesInMillis = triggerTimeFrame * 60 * 1000; + long beforeTimeMillis = currentTimeMillis - fiveMinutesInMillis; + + TdHistorySelectDto tdHistorySelectDto = new TdHistorySelectDto(); + tdHistorySelectDto.setDatabaseName(databaseName); + tdHistorySelectDto.setTableName(tableName); + tdHistorySelectDto.setStartTime(beforeTimeMillis); + tdHistorySelectDto.setEndTime(currentTimeMillis); + tdHistorySelectDto.setFirstFieldName("ts"); + R result = this.remoteTdEngineService.getHistoryData(tdHistorySelectDto , SecurityConstants.INNER); + + if (result.getCode() == ResultEnums.SUCCESS.getCode()) { + logger.info("Get history data result: {}", result.getCode()); + + TdReturnDataVo data = (TdReturnDataVo) result.getData(); + + List> dataList = data.getDataList(); + List value = dataList.stream().map(map -> (Double)map.get("value1")).distinct().filter(x -> x > 0).collect(Collectors.toList()); + + System.out.println(triggerTimeFrame +"分钟内数据:"+JSONArray.toJSON(value) ); + + OptionalDouble maxOptional = value.stream().mapToDouble(Double::doubleValue).max(); + OptionalDouble minOptional = value.stream().mapToDouble(Double::doubleValue).min(); + + if (maxOptional.isPresent() && minOptional.isPresent()) { + double maxValue = maxOptional.getAsDouble(); + double minValue = minOptional.getAsDouble(); + differenceValue = maxValue - minValue; + System.out.println("Difference between max and min values: " + differenceValue); + } else { + System.out.println("No values found to calculate difference."); + } + + } else { + logger.error("Get history data Exception: {}", result.getMsg()); + } + + return differenceValue; + + } + /** * 新增报警详情信息信息 * @@ -516,6 +577,26 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData } } + /** + * 发送报警短信 + * @param deviceId + * @param alarmRule + */ + private void SendAlarmInfoBySms(Long deviceId,HwAlarmRule alarmRule){ + try { + HwDevice hwDevice = hwDeviceMapper.GetDeviceById(deviceId); + if(hwDevice != null){ + AlarmMsgTemplateParam alarmMsgTemplateParam = new AlarmMsgTemplateParam(); + alarmMsgTemplateParam.setWarning(alarmRule.getAlarmRuleName()); + alarmMsgTemplateParam.setParentname(hwDevice.getMonitorUnitName()); + alarmMsgTemplateParam.setSensorID(deviceId.toString()); + AliSmsHandle(alarmRule.getPhoneNumbers(),alarmMsgTemplateParam); + } + } catch (ExecutionException | InterruptedException e) { + throw new InvalidOperationException(String.format("报警信息发送异常:%e",e.getMessage())); + } + } + /** * 报警短信推送 * @param phoneNumbers @@ -523,7 +604,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData * @throws ExecutionException * @throws InterruptedException */ - private void SendAlarmInfoToSms(String phoneNumbers,AlarmMsgTemplateParam alarmMsgTemplateParam ) throws ExecutionException, InterruptedException { + private void AliSmsHandle(String phoneNumbers,AlarmMsgTemplateParam alarmMsgTemplateParam ) throws ExecutionException, InterruptedException { StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() .accessKeyId(AccessKeyId) .accessKeySecret(AccessKeySecret) @@ -546,7 +627,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData .templateCode("SMS_468740027") .templateParam(jsonString) .build(); - + logger.info("向:{};发送报警信息:{}",phoneNumbers,jsonString); CompletableFuture response = client.sendSms(sendSmsRequest); SendSmsResponse resp = response.get(); diff --git a/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml b/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml index 11b306f..4183673 100644 --- a/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml +++ b/ruoyi-modules/hw-data-process/src/main/resources/mapper/dataprocess/HwAlarmRuleMapper.xml @@ -32,6 +32,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + @@ -51,13 +53,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select alarm_rule_id, alarm_rule_name, tenant_id, scene_id, language_code, alarm_level_id, alarm_type_id, rule_type, rule_device_id, rule_function_total, trigger_expression, link_flag, alarm_rule_status, alarm_push_flag, alarm_push_content, alarm_recover_content, remark, create_by, create_time, update_by, update_time, alarm_rule_field from hw_alarm_rule + select alarm_rule_id, alarm_rule_name, tenant_id, scene_id, language_code, alarm_level_id, alarm_type_id, rule_type, rule_device_id, rule_function_total, trigger_expression, link_flag, alarm_rule_status, alarm_push_flag, alarm_push_content, alarm_recover_content, remark, create_by, create_time, update_by, update_time, alarm_rule_field,trigger_time_frame,phone_numbers from hw_alarm_rule