package com.iwunu.nbiot.lock.listener;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.ctiot.aep.mqmsgpush.sdk.IMsgConsumer;
import com.ctiot.aep.mqmsgpush.sdk.IMsgListener;
import com.ctiot.aep.mqmsgpush.sdk.MqMsgConsumer;
import com.iwunu.nbiot.common.util.Base64;
import com.iwunu.nbiot.common.util.ByteTools;
import com.iwunu.nbiot.common.util.StringUtil;
import com.iwunu.nbiot.lock.config.AepMqConfig;
import com.iwunu.nbiot.lock.config.MessageConfig;
import com.iwunu.nbiot.lock.config.MqttCofigBean;
import com.iwunu.nbiot.lock.service.LockMqttServerService;
import com.iwunu.nbiot.lock.util.MessageMapParse;
@Component
public class AepMqMessageListener implements CommandLineRunner, DisposableBean {
	@Autowired
	private LockMqttServerService lockMqttServerService;
	@Autowired
	private AepMqConfig aepMqConfig;
	@Autowired
	private MessageConfig messageConfig;
	@Autowired
	private MqttCofigBean qttCofigBean;
	
	private IMsgConsumer consumer = null;
	private final static String noAvail ="//8=";
	@Override
	public void run(String... args) throws Exception {
		
		//创建消息接收Listener
		IMsgListener msgListener = new IMsgListener() {
			@Override
			public void onMessage(String msg) {
				JSONObject sendJson = new JSONObject();
				JSONObject msgJson = JSONObject.parseObject(msg);
				if(StringUtil.strIsNullOrEmpty(msgJson.getString("payload"))) {return;}
				if(JSONObject.parseObject(msgJson.getString("payload")).get("APPdata").equals(noAvail)){
					return ;
				}
				String appdata = JSONObject.parseObject(msgJson.getString("payload")).get("APPdata").toString();
				String deviceId = msgJson.get("deviceId").toString();
				String imei = msgJson.get("IMEI").toString();
				String message = ByteTools.bytesToString(Base64.decode(appdata));
				sendJson.put("deviceId", deviceId);
				sendJson.put("imei", imei);
				sendJson.put("message", message);
				if(MessageMapParse.getServerType(message).equals(messageConfig.getOnlineType())){
					lockMqttServerService.publish(qttCofigBean.getRTopics()[0], sendJson.toJSONString());
				}else if(MessageMapParse.getServerType(message).equals(messageConfig.getCmdTyp())) {
					if(messageConfig.reoprtTypeList().contains(MessageMapParse.getMessageCmdType(message))) {
						lockMqttServerService.publish(qttCofigBean.getRTopics()[3], sendJson.toJSONString());
					}else {
						lockMqttServerService.publish(qttCofigBean.getRTopics()[1], sendJson.toJSONString());
					}
				}else if(MessageMapParse.getServerType(message).equals(messageConfig.getOpenlockType())) {
					lockMqttServerService.publish(qttCofigBean.getRTopics()[2], sendJson.toJSONString());
				}else {
					return ;
				}
			}
		};
		//创建消息接收类
		 consumer = new MqMsgConsumer();
			try {
				//初始化
				/**
				 * @param server  消息服务server地址
				 * @param tenantId 租户Id
				 * @param token    用户认证token
				 * @param certFilePath 证书文件路径
				 * @param topicNames   主题名列表，如果该列表为空或null，则自动消费该租户所有主题消息
				 * @param msgListener 消息接收者
				 * @return 是否初始化成功
				 */
				consumer.init(aepMqConfig.getServerUrlAndPort(), aepMqConfig.getTenantId(),aepMqConfig.getToken(), "", null, msgListener);
		
				//开始接收消息
				consumer.start();
		

			} catch (Exception e) {
				e.printStackTrace();
			}
		
	}
	@Override
	public void destroy() throws Exception {
		//程序退出时，停止接收、销毁
		consumer.stop();
		consumer.destroy();
	}
}
