Skip to content

Commit b8b9f42

Browse files
committed
Merge branch 'dev'
# Conflicts: # clustermanage-server/pom.xml # clustermanage-server/src/main/java/cc/bitky/clustermanage/global/ServerSetting.java # clustermanage-server/src/main/java/cc/bitky/clustermanage/server/bean/KyServerCenterHandler.java # clustermanage-server/src/main/java/cc/bitky/clustermanage/tcp/TcpMediator.java # clustermanage-server/src/main/java/cc/bitky/clustermanage/tcp/server/MsgToCanParser.java # clustermanage-server/src/main/java/cc/bitky/clustermanage/tcp/server/PolicyCanTransmitter.java # clustermanage-server/src/main/java/cc/bitky/clustermanage/web/OperateRestController.java
2 parents 1d72e15 + 70b9a84 commit b8b9f42

24 files changed

+359
-178
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
整理/
2+
13
# Node.js
24
# *****************************
35
# Logs

clustermanage-server/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cc.bitky.clustermanage</groupId>
88
<artifactId>clustermanage-server</artifactId>
9-
<version>0.9.3-release</version>
9+
<version>1.0.0-release</version>
1010
<packaging>jar</packaging>
1111

1212
<name>clustermanage-server</name>
@@ -15,7 +15,7 @@
1515
<parent>
1616
<groupId>org.springframework.boot</groupId>
1717
<artifactId>spring-boot-starter-parent</artifactId>
18-
<version>1.5.4.RELEASE</version>
18+
<version>1.5.6.RELEASE</version>
1919
<relativePath/>
2020
<!-- lookup parent from repository -->
2121
</parent>
@@ -36,6 +36,11 @@
3636
<artifactId>spring-boot-starter-data-mongodb</artifactId>
3737
</dependency>
3838

39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-data-redis</artifactId>
42+
</dependency>
43+
3944
<dependency>
4045
<groupId>org.springframework.boot</groupId>
4146
<artifactId>spring-boot-starter-web</artifactId>

clustermanage-server/src/main/java/cc/bitky/clustermanage/db/bean/Device.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Device {
1212
* 员工卡号
1313
*/
1414
@Field("CardNumber")
15-
String cardNumber;
15+
private String cardNumber;
1616

1717
@Id
1818
private String id;

clustermanage-server/src/main/java/cc/bitky/clustermanage/db/bean/Employee.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
public class Employee {
99

1010
@Id
11-
String id;
11+
private String id;
1212

1313
//个人信息
1414
@Field("Name")
15-
String name;
15+
private String name;
1616

1717
@Field("Department")
18-
String department;
18+
private String department;
1919

2020
//设备位置
2121
@Field("GroupId")
22-
int groupId;
22+
private int groupId;
2323

2424
@Field("DeviceId")
25-
int deviceId;
25+
private int deviceId;
2626

2727
public Employee(String name, String department) {
2828
this.name = name;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cc.bitky.clustermanage.db.mongoops;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.connection.RedisConnectionFactory;
6+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
7+
import org.springframework.data.redis.core.StringRedisTemplate;
8+
9+
import cc.bitky.clustermanage.global.ServerSetting;
10+
11+
@Configuration
12+
public class KyRedisConfig {
13+
14+
@Bean
15+
public RedisConnectionFactory redisConnectionFactory() {
16+
JedisConnectionFactory factory = new JedisConnectionFactory();
17+
factory.setHostName(ServerSetting.HOST);
18+
return factory;
19+
}
20+
21+
@Bean
22+
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
23+
return new StringRedisTemplate(redisConnectionFactory);
24+
}
25+
}

clustermanage-server/src/main/java/cc/bitky/clustermanage/db/presenter/DbDevicePresenter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import cc.bitky.clustermanage.server.message.tcp.TcpMsgResponseStatus;
1616

1717
@Service
18-
class DbDevicePresenter {
18+
class
19+
DbDevicePresenter {
1920
private final DeviceRepository deviceRepository;
2021
private Logger logger = LoggerFactory.getLogger(DbDevicePresenter.class);
2122

clustermanage-server/src/main/java/cc/bitky/clustermanage/db/presenter/KyDbPresenter.java

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.redis.core.StringRedisTemplate;
67
import org.springframework.stereotype.Repository;
78

8-
import java.util.Date;
99
import java.util.List;
10+
import java.util.Random;
11+
import java.util.concurrent.TimeUnit;
1012

1113
import cc.bitky.clustermanage.db.bean.Device;
12-
import cc.bitky.clustermanage.db.bean.DeviceGroup;
1314
import cc.bitky.clustermanage.db.bean.Employee;
1415
import cc.bitky.clustermanage.db.repository.DeviceGroupRepository;
1516
import cc.bitky.clustermanage.server.message.CardType;
@@ -23,50 +24,54 @@ public class KyDbPresenter {
2324
private final DbSettingPresenter dbSettingPresenter;
2425
private final DbDevicePresenter dbDevicePresenter;
2526
private final DbRoutinePresenter dbRoutinePresenter;
26-
27-
private int groupSize = 0;
27+
private final StringRedisTemplate stringRedisTemplate;
28+
Random random = new Random();
29+
// private int groupSize = 0;
30+
private int maxGroupId = 0;
2831

2932
private Logger logger = LoggerFactory.getLogger(KyDbPresenter.class);
3033

3134
@Autowired
3235
public KyDbPresenter(DbRoutinePresenter dbRoutinePresenter, DbEmployeePresenter dbEmployeePresenter
3336
, DeviceGroupRepository deviceGroupRepository, DbDevicePresenter dbDevicePresenter
34-
, DbSettingPresenter dbSettingPresenter) {
37+
, DbSettingPresenter dbSettingPresenter, StringRedisTemplate stringRedisTemplate) {
3538
this.dbRoutinePresenter = dbRoutinePresenter;
3639
this.deviceGroupRepository = deviceGroupRepository;
3740
this.dbSettingPresenter = dbSettingPresenter;
3841
this.dbDevicePresenter = dbDevicePresenter;
3942
this.dbEmployeePresenter = dbEmployeePresenter;
43+
this.stringRedisTemplate = stringRedisTemplate;
4044
}
4145

4246

43-
/**
44-
* 调整(或增加)设备组的数量,使之满足获取到的数据帧的要求
45-
*
46-
* @param groupId 获取到的数据帧的组 Id
47-
* @param autoCreate 是否自动初始化任意组设备
48-
*/
49-
private void initDbDeviceGroup(int groupId, boolean autoCreate) {
50-
if (groupSize < groupId)
51-
groupSize = (int) deviceGroupRepository.count();
52-
53-
while (groupSize < groupId) {
54-
groupSize++;
55-
deviceGroupRepository.save(new DeviceGroup(groupSize));
56-
if (autoCreate)
57-
dbDevicePresenter.InitDbDevices(groupSize);
58-
}
59-
}
47+
// /**
48+
// * 调整(或增加)设备组的数量,使之满足获取到的数据帧的要求
49+
// *
50+
// * @param groupId 获取到的数据帧的组 Id
51+
// * @param autoCreate 是否自动初始化任意组设备
52+
// */
53+
// private void initDbDeviceGroup(int groupId, boolean autoCreate) {
54+
// if (groupSize < groupId)
55+
// groupSize = (int) deviceGroupRepository.count();
56+
//
57+
// while (groupSize < groupId) {
58+
// groupSize++;
59+
// deviceGroupRepository.save(new DeviceGroup(groupSize));
60+
// if (autoCreate)
61+
// dbDevicePresenter.InitDbDevices(groupSize);
62+
// }
63+
// }
6064

6165
/**
62-
* 获得设备组的数量
66+
* 获得最大的设备组 ID
6367
*
64-
* @return 设备组的数量
68+
* @return 设备组的ID
6569
*/
66-
public int obtainDeviceGroupCount() {
67-
if (groupSize == 0)
68-
groupSize = (int) deviceGroupRepository.count();
69-
return groupSize;
70+
public int obtainMaxDeviceGroupId() {
71+
// if (groupSize == 0)
72+
// groupSize = (int) deviceGroupRepository.count();
73+
// return groupSize;
74+
return maxGroupId;
7075
}
7176

7277
/**
@@ -76,17 +81,21 @@ public int obtainDeviceGroupCount() {
7681
* @param autoCreate 自动在数据库中创建设备
7782
*/
7883
private void handleMsgHeartBeat(IMessage tcpMsgHeartBeat, boolean autoCreate) {
79-
80-
initDbDeviceGroup(tcpMsgHeartBeat.getGroupId(), autoCreate);
81-
82-
DeviceGroup deviceGroup = deviceGroupRepository.findByGroupId(tcpMsgHeartBeat.getGroupId());
83-
if (deviceGroup == null) {
84-
logger.warn("设备组(" + tcpMsgHeartBeat.getGroupId() + ") 不存在,心跳无法处理");
85-
return;
86-
}
87-
deviceGroup.setHeartBeatTime(new Date(System.currentTimeMillis()));
88-
deviceGroupRepository.save(deviceGroup);
89-
logger.info("设备组(" + tcpMsgHeartBeat.getGroupId() + ") 心跳处理完毕");
84+
int groupId = tcpMsgHeartBeat.getGroupId();
85+
if (maxGroupId < groupId) maxGroupId = groupId;
86+
String exec = "deviceGroup-exist-" + groupId;
87+
stringRedisTemplate.opsForValue().set(exec, "1", 60, TimeUnit.SECONDS);
88+
//
89+
// initDbDeviceGroup(tcpMsgHeartBeat.getGroupId(), autoCreate);
90+
//
91+
// DeviceGroup deviceGroup = deviceGroupRepository.findByGroupId(tcpMsgHeartBeat.getGroupId());
92+
// if (deviceGroup == null) {
93+
// logger.warn("设备组(" + tcpMsgHeartBeat.getGroupId() + ") 不存在,心跳无法处理");
94+
// return;
95+
// }
96+
// deviceGroup.setHeartBeatTime(new Date(System.currentTimeMillis()));
97+
// deviceGroupRepository.save(deviceGroup);
98+
// logger.info("设备组(" + tcpMsgHeartBeat.getGroupId() + ") 心跳处理完毕");
9099
}
91100

92101
/**
@@ -103,12 +112,30 @@ public Device handleMsgDeviceStatus(TcpMsgResponseStatus tcpMsgResponseStatus, b
103112
handleMsgHeartBeat(tcpMsgResponseStatus, autoCreate);
104113
long l2 = System.currentTimeMillis();
105114

115+
int status = -1;
106116
//更新设备的状态信息
107-
Device device = dbDevicePresenter.handleMsgDeviceStatus(tcpMsgResponseStatus);
117+
String s = stringRedisTemplate.opsForValue().get("deviceStatus-" + tcpMsgResponseStatus.getGroupId() + "-" + tcpMsgResponseStatus.getDeviceId());
118+
try {
119+
status = Integer.valueOf(s);
120+
} catch (NumberFormatException ignored) {
121+
}
122+
Device device = null;
123+
if (status == -1 || status != tcpMsgResponseStatus.getStatus())
124+
device = dbDevicePresenter.handleMsgDeviceStatus(tcpMsgResponseStatus);
125+
108126
if (device == null) {
109-
logger.warn("设备(" + tcpMsgResponseStatus.getGroupId() + ", " + tcpMsgResponseStatus.getDeviceId() + ") 不存在,无法处理");
127+
if (status == tcpMsgResponseStatus.getStatus()) {
128+
logger.info("设备「" + tcpMsgResponseStatus.getGroupId() + ", " + tcpMsgResponseStatus.getDeviceId() + "」『"
129+
+ status + "->" + status + "』: 状态无更新");
130+
} else
131+
logger.warn("设备(" + tcpMsgResponseStatus.getGroupId() + ", " + tcpMsgResponseStatus.getDeviceId() + ") 不存在,无法处理");
110132
return null;
111133
}
134+
if (device.getStatus() != -1) {
135+
String exec = "deviceStatus-" + tcpMsgResponseStatus.getGroupId() + "-" + tcpMsgResponseStatus.getDeviceId();
136+
stringRedisTemplate.opsForValue().set(exec, device.getStatus() + "", (600 + random.nextInt(60)), TimeUnit.SECONDS);
137+
}
138+
112139
if (device.getStatus() == -1) {
113140
logger.info("考勤表无更新");
114141
logger.info("时间耗费:" + (l2 - l1) + "ms; " + (System.currentTimeMillis() - l2) + "ms");

clustermanage-server/src/main/java/cc/bitky/clustermanage/global/ServerSetting.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@ public class ServerSetting {
5353
*/
5454
public static int AUTO_REPEAT_REQUEST_TIMES = 5;
5555

56-
//-----------------------接收到充电状态帧时的处理策略----------------------------
56+
//-----------------------------服务器总体配置----------------------------------
5757
/**
5858
* 项目版本号
5959
*/
60-
public static final String VERSION = "0.9.3";
60+
public static final String VERSION = "1.0.0";
61+
/**
62+
* 最大设备组数量
63+
*/
64+
public static final int MAX_DEVICE_GROUP_SIZE = 127;
65+
/**
66+
* 单设备组中最大的设备数量
67+
*/
68+
public static final int MAX_DEVICE_SIZE_EACH_GROUP = 100;
6169
/**
6270
* 主机名
6371
*/

clustermanage-server/src/main/java/cc/bitky/clustermanage/server/bean/KyServerCenterHandler.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cc.bitky.clustermanage.server.bean;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.stereotype.Component;
57

6-
import cc.bitky.clustermanage.global.ServerSetting;
78
import cc.bitky.clustermanage.db.presenter.KyDbPresenter;
9+
import cc.bitky.clustermanage.global.ServerSetting;
810
import cc.bitky.clustermanage.server.message.CardType;
911
import cc.bitky.clustermanage.server.message.base.IMessage;
1012
import cc.bitky.clustermanage.server.message.send.WebMsgSpecial;
@@ -17,6 +19,7 @@ public class KyServerCenterHandler {
1719
private final ServerTcpMessageHandler serverTcpMessageHandler;
1820
private final KyDbPresenter kyDbPresenter;
1921
private final SendingMsgRepo sendingMsgRepo;
22+
private final Logger logger = LoggerFactory.getLogger(getClass());
2023

2124
@Autowired
2225
public KyServerCenterHandler(SendingMsgRepo sendingMsgRepo, ServerTcpMessageHandler serverTcpMessageHandler, ServerWebMessageHandler serverWebMessageHandler, KyDbPresenter kyDbPresenter) {
@@ -70,27 +73,40 @@ boolean deployFreeCard(int groupId, int deviceId, int maxGroupId) {
7073
* @param message 原始信息
7174
* @return Message 信息是否已成功发送至 TCP 端
7275
*/
73-
private boolean deployGroupedMessage(IMessage message, int maxGroupId, boolean urgent, boolean responsive) {
74-
boolean groupedGroup = message.getGroupId() == 255 || message.getGroupId() == 0;
75-
boolean groupedBox = message.getDeviceId() == 255 || message.getDeviceId() == 0;
76-
76+
private boolean deployGroupedMessage(final IMessage message, int maxGroupId, final boolean urgent, final boolean responsive) {
77+
final int groupId = message.getGroupId();
78+
final int deviceId = message.getDeviceId();
79+
final boolean groupedGroup = groupId == 255 || groupId == 0;
80+
final boolean groupedBox = deviceId == 255 || deviceId == 0;
81+
82+
//消息中位置信息有效性判断有效性判断
83+
if (groupId < 0 || deviceId < 0) {
84+
logger.warn("设备位置信息有效性判断异常「异常小值」,设定组号:" + message.getGroupId() + ", 设备号:" + message.getDeviceId());
85+
return false;
86+
}
87+
if ((deviceId > ServerSetting.MAX_DEVICE_SIZE_EACH_GROUP && groupId != 255)
88+
|| groupId > ServerSetting.MAX_DEVICE_GROUP_SIZE && groupId != 255) {
89+
logger.warn("设备位置信息有效性判断异常「异常大值」,设定组号:" + message.getGroupId() + ", 设备号:" + message.getDeviceId());
90+
return false;
91+
}
92+
//发送成组消息帧时,最大组号设定值必须不小于 0
93+
if (groupedGroup && maxGroupId < 0) return false;
7794

7895
if (!groupedGroup && !groupedBox) {
7996
return sendMsgToTcpSpecial(message, urgent, responsive);
8097
}
8198

99+
if (!groupedGroup && groupedBox) {
100+
return sendMsgTrafficControl(WebMsgSpecial.forBox(message, urgent, responsive));
101+
}
102+
82103
if (groupedGroup && groupedBox) {
83104
if (maxGroupId == 0)
84-
maxGroupId = kyDbPresenter.obtainDeviceGroupCount();
105+
maxGroupId = kyDbPresenter.obtainMaxDeviceGroupId();
85106
if (maxGroupId == 0) return false;
86107

87108
return sendMsgTrafficControl(WebMsgSpecial.forAll(message, maxGroupId, urgent, responsive));
88109
}
89-
90-
if (!groupedGroup && groupedBox) {
91-
return sendMsgTrafficControl(WebMsgSpecial.forBox(message, urgent, responsive));
92-
}
93-
94110
return false;
95111
}
96112

@@ -137,12 +153,13 @@ boolean marchConfirmCard(String cardNumber) {
137153

138154
/**
139155
* 获取 CAN 帧发送队列的信息
156+
*
140157
* @return CAN帧发送队列信息集合
141158
*/
142159
QueueInfo obtainQueueFrame() {
143160
int size = getSendingMsgRepo().getLinkedBlockingDeque().size();
144161
int capacity = ServerSetting.LINKED_DEQUE_LIMIT_CAPACITY;
145162
int interval = ServerSetting.FRAME_SEND_INTERVAL;
146163
return new QueueInfo(size, capacity, interval);
147-
}
164+
}
148165
}

clustermanage-server/src/main/java/cc/bitky/clustermanage/server/bean/ServerTcpMessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public SendingMsgRepo getSendingMsgRepo() {
4646
*
4747
* @param message 设备状态回复 bean
4848
*/
49-
public void handleResDeviceStatus(TcpMsgResponseStatus message) {
49+
public void handleReceivedDeviceStatus(TcpMsgResponseStatus message) {
5050
logger.info("");
5151
logger.info("");
5252
logger.info("***********进入功能消息处理方法「" + message.getGroupId() + ", " + message.getDeviceId() + "」***********");

0 commit comments

Comments
 (0)