|
@@ -1,5 +1,11 @@
|
|
|
package com.tidecloud.datacceptance.service.impl;
|
|
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.nio.ByteBuffer;
|
|
|
import java.nio.CharBuffer;
|
|
|
import java.nio.charset.Charset;
|
|
@@ -13,6 +19,7 @@ import java.util.Map;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.tidecloud.dataacceptance.entity.Advice;
|
|
@@ -29,8 +36,13 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
*/
|
|
|
@Component
|
|
|
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
+
|
|
|
+ @Value("${server.dataPath}")
|
|
|
+ private String dataPath;
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DiscardServerHandler.class);
|
|
|
+ private static final Long TEN_M = 10485760l;
|
|
|
+ private static final String prefixName = "watch";
|
|
|
|
|
|
private Map<String, Channel> channelMap = new HashMap<String, Channel>();
|
|
|
private List<Channel> channelList = new ArrayList<Channel>();
|
|
@@ -131,24 +143,59 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
Device device = new Device();
|
|
|
String gpsState = bodys[3];
|
|
|
logger.info("正在解析device,gpsState:" + gpsState);
|
|
|
- if (!"A".equals(gpsState)) {
|
|
|
+ /*if (!"A".equals(gpsState)) {
|
|
|
logger.info("gps定位为:" + gpsState + "无效");
|
|
|
return null;
|
|
|
- }
|
|
|
+ }*/
|
|
|
String date = bodys[1];
|
|
|
String time = bodys[2];
|
|
|
Date timestamp = new SimpleDateFormat("yyMMddHHmmss").parse(date + time);
|
|
|
device.setTimestamp(timestamp);
|
|
|
device.setLat(getDouble(bodys[4]));
|
|
|
device.setLng(getDouble(bodys[6]));
|
|
|
- device.setSpeed(getDouble(bodys[5]));
|
|
|
- device.setElectric(getDouble(bodys[13]));
|
|
|
- device.setStep(getInteger(bodys[14]));
|
|
|
+ device.setSpeed(getDouble(bodys[8]));
|
|
|
+ device.setElectric(getDouble(bodys[14]));
|
|
|
+ device.setStep(getInteger(bodys[15]));
|
|
|
+ device.setItemState(bodys[17]);
|
|
|
// getDouble()
|
|
|
logger.info("设备上传具体监控项信息:" + device.toString());
|
|
|
return device;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ private void dataStorage(Device device) {
|
|
|
+ String deviceStr = Device.buildDeviceStr(device);
|
|
|
+ File path = new File(dataPath);
|
|
|
+ File[] listFiles = path.listFiles();
|
|
|
+ boolean isTouch = true;
|
|
|
+ for (File sonFile : listFiles) {
|
|
|
+ long len = sonFile.length();
|
|
|
+ if (len < TEN_M) {
|
|
|
+ // String fileName = dataPath + prefixName + new SimpleDateFormat("yyMMddHHmmss").format(new Date());
|
|
|
+ // File file = new File(fileName);
|
|
|
+ writeDevice2File(sonFile, deviceStr);
|
|
|
+ isTouch = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (isTouch) {
|
|
|
+ String fileName = dataPath + prefixName + new SimpleDateFormat("yyMMddHHmmss").format(new Date());
|
|
|
+ File file = new File(fileName);
|
|
|
+ writeDevice2File(file, deviceStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void writeDevice2File(File file, String deviceStr){
|
|
|
+ Integer length = deviceStr.getBytes().length;
|
|
|
+ try {
|
|
|
+ DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(file, true));
|
|
|
+ dataOutputStream.write(int2bytes(length));;
|
|
|
+ dataOutputStream.writeUTF(deviceStr);
|
|
|
+ dataOutputStream.flush();
|
|
|
+ dataOutputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
cause.printStackTrace();
|
|
@@ -174,6 +221,17 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
* 【Receive from 223.104.255.118 :61922】:[3G*3918197044*000D*LK,12642,0,93]
|
|
|
*/
|
|
|
public static void main(String[] args) {
|
|
|
+ Device device = new Device();
|
|
|
+ device.setDeviceId("3918197044");
|
|
|
+ device.setElectric(11.2d);
|
|
|
+ device.setItemState("125");
|
|
|
+ device.setLat(24.4441);
|
|
|
+ device.setLng(114.223);
|
|
|
+ device.setSpeed(21.2);
|
|
|
+ device.setStep(12);
|
|
|
+ device.setTerminalState(12);
|
|
|
+ device.setTimestamp(new Date());
|
|
|
+ new DiscardServerHandler().dataStorage(device);
|
|
|
}
|
|
|
|
|
|
public static String byteBufferToString(ByteBuffer buffer) {
|
|
@@ -203,4 +261,14 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
return Double.valueOf(str);
|
|
|
}
|
|
|
+
|
|
|
+ static byte[] int2bytes(int num)
|
|
|
+ {
|
|
|
+ byte[] b=new byte[4];
|
|
|
+ //int mask=0xff;
|
|
|
+ for(int i=0;i<4;i++){
|
|
|
+ b[3-i]=(byte)(num>>>(24-i*8));
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
}
|