package com.evps.comm.local.service; import com.evps.comm.local.config.ApplicationConfig; import com.evps.comm.local.dto.CenterDto; import com.evps.comm.local.repository.ApplicationRepository; import com.evps.comm.local.xnet.client.process.request.*; import com.evps.common.dto.NET; import com.evps.common.kafka.dto.*; import com.evps.common.utils.EvpsUtils; import com.its.common.utils.Elapsed; import com.its.common.utils.TimeUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @Slf4j @Service @RequiredArgsConstructor public class EvpsLocalClientManagerService { private final ApplicationConfig config; private boolean isRunning = false; @PostConstruct private void init() { this.isRunning = false; } public void sendPing() { if (this.isRunning) { log.info("Sending ping: Evps Data Sending...."); return; // 시뮬레이션 데이터 전송 중이면 리턴 } CenterDto center = ApplicationRepository.center; if (center.getNetState().getState() < NET.LOGIN_WAIT || center.getNetState().getChannel() == null) { log.info("Sending ping: Center communication failed."); return; // 통신연결이 해제 되었으면 리턴 } long currentTimeMilliSeconds = System.currentTimeMillis(); if (currentTimeMilliSeconds - center.getLastCommTm() >= (this.config.getNetPingSeconds() * 1000L)) { // 마지막 전송 시각을 계산해서 전송해야 할 경우에만 전송 String now = TimeUtils.getCurrentTimeString(); String serviceId = this.config.getRegionId() + now + "00"; EvpsNetPing ping = new EvpsNetPing(serviceId, center.getSeq().nextValue()); center.sendData(ping); } else { log.info("Sending ping: Last ping sent {} seconds ago. net ping duration {} seconds.", (currentTimeMilliSeconds - center.getLastCommTm()) / 1000L, this.config.getNetPingSeconds()); } } public void run() { if (this.isRunning) { log.warn("EvpsLocalClientManagerService: Already running...................."); return; } CenterDto center = ApplicationRepository.center; if (center.getNetState().getState() < NET.LOGIN_WAIT || center.getNetState().getChannel() == null) { log.info("EvpsLocalClientManagerService: Center communication failed."); return; // 통신연결이 해제 되었으면 리턴 } this.isRunning = true; Elapsed elapsed = new Elapsed(); EvpsDataSimGen simGenData = new EvpsDataSimGen(this.config.getRegionId(), "대종로 사거리", "74무5035", 2); simGenData.makeEvpsKafkaSimulatorData(); KafkaEvpsServiceDto serviceDto = simGenData.getServiceStartDto(); serviceDto.setClctDt(EvpsUtils.getClctDt()); EvpsService service = new EvpsService(serviceDto); center.sendData(service); KafkaEvpsNodeDto nodeDto = simGenData.getEvpsNodeDto(); nodeDto.setClctDt(EvpsUtils.getClctDt()); EvpsNode node = new EvpsNode(nodeDto); center.sendData(node); for (int ii = 0; ii < simGenData.getListSignalDto().size(); ii++) { TimeUtils.sleep(1000); KafkaEvpsSignalDto signalDto = simGenData.getListSignalDto().get(ii); signalDto.setClctDt(EvpsUtils.getClctDt()); EvpsSignal signal = new EvpsSignal(signalDto); center.sendData(signal); KafkaEvpsEventDto eventDto = simGenData.getListEventDto().get(ii); eventDto.setClctDt(EvpsUtils.getClctDt()); EvpsEvent event = new EvpsEvent(eventDto); center.sendData(event); } KafkaEvpsServiceEndDto endDto = simGenData.getServiceEndDto(); endDto.setClctDt(EvpsUtils.getClctDt()); EvpsServiceEnd serviceEnd = new EvpsServiceEnd(endDto); center.sendData(serviceEnd); log.info("EvpsLocalClientManagerService: Run completed. {}", elapsed.elapsedTimeStr()); this.isRunning = false; } }