/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hw.mqtt.listener;

import com.hw.mqtt.enums.RedisKeys;
import net.dreamlu.iot.mqtt.core.server.MqttServerCreator;
import net.dreamlu.iot.mqtt.core.server.event.IMqttConnectStatusListener;
import net.dreamlu.mica.redis.cache.MicaRedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;

/**
 * 状态监听器
 * 性能损失小
 * @author WenJY
 * @date 2023-03-14 12:17
 * @param null 
 * @return null
 */
@Service
public class MqttConnectStatusListener implements IMqttConnectStatusListener, SmartInitializingSingleton, DisposableBean {
	private static final Logger logger = LoggerFactory.getLogger(MqttConnectStatusListener.class);

	private final ApplicationContext context;
	private final MicaRedisCache redisCache;
	private MqttServerCreator serverCreator;

	public MqttConnectStatusListener(ApplicationContext context, MicaRedisCache redisCache) {
		this.context = context;
		this.redisCache = redisCache;
	}

	@Override
	public void online(ChannelContext context, String clientId, String username) {
		logger.info("Mqtt clientId:{} username:{} online.", clientId, username);
		redisCache.sAdd(getRedisKey(), clientId);
	}

	@Override
	public void offline(ChannelContext context, String clientId, String username, String reason) {
		logger.info("Mqtt clientId:{} username:{} offline reason:{}.", clientId, username, reason);
		redisCache.sRem(getRedisKey(), clientId);
	}

	/**
	 * 设备上下线存储，key 的值为 前缀:nodeName
	 *
	 * @return redis key
	 */
	private String getRedisKey() {
		return RedisKeys.CONNECT_STATUS.getKey(serverCreator.getNodeName());
	}

	@Override
	public void afterSingletonsInstantiated() {
		this.serverCreator = context.getBean(MqttServerCreator.class);
	}

	@Override
	public void destroy() throws Exception {
		// 停机时删除集合
		redisCache.del(getRedisKey());
	}
}
