|
@@ -1,6 +1,5 @@
|
|
|
package com.tidecloud.dataacceptance.service.impl;
|
|
|
|
|
|
-import java.io.DataOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
@@ -10,17 +9,18 @@ import java.nio.CharBuffer;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.nio.charset.CharsetDecoder;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
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 io.netty.buffer.ByteBuf;
|
|
@@ -28,6 +28,8 @@ import io.netty.buffer.Unpooled;
|
|
|
import io.netty.channel.Channel;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
+import redis.clients.jedis.Jedis;
|
|
|
+import redis.clients.jedis.JedisPool;
|
|
|
|
|
|
/**
|
|
|
* Created by vinson on 2017/9/7.
|
|
@@ -40,9 +42,15 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
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>();
|
|
|
+ private static String PREFIX_LINK = "s.";
|
|
|
+ private static String PREFIX_DEVICE = "d.";
|
|
|
+
|
|
|
+ public static Map<Channel, String> channelMap = new HashMap<Channel, String>();
|
|
|
+ public static Map<String, Channel> manageChannelMap = new HashMap<>();
|
|
|
+ public static Map<String, String> commandCopy = new HashMap<>();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JedisPool jedisPool;
|
|
|
|
|
|
@Override
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
@@ -61,9 +69,9 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
String deviceId = advice.getDeviceId();
|
|
|
String adviceType = advice.getAdviceType();
|
|
|
Channel channel = ctx.channel();
|
|
|
- Channel channelInMap = channelMap.get(deviceId);
|
|
|
- if (channelInMap == null) {
|
|
|
- channelMap.put(deviceId, channel);
|
|
|
+ String deviceIdInMap = channelMap.get(channel);
|
|
|
+ if (deviceIdInMap == null) {
|
|
|
+ manageLink(channel, deviceIdInMap);
|
|
|
}
|
|
|
switch (adviceType) {
|
|
|
|
|
@@ -80,14 +88,36 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
logger.info("正常存储设备信息:" + getDevice(msg).toString());
|
|
|
break;
|
|
|
case "LK":
|
|
|
- normalReply(advice);
|
|
|
+ normalReply(advice, channel);
|
|
|
+ break;
|
|
|
+ case "UPLOAD":
|
|
|
+ logger.info("device [{}] setting copy time success [{}]", deviceId, new Date());
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void normalReply(Advice advice) {
|
|
|
+ private void manageLink(Channel channel, String deviceIdInMap) {
|
|
|
+ logger.info("链接管理。。。。");
|
|
|
+ String channelId = channel.id().asLongText();
|
|
|
+ manageChannelMap.put(channelId, channel);
|
|
|
+ channelMap.put(channel, deviceIdInMap);
|
|
|
+
|
|
|
+ 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 + deviceIdInMap;
|
|
|
+
|
|
|
+ jedis.set(insertKey, deviceIdInMap);
|
|
|
+ jedis.set(selectKey, StringUtil.convert2String(cMsg));
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getLocalizedMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void normalReply(Advice advice, Channel channel) {
|
|
|
String facotry = advice.getFacotry();
|
|
|
String adviceType = advice.getAdviceType();
|
|
|
String deviceId = advice.getDeviceId();
|
|
@@ -102,7 +132,6 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
logger.info("Normal reply :" + replyCommandStr);
|
|
|
ByteBuf buffer = Unpooled.buffer(replyCommandStr.getBytes().length);
|
|
|
buffer.writeBytes(replyCommandStr.getBytes());
|
|
|
- Channel channel = channelMap.get(deviceId);
|
|
|
channel.write(buffer);
|
|
|
}
|
|
|
|
|
@@ -228,13 +257,6 @@ public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
|
|
|
private void saveChannel(ChannelHandlerContext ctx) {
|
|
|
- // 注册
|
|
|
- // 认证
|
|
|
- // 保存channel
|
|
|
- Channel channel = ctx.channel();
|
|
|
- if (!channelList.contains(channel)) {
|
|
|
- channelList.add(channel);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|