package com.jsowell.netty.handler;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 地锁数据上送
 *
 * 地锁状态/报警信息变化时，桩立刻上送变位/报警信息；地锁状态不变化时，每隔 5 分钟周期 性上送地锁状态。若无报警信息，不上送。
 *
 * @author JS-ZZA
 * @date 2022/9/19 15:21
 */
@Slf4j
@Component
public class GroundLockDataUploadHandler extends AbstractHandler{
    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.GROUND_LOCK_DATA_UPLOAD_CODE.getBytes());

    @Override
    public void afterPropertiesSet() throws Exception {
        YKCOperateFactory.register(type, this);
    }

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        log.info("[===地锁数据上送===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString());
        // 消息体
        byte[] msgBody = ykcDataProtocol.getMsgBody();
        // 桩编码
        byte[] pileSnByteArr = BytesUtil.copyBytes(msgBody, 0, 7);
        // 枪号
        byte[] connectorByteArr = BytesUtil.copyBytes(msgBody, 7, 1);
        /**
         * 车位锁状态
         * 0x00：未到位状态
         * 0x55：升锁到位状态
         * 0xFF：降锁到位状态
         */
        byte[] parkingLockStatusByteArr = BytesUtil.copyBytes(msgBody, 8, 1);
        // 车位状态  0x00：无车辆 0xFF：停放车辆
        byte[] parkingStatusByteArr = BytesUtil.copyBytes(msgBody, 9, 1);
        // 地锁电量状态  百分比值0~100
        byte[] groundLockElectricByteArr = BytesUtil.copyBytes(msgBody, 10, 1);
        // 报警状态   0x00：正常无报警  0xFF：待机状态摇臂破坏  0x55：摇臂升降异常(未到位)
        byte[] alarmStatusByteArr = BytesUtil.copyBytes(msgBody, 11, 1);
        // 预留位  全部置0
        byte[] waitingUseByteArr = BytesUtil.copyBytes(msgBody, 12, 4);

        return null;
    }
}
