package net.vtstar.zhongtong.avi.mqtt.handle;

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.mqtt.handler.BizMessageHandler;
import net.vtstar.scada.base.mqtt.utils.MqttConstants;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.Assert;

@Slf4j
public abstract class AbstractTesterMessageHandler implements BizMessageHandler, InitializingBean {

	private final String equipCode;
	private final String topicType;

	public AbstractTesterMessageHandler(String equipCode, String topicType) {
		this.equipCode = equipCode;
		this.topicType = topicType;
	}

	@Override
	public boolean match(Message<?> message) {
		MessageHeaders headers = message.getHeaders();

		String equipmentName = headers.get(MqttConstants.MSG_HEADER_KEY_EQUIPMENTNAME, String.class);
		String type = headers.get(MqttConstants.MSG_HEADER_KEY_TOPICTYPE, String.class);

		return this.equipCode.startsWith(equipmentName) && topicType.equals(type);
	}

	@Override
	public void afterPropertiesSet() {
		Assert.notNull(topicType, "argument 'topicType' cannot be null");
		Assert.notNull(equipCode, "argument 'equipType' cannot be null");
	}

}
