package kr.gmtc.eyesvmsg.send.passivlc; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import gmt.common.type.LogLevelType; import gmt.logger.GmtLogManager; import gmt.proj.sv_msg.controller.PacketController; import gmt.proj.sv_msg.vo.LastLcTrackVO; import gmt.proj.sv_msg.vo.ShipInfoVO; import kr.gmtc.eyesvmsg.scheduler.ScheduleManager; @Component("checkLastLcController") public class CheckLastLcController { // 메시지 별 송신 텀 @Value("${msg.sndCnt}") private int msgSndCnt = 2; private PacketController packetController = new PacketController(); private Queue stdQue = new LinkedList(); @Autowired ScheduleManager scheduleManager; @Autowired private PassivlcService passivlcService; public void CheckLastLc(int min) { List lcVoList = new ArrayList(); try { // 30분, 60분 동안 위치 수신이 없는 선박목록(최종위치테이블) LastLcTrackVO ltVO = new LastLcTrackVO(); ltVO.setMin(min); lcVoList = passivlcService.selectLastLcList(ltVO); } catch (SQLException se) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - selectLastLcList : " + se.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } catch (Exception e) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - selectLastLcList : " + e.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } if(lcVoList.isEmpty()) return; GmtLogManager.getInstance().writeLevelLog("------------------------------------------------------", LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - lcVoList.size() : " + lcVoList.size(), LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); for(int i = 0; i < lcVoList.size(); i++) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - FOR문(i) 현재 카운트 : " + i, LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); LastLcTrackVO lcVO = new LastLcTrackVO(); lcVO = lcVoList.get(i); // Slot번호 와 Report_dt 기준 시간으로 30분 이내, 60분 이내 수동위치 요청 내역 조회 int slot_no = lcVO.getSlot_no(); int requst_at = -1; // 출입항 조회, 출항시간, mmsi 정보 ShipInfoVO ship = scheduleManager.getShipInfo().get(slot_no); if(ship == null) continue; // Inout_part = I: 입항, O: 출항 if(ship.getInout_part().equals("I")) continue; LocalDateTime report_dt = lcVO.getReport_dt(); // 출항시간(out_dt)는 KST LocalDateTime out_dt = ship.getOut_time().minusHours(9); LocalDateTime server_utc_now = LocalDateTime.now().minusHours(9); LocalDateTime db_utc_now = lcVO.getUtc_now_dt(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - slot_no : " + slot_no+ ", min : " + min, LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - " + report_dt.format(dateTimeFormatter)+"(report_dt)", LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - " + out_dt.format(dateTimeFormatter)+"(out_dt)", LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - " + server_utc_now.format(dateTimeFormatter)+"(server_utc_now)", LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - " + db_utc_now.format(dateTimeFormatter) + "(db_utc_now)", LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); // report_dt가 출항시간(out_dt) 이전 일 시 출항시간을 기준. if(report_dt.isBefore(out_dt)) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - report_dt.isBefore(out_dt) : " + report_dt.isBefore(out_dt), LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); // report_dt보다 out_dt가 최근일 시 out_dt가 now보다 30분 또는 60분이 안지났으면 return; if(out_dt.plusMinutes(min).isAfter(db_utc_now)) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - out_dt.plusMinutes(min).isAfter(utc_now) : " + out_dt.plusMinutes(min).isAfter(db_utc_now), LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); continue; } // out_dt는 KST(쿼리에서 UTC를 KST로 바꾸기위해 +9하기때문에 9시간 -해줌) report_dt = out_dt; GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - report_dt = out_dt : " + report_dt, LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); } String mmsi = ship.getMmsi(); if(mmsi == null || "".equals(mmsi)) mmsi = "0"; lcVO.setMmsi(mmsi); int cnt = -1; switch (min) { case 30: requst_at = 1; lcVO.setRequst_at(requst_at); break; case 60: requst_at = 2; lcVO.setRequst_at(requst_at); break; } try { cnt = passivlcService.selectPassiv30or60Min(slot_no, report_dt, requst_at, min); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - passivlcService.selectPassiv30or60Min(cnt) : " + cnt, LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); } catch (SQLException se) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - selectPassiv30or60Min : " + se.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } catch (Exception e) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] CheckLastLc - selectPassiv30or60Min : " + e.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } // 30분, 60분 송신내역이 없으면 수동위치 30분차 송신 if(cnt < 1 && cnt != -1) { insertPassivLC(lcVO, min); } } } private void insertPassivLC(LastLcTrackVO lcVO, int min) { // 가장 최근 MODEM 조회 String tableYY = String.valueOf(lcVO.getReport_dt().getYear()).substring(2,4); String tableMM = String.valueOf(lcVO.getReport_dt().getMonthValue()); if(tableMM.length()==1) { tableMM = "0"+tableMM; } String table_name = "TB_LC_FSHOPRRPT_20" + tableYY + tableMM; GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - table_name : " + table_name, LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); LastLcTrackVO tmpVO = new LastLcTrackVO(); try { if(lcVO.getMssage_ty() == 12) { tmpVO = passivlcService.selectPassivLastModem(lcVO.getSlot_no(), lcVO.getReport_dt()); }else { tmpVO = passivlcService.selectLastModem(lcVO.getSlot_no(), lcVO.getReport_dt(), table_name); } } catch (SQLException se) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - selectLastModem : " + se.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } catch (Exception e) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - selectLastModem : " + e.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } if(tmpVO == null) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - insertPassivLC : SLOT_NO = "+ lcVO.getSlot_no() +" tmpVO is null", LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); return; } String modem = tmpVO.getSnd_modem(); lcVO.setLast_la(tmpVO.getLast_la()); lcVO.setLast_lo(tmpVO.getLast_lo()); lcVO.setLast_modem(tmpVO.getLast_modem()); lcVO.setSnd_modem(tmpVO.getSnd_modem()); lcVO.setFqnc(tmpVO.getFqnc()); String modemList = packetController.modemList(modem); lcVO.setModemList(modemList.toString()); lcVO.setSamenss_snd_dt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))); try { int cnt = 0; for(int i = 0; i < msgSndCnt; i++) { cnt += passivlcService.insertPassiv(lcVO); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - insertPassiv for cnt : " + i, LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - passivlcService.insertPassiv(cnt) : " + cnt, LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); Thread.sleep(100); if(0 < cnt) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - insertPassiv(Success) : MMSI : " + lcVO.getMmsi() + ", SLOT_NO : " + lcVO.getSlot_no() + ", MODEM_LIST : " + lcVO.getModemList() + ", SAMENSS_SND_DT : " + lcVO.getSamenss_snd_dt() , LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); }else { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - insertPassiv(Fail) : MMSI : " + lcVO.getMmsi() + ", SLOT_NO : " + lcVO.getSlot_no() + ", MODEM_LIST : " + lcVO.getModemList() + ", SAMENSS_SND_DT : " + lcVO.getSamenss_snd_dt() , LogLevelType.LOG_INFO, "PASSIVLC_AUTO"); } } if(0 < cnt) { // Insert는 n번 되지만 STD는 한번만 송신 STDMaker(lcVO); } } catch (SQLException se) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - insertPassiv : " + se.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } catch (Exception e) { GmtLogManager.getInstance().writeLevelLog("[CheckLastLcController] insertPassivLC - insertPassiv : " + e.getMessage(), LogLevelType.LOG_ERROR, "AllLog"); } } private void STDMaker(LastLcTrackVO lcVO) { int slot_no = lcVO.getSlot_no(); ShipInfoVO ship = scheduleManager.getShipInfo().get(slot_no); String STD = packetController.notRcvSTD(lcVO, ship); stdQue.add(STD); } public List SendSTD(){ List sendStdList = new ArrayList<>(); if(!stdQue.isEmpty()) { for(int i = 0; i < stdQue.size(); i++) { sendStdList.add(stdQue.poll()); } } return sendStdList; } }