package com.imdroid.sideslope.executor;

import com.imdroid.sideslope.calc.GNSSDataCalcService;
import com.imdroid.sideslope.message.D341LocationMessage;
import com.imdroid.sideslope.sal.Device;
import com.imdroid.sideslope.sal.DeviceService;
import com.imdroid.common.util.ThreadManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 D341LocationMessageExecutor implements Executor<D341LocationMessage, Void> {

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

    @Autowired
    private GNSSDataCalcService gnssCalcService;
    @Resource(name = "local")
    private DeviceService deviceService;

    @Override
    public Void execute(D341LocationMessage message) {
        if (logger.isDebugEnabled()) {
            logger.debug("receive d341 location message of device:{}", message.getId());
        }
        // 补齐tenantId，要求快速处理，因此用缓存，不要每次都查数据库
        Device device = deviceService.findByDeviceId(message.getId());
        if(device == null) return null;
        message.setTenantId(device.getTenantId());
        device.updateD341Bytes(message.getLen());

        ThreadManager.getFixedThreadPool().submit(() -> {
            gnssCalcService.calcSingle(message,true);
        });

        return null;
    }

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