package com.xt.hb.gis.ctl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Controller;

import com.google.gson.JsonObject;
import com.xt.hb.gis.bean.GpsRealtimeBean;
import com.xt.hb.gis.common.GlobalData;
import com.xt.hb.gis.util.HttpProxy;

/**
 * 海事AIS相关Flex服务接口
 * 
 * @author 袁晓冬
 *
 */
@Controller
@RemotingDestination("gpsCtl")
public class GpsCtl extends BaseCtl {

	/**
	 * 获取港口港区企业树
	 * 
	 * @return
	 */
	public ArrayList<GpsRealtimeBean> queryGpsRealtime() {
		ArrayList<GpsRealtimeBean> beanList = new ArrayList<GpsRealtimeBean>();
		String url = GlobalData.GPS_REALTIME_URL;
		JSONArray jsonArray = null;
		try {
			String strJson = HttpProxy.http_get_json(url);
			jsonArray = JSONArray.fromObject(strJson);
			if(null != jsonArray && jsonArray.size() > 0) {
				GpsRealtimeBean bean = null;
				for (int i=0; i<jsonArray.size();i++) {
					bean = new GpsRealtimeBean();
					JSONObject obj = (JSONObject) jsonArray.get(i);
					bean.setAlarm(obj.get("alarm").toString());
					bean.setAreaid(obj.get("areaid").toString());
					bean.setFx(obj.get("fx").toString());
					bean.setGd(obj.get("gd").toString());
					bean.setGpsid(obj.get("gpsid").toString());
					String lon = obj.get("jd").toString();
					if(lon.length() > 3) {
						if(lon.startsWith("1")) {
							lon = lon.substring(0, 3) + "." + lon.substring(4, lon.length());
						} else {
							lon = lon.substring(0, 2) + "." + lon.substring(3, lon.length());
						}
						bean.setJd(lon);
					}
					bean.setRecvdate(obj.get("recvdate").toString());
					bean.setRecvtime(obj.get("recvtime").toString());
					String sd = obj.get("sd").toString();
					if(sd.length() > 0 && !"0".equals(sd)) {
						sd = String.valueOf(Integer.parseInt(sd)/1000);
					}
					bean.setSd(sd);
					bean.setSegment(obj.get("segment").toString());
					bean.setStatus1(obj.get("status1").toString());
					bean.setStatus2(obj.get("status2").toString());
					String lat = obj.get("wd").toString();
					if(lat.length() > 3) {
						lat = lat.substring(0, 2) + "." + lat.substring(3, lat.length());
						bean.setWd(lat);
					}
					beanList.add(bean);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return beanList;
	}
	
}
