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 TrafficEventAlarmPushlet{
	static Logger logger = Logger.getLogger(TrafficEventAlarmPushlet.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 TrafficEventAlarmPushletData 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("/trafficEventAlarmData");
			List list = new ArrayList();
			try {
//				list = BeanHelper.getTrafficEventAlarmStateDataService().getTrafficEventAlarmStateList();
			} 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;
		}
	}
}
