package cn.base.pushlet;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;

import org.apache.log4j.Logger;

import cn.base.bean.BeanHelper;
import cn.base.util.JsonUtil;
import cn.base.util.RHUtil;

/**
 * 实时接收amq发送的巡检任务告警，推送至前端
 * */
public class VideoTaskAlarmPushlet{
	static Logger logger = Logger.getLogger(VideoTaskAlarmPushlet.class);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	// 解决中文编码
	public static String encode(String str) {
		try {
			str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	//将最新的告警信息推送至地图页面中。
	public static class VideoTaskAlarmPushletData extends EventPullSource{
		@Override
		protected long getSleepTime() {
			// TODO Auto-generated method stub
			return 5000;   //1秒=1000毫秒
		}
		
		@Override
		protected Event pullEvent() {
			// TODO Auto-generated method stub
			Event event = Event.createDataEvent("/videoTaskAlarmData");
			List list = new ArrayList();
			try {
				list = BeanHelper.getVideoTaskAlarmDateService().getVideoTaskAlarmDataList();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(RHUtil.isEmpty(list)){
				event.setField("json", "");
			}else{
				String jsonStr = JsonUtil.renderDeepJson(list);
				jsonStr = encode(jsonStr);
				event.setField("json", jsonStr);
			}		
			return event;
		}
	}
}
