package com.imdroid.ntripproxy.executor;

import com.imdroid.ntripproxy.message.D341GgaMessage;
import com.imdroid.ntripproxy.service.UDPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author Layton
 * @date 2023/2/2 20:50
 */
@Component
public class D341GgaMessageExecutor implements Executor<D341GgaMessage, Void> {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UDPClient ntripClient;
    @Autowired
    UDPClient rtcmClient;

    @Override
    public Void execute(D341GgaMessage message) {
        if (logger.isDebugEnabled()) {
            logger.debug("receive d341 message of device: "+message.getId()+", seq:"+message.getSeq()+", len:"+message.getLen());
        }
        // 发给ntrip server
        try {
            byte[] transData = new byte[message.getTransBuf().readableBytes()];
            message.getTransBuf().getBytes(0, transData);
            byte[] srcData = new byte[message.getSrcBuf().readableBytes()];
            message.getSrcBuf().getBytes(0, srcData);

            // send to ntrip server
            ntripClient.sendWithDebug(transData);
            // send to rtcm server
            rtcmClient.send(srcData);
        }
        catch (Exception e){
            logger.error("Exception {}",e);
        }

        return null;
    }

    @Override
    public Class<?> getMessageType() {
        return D341GgaMessage.class;
    }
}
