package com.imdroid.sideslope.executor;

import com.imdroid.sideslope.calc.MultiLineGNSSCalcService;
import com.imdroid.sideslope.message.D342LocationMessage;
import com.imdroid.sideslope.sal.Device;
import com.imdroid.sideslope.sal.DeviceService;
import com.imdroid.common.util.ThreadManager;
import com.imdroid.sideslope.service.DataPersistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

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

    @Autowired
    private MultiLineGNSSCalcService gnssCalcService;
    @Resource(name = "local")
    private DeviceService deviceService;
    @Autowired
    private DataPersistService dataPersistService;

    @Override
    public Void execute(D342LocationMessage message) {

        // 补齐tenantId
        Device device = deviceService.findByDeviceId(message.getId());
        if(device == null) return null;
        message.setTenantId(device.getTenantId());
        message.setProjectId(device.getProjectId());
        message.setFwdId(device.getFwdId());
        device.updateRx(message.getHeader(), message.getLen(),message.getMessageList().size());

        ThreadManager.getFixedThreadPool().submit(() -> {
            gnssCalcService.calc(message);
            if(message.getMessageList().size()==0){
                dataPersistService.saveD342Stat(message, device);
                device.clearD342Stat();
            }
        });

        return null;
    }

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