package com.imdroid.sideslope.executor;

import com.imdroid.secapi.client.BeidouClient;
import com.imdroid.common.util.ByteUtil;
import com.imdroid.sideslope.message.D31xConfigAckMessage;
import com.imdroid.sideslope.sal.Device;
import com.imdroid.sideslope.sal.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Layton
 * @date 2023/2/2 20:40
 */
@Component
public class D31xConfigAckMessageExecutor implements Executor<D31xConfigAckMessage, Void> {
    @Autowired
    private BeidouClient beidouClient;
    @Resource(name = "local")
    private DeviceService deviceService;

    @Override
    public Void execute(D31xConfigAckMessage message) {
        // 补齐tenantId
        Device device = deviceService.findByDeviceId(message.getId());
        if(device == null) return null;
        message.setTenantId(device.getTenantId());
        // 转发应答
        try {
            beidouClient.onConfigAck(message.getId(), device.getTenantId(),
                    ByteUtil.bytesToHexString(message.getSrcData()));
        }
        catch (Exception e){

        }
        return null;
    }

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