123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package com.its.pis.websocket;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.its.app.utils.ItsUtils;
- import com.its.pis.entity.TbPisInfr;
- import com.its.pis.process.DbmsJobData;
- import com.its.pis.process.DbmsJobType;
- import com.its.pis.websocket.C2F.message.*;
- import com.its.pis.websocket.common.SubscribeIdentifier;
- import com.its.pis.websocket.common.SubscribeRequest;
- import com.its.pis.websocket.common.SubscribeResponseAccept;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.FileUtils;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.socket.TextMessage;
- import org.springframework.web.socket.WebSocketSession;
- import java.io.File;
- import java.util.Base64;
- @Slf4j
- @Controller
- @RequestMapping(PisWebSocketConfig.WS_C2C_ENDPOINT)
- public class PisWebSocketHandlerC2C extends PisWebSocketHandler {
- public PisWebSocketHandlerC2C() {
- super();
- log.info("C2CWebSocketHandler() START");
- }
- //클라이언트가 웹소켓 서버로 메시지를 전송했을 때 실행
- @Override
- protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
- String payloadMessage = message.getPayload();
- if (payloadMessage == null || payloadMessage.trim().isEmpty()) {
- log.error("handleTextMessage: Payload data is empty");
- return;
- }
- log.info("handleTextMessage: Payload, RX] {}", payloadMessage);
- PisWebSocketSession sessionClient = PisWebSocketSessionManager.getInstance().getSession(session);
- if (sessionClient == null) {
- log.error("handleTextMessage: Request session not found, session will be closed {}", session);
- session.close();
- return;
- }
- TbPisInfr pis = sessionClient.getObj();
- if (pis == null) {
- log.error("handleTextMessage: PIS object not found, session will be closed {}", session);
- session.close();
- return;
- }
- C2FMessage<C2FMessagePayload> c2fMessage = this.mapper.readValue(payloadMessage, new TypeReference<C2FMessage<C2FMessagePayload>>(){});
- if (c2fMessage.getCommand() == null) {
- log.error("handleTextMessage: C2FMessage, {}", c2fMessage);
- log.error("handleTextMessage: command data not found, session will be closed {}", session);
- session.close();
- return;
- }
- if ("subscribe".equals(c2fMessage.getCommand())) {
- log.info("handleTextMessage: RX] subscribe");
- SubscribeRequest subscribe = this.mapper.readValue(payloadMessage, SubscribeRequest.class);
- //log.info("handleTextMessage: RX] {}", subscribe);
- log.info("RX] {}", this.mapper.writeValueAsString(subscribe));
- pis.channelLogin(pis.getSession());
- SubscribeResponseAccept response = SubscribeResponseAccept.builder()
- .identifier(SubscribeIdentifier.builder()
- .channel("ParkingLotChannel").build())
- .essntl_info("Not Setting Info")
- .type("confirm_subscription").build();
- String strMessage = this.mapper.writeValueAsString(response);
- sessionClient.sendMessage(response.getType(), new TextMessage(strMessage));
- log.info("TX] {}", strMessage);
- return;
- }
- try {
- String eventName = c2fMessage.getData().getPayload().getEvent_name();
- if (C2FConstants.prk_plce_sttus_info.equals(eventName)) {
- C2FMessage<PrkPlceSttusInfo> sttusInfo = this.mapper.readValue(payloadMessage, new TypeReference<C2FMessage<PrkPlceSttusInfo>>(){});
- C2FMessage<PrkPlceSttusInfo> sttusTemp = sttusInfo;
- sttusTemp.getData().getPayload().getPrk_place_image().setPrk_plce_image_data("base64 image data string");
- log.info("handleTextMessage: RX] prk_plce_sttus_info");
- log.info("RX] {}", this.mapper.writeValueAsString(sttusTemp));
- if (sttusInfo.getData().getPayload().getPrk_place_image() != null) {
- int imageType = sttusInfo.getData().getPayload().getPrk_place_image().getPrk_plce_image_type();
- String imageStringData = sttusInfo.getData().getPayload().getPrk_place_image().getPrk_plce_image_data();
- byte[] decodedBytes = Base64.getDecoder().decode(imageStringData);
- String saveDir = ItsUtils.createUserDir("/images/");
- String outputFileName = saveDir + sttusInfo.getData().getPayload().getPrk_plce_manage_no();
- switch(imageType) {
- case 0: outputFileName += ".bmp"; break;
- case 1: outputFileName += ".gif"; break;
- case 2: outputFileName += ".jpg"; break;
- case 3: outputFileName += ".png"; break;
- default: outputFileName += ""; break;
- }
- FileUtils.writeByteArrayToFile(new File(outputFileName), decodedBytes);
- }
- // byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
- // FileUtils.writeByteArrayToFile(new File(outputFileName), decodedBytes);
- // byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));
- // String encodedString = Base64.getEncoder().encodeToString(fileContent);
- } else if (C2FConstants.prk_plce_opr_info.equals(eventName)) {
- C2FMessage<PrkPlceOprInfo> oprInfo = this.mapper.readValue(payloadMessage, new TypeReference<C2FMessage<PrkPlceOprInfo>>(){});
- log.info("handleTextMessage: RX] prk_plce_opr_info");
- log.info("RX] {}", this.mapper.writeValueAsString(oprInfo));
- } else if (C2FConstants.prk_plce_rl_time_info.equals(eventName) || C2FConstants.prk_plce_rl_time_info_cycle.equals(eventName)) {
- C2FMessage<PrkPlceRlTimeResponseInfo> rlTimeInfo = this.mapper.readValue(payloadMessage, new TypeReference<C2FMessage<PrkPlceRlTimeResponseInfo>>(){});
- pis.setRlTimeInfo(rlTimeInfo);
- dbmsJobProcess.add(new DbmsJobData(DbmsJobType.DATA_TYPE_RL_TIME, false, rlTimeInfo, pis));
- log.info("handleTextMessage: RX] prk_plce_rl_time_info");
- log.info("RX] {}", this.mapper.writeValueAsString(rlTimeInfo));
- } else if (C2FConstants.prk_plce_reservation_response_info.equals(eventName)) {
- C2FMessage<PrkPlceReservationResponseInfo> reservationInfo = this.mapper.readValue(payloadMessage, new TypeReference<C2FMessage<PrkPlceReservationResponseInfo>>(){});
- log.info("handleTextMessage: RX] prk_plce_reservation_response_info");
- log.info("RX] {}", this.mapper.writeValueAsString(reservationInfo));
- } else if (C2FConstants.prk_plce_vhcl_location_response_info.equals(eventName)) {
- C2FMessage<PrkPlceVhclLocationResponseInfo> locationInfo = this.mapper.readValue(payloadMessage, new TypeReference<C2FMessage<PrkPlceVhclLocationResponseInfo>>(){});
- log.info("handleTextMessage: RX] prk_plce_vhcl_location_response_info");
- log.info("RX] {}", this.mapper.writeValueAsString(locationInfo));
- } else {
- log.error("handleTextMessage: RX] Unknown event name, {}", eventName);
- log.error("RX] Payload, {}", payloadMessage);
- }
- } catch(NullPointerException e) {
- log.error("handleTextMessage: RX] NullPointerException, Payload data null, {}", payloadMessage);
- }
- }
- }
|