| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.io.File; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | |
| | | public Map<String, Object> getAlarmLevel1ListMap() { |
| | | Long userId = ActionUtil.getUser().getUId(); |
| | | String lang = ActionUtil.getLang(); |
| | | List<BattalarmData> battAlarmList = battAlarmDataService.getList(userId, 1); |
| | | battAlarmList = battAlarmList.stream().filter(alarm -> alarm.getAlmLevel().equals(1)).collect(Collectors.toList()); |
| | | //相同设备id+告警类型的语音,只保留一条 |
| | | Map<String, List<BattalarmData>> battAlarmListMap = battAlarmList.stream().collect(Collectors.groupingBy(a -> a.getBattGroupId() + "_" + a.getAlmSignalId())); |
| | | Set<String> keySet = battAlarmListMap.keySet(); |
| | | battAlarmList.clear(); |
| | | for (String key : keySet) { |
| | | battAlarmList.add(battAlarmListMap.get(key).get(0)); |
| | | } |
| | | |
| | | for (BattalarmData adata : battAlarmList) { |
| | | adata.setAlmSignalIdName(MessageUtils.getMessageSocket(AlarmDaoFactory.getAlarmName(adata.getAlmSignalId()), lang)); |
| | | adata.setAlmLevelName(MessageUtils.getMessageSocket(AlarmDaoFactory.getAlarmType(adata.getAlmLevel()), lang)); |
| | | } |
| | | |
| | | List<DevalarmData> devAlarmList = devAlarmDataService.getList(userId,1); |
| | | devAlarmList = devAlarmList.stream().filter(alarm->alarm.getAlmLevel().equals(1)).collect(Collectors.toList()); |
| | | //相同设备id+告警类型的语音,只保留一条 |
| | | Map<String, List<DevalarmData>> devAlarmListMap = devAlarmList.stream().collect(Collectors.groupingBy(a -> a.getDevId() + "_" + a.getAlmType())); |
| | | Set<String> keySet2 = devAlarmListMap.keySet(); |
| | | devAlarmList.clear(); |
| | | for (String key:keySet2){ |
| | | devAlarmList.add(devAlarmListMap.get(key).get(0)); |
| | | } |
| | | |
| | | List<PwrdevAlarm> powerAlarmList = pwrdevAlarmService.getList(userId,1); |
| | | powerAlarmList = powerAlarmList.stream().filter(alarm->alarm.getAlmLevel().equals(1)).collect(Collectors.toList()); |
| | | //相同设备id+告警类型的语音,只保留一条 |
| | | Map<String, List<PwrdevAlarm>> powerAlarmListMap = powerAlarmList.stream().collect(Collectors.groupingBy(a -> a.getPowerDeviceId() + "_" + a.getAlmType())); |
| | | Set<String> keySet3 = powerAlarmListMap.keySet(); |
| | | powerAlarmList.clear(); |
| | | for (String key:keySet3){ |
| | | powerAlarmList.add(powerAlarmListMap.get(key).get(0)); |
| | | } |
| | | for (PwrdevAlarm p : powerAlarmList) { |
| | | p.setAlarmName(AlarmDaoFactory.getAllAlarmName(p.getAlmType())); |
| | | if (p.getAlmIndex() != 0) { |
| | | p.setAlarmName(p.getAlarmName().replace("N", "第" + p.getAlmIndex() + "路")); |
| | | } |
| | | } |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("battAlarmList", battAlarmList); |
| | | map.put("devAlarmList", devAlarmList); |
| | | map.put("powerAlarmList", powerAlarmList); |
| | | return map; |
| | | } |
| | | |
| | | public Map<String, Object> getAlarmLevel1ListMap4Socket(Long userId, HttpSession httpSession) { |
| | | //Long userId = ActionUtil.getUser().getUId(); |
| | | String lang = ActionUtil.getLang4Socket(httpSession); |
| | | List<BattalarmData> battAlarmList = battAlarmDataService.getList(userId, 1); |
| | | battAlarmList = battAlarmList.stream().filter(alarm -> alarm.getAlmLevel().equals(1)).collect(Collectors.toList()); |
| | | //相同设备id+告警类型的语音,只保留一条 |
| | |
| | | * |
| | | * @param readDirPath |
| | | * @param application |
| | | * @param httpSession |
| | | */ |
| | | public void monitorAlarm4Socket(String readDirPath, Long uId, ServletContext application) { |
| | | public void monitorAlarm4Socket(String readDirPath, Long uId, ServletContext application, HttpSession httpSession) { |
| | | //获取ServletContext |
| | | |
| | | |
| | | //用户 |
| | | Map<String,Object> alarmMap = (Map<String, Object>) application.getAttribute("alarmMap_" + uId); |
| | | Map<String, Object> alarmMapNow = getAlarmLevel1ListMap(); |
| | | Map<String, Object> alarmMapNow = getAlarmLevel1ListMap4Socket(uId,httpSession); |
| | | |
| | | List<BattalarmData> battAlarmListNow = (List<BattalarmData>) alarmMapNow.get("battAlarmList"); |
| | | List<DevalarmData> devAlarmListNow = (List<DevalarmData>) alarmMapNow.get("devAlarmList"); |
| | |
| | | application.setAttribute("alarmMap_" + uId, alarmMapNow); |
| | | } |
| | | |
| | | formatResultAndSaveToWav(readDirPath,battAlarmListAdded, devAlarmListAdded, powerAlarmListAdded); |
| | | formatResultAndSaveToWav4Socket(readDirPath,battAlarmListAdded, devAlarmListAdded, powerAlarmListAdded,uId); |
| | | } |
| | | } |
| | | |
| | |
| | | //生成语音文件,命名规则 {alarmStartTime}_batt_{num}_{battGroupId}_{almSignalId}_{readableVoiceTime} |
| | | service.saveToWav(alarmMsgBuilder.toString(), dirPath + File.separator |
| | | + alarm.getAlmStartTime().getTime()+"_batt_"+alarm.getNum()+"_"+ battGroupId +"_"+ almSignalId +"_"+nextTimestamp.get()+".wav"); |
| | | |
| | | } |
| | | for (DevalarmData alarm : devAlarmList) { |
| | | StringBuilder alarmMsgBuilder = new StringBuilder(); |
| | | Integer almType = alarm.getAlmType(); |
| | | String alarmName = AlarmDaoFactory.getAllAlarmName(almType); |
| | | String alarmStartTime = format.format(alarm.getAlmStartTime()); |
| | | String stationName = alarm.getStationName(); |
| | | String district = stationName.split("-")[2]; |
| | | String station = stationName.split("-")[3]; |
| | | String stationNameUnion = district+"-"+station; |
| | | |
| | | //查询是否存在 设备id+告警类型 相同的记录 |
| | | Integer devId = alarm.getDevId(); |
| | | AtomicLong nextTimestamp = new AtomicLong(currentTimeMillis); |
| | | setListDevice.forEach(set->{ |
| | | if(set.getDeviceId().intValue() == devId && set.getAlarmType().equals(almType)){ |
| | | nextTimestamp.set(set.getNextTimestamp()); |
| | | } |
| | | }); |
| | | //alarmMsgBuilder.append(stationNameUnion).append(",设备编号为").append(alarm.getDevId()).append(",在").append(alarmStartTime).append("发生了").append(alarmName).append("告警;"); |
| | | alarmMsgBuilder.append(stationNameUnion).append("发生了").append(alarmName).append("告警;"); |
| | | //生成语音文件,命名规则 {alarmStartTime}_device_{num}_{deviceId}_{almType}_{readableVoiceTime} |
| | | service.saveToWav(alarmMsgBuilder.toString(), dirPath + File.separator |
| | | + alarm.getAlmStartTime().getTime()+"_device_"+alarm.getNum()+"_"+alarm.getDevId()+"_"+ almType +"_"+nextTimestamp.get()+".wav"); |
| | | |
| | | } |
| | | for (PwrdevAlarm alarm : powerAlarmList) { |
| | | StringBuilder alarmMsgBuilder = new StringBuilder(); |
| | | Integer almType = alarm.getAlmType(); |
| | | String alarmName = AlarmDaoFactory.getAllAlarmName(almType); |
| | | String alarmStartTime = format.format(alarm.getAlmStartTime()); |
| | | String stationName = alarm.getStationName(); |
| | | String district = stationName.split("-")[2]; |
| | | String station = stationName.split("-")[3]; |
| | | String stationNameUnion = district+"-"+station; |
| | | |
| | | //查询是否存在 设备id+告警类型 相同的记录 |
| | | Long powerDeviceId = alarm.getPowerDeviceId(); |
| | | AtomicLong nextTimestamp = new AtomicLong(currentTimeMillis); |
| | | setListPowerDevice.forEach(set->{ |
| | | if(set.getPowerDeviceId().equals(powerDeviceId) && set.getAlarmType().equals(almType)){ |
| | | nextTimestamp.set(set.getNextTimestamp()); |
| | | } |
| | | }); |
| | | //alarmMsgBuilder.append(stationNameUnion).append(",电源编号为").append(alarm.getPowerDeviceId()).append(",在").append(alarmStartTime).append("发生了").append(alarmName).append("告警;"); |
| | | alarmMsgBuilder.append(stationNameUnion).append("发生了").append(alarmName).append("告警;"); |
| | | //生成语音文件,命名规则 {alarmStartTime}_device_{num}_{deviceId}_{almType}_{readableVoiceTime} |
| | | service.saveToWav(alarmMsgBuilder.toString(), dirPath + File.separator |
| | | + alarm.getAlmStartTime().getTime()+"_power_"+alarm.getNum()+"_"+alarm.getPowerDeviceId()+"_"+ almType +"_"+nextTimestamp.get()+".wav"); |
| | | |
| | | } |
| | | |
| | | /*if(!alarmMsg.isEmpty()) { |
| | | service.setRate(1); |
| | | service.saveToWav(alarmMsg, dirPath + File.separator + System.currentTimeMillis() + ".wav"); |
| | | }*/ |
| | | } |
| | | |
| | | private void formatResultAndSaveToWav4Socket(String dirPath, |
| | | List<BattalarmData> battAlarmList, |
| | | List<DevalarmData> devAlarmList, |
| | | List<PwrdevAlarm> powerAlarmList,Long userId) { |
| | | |
| | | SimpleDateFormat format = new SimpleDateFormat(ActionUtil.time_yyyyMMddHHmmss); |
| | | long currentTimeMillis = System.currentTimeMillis(); |
| | | //查询告警语音设置表的所有记录,并更新下次时间戳到到相关的语音文件中 |
| | | //Long userId = ActionUtil.getUser().getUId(); |
| | | List<AlarmVoiceSet> setList = alarmVoiceSetService.getAllByUserId(userId); |
| | | List<AlarmVoiceSet> setListBatt = setList.stream().filter(set->set.getBattGroupId()!=null).collect(Collectors.toList()); |
| | | List<AlarmVoiceSet> setListDevice = setList.stream().filter(set->set.getDeviceId()!=null).collect(Collectors.toList()); |
| | | List<AlarmVoiceSet> setListPowerDevice = setList.stream().filter(set->set.getPowerDeviceId()!=null).collect(Collectors.toList()); |
| | | |
| | | File file = new File(dirPath); |
| | | if(!file.exists()){ |
| | | file.mkdirs(); |
| | | } |
| | | for (BattalarmData alarm : battAlarmList) { |
| | | StringBuilder alarmMsgBuilder = new StringBuilder(); |
| | | Integer almSignalId = alarm.getAlmSignalId(); |
| | | String alarmName = AlarmDaoFactory.getAllAlarmName(almSignalId); |
| | | String alarmStartTime = format.format(alarm.getAlmStartTime()); |
| | | String stationName = alarm.getStationName(); |
| | | String district = stationName.split("-")[2]; |
| | | String station = stationName.split("-")[3]; |
| | | String stationNameUnion = district+"-"+station; |
| | | |
| | | //查询是否存在 设备id+告警类型 相同的记录 |
| | | Integer battGroupId = alarm.getBattGroupId(); |
| | | AtomicLong nextTimestamp = new AtomicLong(currentTimeMillis); |
| | | setListBatt.forEach(set->{ |
| | | if(set.getBattGroupId().equals(battGroupId) && set.getAlarmType().equals(almSignalId)){ |
| | | nextTimestamp.set(set.getNextTimestamp()); |
| | | } |
| | | }); |
| | | |
| | | //alarmMsgBuilder.append(stationNameUnion).append(",电池组编号为").append(alarm.getBattgroupname()).append(",单体为").append(alarm.getMonNum()).append(",在").append(alarmStartTime).append("发生了").append(alarmName).append("告警;"); |
| | | alarmMsgBuilder.append(stationNameUnion).append("发生了").append(alarmName).append("告警;"); |
| | | //生成语音文件,命名规则 {alarmStartTime}_batt_{num}_{battGroupId}_{almSignalId}_{userId}_{nextTimeInterval}_{voiceTime} ->更改为 |
| | | //生成语音文件,命名规则 {alarmStartTime}_batt_{num}_{battGroupId}_{almSignalId}_{readableVoiceTime} |
| | | service.saveToWav(alarmMsgBuilder.toString(), dirPath + File.separator |
| | | + alarm.getAlmStartTime().getTime()+"_batt_"+alarm.getNum()+"_"+ battGroupId +"_"+ almSignalId +"_"+nextTimestamp.get()+".wav"); |
| | | |
| | | } |
| | | for (DevalarmData alarm : devAlarmList) { |
| | |
| | | } |
| | | } |
| | | |
| | | public Response getWavRead4Socket(UserInf user, ServletContext application) { |
| | | public Response getWavRead4Socket(UserInf user, ServletContext application, HttpSession httpSession) { |
| | | //获取绝对文件夹路径 |
| | | String readSuffixPath = "wav" + File.separator + "alarm" + File.separator + user.getUId() + File.separator; |
| | | String readDirPath = CommonUtil.getRootFile() + readSuffixPath; |
| | | File readDir = new File(readDirPath); |
| | | //监控用户的告警,包含生成,内存信息更新 |
| | | monitorAlarm4Socket(readDirPath,user.getUId(),application); |
| | | monitorAlarm4Socket(readDirPath,user.getUId(),application,httpSession); |
| | | if(!readDir.exists()){ |
| | | return new Response().set(1,false,"无未读告警语音"); |
| | | }else{ |