package com.zd.laboratory.mqtt.service.hxp;

import com.zd.laboratory.mapper.HxpTerminalMapper;
import com.zd.laboratory.mapper.LabHardwareMapper;
import com.zd.laboratory.mqtt.constants.MqttConstants;
import com.zd.laboratory.mqtt.entiy.MessageBody;
import com.zd.laboratory.mqtt.enums.SendMode;
import com.zd.laboratory.mqtt.service.TerminalRouter;
import com.zd.laboratory.mqtt.service.impl.CommonSend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HxpLockService implements TerminalRouter {
    private Logger logger = LoggerFactory.getLogger(HxpLockService.class);

    @Autowired
    private HxpTerminalMapper hxpTerminalMapper;
    @Autowired
    private CommonSend commonSend;

    public HxpLockService() {
        TerminalRouter.routerMap.put("HxpLockService", this);
    }

    @Override
    public void onLine(String codeNum) {
        String[] codes = codeNum.split("/");
        if(codes != null && codes.length == 2){
            String relayCode = codes[0];
            String lockId = codes[1];
            Long subId = hxpTerminalMapper.selectSubIdByLockId(relayCode, lockId);
            if(subId != null){
                MessageBody messageBody = new MessageBody();
                messageBody.setData("柜锁操作成功");
                logger.info("发送柜锁操作mqtt");
                commonSend.send(MqttConstants.SYS_LOGO_CONFIG + subId + "/" + lockId, messageBody, SendMode.ONCE);
            }
        }
    }

    /**
     * 柜锁是否下线检测
     * @param codeNum
     */
    @Override
    public void offLine(String codeNum) {
        logger.info("智能柜锁采集器编码：" + codeNum);
    }
}
