|
@@ -1,7 +1,9 @@
|
|
|
package com.tidecloud.dataacceptance.service.impl;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -9,6 +11,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.tidecloud.dataacceptance.entity.Advice;
|
|
|
import com.tidecloud.dataacceptance.entity.Device;
|
|
|
+import com.tidecloud.dataacceptance.entity.LbsInfo;
|
|
|
import com.tidecloud.dataacceptance.service.StringAcceptanceHandlerAdapter;
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
@@ -25,137 +28,159 @@ import io.netty.util.concurrent.GenericFutureListener;
|
|
|
@Sharable
|
|
|
@Component(WatchServerHandler.name)
|
|
|
public class WatchServerHandler extends StringAcceptanceHandlerAdapter {
|
|
|
-
|
|
|
+
|
|
|
public static final String name = "WatchServerHandler";
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(WatchServerHandler.class);
|
|
|
-
|
|
|
-
|
|
|
- public void handle(String msg,Channel channel) throws Exception {
|
|
|
- logger.info("设备上传数据:" + msg);
|
|
|
- Advice advice = getAdevice(msg);
|
|
|
- String deviceId = advice.getDeviceId();
|
|
|
- String adviceType = advice.getAdviceType();
|
|
|
-
|
|
|
-
|
|
|
- switch (adviceType) {
|
|
|
-
|
|
|
- case "UD":
|
|
|
- case "heart":
|
|
|
- case "UD2":
|
|
|
- receiveMsg(msg,deviceId,channel);
|
|
|
- logger.info("正常存储设备信息:" + getDevice(msg).toString());
|
|
|
- break;
|
|
|
- case "LK":
|
|
|
- normalReply(advice, channel);
|
|
|
- break;
|
|
|
- case "UPLOAD":
|
|
|
- logger.info("device [{}] setting copy time success [{}]", deviceId, new Date());
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void normalReply(Advice advice, Channel channel) {
|
|
|
- String facotry = advice.getFacotry();
|
|
|
- String adviceType = advice.getAdviceType();
|
|
|
- String deviceId = advice.getDeviceId();
|
|
|
- StringBuilder replyCommand = new StringBuilder();
|
|
|
- replyCommand.append("[");
|
|
|
- replyCommand.append("3g").append("*");
|
|
|
- replyCommand.append(deviceId).append("*");
|
|
|
- replyCommand.append("0002").append("*");
|
|
|
- replyCommand.append(adviceType);
|
|
|
- replyCommand.append("]");
|
|
|
- String replyCommandStr = replyCommand.toString();
|
|
|
- ByteBuf buffer = Unpooled.buffer(replyCommandStr.getBytes().length);
|
|
|
- buffer.writeBytes(replyCommandStr.getBytes());
|
|
|
- ChannelFuture channelFuture = channel.writeAndFlush(buffer);
|
|
|
- channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void operationComplete(Future<? super Void> future) throws Exception {
|
|
|
- logger.info("Normal reply :" + replyCommandStr);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- private Advice getAdevice(Object msg) {
|
|
|
- Advice advice = new Advice();
|
|
|
- try {
|
|
|
- String message = String.valueOf(msg); // "【Receive from
|
|
|
- // 223.104.255.118
|
|
|
- // :61922】:[3G*3918197044*000D*LK,12642,0,93]";
|
|
|
- int startIndex = message.indexOf("[");
|
|
|
- int endIndex = message.indexOf("]");
|
|
|
- String data = message.substring(startIndex + 1, endIndex); // [3G*3918197044*000D*LK,12642,0,93]
|
|
|
- String[] bodys = data.split(",");
|
|
|
- String headers = bodys[0];
|
|
|
- String[] headersBodys = headers.split("\\*");
|
|
|
- advice.setFacotry(headersBodys[0]);
|
|
|
- advice.setDeviceId(headersBodys[1]);
|
|
|
- advice.setAdvicelength(headersBodys[2]);
|
|
|
- advice.setAdviceType(headersBodys[3]);
|
|
|
- logger.info("设备上传头信息:" + advice.toString());
|
|
|
- return advice;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /* 数据[手表具体数据] [对应位数] [数据内容]
|
|
|
- * 3G*3918197044*0107*UD [0] [厂商*设备 ID*内容长度*内容类型]
|
|
|
- 200917 [1] 日期
|
|
|
- 054140 [2] 时间
|
|
|
- V [3] gps定位是否有效[A]有效,[V]无效
|
|
|
- 31.171093 [4] 纬度
|
|
|
- N [5] 纬度表示
|
|
|
- 121.4215967 [6] 经度
|
|
|
- E [7] 经度表示
|
|
|
- 0.00 [8] 速度
|
|
|
- 0.0 [9] 方向
|
|
|
- 0.0 [10] 海拔
|
|
|
- 0 [11] 卫星个数
|
|
|
- 42,50 [12] gsm信号强度
|
|
|
- 14420 [13] 电量
|
|
|
- 0 [14] 计步数
|
|
|
- 00000010 [15] 终端状态
|
|
|
- 7,255 [16]
|
|
|
- 460 [17]
|
|
|
- 0 [18]
|
|
|
- ... ...
|
|
|
- */
|
|
|
- protected Device getDevice(String msg) throws Exception {
|
|
|
- int startIndex = msg.indexOf("[");
|
|
|
- int endIndex = msg.indexOf("]");
|
|
|
- String data = msg.substring(startIndex + 1, endIndex);
|
|
|
- String[] bodys = data.split(",");
|
|
|
-
|
|
|
- Device device = new Device();
|
|
|
- String gpsState = bodys[3];
|
|
|
- logger.info("正在解析device,gpsState:" + gpsState);
|
|
|
- /*if (!"A".equals(gpsState)) {
|
|
|
- logger.info("gps定位为:" + gpsState + "无效");
|
|
|
- return null;
|
|
|
- }*/
|
|
|
- String date = bodys[1];
|
|
|
- String time = bodys[2];
|
|
|
- Date timestamp = new SimpleDateFormat("ddMMyyHHmmss").parse(date + time);
|
|
|
- device.setTimestamp(timestamp);
|
|
|
- device.setLat(getDouble(bodys[4]));
|
|
|
- device.setLng(getDouble(bodys[6]));
|
|
|
- device.setSpeed(getDouble(bodys[8]));
|
|
|
- device.setElectric(getDouble(bodys[13]));
|
|
|
- device.setStep(getInteger(bodys[14]));
|
|
|
- device.setItemState(bodys[16]);
|
|
|
- // getDouble()
|
|
|
- logger.info("设备上传具体监控项信息:" + device.toString());
|
|
|
- return device;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(WatchServerHandler.class);
|
|
|
+
|
|
|
+ public void handle(String msg, Channel channel) throws Exception {
|
|
|
+ logger.info("设备上传数据:" + msg);
|
|
|
+ Advice advice = getAdevice(msg);
|
|
|
+ String deviceId = advice.getDeviceId();
|
|
|
+ String adviceType = advice.getAdviceType();
|
|
|
+
|
|
|
+ switch (adviceType) {
|
|
|
+
|
|
|
+ case "UD":
|
|
|
+ case "heart":
|
|
|
+ case "UD2":
|
|
|
+ receiveMsg(msg, deviceId, channel);
|
|
|
+ logger.info("正常存储设备信息:" + getDevice(msg).toString());
|
|
|
+ break;
|
|
|
+ case "LK":
|
|
|
+ normalReply(advice, channel);
|
|
|
+ break;
|
|
|
+ case "UPLOAD":
|
|
|
+ logger.info("device [{}] setting copy time success [{}]", deviceId, new Date());
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void normalReply(Advice advice, Channel channel) {
|
|
|
+ String facotry = advice.getFacotry();
|
|
|
+ String adviceType = advice.getAdviceType();
|
|
|
+ String deviceId = advice.getDeviceId();
|
|
|
+ StringBuilder replyCommand = new StringBuilder();
|
|
|
+ replyCommand.append("[");
|
|
|
+ replyCommand.append("3g").append("*");
|
|
|
+ replyCommand.append(deviceId).append("*");
|
|
|
+ replyCommand.append("0002").append("*");
|
|
|
+ replyCommand.append(adviceType);
|
|
|
+ replyCommand.append("]");
|
|
|
+ String replyCommandStr = replyCommand.toString();
|
|
|
+ ByteBuf buffer = Unpooled.buffer(replyCommandStr.getBytes().length);
|
|
|
+ buffer.writeBytes(replyCommandStr.getBytes());
|
|
|
+ ChannelFuture channelFuture = channel.writeAndFlush(buffer);
|
|
|
+ channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void operationComplete(Future<? super Void> future) throws Exception {
|
|
|
+ logger.info("Normal reply :" + replyCommandStr);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private Advice getAdevice(Object msg) {
|
|
|
+ Advice advice = new Advice();
|
|
|
+ try {
|
|
|
+ String message = String.valueOf(msg); // "【Receive from
|
|
|
+ // 223.104.255.118
|
|
|
+ // :61922】:[3G*3918197044*000D*LK,12642,0,93]";
|
|
|
+ int startIndex = message.indexOf("[");
|
|
|
+ int endIndex = message.indexOf("]");
|
|
|
+ String data = message.substring(startIndex + 1, endIndex); // [3G*3918197044*000D*LK,12642,0,93]
|
|
|
+ String[] bodys = data.split(",");
|
|
|
+ String headers = bodys[0];
|
|
|
+ String[] headersBodys = headers.split("\\*");
|
|
|
+ advice.setFacotry(headersBodys[0]);
|
|
|
+ advice.setDeviceId(headersBodys[1]);
|
|
|
+ advice.setAdvicelength(headersBodys[2]);
|
|
|
+ advice.setAdviceType(headersBodys[3]);
|
|
|
+ logger.info("设备上传头信息:" + advice.toString());
|
|
|
+ return advice;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 数据[手表具体数据] [对应位数] [数据内容] 3G*3918197044*0107*UD [0] [厂商*设备 ID*内容长度*内容类型]
|
|
|
+ * 200917 [1] 日期 054140 [2] 时间 V [3] gps定位是否有效[A]有效,[V]无效 31.171093 [4] 纬度 N [5]
|
|
|
+ * 纬度表示 121.4215967 [6] 经度 E [7] 经度表示 0.00 [8] 速度 0.0 [9] 方向 0.0 [10] 海拔 0 [11]
|
|
|
+ * 卫星个数 42,50 [12] gsm信号强度 14420 [13] 电量 0 [14] 计步数 00000010 [15] 终端状态 7,255
|
|
|
+ * [16] 460 [17] 0 [18] ... ...
|
|
|
+ */
|
|
|
+ public Device getDevice(String msg) throws Exception {
|
|
|
+ int startIndex = msg.indexOf("[");
|
|
|
+ int endIndex = msg.indexOf("]");
|
|
|
+ String data = msg.substring(startIndex + 1, endIndex);
|
|
|
+ String[] bodys = data.split(",");
|
|
|
+
|
|
|
+ Device device = new Device();
|
|
|
+ String gpsState = bodys[3];
|
|
|
+ logger.info("正在解析device,gpsState:" + gpsState);
|
|
|
+ /*
|
|
|
+ * if (!"A".equals(gpsState)) { logger.info("gps定位为:" + gpsState + "无效"); return
|
|
|
+ * null; }
|
|
|
+ */
|
|
|
+ String date = bodys[1];
|
|
|
+ String time = bodys[2];
|
|
|
+ Date timestamp = new SimpleDateFormat("ddMMyyHHmmss").parse(date + time);
|
|
|
+ device.setTimestamp(timestamp);
|
|
|
+ device.setLat(getDouble(bodys[4]));
|
|
|
+ device.setLng(getDouble(bodys[6]));
|
|
|
+ device.setSpeed(getDouble(bodys[8]));
|
|
|
+ device.setElectric(getDouble(bodys[13]));
|
|
|
+ device.setStep(getInteger(bodys[14]));
|
|
|
+ device.setItemState(bodys[16]);
|
|
|
+ // getDouble()
|
|
|
+ logger.info("设备上传具体监控项信息:" + device.toString());
|
|
|
+ return device;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<LbsInfo> getLBsInfo(String msg) throws Exception {
|
|
|
+ List<LbsInfo> lbsList = new ArrayList<>();
|
|
|
+ int startIndex = msg.indexOf("[");
|
|
|
+ int endIndex = msg.indexOf("]");
|
|
|
+ String data = msg.substring(startIndex + 1, endIndex);
|
|
|
+ String[] bodys = data.split(",");
|
|
|
+ String headers = bodys[0];
|
|
|
+ String[] headersBodys = headers.split("\\*");
|
|
|
+ String gpsState = bodys[3];
|
|
|
+ logger.info("正在解析device,gpsState:" + gpsState);
|
|
|
+
|
|
|
+ String imei = "3588"+headersBodys[1]+"8";
|
|
|
+ String type = headersBodys[3];
|
|
|
+ if (("UD".equals(type) || "UD2".equals(type))&&!"A".equals(gpsState)) {
|
|
|
+ int length = Integer.valueOf(bodys[17]);
|
|
|
+
|
|
|
+ String mcc = bodys[19];
|
|
|
+ String mnc = bodys[20];
|
|
|
+ int start = 21;
|
|
|
+
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ LbsInfo lbsInfo = new LbsInfo();
|
|
|
+ lbsInfo.setImei(imei);
|
|
|
+ lbsInfo.setMcc(mcc);
|
|
|
+ lbsInfo.setMnc(mnc);
|
|
|
+ lbsInfo.setLac(bodys[start]);
|
|
|
+ start++;
|
|
|
+ lbsInfo.setCellid(bodys[start]);
|
|
|
+ start++;
|
|
|
+ int signal = Integer.valueOf(bodys[start]);
|
|
|
+// if (signal>0) {
|
|
|
+// signal = signal*2 - 113;
|
|
|
+// }
|
|
|
+ lbsInfo.setSignal(signal);
|
|
|
+ start++;
|
|
|
+ lbsList.add(lbsInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return lbsList;
|
|
|
+ }
|
|
|
+
|
|
|
}
|