package com.pwt.provider;

import com.pwt.Globals;
import com.pwt.api.ChargerApi;
import com.pwt.netty.ChargerServer;
import com.pwt.netty.handler.ChargerServerHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.pwt.Globals.plcClient;

/**
 * @author hanshuangxi
 * @date 2023/7/31 10:48
 */
@Service
@Slf4j
public class ChargerProvider implements ChargerApi {

    @Autowired
    private ChargerServer chargerServer;

    @Override
    public void startCharge(int holderNo) {
        //判断当前是否有电池
        boolean[] holderState = Globals.plcClient.getHolderState();
        if (!holderState[holderNo - 1]) {
            log.error("定时充电失败,{}号仓位没有电池！", holderNo);
            return;
        }
        String gun = ChargerServerHandler.HOLDER_AND_GUN_MAP.get(holderNo);
        try {
            chargerServer.startCharge(gun.substring(0, gun.length() - 1), gun, (int) System.currentTimeMillis() / 1000, false);
        } catch (Exception e) {
            log.error("定时充电失败，{}", e.getMessage());
        }
    }
}
