|
- package com.ssjl.zhaobiao.guizhou.controller;
-
- import com.alibaba.fastjson.JSON;
- import com.google.common.base.Strings;
- //import com.google.gson.annotations.JsonAdapter;
- import com.ssjl.zhaobiao.guizhou.entity.*;
- import com.ssjl.zhaobiao.guizhou.entity.GZMessage.*;
- import com.ssjl.zhaobiao.guizhou.entity.IOT.*;
- import com.ssjl.zhaobiao.guizhou.entity.IOT.Api.IotApiActiveDevice;
- import com.ssjl.zhaobiao.guizhou.entity.IOT.Api.IotApiResponse;
- import com.ssjl.zhaobiao.guizhou.handle.GZResponseHandle;
- import com.ssjl.zhaobiao.guizhou.repository.DianxinGzDeviceConfigMapper;
- import com.ssjl.zhaobiao.guizhou.repository.GpsDeviceStatusMapper;
- import com.ssjl.zhaobiao.guizhou.service.IGZPlatformCmdService;
- import com.ssjl.zhaobiao.guizhou.utils.*;
- import lombok.extern.slf4j.Slf4j;
- import org.checkerframework.checker.units.qual.A;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.io.IOException;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.List;
-
- @Slf4j
- @RestController
- @RequestMapping("/iot")
- public class IotController {
-
- @Autowired
- IGZPlatformCmdService cmdService;
- @Autowired
- GZHttpUtil httpUtil;
- @Autowired
- GZResponseHandle responseHandle;
- @Autowired
- GpsDeviceStatusMapper gpsDeviceStatusMapper;
- @Autowired
- DianxinGzDeviceConfigMapper dianxinGzDeviceConfigMapper;
- @Autowired
- OkHttpUtil okHttpUtil;
- @Autowired
- IOTApiUtil iotApiUtil;
- @Value("${guizhou.device.zhizhong}")
- String zhizhongDevices;
- @Value("${guizhou.device.zhanneng}")
- String zhannengDevices;
- @Value("${guizhou.device.tianbo}")
- String tianboDevice;
- @Value("${guizhou.http.zhizhong}")
- String zhizhongUrl;
- @Value("${guizhou.http.zhanneng}")
- String zhannengUrl;
- @Value("${guizhou.factory}")
- String factory;
-
- @RequestMapping("/online")
- public String online(String imei) throws IOException {
- // 接收到上线消息,则认为是开机。开机流程有两种,根据电信平台返回的情况而定
- // 1.1 终端主动向平台发起数据链接, 并发送位置信息, 消息参见 4.2.8;
- //1.2 平台发起立即查询, 参见 4.2.2, 发送的消息为表 4-3 中的立即上报终端信息, 需要查询的
- //参数包括终端执行的协议版本号、 终端型号、 终端型号生产厂家、 终端 IMSI 号、 终端软件版本号, 为
- //表 4-1-3 中的参数类型 u、 v、 x、 y、 z。 与数据库原保存数据比对;
- //1.3 平台返回“转发短信到指到号码” 的指令, 数据指令参见 4.2.3, 短信内容见 5.2.1;
- //1.4 终端执行“转发短信到指到号码” 的指令, 并等待 20 - 30 秒;
- //1.5 终端向平台发送中 4.2.9 的指令消息;
- //1.6 平台返回全部配置;
- //1.7 终端上行应答指令;
- //1.8 平台通过数据通道向终端发送 4.2.3 中的消息, 命令终端转发开关机提醒短信;
- //1.9 终端按照步骤 1.8 中的命令, 向监护人终端发送短信提醒。
- //注: 当终端没有设置本机号码时, 步骤中 1.1, 1.5 中的终端号码为终端机器 MEID 码, 见 4.1.3.7
- //中的 D
-
- // 2.1 终端主动向平台发起数据链接, 并发送位置信息, 消息参见 4.2.8;
- //2.2 平台发起立即查询, 参见 4.2.2, 发送的消息为表 4-3 中的立即上报终端信息, 需要查询的
- //参数包括终端执行的协议版本号、 终端型号、 终端型号生产厂家、 终端 IMSI 号、 终端软件版本号,
- //为表 4-1-3 中的参数类型 u、 v、 x、 y、 z。 与数据库原保存数据比对;
- //2.3 平台返回全部配置;
- //2.4 终端上行应答指令;
- //2.5 平台通过数据通道向终端发送 4.2.3 中的消息, 命令终端转发开关机提醒短信;
- //2.6 终端按照步骤 1.8 中的命令, 向监护人终端发送短信提醒。
- log.info(imei + "上线");
- if(!checkImei(imei, "/online", "")){
- return "success";
- }
- GZLocationCDMA cdma = new GZLocationCDMA();
- GZLocationExtend extend = new GZLocationExtend();
- extend.setNetType(GZNetType.CDMA);
- extend.setData1(cdma);
- byte[] bid = { 0x00, 0x02 };
- cdma.setBID(bid);
- byte[] sid = {0x22, 0x41};
- cdma.setSID(sid);
- byte[] nid = { 0x36, 0x14};
- cdma.setNID(nid);
-
- int power = 50;
- GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(imei);
- if(gpsDeviceStatus != null){
- String statusStr = gpsDeviceStatus.getDeviceStatus();
- IOTDeviceStatus status = IOTDeviceStatus.build(statusStr);
- power = status.getBattery();
- }
-
- GZDeviceStatus deviceStatus = new GZDeviceStatus();
- deviceStatus.setOpenFirst(true);
- GZDevicePower devicePower = new GZDevicePower();
- devicePower.setCharge(false);
- devicePower.setPower(power);
- LocalDateTime time = LocalDateTime.now();
- GZAskLocation askLocation = new GZAskLocation(imei, time.getHour(),time.getMinute(),time.getSecond(),time.getDayOfMonth(),15976140,0,
- 63968668,0,0,0, deviceStatus, devicePower, 0, 0, true, extend);
-
- GZRequest req = new GZRequest();
- req.setImei(imei);
- req.setIndex(GZUtil.getMessageIndex());
- req.setStatus(GZRequestStatus.RESPONSE_ALL);
- req.setContent(askLocation);
- byte[] res = httpUtil.send(imei, "", req);
- System.out.println(ByteUtil.bytesToHexString(res));
-
- // 调用IOTAPI激活设备
- log.info(imei + "激活设备");
- IotApiActiveDevice active = new IotApiActiveDevice();
- active.setSerialNo(imei);
- active.setCommandType(1);
- IotApiResponse rs = iotApiUtil.activeDevice(active);
- System.out.println(JSON.toJSONString(rs));
- log.info(imei + "激活返回:" + JSON.toJSONString(rs));
- // 处理电信返回数据
- res = (byte[]) responseHandle.process(imei, res);
- System.out.println(ByteUtil.bytesToHexString(res));
- log.info(imei + "电信返回:" + ByteUtil.bytesToHexString(res));
- return "success";
- }
-
- @RequestMapping("/gps")
- public String gpsHandle(@RequestBody IOTGps gps, String imei) throws IOException {
- log.info(imei + ":gps ");
- if(!checkImei(imei, "/gps", JSON.toJSONString(gps))){
- log.info(imei + ",gps not in device");
- return "not in device";
- }
- DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
- if(!isAtLocationTime(gzDeviceConfig.getLocationInterval())){
- log.info(imei + ",gps not in time");
- return "not in time";
- }
- log.info(imei + ",gps ,v=" + JSON.toJSONString(gps));
- // 没有基站数据,先写死
- GZRequest req = new GZRequest();
- gps.setImei(imei);
- req.setImei(gps.getImei());
- req.setIndex(GZUtil.getMessageIndex());
- req.setStatus(GZRequestStatus.RESPONSE_NO);
- GZLocationCDMA cdma = new GZLocationCDMA();
- GZLocationExtend extend = new GZLocationExtend();
- extend.setNetType(GZNetType.CDMA);
- extend.setData1(cdma);
- byte[] bid = { 0x00, 0x02 };
- cdma.setBID(bid);
- byte[] sid = {0x22, 0x41};
- cdma.setSID(sid);
- byte[] nid = { 0x36, 0x14};
- cdma.setNID(nid);
- GZDeviceStatus deviceStatus = new GZDeviceStatus();
- // deviceStatus.setOpenFirst(true);
- GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(gps.getImei());
- IOTDeviceStatus iotDeviceStatus = IOTDeviceStatus.build(gpsDeviceStatus.getDeviceStatus());
- GZDevicePower power = new GZDevicePower();
- power.setCharge(false);
- power.setPower(iotDeviceStatus.getBattery());
-
- LocalDateTime time = LocalDateTime.now();
-
-
- GZLocation location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
- GZUtil.convertLocation(gps.getLatitude()), 0, GZUtil.convertLocation(gps.getLongitude()), 0, 0, 0,
- deviceStatus, power, 70, 3, false, extend);
- req.setContent(location);
- byte[] b = req.toByte();
- System.out.println(ByteUtil.bytesToHexString(b));
- String phone = gzDeviceConfig == null ? "" : gzDeviceConfig.getPhone();
- byte[] res = httpUtil.send(imei, phone, req);
- log.info(imei + ",gps 电信返回:" + ByteUtil.bytesToHexString(res));
- System.out.println(ByteUtil.bytesToHexString(res));
- return "success";
- }
-
- @RequestMapping("/lbs")
- public String lbsHandle(@RequestBody IOTLbs lbs, String imei) throws IOException {
- log.info(imei + ",lbs");
- if(!checkImei(imei, "/lbs", JSON.toJSONString(lbs))){
- log.info(imei + ",lbs not in device");
- return "not in device";
- }
- DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
- if(!isAtLocationTime(gzDeviceConfig.getLocationInterval())){
- log.info(imei + ",lbs not in time");
- return "not in time";
- }
- log.info(imei + "lbs,v=" + JSON.toJSONString(lbs));
- GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(imei);
- IOTDeviceStatus iotDeviceStatus = IOTDeviceStatus.build(gpsDeviceStatus.getDeviceStatus());
- GZDevicePower power = new GZDevicePower();
- power.setCharge(false);
- power.setPower(iotDeviceStatus.getBattery());
- GZDeviceStatus deviceStatus = new GZDeviceStatus();
- LocalDateTime time = LocalDateTime.now();
- GZLocationExtend extend = new GZLocationExtend();
- GZLocation location;
- if(lbs.getCdma().equals("0")){
- // GSM
- GZLocationGSM gsm = GZUtil.toGSM(lbs.getBts(), lbs.getNearbts());
- extend.setNetType(GZNetType.GSM);
- extend.setData1(gsm);
- location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
- 0, 0, 0, 0, 0, 0,
- deviceStatus, power, 70, 0, true, extend);
- }else{
- // CDMA
- String[] bts = lbs.getBts().split(",");
- GZLocationCDMA cdma = new GZLocationCDMA();
- cdma.setSID(ByteUtil.toByte(Integer.parseInt(bts[0]), 2));
- cdma.setNID(ByteUtil.toByte(Integer.parseInt(bts[1]), 2));
- cdma.setBID(ByteUtil.toByte(Integer.parseInt(bts[2]), 2));
- double lat = Double.parseDouble(Strings.isNullOrEmpty(bts[3]) ? bts[3] : "0");
- double lon = Double.parseDouble(Strings.isNullOrEmpty(bts[4]) ? bts[4] : "0");
- int signal = Integer.parseInt(bts[6]);
- extend.setNetType(GZNetType.CDMA);
- extend.setData1(cdma);
- location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
- GZUtil.convertLocation(lat), 0, GZUtil.convertLocation(lon), 0, 0, 0,
- deviceStatus, power, signal, 0, true, extend);
- }
-
- byte[] res = httpUtil.send(imei, gzDeviceConfig.getPhone(), location);
- System.out.println(ByteUtil.bytesToHexString(res));
- log.info(imei + ",lbs 电信返回:" + ByteUtil.bytesToHexString(res));
- return "success";
- }
-
- @RequestMapping("/wifi")
- public String wifiHandle(@RequestBody IOTWifi wifi, String imei) throws IOException {
- if(!checkImei(imei, "/wifi", JSON.toJSONString(wifi))){
- log.info(imei + ",wifi not in device");
- return "not in device";
- }
- DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
- if(!isAtLocationTime(gzDeviceConfig.getLocationInterval())){
- log.info(imei + ",wifi not in time");
- return "not in time";
- }
- log.info(imei + ",wifi ,v=" + JSON.toJSONString(wifi));
- GpsDeviceStatus gpsDeviceStatus = gpsDeviceStatusMapper.selectByImei(imei);
- IOTDeviceStatus iotDeviceStatus = IOTDeviceStatus.build(gpsDeviceStatus.getDeviceStatus());
- GZDevicePower power = new GZDevicePower();
- power.setCharge(false);
- power.setPower(iotDeviceStatus.getBattery());
- GZDeviceStatus deviceStatus = new GZDeviceStatus();
- LocalDateTime time = LocalDateTime.now();
- GZLocationExtend extend = new GZLocationExtend();
- List<GZLocationWIFIItem> itemList = new ArrayList<>();
- itemList.add(GZUtil.toWifiItem(wifi.getMmac()));
- String[] macs = wifi.getMacs().split("\\|");
- if(Strings.isNullOrEmpty(wifi.getMacs())){
- macs = new String[0];
- }
- for (int i = 0; i < macs.length; i++) {
- itemList.add(GZUtil.toWifiItem(macs[i]));
- }
- GZLocationWIFI gzWifi = new GZLocationWIFI(time.getHour(), time.getMinute(), time.getSecond(),itemList.size(), itemList);
- extend.setNetType(GZNetType.WIFI);
- extend.setData1(gzWifi);
- GZLocation location = new GZLocation(time.getHour(), time.getMinute(),time.getSecond(), time.getDayOfMonth(),
- 0, 0, 0, 0, 0, 0,
- deviceStatus, power, 0, 0, true, extend);
- byte[] res = httpUtil.send(imei, gzDeviceConfig.getPhone(), location);
- System.out.println(ByteUtil.bytesToHexString(res));
- log.info(imei + ",wifi 电信返回:" + ByteUtil.bytesToHexString(res));
- return "success";
- }
-
- private boolean checkImei(String imei, String api, String json) throws IOException {
- if(factory.equals("telpo") && tianboDevice.indexOf(imei) > -1){
- return true;
- }
- if(factory.equals("zhizhong") && zhizhongDevices.indexOf(imei) > -1){
- return true;
- }
- if(factory.equals("zhanneng") && zhannengDevices.indexOf(imei) > -1){
- return true;
- }
- // 天波服务负责转发
- if(factory.equals("telpo") && zhizhongDevices.indexOf(imei) > -1){
- okHttpUtil.postJson(zhizhongUrl + api + "?imei=" + imei, json);
- }
- if(factory.equals("telpo") && zhannengDevices.indexOf(imei) > -1){
- okHttpUtil.postJson(zhannengUrl + api + "?imei=" + imei, json);
- }
- return false;
- }
-
- private boolean isAtLocationTime(String locationInterval){
- List<GZLocationInterval> interval = GZLocationInterval.build(locationInterval);
- return GZLocationInterval.isIn(LocalDateTime.now(), interval);
- }
-
-
- @RequestMapping("/call")
- public String callHandle(@RequestBody IOTCall call, String imei) throws IOException {
-
- if(!checkImei(imei, "/call", JSON.toJSONString(call))){
- log.info(imei + ",wifi not in device");
- return "not in device";
- }
- DianxinGzDeviceConfig gzDeviceConfig = dianxinGzDeviceConfigMapper.selectByImei(imei);
-
- log.info(imei + ",call ,v=" + JSON.toJSONString(call));
- GZUpCall calls=new GZUpCall();
- calls.setPhoneNumber(call.getPhoneNumber());
- calls.setDateTime(call.getDateTime());
- calls.setCallFlag(call.getCallFlag());
- calls.setDuration(call.getDuration());
- byte[] res = httpUtil.send(imei, gzDeviceConfig.getPhone(), calls);
- System.out.println(ByteUtil.bytesToHexString(res));
- log.info(imei + ",call 电信返回:" + ByteUtil.bytesToHexString(res));
- return "success";
- }
-
-
-
- }
|