Skip to content

Commit

Permalink
Merge pull request #1 from inramento/master
Browse files Browse the repository at this point in the history
修改了一些内容
  • Loading branch information
ZXQ-Kyle authored Mar 24, 2020
2 parents d54333c + a5ae0b1 commit 0118190
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Dc1Server
Dc1Server

##### 项目Fork自ZXQ-Kyle/Dc1Server


## DC1插座复活的服务器端

修改了如下:
#### 1.查询间隔由1分钟改为5秒,以便于实时观察功率变化,会增加网络传输
#### 2.将ConnectionManager中的mDeviceConnectionMap的key由ip改为ip+port,以解决将server部署到云服务器,同一路由器下的DC1 ip相同,mDeviceConnectionMap覆盖重复查询问题 2.将ConnectionManager中的mDeviceConnectionMap的key由ip改为ip+port,以解决将server部署到云服务器,同一路由器下的DC1 ip相同,mDeviceConnectionMap覆盖重复查询问题
#### 3.新增心跳检测,15秒未通讯关闭channel
#### 4.关闭channel时,将DC1 online置为false,以便客户端显示离线状态


### 感谢原作者.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void dispatchMsg(Channel channel, String msg) {
if (localPort == 8800) {
executorService.execute(() -> mPhoneConnectionMap.get(ip + ":" + remotePort).processMessage(msg));
} else {
executorService.execute(() -> mDeviceConnectionMap.get(ip).processMessage(msg));
executorService.execute(() -> mDeviceConnectionMap.get(ip + ":" + remotePort).processMessage(msg));
}
}

Expand All @@ -41,7 +41,7 @@ public IConnection addChannel(Channel channel) {
int localPort = localAddress.getPort();
if (localPort == 8800) {
//手机连接
PhoneConnection connection = mPhoneConnectionMap.get(ip);
PhoneConnection connection = mPhoneConnectionMap.get(ip + ":" + remotePort);
if (connection == null) {
connection = new PhoneConnection();
mPhoneConnectionMap.put(ip + ":" + remotePort, connection);
Expand All @@ -52,7 +52,7 @@ public IConnection addChannel(Channel channel) {
DeviceConnection connection = mDeviceConnectionMap.get(ip);
if (connection == null) {
connection = new DeviceConnection();
mDeviceConnectionMap.put(ip, connection);
mDeviceConnectionMap.put(ip + ":" + remotePort, connection);
}
connection.setChannel(channel);
return connection;
Expand All @@ -72,10 +72,11 @@ public void removeChannel(Channel channel) {
}
mPhoneConnectionMap.remove(ip + ":" + remotePort);
} else {
if (mDeviceConnectionMap.get(ip).isActive()) {
if (mDeviceConnectionMap.get(ip + ":" + remotePort).isActive()) {
return;
}
mDeviceConnectionMap.remove(ip);
DataPool.offline(mDeviceConnectionMap.get(ip + ":" + remotePort).getId());
mDeviceConnectionMap.remove(ip + ":" + remotePort);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void processMessage(String msg) {
}.getType();
AskBean<ActivateBean> askBean = gson.fromJson(msg, type);
id = askBean.getParams().getMac();
sendMessageScheduleThread.scheduleWithFixedDelay(new QueryTask(), 0, 1, TimeUnit.MINUTES);
sendMessageScheduleThread.scheduleWithFixedDelay(new QueryTask(), 0, 5, TimeUnit.SECONDS);
} else if (msg.contains(IDENTIFY)) {
//收到dc1上线数据 第二种数据格式
Type type = new TypeToken<AskBean<IdentifyBean>>() {
Expand All @@ -110,7 +110,7 @@ public void processMessage(String msg) {
.setStatus(200)
.setMsg("device identified");
appendMsgToQueue(gson.toJson(answerBean));
sendMessageScheduleThread.scheduleWithFixedDelay(new QueryTask(), 0, 1, TimeUnit.MINUTES);
sendMessageScheduleThread.scheduleWithFixedDelay(new QueryTask(), 0, 5, TimeUnit.SECONDS);
} else if (msg.contains(DETAL_KWH)) {
//收到用电量增加
Type type = new TypeToken<AskBean<DetalKwhBean>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ protected void initChannel(SocketChannel socketChannel) throws Exception {
// pipeline.addLast(new DelimiterBasedFrameDecoder(1024 * 1024, Delimiters.lineDelimiter()));
pipeline.addLast(new LineBasedFrameDecoder(1024 * 1024));
pipeline.addLast(new IdleStateHandler(15, 15, 15));
pipeline.addLast(new HeartBeatServerHandler());
pipeline.addLast("handler", new ServerHandler());//服务器处理客户端请求
}
}
Expand Down Expand Up @@ -117,4 +118,15 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
super.exceptionCaught(ctx, cause);
}
}
}

public static class HeartBeatServerHandler extends ChannelInboundHandlerAdapter {
private int lossConnectCount = 0;

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
System.out.println("已经15秒未收到客户端的消息了!close");
ctx.channel().close();

}
}
}

0 comments on commit 0118190

Please sign in to comment.