package com.its.dsrc.xnettcp.dsrc.task;

import com.its.app.AppUtils;
import com.its.dsrc.entity.TbRseCtlr;
import com.its.dsrc.entity.TbRseCtrlHs;
import com.its.dsrc.xnetudp.service.CenterCommResponseService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import java.util.TimerTask;

@Slf4j
public class DsrcAsn1TimeoutTask extends TimerTask {

    private final TbRseCtlr obj;
    private final int       packetNmbr;

    public DsrcAsn1TimeoutTask(TbRseCtlr obj, int packetNmbr) {
        this.obj = obj;
        this.packetNmbr = packetNmbr;
    }

    @Override
    public void run() {

        MDC.put("id", this.obj.getLogKey());

        //log.info("DsrcAsn1TimeoutTask-run: packetNmbr: {}, obj: {}", this.packetNmbr, this.obj.toString());
        log.info("DsrcAsn1TimeoutTask-run: packetNmbr: {}, obj: {}", this.packetNmbr, this.obj.getID());
        this.obj.removeRegisteredCommands(this.packetNmbr, false);
        TbRseCtrlHs command = this.obj.getUserCommands(this.packetNmbr);
        if (command != null) {
            this.obj.removeUserCommands(this.packetNmbr);

            // 데이터베이스에 저장하고 운영단말로 전송한다.
            CenterCommResponseService responseService = (CenterCommResponseService) AppUtils.getBean(CenterCommResponseService.class);
            responseService.response(this.obj, command, (byte) 0x02);   // 명령 실패(타임아웃)로 설정
        }

        MDC.remove(this.obj.getLogKey());
        MDC.clear();
    }
}
