|
@@ -1,29 +1,15 @@
|
|
|
package com.tidecloud.dataacceptance.service.impl;
|
|
|
|
|
|
-import java.io.*;
|
|
|
-import java.nio.ByteBuffer;
|
|
|
-import java.nio.CharBuffer;
|
|
|
-import java.nio.charset.Charset;
|
|
|
-import java.nio.charset.CharsetDecoder;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.UUID;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
|
|
|
-import org.apache.commons.lang.ArrayUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import com.smartsanitation.common.util.StringUtil;
|
|
|
import com.tidecloud.dataacceptance.entity.Advice;
|
|
|
-import com.tidecloud.dataacceptance.entity.ConnectMsg;
|
|
|
import com.tidecloud.dataacceptance.entity.Device;
|
|
|
-import com.tidecloud.dataacceptance.entity.HrtStart;
|
|
|
+import com.tidecloud.dataacceptance.service.AcceptanceInboundHandlerAdapter;
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.buffer.Unpooled;
|
|
@@ -31,52 +17,23 @@ import io.netty.channel.Channel;
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
import io.netty.channel.ChannelHandler.Sharable;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
-import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
import io.netty.util.concurrent.GenericFutureListener;
|
|
|
-import redis.clients.jedis.Jedis;
|
|
|
-import redis.clients.jedis.JedisPool;
|
|
|
|
|
|
/**
|
|
|
* Created by vinson on 2017/9/7.
|
|
|
*/
|
|
|
@Sharable
|
|
|
-@Component
|
|
|
-public class WatchServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
-
|
|
|
- private String dataPath = "/home/service/collector_feidong/rawdata/";
|
|
|
- // private String dataPath = "D:\\work\\rawdata1\\";
|
|
|
+@Component(WatchServerHandler.name)
|
|
|
+public class WatchServerHandler extends AcceptanceInboundHandlerAdapter {
|
|
|
+
|
|
|
+ public static final String name = "WatchServerHandler";
|
|
|
private static final Logger logger = LoggerFactory.getLogger(WatchServerHandler.class);
|
|
|
- private static final Long TEN_M = 10485760l;
|
|
|
- private static final String prefixName = "feidong";
|
|
|
- private static String PREFIX_LINK = "s.";
|
|
|
- private static String PREFIX_DEVICE = "d.";
|
|
|
-
|
|
|
- public static Map<String, String> channelMap = new HashMap<String, String>();
|
|
|
- public static Map<String, Channel> manageChannelMap = new HashMap<>();
|
|
|
- public static Map<String, Channel> channelMapOfChannelKey = new HashMap<>();
|
|
|
- public static Map<String, String> commandCopy = new HashMap<>();
|
|
|
- private static ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
|
|
|
- @Autowired
|
|
|
- private JedisPool jedisPool;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
- ByteBuf byteBuf = (ByteBuf)msg;
|
|
|
- String str = byteBufferToString(byteBuf.nioBuffer());
|
|
|
- logger.info("上传数据:{}", str);
|
|
|
-
|
|
|
- singleThreadPool.execute(()->dataStorage(str));
|
|
|
-
|
|
|
-
|
|
|
- //try {
|
|
|
- // reply(ctx, str);
|
|
|
- //} catch (Exception e) {
|
|
|
- // logger.error(e.getMessage());
|
|
|
- //}
|
|
|
+
|
|
|
+ public WatchServerHandler() {
|
|
|
+ System.out.println(name);
|
|
|
}
|
|
|
-
|
|
|
- private void reply(ChannelHandlerContext ctx, String msg) throws Exception {
|
|
|
+ public void reply(ChannelHandlerContext ctx, String msg) throws Exception {
|
|
|
logger.info("设备上传数据:" + msg);
|
|
|
Advice advice = getAdevice(msg);
|
|
|
String deviceId = advice.getDeviceId();
|
|
@@ -89,15 +46,9 @@ public class WatchServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
switch (adviceType) {
|
|
|
|
|
|
case "UD":
|
|
|
- Device deviceUD = getDevice(msg);
|
|
|
- deviceUD.setDeviceId(deviceId);
|
|
|
- dataStorage(deviceUD);
|
|
|
- logger.info("正常存储设备信息:" + deviceUD.toString());
|
|
|
- break;
|
|
|
+ case "heart":
|
|
|
case "UD2":
|
|
|
- Device deviceUD2 = getDevice(msg);
|
|
|
- deviceUD2.setDeviceId(deviceId);
|
|
|
- dataStorage(deviceUD2);
|
|
|
+ sendMsg(msg,deviceId);
|
|
|
logger.info("正常存储设备信息:" + getDevice(msg).toString());
|
|
|
break;
|
|
|
case "LK":
|
|
@@ -106,66 +57,17 @@ public class WatchServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
case "UPLOAD":
|
|
|
logger.info("device [{}] setting copy time success [{}]", deviceId, new Date());
|
|
|
break;
|
|
|
- case "heart":
|
|
|
- dataStorageHartstart(deviceId, msg);
|
|
|
+
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
|
|
|
- private void dataStorageHartstart(String deviceId, String msg) {
|
|
|
- int startIndex = msg.indexOf("[");
|
|
|
- int endIndex = msg.indexOf("]");
|
|
|
- String data = msg.substring(startIndex + 1, endIndex);
|
|
|
- String[] bodys = data.split(",");
|
|
|
-
|
|
|
- HrtStart hrtStart = new HrtStart();
|
|
|
- hrtStart.setDate(new Date());
|
|
|
- hrtStart.setDeviceId(deviceId);
|
|
|
- hrtStart.setHrtCount(getInteger(bodys[1]));
|
|
|
- String deviceStr = HrtStart.buildHrtStart(hrtStart);
|
|
|
-
|
|
|
- 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);
|
|
|
- logger.info("正在写入数据: " + deviceStr);
|
|
|
- isTouch = false;
|
|
|
- }
|
|
|
- }
|
|
|
- if (isTouch) {
|
|
|
- String fileName = dataPath + prefixName + new SimpleDateFormat("yyMMddHHmmss").format(new Date());
|
|
|
- File file = new File(fileName);
|
|
|
- writeDevice2File(file, deviceStr);
|
|
|
- logger.info("满10M,创建新的文件 正在写入数据:" + deviceStr + "timestamp:" + new SimpleDateFormat("yyMMddHHmmss").format(new Date()));
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
- private void manageLink(Channel channel, String deviceId) {
|
|
|
- String channelId = UUID.randomUUID().toString();
|
|
|
- manageChannelMap.put(channelId, channel);
|
|
|
- channelMap.put(channelId, deviceId);
|
|
|
- channelMapOfChannelKey.put(deviceId, channel);
|
|
|
- logger.info("链接管理,链接id [{}]", channelId);
|
|
|
-
|
|
|
- ConnectMsg cMsg = new ConnectMsg("10.27.118.76", channelId);
|
|
|
- try (Jedis jedis = jedisPool.getResource()) {
|
|
|
- jedis.select(15);
|
|
|
- String insertKey = PREFIX_LINK + "10.27.118.76";
|
|
|
- String selectKey = PREFIX_DEVICE + deviceId;
|
|
|
-
|
|
|
- jedis.set(insertKey, deviceId);
|
|
|
- jedis.set(selectKey, StringUtil.convert2String(cMsg));
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error(e.getLocalizedMessage());
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
private void normalReply(Advice advice, Channel channel) {
|
|
|
String facotry = advice.getFacotry();
|
|
@@ -237,7 +139,7 @@ public class WatchServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
0 [18]
|
|
|
... ...
|
|
|
*/
|
|
|
- private Device getDevice(String msg) throws Exception {
|
|
|
+ protected Device getDevice(String msg) throws Exception {
|
|
|
int startIndex = msg.indexOf("[");
|
|
|
int endIndex = msg.indexOf("]");
|
|
|
String data = msg.substring(startIndex + 1, endIndex);
|
|
@@ -265,126 +167,26 @@ public class WatchServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
return device;
|
|
|
}
|
|
|
|
|
|
- private void dataStorage(Device device) {
|
|
|
- String deviceStr = Device.buildDeviceStr(device);
|
|
|
- dataStorage(deviceStr);
|
|
|
- }
|
|
|
-
|
|
|
- private void dataStorage(String deviceStr) {
|
|
|
- File path = new File(dataPath);
|
|
|
- File[] listFiles = path.listFiles();
|
|
|
- boolean isTouch = true;
|
|
|
- if (listFiles != null) {
|
|
|
- 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);
|
|
|
- logger.info("正在写入数据: " + deviceStr);
|
|
|
- isTouch = false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (isTouch) {
|
|
|
- String fileName = dataPath + prefixName + new SimpleDateFormat("yyMMddHHmmss").format(new Date());
|
|
|
- File file = new File(fileName);
|
|
|
- writeDevice2File(file, deviceStr);
|
|
|
- logger.info("满10M,创建新的文件 正在写入数据:" + deviceStr + "timestamp:" + new SimpleDateFormat("yyMMddHHmmss").format(new Date()));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void writeDevice2File(File file, String deviceStr){
|
|
|
- BufferedWriter bufferedWriter = null;
|
|
|
- try {
|
|
|
- bufferedWriter = new BufferedWriter(new FileWriter(file, true));
|
|
|
- bufferedWriter.write(deviceStr);
|
|
|
- bufferedWriter.newLine();
|
|
|
- bufferedWriter.flush();
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error(e.getMessage());
|
|
|
- } finally {
|
|
|
- if (bufferedWriter != null) {
|
|
|
- try {
|
|
|
- bufferedWriter.close();
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("close file[{}] failed: {}", file.getName(), e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
- cause.printStackTrace();
|
|
|
- ctx.close();
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public void channelActive(final ChannelHandlerContext ctx) throws Exception {
|
|
|
- saveChannel(ctx);
|
|
|
- }
|
|
|
-
|
|
|
- private void saveChannel(ChannelHandlerContext ctx) {
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 【Receive from 223.104.255.118 :61922】:[3G*3918197044*000D*LK,12642,0,93]
|
|
|
*/
|
|
|
public static void main(String[] args) {
|
|
|
- for (int i = 0; i < 100; i++) {
|
|
|
- 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 WatchServerHandler().dataStorage(device);
|
|
|
- }
|
|
|
+// for (int i = 0; i < 100; i++) {
|
|
|
+// 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 WatchServerHandler().dataStorage(device);
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
- public static String byteBufferToString(ByteBuffer buffer) {
|
|
|
- CharBuffer charBuffer = null;
|
|
|
- try {
|
|
|
- Charset charset = Charset.forName("UTF-8");
|
|
|
- CharsetDecoder decoder = charset.newDecoder();
|
|
|
- charBuffer = decoder.decode(buffer);
|
|
|
- buffer.flip();
|
|
|
- return charBuffer.toString();
|
|
|
- } catch (Exception ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private Integer getInteger(String str) {
|
|
|
- if (str == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return Integer.valueOf(str);
|
|
|
- }
|
|
|
-
|
|
|
- private Double getDouble(String str) {
|
|
|
- if (str == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
}
|