package com.ruoyi.quartz.task; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ruoyi.fuzhou.domain.*; import com.ruoyi.fuzhou.enums.DataTypeEnum; import com.ruoyi.fuzhou.service.*; import com.ruoyi.fuzhou.utils.electricitymodbus.HexStringToInt; import com.ruoyi.fuzhou.utils.hj1239.FileReadUtils; import com.ruoyi.fuzhou.utils.hj1239.StringToHexUtil; import jakarta.annotation.Resource; import org.springframework.stereotype.Component; import com.ruoyi.common.utils.StringUtils; import java.io.File; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 定时任务调度测试 * * @author ruoyi */ @Component("ryTask") public class RyTask { @Resource private EquipmentService equipmentService; @Resource private ReceiveElectricityInfoService receiveElectricityInfoService; @Resource private ReceiveOilInfoService receiveOilInfoService; @Resource private ReceiveWaterInfoService receiveWaterInfoService; @Resource private ReceiveCarRuleService receiveCarRuleService; @Resource private ReceiveCarValueService receiveCarValueService; @Resource private ReceiveWeatherInfoService receiveWeatherInfoService; @Resource private ReceiveSlmInfoService receiveSlmInfoService; @Resource private ReceiveWeatherValueService receiveWeatherValueService; @Resource private HikService hikService; @Resource private ReceiveElectricityValueService receiveElectricityValueService; /** * 电数据接入 */ public void ryMultipleParams(String name) { List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).eq(DpEquipment::getEquipmentTypeId, DataTypeEnum.ELECTRICITY.getCode()); }}); for (DpEquipment info : equipments) { List list = receiveElectricityInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveElectricityInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveElectricityInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveElectricityInfoService.saveElectricity(list, info); } } } /** * 油数据接入 * * @param name */ public void ryParams(String name) { System.out.println("执行有参方法:" + name); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.OIL.getCode()); }}); for (DpEquipment info : equipments) { List list = receiveOilInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveOilInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveOilInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveOilInfoService.saveOil(list, info); } } } /** * 气象数据数据接入 * * @param name */ public void wsParams(String name) { System.out.println("执行有参方法:" + name); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.WEATHER.getCode()); }}); for (DpEquipment info : equipments) { List list = receiveWeatherInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveWeatherInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveWeatherInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveWeatherInfoService.saveData(list, info); } } } /** * 气象数据告警 * * @param windSpeedValue 风速阈值 * @param windSpeedTime 风速检测时间 * @param tempSpeedValue 温度阈值 * @param tempSpeedTime 温度检测时间 */ public void wsGaoJingParams(Integer windSpeedValue,Integer windSpeedTime,Integer tempSpeedValue,Integer tempSpeedTime,Integer interval) { System.out.println("执行气象数据--告警有参方法:" + windSpeedValue+";"+ windSpeedTime+";"+ tempSpeedValue+";"+ tempSpeedTime+";"); if(interval==null){ interval=10; } LocalDateTime now=LocalDateTime.now(); Date nowDate=new Date(); LocalDateTime agoSpeedTime=now.minusMinutes(windSpeedTime); LocalDateTime agoWenduSpeedTime=now.minusMinutes(tempSpeedTime); List list = receiveWeatherValueService.list(new LambdaQueryWrapper() {{ ge(ReceiveWeatherValue::getCreateTime,agoSpeedTime).le(ReceiveWeatherValue::getCreateTime,now); }}); Boolean hasfengsubaojing=fengsubaojingpanduan(list,windSpeedValue,nowDate,interval); if(hasfengsubaojing){ HikEvent hikEvent=new HikEvent(); hikEvent.setHappenTime(list.get(0).getCreateTime()); hikEvent.setSendTime(nowDate); hikEvent.setEventType(101);//风速101 温度102 hikEvent.setSrcName("风速告警"); hikEvent.setSrcType("气象设备"); hikService.saveInsert(hikEvent); } List listTemp = receiveWeatherValueService.list(new LambdaQueryWrapper() {{ ge(ReceiveWeatherValue::getCreateTime,agoWenduSpeedTime).le(ReceiveWeatherValue::getCreateTime,now); }}); Boolean haswendubaojing=wendubaojingpanduan(listTemp,tempSpeedValue,nowDate,interval); if(haswendubaojing){ HikEvent hikEvent=new HikEvent(); hikEvent.setHappenTime(list.get(0).getCreateTime()); hikEvent.setSendTime(nowDate); hikEvent.setEventType(102);//风速101 温度102 hikEvent.setSrcName("温度告警"); hikEvent.setSrcType("气象设备"); hikService.saveInsert(hikEvent); } } /** * 智能电表报警 * @param lXian 线电压-低 * @param hXian 线电压-高 * @param lXiang 相电压-低 * @param hXiang 相电压-高 */ public void dbGaoJing(Integer lXian, Integer hXian, Integer lXiang, Integer hXiang) { List list = equipmentService.getListByType(3); if (null == list || list.isEmpty()) return; for (DpEquipment dp : list) { QueryWrapper wrapper = new QueryWrapper<>(); //wrapper.eq(ReceiveElectricityValue::getDeviceName, String.valueOf(dp.getId())); wrapper.eq("DEVICE_NAME", String.valueOf(dp.getId())); //wrapper.orderByDesc(ReceiveElectricityValue::getCreateTime); wrapper.orderByDesc("CREATE_TIME"); wrapper.last("limit 1"); ReceiveElectricityValue rs = receiveElectricityValueService.getOne(wrapper); if (null == rs) continue; double voltageA = Double.parseDouble(rs.getVoltageA()); double voltageB = Double.parseDouble(rs.getVoltageB()); double voltageC = Double.parseDouble(rs.getVoltageC()); double voltageCa = Double.parseDouble(rs.getVoltageCa()); double voltageAb = Double.parseDouble(rs.getVoltageAb()); double voltageBc = Double.parseDouble(rs.getVoltageBc()); HikEvent hikEvent = new HikEvent(); hikEvent.setHappenTime(rs.getCreateTime()); hikEvent.setSendTime(new Date()); hikEvent.setSrcName(dp.getEquName()); hikEvent.setSrcIndex(String.valueOf(dp.getId())); // voltageA-A相电压, B相电压-voltageB, C相电压-voltageC List ycList = new ArrayList<>(); if (voltageA < 1) ycList.add("A"); if (voltageB < 1) ycList.add("B"); if (voltageC < 1) ycList.add("C"); if (!ycList.isEmpty()) { hikEvent.setEventType(103); // 103-电压异常, 104-过欠压 hikEvent.setSrcType(StringUtils.join(ycList, " | ")); hikService.saveInsert(hikEvent); continue; } List qyList = new ArrayList<>(); // voltageCa-CA线电压, AB线电压-voltageAb, BC线电压-voltageBc if (voltageCa < lXian || voltageCa > hXian) qyList.add("CA"); if (voltageAb < lXian || voltageAb > hXian) qyList.add("AB"); if (voltageBc < lXian || voltageBc > hXian) qyList.add("BC"); // voltageA-A相电压, B相电压-voltageB, C相电压-voltageC if (voltageA < lXiang || voltageA > hXiang) qyList.add("A"); if (voltageB < lXiang || voltageB > hXiang) qyList.add("B"); if (voltageC < lXiang || voltageC > hXiang) qyList.add("C"); if (!qyList.isEmpty()) { hikEvent.setEventType(104); // 103-电压异常, 104-过欠压 hikEvent.setSrcType(StringUtils.join(qyList, " | ")); hikService.saveInsert(hikEvent); } } } public Boolean fengsubaojingpanduan(List list,int value,Date now,Integer interval){ if(list.size()>=5){ for (int i = 0; i < list.size(); i++) { if(list.get(i).getWindSpeed()!=null&&Double.parseDouble(list.get(i).getWindSpeed()) hList=hikService.list(new LambdaQueryWrapper() {{ eq(HikEvent::getEventType,101).orderByDesc(HikEvent::getSendTime).last("LIMIT 1"); }}); if(hList.size()>0){ HikEvent hData=hList.get(0); if(now.getTime()-hData.getSendTime().getTime()>=1000*60*interval){ return true; } } return false; }else { return false; } } public Boolean wendubaojingpanduan(List list,int value,Date now,Integer interval){ if(list.size()>=5){ for (int i = 0; i < list.size(); i++) { if(list.get(i).getTemp()!=null&&Double.parseDouble(list.get(i).getTemp()) hList=hikService.list(new LambdaQueryWrapper() {{ eq(HikEvent::getEventType,102).orderByDesc(HikEvent::getSendTime).last("LIMIT 1"); }}); if(hList.size()>0){ HikEvent hData=hList.get(0); if(now.getTime()-hData.getSendTime().getTime()>=1000*60*interval){ return true; } } return false; }else { return false; } } /** * 激光雷达数据数据接入 * * @param name */ public void slmParams(String name) { System.out.println("执行有参方法:" + name); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.SLM.getCode()); }}); for (DpEquipment info : equipments) { List list = receiveSlmInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveSlmInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveSlmInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveSlmInfoService.saveData(list, info); } } } /** * 利旧油数据接入 * * @param name */ public void ryLijiuParams(String name) { System.out.println("执行有参方法:" + name); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.LIJIUOIL.getCode()); }}); for (DpEquipment info : equipments) { List list = receiveOilInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveOilInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveOilInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveOilInfoService.saveLijiuOil(list, info); } } } /** * 宁德退油数据接入 * * @param name */ public synchronized void ningdeLijiuParams(String name) { System.out.println("执行有参方法:" + name); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.LIJIUJUN.getCode()); }}); for (DpEquipment info : equipments) { try { List list = receiveOilInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveOilInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveOilInfo::getParamCode, "createTime"); }}); Thread.sleep(1000); if (list != null && list.size() > 0) { receiveOilInfoService.saveOilLijiuNingde(list, info); } } catch (Exception e) { e.printStackTrace(); } } } /** * 水数据接入 */ public void ryNoParams(String name) { System.out.println("执行无参方法"); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.WATER_FLOW.getCode()); }}); for (DpEquipment info : equipments ) { List list = receiveWaterInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveWaterInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveWaterInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveWaterInfoService.saveWater(list, info); } } } /** * 水数据接入 */ public void saveWaterYa(String name) { System.out.println("执行无参方法"); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.WATER_YA.getCode()); }}); for (DpEquipment info : equipments ) { List list = receiveWaterInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveWaterInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveWaterInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveWaterInfoService.saveWaterYa(list, info); } } } /** * 水数据接入 */ public void saveWaterDept(String name) { System.out.println("执行无参方法"); List equipments = equipmentService.list(new LambdaQueryWrapper() {{ or().like(StringUtils.isNotEmpty(name), DpEquipment::getEquName, name).in(DpEquipment::getEquipmentTypeId, DataTypeEnum.WATER_DEPT.getCode()); }}); for (DpEquipment info : equipments ) { List list = receiveWaterInfoService.list(new LambdaQueryWrapper() {{ or().eq(ReceiveWaterInfo::getDeviceName, String.valueOf(info.getId())).notIn(ReceiveWaterInfo::getParamCode, "createTime"); }}); if (list != null && list.size() > 0) { receiveWaterInfoService.saveWaterDept(list, info); } } } public void carHj1239Task(String ftpFilePath, String tmpPath) { File file = new File(ftpFilePath); File[] fs = file.listFiles(); List filePath = new ArrayList<>(); for (File f : fs) { if (!f.isDirectory()) { filePath.add(f.getAbsolutePath()); } } List receiveCarRules = receiveCarRuleService.list(new LambdaQueryWrapper() {{ orderByAsc(ReceiveCarRule::getId); }}); for (String path : filePath ) { try { byte[] bytes = FileReadUtils.readFromByteFile(path); String content = FileReadUtils.bytesToHex(bytes); content = content.replace("aaaabbbbccccaaaa", "aaaabbbb,ccccaaaa"); String[] src = content.split(","); List receiveCarValues = new ArrayList<>(); for (int i = 0; i < src.length; i++) { String item = src[i]; JSONObject jsonObject = getvalue(receiveCarRules, item); ReceiveCarValue receiveCarValue = JSON.parseObject(jsonObject.toJSONString(), ReceiveCarValue.class); receiveCarValue.setCreateTime(new Date()); receiveCarValues.add(receiveCarValue); System.out.println(jsonObject); } receiveCarValueService.saveOrUpdateBatch(receiveCarValues); //源文件 File a = new File(path); //目标目录 File b = new File(tmpPath); if (!b.exists()) { b.mkdirs(); } File bFile = new File(b + a.getName()); //调用File类的核心方法renameTo a.renameTo(bFile); } catch (Exception e) { e.printStackTrace(); } } } private static JSONObject getvalue(List rule, String content) { JSONObject jsonObject = new JSONObject(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (ReceiveCarRule r : rule ) { if (!"createTime".equals(r.getCarCode())) { String xx = content.substring(r.getStart() * 2, r.getStart() * 2 + r.getDataLength() * 2); if ("VARCHAR".equals(r.getDataType()) && "NONE".equals(r.getRule())) { jsonObject.put(r.getCarCode(), StringToHexUtil.convertHexToString(xx)); } else if ("WORD".equals(r.getDataType()) && "NONE".equals(r.getRule())) { jsonObject.put(r.getCarCode(), String.valueOf(HexStringToInt.hexStringToInt2(xx))); } else if ("BYTE[6]".equals(r.getDataType()) && "NONE".equals(r.getRule())) { String dateTime = "20"; dateTime = dateTime + HexStringToInt.hexStringToString(xx.substring(0, 2)) + "-"; dateTime = dateTime + HexStringToInt.hexStringToString(xx.substring(2, 4)) + "-"; dateTime = dateTime + HexStringToInt.hexStringToString(xx.substring(4, 6)) + " "; dateTime = dateTime + HexStringToInt.hexStringToString(xx.substring(6, 8)) + ":"; dateTime = dateTime + HexStringToInt.hexStringToString(xx.substring(8, 10)) + ":"; dateTime = dateTime + HexStringToInt.hexStringToString(xx.substring(10, 12)); jsonObject.put(r.getCarCode(), dateTime); } else if ("WORD".equals(r.getDataType()) && "COMPUTE".equals(r.getRule())) { if ("ffff".equals(xx)) { jsonObject.put(r.getCarCode(), ""); } else { jsonObject.put(r.getCarCode(), String.valueOf(new BigDecimal(HexStringToInt.hexStringToInt2(xx) * Double.valueOf(r.getRuleValue()) + Double.valueOf(r.getOffset())).setScale(3, RoundingMode.UP).doubleValue())); } } else if ("BYTE".equals(r.getDataType()) && "COMPUTE".equals(r.getRule())) { if ("ff".equals(xx)) { jsonObject.put(r.getCarCode(), ""); } else { jsonObject.put(r.getCarCode(), String.valueOf(HexStringToInt.hexStringToInt2(xx) * Double.valueOf(r.getRuleValue()) + Double.valueOf(r.getOffset()))); } } else if ("DWORD".equals(r.getDataType()) && "COMPUTE".equals(r.getRule())) { if ("ffffffff".equals(xx)) { jsonObject.put(r.getCarCode(), ""); } else { jsonObject.put(r.getCarCode(), String.valueOf(Long.parseLong(xx, 16) * Double.valueOf(r.getRuleValue()) + Double.valueOf(r.getOffset()))); } } } } try { jsonObject.put("id", sdf.parse(jsonObject.getString("dataTime")).getTime() + jsonObject.getString("vin")); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } }