package com.its.dsrc.xnetudp.service;

import com.its.app.AppUtils;
import com.its.dsrc.entity.TbRseCtlr;
import com.its.dsrc.entity.TbRseCtrlHs;
import com.its.dsrc.process.DbmsData;
import com.its.dsrc.process.DbmsDataProcess;
import com.its.dsrc.process.DbmsDataType;
import com.its.dsrc.xnetudp.protocol.CENTER_DSRC_RES_CONTROL;
import com.its.dsrc.xnetudp.thread.CenterCommClientSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CenterCommResponseService {

    public int response(TbRseCtlr obj, TbRseCtrlHs ctlrCntl, byte result) {
        int rspsType = (int)result;
        ctlrCntl.setRSPS_TYPE(String.valueOf(rspsType));

        // 제어명령 데이터베이스 저장
        DbmsDataProcess dbmsDataProcess = (DbmsDataProcess) AppUtils.getBean(DbmsDataProcess.class);
        dbmsDataProcess.add(new DbmsData(DbmsDataType.DBMS_DATA_CTRL_HS, false, ctlrCntl));

        // 제어명령 운영단말 응답
        CENTER_DSRC_RES_CONTROL res = new CENTER_DSRC_RES_CONTROL(ctlrCntl.getReq().getMsgSeq());
        res.setMsgSeq(ctlrCntl.getReq().getMsgSeq());
        res.setCtlrNmbr(Integer.valueOf(ctlrCntl.getReq().getCtlrNmbr()).intValue());
        res.setReqSeq(ctlrCntl.getReq().getReqSeq());
        res.setResCode(result);
        CenterCommClientSender response = (CenterCommClientSender) AppUtils.getBean(CenterCommClientSender.class);
        response.run(ctlrCntl.getReqIpAddress(), ctlrCntl.getReqPort(), res.getBuffer());

        return 0;
    }
}
