UpstreamEntrance.java 2.78 KB
package com.bsth.controller.directive;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsth.data.directive.DayOfDirectives;
import com.bsth.data.pilot80.PilotReport;
import com.bsth.entity.directive.D80;
import com.bsth.entity.directive.DC0_A4;
import com.bsth.entity.directive.DirectiveReponse;

/**
 * 
 * @ClassName: UpstreamEntrance
 * @Description: TODO(车载网关HTTP上行入口)
 * @author PanZhao
 * @date 2016年6月7日 下午3:00:01
 *
 */
@RestController
@RequestMapping("/control")
public class UpstreamEntrance {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	DayOfDirectives dayOfDirectives;
	
	@Autowired
	PilotReport pilotReport;

	@RequestMapping(value = "/upstream", method = RequestMethod.POST)
	public String main(@RequestParam String json) {
		logger.info("upstream: " + json);
		try {
			JSONObject jsonParam = JSONObject.parseObject(json);

			// 60协议回复
			if (jsonParam.getInteger("operCode") == null && jsonParam.getInteger("status") != null) {
				try {
					DirectiveReponse reply = JSON.toJavaObject(jsonParam, DirectiveReponse.class);
					dayOfDirectives.reply(reply);
				} catch (NumberFormatException e) {
					logger.error("NumberFormatException ,,,,一般是老数据,msgId太大");
				}
			}
			// 80协议上报
			else if (jsonParam.getInteger("operCode") == 0X80) {
				try {
					JSONObject data = jsonParam.getJSONObject("data");
					switch (data.getShort("operCode2")) {
					case 0x26:
						// 驾驶员上报
						D80 d80 = JSON.toJavaObject(jsonParam, D80.class);
						pilotReport.report(d80);
						break;

					case 0xA4:
						data.put("port", data.getString("port").trim());
						data.put("posPort", data.getString("posPort").trim());
						data.put("posIpAddress", data.getString("posIpAddress").trim());
						data.put("ipAddress", data.getString("ipAddress").trim());
						
						DC0_A4 c0a4 = JSON.toJavaObject(jsonParam, DC0_A4.class);
						System.out.println(c0a4);
						break;
					}
					
				} catch (Exception e) {
					logger.error("", e);
				}
			}
			// 64协议 线路切换回复
			else if (jsonParam.getInteger("operCode") == 0X64) {
				dayOfDirectives.reply64(jsonParam);
			} else
				logger.warn("未知的上行数据,p: " + json);

		} catch (Exception e) {
			logger.error("", e);
		}
		return "{\"errCode\":0}";
	}
}