dianxin-guizhou-forward
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
16KB

  1. package com.ssjl.zhaobiao.guizhou.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.google.common.base.Strings;
  4. //import com.google.gson.annotations.JsonAdapter;
  5. import com.ssjl.zhaobiao.guizhou.entity.*;
  6. import com.ssjl.zhaobiao.guizhou.entity.GZMessage.*;
  7. import com.ssjl.zhaobiao.guizhou.entity.IOT.*;
  8. import com.ssjl.zhaobiao.guizhou.entity.IOT.Api.IotApiActiveDevice;
  9. import com.ssjl.zhaobiao.guizhou.entity.IOT.Api.IotApiResponse;
  10. import com.ssjl.zhaobiao.guizhou.handle.GZResponseHandle;
  11. import com.ssjl.zhaobiao.guizhou.repository.DianxinGzDeviceConfigMapper;
  12. import com.ssjl.zhaobiao.guizhou.repository.GpsDeviceStatusMapper;
  13. import com.ssjl.zhaobiao.guizhou.service.IGZPlatformCmdService;
  14. import com.ssjl.zhaobiao.guizhou.utils.*;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.checkerframework.checker.units.qual.A;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import java.io.IOException;
  23. import java.time.LocalDateTime;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. @Slf4j
  27. @RestController
  28. @RequestMapping("/iot")
  29. public class IotController {
  30. @Autowired
  31. IGZPlatformCmdService cmdService;
  32. @Autowired
  33. GZHttpUtil httpUtil;
  34. @Autowired
  35. GZResponseHandle responseHandle;
  36. @Autowired
  37. GpsDeviceStatusMapper gpsDeviceStatusMapper;
  38. @Autowired
  39. DianxinGzDeviceConfigMapper dianxinGzDeviceConfigMapper;
  40. @Autowired
  41. OkHttpUtil okHttpUtil;
  42. @Autowired
  43. IOTApiUtil iotApiUtil;
  44. @Value("${guizhou.device.zhizhong}")
  45. String zhizhongDevices;
  46. @Value("${guizhou.device.zhanneng}")
  47. String zhannengDevices;
  48. @Value("${guizhou.device.tianbo}")
  49. String tianboDevice;
  50. @Value("${guizhou.http.zhizhong}")
  51. String zhizhongUrl;
  52. @Value("${guizhou.http.zhanneng}")
  53. String zhannengUrl;
  54. @Value("${guizhou.factory}")
  55. String factory;
  56. @RequestMapping("/online")
  57. public String online(String imei) throws IOException {
  58. // 接收到上线消息,则认为是开机。开机流程有两种,根据电信平台返回的情况而定
  59. // 1.1 终端主动向平台发起数据链接, 并发送位置信息, 消息参见 4.2.8;
  60. //1.2 平台发起立即查询, 参见 4.2.2, 发送的消息为表 4-3 中的立即上报终端信息, 需要查询的
  61. //参数包括终端执行的协议版本号、 终端型号、 终端型号生产厂家、 终端 IMSI 号、 终端软件版本号, 为
  62. //表 4-1-3 中的参数类型 u、 v、 x、 y、 z。 与数据库原保存数据比对;
  63. //1.3 平台返回“转发短信到指到号码” 的指令, 数据指令参见 4.2.3, 短信内容见 5.2.1;
  64. //1.4 终端执行“转发短信到指到号码” 的指令, 并等待 20 - 30 秒;
  65. //1.5 终端向平台发送中 4.2.9 的指令消息;
  66. //1.6 平台返回全部配置;
  67. //1.7 终端上行应答指令;
  68. //1.8 平台通过数据通道向终端发送 4.2.3 中的消息, 命令终端转发开关机提醒短信;
  69. //1.9 终端按照步骤 1.8 中的命令, 向监护人终端发送短信提醒。
  70. //注: 当终端没有设置本机号码时, 步骤中 1.1, 1.5 中的终端号码为终端机器 MEID 码, 见 4.1.3.7
  71. //中的 D
  72. // 2.1 终端主动向平台发起数据链接, 并发送位置信息, 消息参见 4.2.8;
  73. //2.2 平台发起立即查询, 参见 4.2.2, 发送的消息为表 4-3 中的立即上报终端信息, 需要查询的
  74. //参数包括终端执行的协议版本号、 终端型号、 终端型号生产厂家、 终端 IMSI 号、 终端软件版本号,
  75. //为表 4-1-3 中的参数类型 u、 v、 x、 y、 z。 与数据库原保存数据比对;
  76. //2.3 平台返回全部配置;
  77. //2.4 终端上行应答指令;
  78. //2.5 平台通过数据通道向终端发送 4.2.3 中的消息, 命令终端转发开关机提醒短信;
  79. //2.6 终端按照步骤 1.8 中的命令, 向监护人终端发送短信提醒。
  80. log.info(imei + "上线");
  81. if(!checkImei(imei, "/online", "")){
  82. return "success";
  83. }
  84. GZLocationCDMA cdma = new GZLocationCDMA();
  85. GZLocationExtend extend = new GZLocationExtend();
  86. extend.setNetType(GZNetType.CDMA);
  87. extend.setData1(cdma);
  88. byte[] bid = { 0x00, 0x02 };
  89. cdma.setBID(bid);
  90. byte[] sid = {0x22, 0x41};
  91. cdma.setSID(sid);
  92. byte[] nid = { 0x36, 0x14};
  93. cdma.setNID(nid);
  94. int power = 50;
  95. GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(imei);
  96. if(gpsDeviceStatus != null){
  97. String statusStr = gpsDeviceStatus.getDeviceStatus();
  98. IOTDeviceStatus status = IOTDeviceStatus.build(statusStr);
  99. power = status.getBattery();
  100. }
  101. GZDeviceStatus deviceStatus = new GZDeviceStatus();
  102. deviceStatus.setOpenFirst(true);
  103. GZDevicePower devicePower = new GZDevicePower();
  104. devicePower.setCharge(false);
  105. devicePower.setPower(power);
  106. LocalDateTime time = LocalDateTime.now();
  107. GZAskLocation askLocation = new GZAskLocation(imei, time.getHour(),time.getMinute(),time.getSecond(),time.getDayOfMonth(),15976140,0,
  108. 63968668,0,0,0, deviceStatus, devicePower, 0, 0, true, extend);
  109. GZRequest req = new GZRequest();
  110. req.setImei(imei);
  111. req.setIndex(GZUtil.getMessageIndex());
  112. req.setStatus(GZRequestStatus.RESPONSE_ALL);
  113. req.setContent(askLocation);
  114. byte[] res = httpUtil.send(imei, "", req);
  115. System.out.println(ByteUtil.bytesToHexString(res));
  116. // 调用IOTAPI激活设备
  117. log.info(imei + "激活设备");
  118. IotApiActiveDevice active = new IotApiActiveDevice();
  119. active.setSerialNo(imei);
  120. active.setCommandType(1);
  121. IotApiResponse rs = iotApiUtil.activeDevice(active);
  122. System.out.println(JSON.toJSONString(rs));
  123. log.info(imei + "激活返回:" + JSON.toJSONString(rs));
  124. // 处理电信返回数据
  125. res = (byte[]) responseHandle.process(imei, res);
  126. System.out.println(ByteUtil.bytesToHexString(res));
  127. log.info(imei + "电信返回:" + ByteUtil.bytesToHexString(res));
  128. return "success";
  129. }
  130. @RequestMapping("/gps")
  131. public String gpsHandle(@RequestBody IOTGps gps, String imei) throws IOException {
  132. log.info(imei + ":gps ");
  133. if(!checkImei(imei, "/gps", JSON.toJSONString(gps))){
  134. log.info(imei + ",gps not in device");
  135. return "not in device";
  136. }
  137. DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
  138. if(!isAtLocationTime(gzDeviceConfig.getLocationInterval())){
  139. log.info(imei + ",gps not in time");
  140. return "not in time";
  141. }
  142. log.info(imei + ",gps ,v=" + JSON.toJSONString(gps));
  143. // 没有基站数据,先写死
  144. GZRequest req = new GZRequest();
  145. gps.setImei(imei);
  146. req.setImei(gps.getImei());
  147. req.setIndex(GZUtil.getMessageIndex());
  148. req.setStatus(GZRequestStatus.RESPONSE_NO);
  149. GZLocationCDMA cdma = new GZLocationCDMA();
  150. GZLocationExtend extend = new GZLocationExtend();
  151. extend.setNetType(GZNetType.CDMA);
  152. extend.setData1(cdma);
  153. byte[] bid = { 0x00, 0x02 };
  154. cdma.setBID(bid);
  155. byte[] sid = {0x22, 0x41};
  156. cdma.setSID(sid);
  157. byte[] nid = { 0x36, 0x14};
  158. cdma.setNID(nid);
  159. GZDeviceStatus deviceStatus = new GZDeviceStatus();
  160. // deviceStatus.setOpenFirst(true);
  161. GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(gps.getImei());
  162. IOTDeviceStatus iotDeviceStatus = IOTDeviceStatus.build(gpsDeviceStatus.getDeviceStatus());
  163. GZDevicePower power = new GZDevicePower();
  164. power.setCharge(false);
  165. power.setPower(iotDeviceStatus.getBattery());
  166. LocalDateTime time = LocalDateTime.now();
  167. GZLocation location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
  168. GZUtil.convertLocation(gps.getLatitude()), 0, GZUtil.convertLocation(gps.getLongitude()), 0, 0, 0,
  169. deviceStatus, power, 70, 3, false, extend);
  170. req.setContent(location);
  171. byte[] b = req.toByte();
  172. System.out.println(ByteUtil.bytesToHexString(b));
  173. String phone = gzDeviceConfig == null ? "" : gzDeviceConfig.getPhone();
  174. byte[] res = httpUtil.send(imei, phone, req);
  175. log.info(imei + ",gps 电信返回:" + ByteUtil.bytesToHexString(res));
  176. System.out.println(ByteUtil.bytesToHexString(res));
  177. return "success";
  178. }
  179. @RequestMapping("/lbs")
  180. public String lbsHandle(@RequestBody IOTLbs lbs, String imei) throws IOException {
  181. log.info(imei + ",lbs");
  182. if(!checkImei(imei, "/lbs", JSON.toJSONString(lbs))){
  183. log.info(imei + ",lbs not in device");
  184. return "not in device";
  185. }
  186. DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
  187. if(!isAtLocationTime(gzDeviceConfig.getLocationInterval())){
  188. log.info(imei + ",lbs not in time");
  189. return "not in time";
  190. }
  191. log.info(imei + "lbs,v=" + JSON.toJSONString(lbs));
  192. GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(imei);
  193. IOTDeviceStatus iotDeviceStatus = IOTDeviceStatus.build(gpsDeviceStatus.getDeviceStatus());
  194. GZDevicePower power = new GZDevicePower();
  195. power.setCharge(false);
  196. power.setPower(iotDeviceStatus.getBattery());
  197. GZDeviceStatus deviceStatus = new GZDeviceStatus();
  198. LocalDateTime time = LocalDateTime.now();
  199. GZLocationExtend extend = new GZLocationExtend();
  200. GZLocation location;
  201. if(lbs.getCdma().equals("0")){
  202. // GSM
  203. GZLocationGSM gsm = GZUtil.toGSM(lbs.getBts(), lbs.getNearbts());
  204. extend.setNetType(GZNetType.GSM);
  205. extend.setData1(gsm);
  206. location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
  207. 0, 0, 0, 0, 0, 0,
  208. deviceStatus, power, 70, 0, true, extend);
  209. }else{
  210. // CDMA
  211. String[] bts = lbs.getBts().split(",");
  212. GZLocationCDMA cdma = new GZLocationCDMA();
  213. cdma.setSID(ByteUtil.toByte(Integer.parseInt(bts[0]), 2));
  214. cdma.setNID(ByteUtil.toByte(Integer.parseInt(bts[1]), 2));
  215. cdma.setBID(ByteUtil.toByte(Integer.parseInt(bts[2]), 2));
  216. double lat = Double.parseDouble(Strings.isNullOrEmpty(bts[3]) ? bts[3] : "0");
  217. double lon = Double.parseDouble(Strings.isNullOrEmpty(bts[4]) ? bts[4] : "0");
  218. int signal = Integer.parseInt(bts[6]);
  219. extend.setNetType(GZNetType.CDMA);
  220. extend.setData1(cdma);
  221. location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
  222. GZUtil.convertLocation(lat), 0, GZUtil.convertLocation(lon), 0, 0, 0,
  223. deviceStatus, power, signal, 0, true, extend);
  224. }
  225. byte[] res = httpUtil.send(imei, gzDeviceConfig.getPhone(), location);
  226. System.out.println(ByteUtil.bytesToHexString(res));
  227. log.info(imei + ",lbs 电信返回:" + ByteUtil.bytesToHexString(res));
  228. return "success";
  229. }
  230. @RequestMapping("/wifi")
  231. public String wifiHandle(@RequestBody IOTWifi wifi, String imei) throws IOException {
  232. if(!checkImei(imei, "/wifi", JSON.toJSONString(wifi))){
  233. log.info(imei + ",wifi not in device");
  234. return "not in device";
  235. }
  236. DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
  237. if(!isAtLocationTime(gzDeviceConfig.getLocationInterval())){
  238. log.info(imei + ",wifi not in time");
  239. return "not in time";
  240. }
  241. log.info(imei + ",wifi ,v=" + JSON.toJSONString(wifi));
  242. GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(imei);
  243. IOTDeviceStatus iotDeviceStatus = IOTDeviceStatus.build(gpsDeviceStatus.getDeviceStatus());
  244. GZDevicePower power = new GZDevicePower();
  245. power.setCharge(false);
  246. power.setPower(iotDeviceStatus.getBattery());
  247. GZDeviceStatus deviceStatus = new GZDeviceStatus();
  248. LocalDateTime time = LocalDateTime.now();
  249. GZLocationExtend extend = new GZLocationExtend();
  250. List<GZLocationWIFIItem> itemList = new ArrayList<>();
  251. itemList.add(GZUtil.toWifiItem(wifi.getMmac()));
  252. String[] macs = wifi.getMacs().split("\\|");
  253. if(Strings.isNullOrEmpty(wifi.getMacs())){
  254. macs = new String[0];
  255. }
  256. for (int i = 0; i < macs.length; i++) {
  257. itemList.add(GZUtil.toWifiItem(macs[i]));
  258. }
  259. GZLocationWIFI gzWifi = new GZLocationWIFI(time.getHour(), time.getMinute(), time.getSecond(),itemList.size(), itemList);
  260. extend.setNetType(GZNetType.WIFI);
  261. extend.setData1(gzWifi);
  262. GZLocation location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
  263. 0, 0, 0, 0, 0, 0,
  264. deviceStatus, power, 0, 0, true, extend);
  265. byte[] res = httpUtil.send(imei, gzDeviceConfig.getPhone(), location);
  266. System.out.println(ByteUtil.bytesToHexString(res));
  267. log.info(imei + ",wifi 电信返回:" + ByteUtil.bytesToHexString(res));
  268. return "success";
  269. }
  270. private boolean checkImei(String imei, String api, String json) throws IOException {
  271. if(factory.equals("telpo") && tianboDevice.indexOf(imei) > -1){
  272. return true;
  273. }
  274. if(factory.equals("zhizhong") && zhizhongDevices.indexOf(imei) > -1){
  275. return true;
  276. }
  277. if(factory.equals("zhanneng") && zhannengDevices.indexOf(imei) > -1){
  278. return true;
  279. }
  280. // 天波服务负责转发
  281. if(factory.equals("telpo") && zhizhongDevices.indexOf(imei) > -1){
  282. okHttpUtil.postJson(zhizhongUrl + api + "?imei=" + imei, json);
  283. }
  284. if(factory.equals("telpo") && zhannengDevices.indexOf(imei) > -1){
  285. okHttpUtil.postJson(zhannengUrl + api + "?imei=" + imei, json);
  286. }
  287. return false;
  288. }
  289. private boolean isAtLocationTime(String locationInterval){
  290. List<GZLocationInterval> interval = GZLocationInterval.build(locationInterval);
  291. return GZLocationInterval.isIn(LocalDateTime.now(), interval);
  292. }
  293. @RequestMapping("/call")
  294. public String callHandle(@RequestBody IOTCall call, String imei) throws IOException {
  295. if(!checkImei(imei, "/call", JSON.toJSONString(call))){
  296. log.info(imei + ",wifi not in device");
  297. return "not in device";
  298. }
  299. DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
  300. log.info(imei + ",call ,v=" + JSON.toJSONString(call));
  301. GZUpCall calls=new GZUpCall();
  302. calls.setPhoneNumber(call.getPhoneNumber());
  303. calls.setDateTime(call.getDateTime());
  304. calls.setCallFlag(call.getCallFlag());
  305. calls.setDuration(call.getDuration());
  306. byte[] res = httpUtil.send(imei, gzDeviceConfig.getPhone(), calls);
  307. System.out.println(ByteUtil.bytesToHexString(res));
  308. log.info(imei + ",call 电信返回:" + ByteUtil.bytesToHexString(res));
  309. return "success";
  310. }
  311. }