|
@@ -20,6 +20,8 @@ import org.slf4j.MDC;
|
|
|
import org.springframework.context.annotation.Scope;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
@@ -43,6 +45,16 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
|
|
|
private static final Long INTERVAL_TIME = 300000L; // 开关时间
|
|
|
private static final Long URGENT_OUT_TIME = 120000L; // 超时时间120S
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 紧急模式
|
|
|
+ */
|
|
|
+ private static final Integer URGENCY = 3;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 其他模式
|
|
|
+ */
|
|
|
+ private static final Integer OTHER = 2;
|
|
|
/**
|
|
|
* 省电模式开关
|
|
|
*/
|
|
@@ -82,10 +94,10 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
if (deviceId != null) {
|
|
|
workModel = modelMap.get(deviceId);//已登录
|
|
|
swm = switchMap.get(deviceId);// 开关
|
|
|
- if (workModel != null && workModel.getUrgentType() == 2
|
|
|
+ if (workModel != null && workModel.getUrgentType() == OTHER
|
|
|
&& (time - workModel.getUrgentTime() > URGENT_OUT_TIME)) {
|
|
|
workModel.setUrgentTime(time);
|
|
|
- workModel.setUrgentType(2);
|
|
|
+ workModel.setUrgentType(OTHER);
|
|
|
modelMap.put(deviceId, workModel);
|
|
|
normalReplyModel(factory, deviceId, channel, 3);
|
|
|
logger.warn("超过指定时间没有收到回复》》》 重新设置");
|
|
@@ -104,23 +116,28 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
break;
|
|
|
case "AP01": // 位置信息
|
|
|
String gpsState = msg.substring(12, 13);
|
|
|
- setSwitchMap(deviceId, gpsState);// 采集数据
|
|
|
- sendMsg2Kafka((msg + deviceId).getBytes(), deviceId, channel);
|
|
|
+ // 如果当前GPS 上报时间和当前服务器时间超过1小时直接断开连接重新连接
|
|
|
+ String gDay = msg.substring(6, 12);
|
|
|
+ String gTime = msg.substring(39, 45);
|
|
|
+ if (checkGpsTime(gDay + gTime, time)) {
|
|
|
+ channel.close();
|
|
|
+ }
|
|
|
+ setSwitchMap(deviceId, gpsState, msg, channel);// 采集数据
|
|
|
normalReply(factory, channel, "BP01");
|
|
|
break;
|
|
|
case "AP33": // 设置模式回复
|
|
|
//IWAP33,080835,1#
|
|
|
if (deviceId != null) {
|
|
|
Integer moderType = getInteger(msg.substring(14, 16));// 收到回复状态
|
|
|
- if (swm.getWorkType() == 3) {// 当前设置的模式
|
|
|
- if (workModel != null && moderType == 3) {
|
|
|
- workModel.setUrgentType(3);
|
|
|
+ if (swm.getWorkType() == URGENCY) {// 当前设置的模式
|
|
|
+ if (workModel != null && moderType == URGENCY) {
|
|
|
+ workModel.setUrgentType(URGENCY);
|
|
|
logger.warn("紧急模式设置成功!>>>>>>>>>>>>>>" + msg);
|
|
|
} else {
|
|
|
logger.warn("紧急模式重新设置!>>>>>>>>>>>>>>");
|
|
|
Thread.sleep(1000);
|
|
|
- normalReplyModel(factory, deviceId, channel, 3);
|
|
|
- workModel.setUrgentType(2);
|
|
|
+ normalReplyModel(factory, deviceId, channel, URGENCY);
|
|
|
+ workModel.setUrgentType(OTHER);
|
|
|
// 更新开关时间
|
|
|
swm.setSwitchTime(time);
|
|
|
switchMap.put(deviceId, swm);
|
|
@@ -153,11 +170,14 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
}
|
|
|
SwitchWorkModel swm = switchMap.get(deviceId);
|
|
|
logger.warn("心跳检测是否更改终端模式:" + (nowTime - swm.getActiveTime() + ";" + (nowTime - swm.getSwitchTime())));
|
|
|
- if (nowTime - swm.getActiveTime() > INTERVAL_TIME
|
|
|
- && nowTime - swm.getSwitchTime() > INTERVAL_TIME) {
|
|
|
- Integer workType = (3 == swm.getWorkType()) ? 2 : 3;
|
|
|
+ if (nowTime - swm.getActiveTime() > INTERVAL_TIME && nowTime - swm.getSwitchTime() > INTERVAL_TIME) {
|
|
|
+ Integer workType = (URGENCY == swm.getWorkType()) ? OTHER : URGENCY;
|
|
|
if (nowTime - swm.getGpsUpTime() > INTERVAL_TIME) {
|
|
|
- workType = 3; // APO1上传间隔大于5分种再次设置为紧急模式
|
|
|
+ workType = URGENCY; // APO1上传间隔大于5分种再次设置为紧急模式
|
|
|
+ }
|
|
|
+ if (URGENCY == workType) {
|
|
|
+ //如果当前设置模式为3 紧急模式,则需要移除第一条GPS 数据
|
|
|
+ swm.setFirstRemove(Boolean.FALSE);
|
|
|
}
|
|
|
// 如果当前状态为A 紧急模式
|
|
|
swm.setSwitchTime(nowTime);
|
|
@@ -165,9 +185,9 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
switchMap.put(deviceId, swm);
|
|
|
logger.warn("心跳检测是下发模式:工作状态" + JSON.toJSONString(swm));
|
|
|
normalReplyModel(factory, deviceId, channel, workType);
|
|
|
- if (workType == 3) {// 设置紧急模式 则更新紧急模式设置状态
|
|
|
+ if (workType == URGENCY) {// 设置紧急模式 则更新紧急模式设置状态
|
|
|
WorkModel wm = new WorkModel();
|
|
|
- wm.setUrgentType(2);//
|
|
|
+ wm.setUrgentType(OTHER);//
|
|
|
wm.setUrgentTime(nowTime);// 设置紧急模式时间
|
|
|
modelMap.put(deviceId, wm);
|
|
|
}
|
|
@@ -190,20 +210,41 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
SwitchWorkModel swm = new SwitchWorkModel();
|
|
|
swm.setActiveTime(time);
|
|
|
swm.setSwitchTime(time);
|
|
|
- swm.setWorkType(3);//1:正常模式,2:省电模式,3:紧急模式
|
|
|
+ swm.setWorkType(URGENCY);//1:正常模式,2:省电模式,3:紧急模式
|
|
|
swm.setGpsUpTime(0L);// GPS 上报时间接口
|
|
|
+ swm.setFirstRemove(Boolean.FALSE);//默认第一条数据丢弃
|
|
|
switchMap.put(deviceId, swm);
|
|
|
logger.warn("初始化数据》》》》》》》》》》" + JSON.toJSONString(swm));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查看GPS 上报时间和当前服务器时间超过2小时 则关闭当前连接
|
|
|
+ * <p>
|
|
|
+ *
|
|
|
+ * @return 返回值为ture 则需要断开连接,如果返回值为false 则不需要断开连接
|
|
|
+ */
|
|
|
+ protected Boolean checkGpsTime(String gTime, Long time) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ DateFormat fmt = new SimpleDateFormat("yyMMddHHmmss");
|
|
|
+ Date date = fmt.parse(gTime);
|
|
|
+ if ((time - date.getTime()) > 3 * 3600 * 1000) {
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* APOI 上报数据 更新活跃时间
|
|
|
*
|
|
|
* @param deviceId
|
|
|
*/
|
|
|
- protected void setSwitchMap(String deviceId, String gpsState) {
|
|
|
+ protected SwitchWorkModel setSwitchMap(String deviceId, String gpsState, String msg, Channel channel) {
|
|
|
if (deviceId == null) {
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
Long time = System.currentTimeMillis();
|
|
|
SwitchWorkModel swm = switchMap.get(deviceId);
|
|
@@ -211,11 +252,18 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
if ("A".equals(gpsState)) {
|
|
|
swm.setActiveTime(time);
|
|
|
logger.warn("更新A 活跃时间》》》》》》》》》》" + JSON.toJSONString(swm));
|
|
|
+ if (swm.getFirstRemove()) {
|
|
|
+ sendMsg2Kafka((msg + deviceId).getBytes(), deviceId, channel);
|
|
|
+ } else {
|
|
|
+ swm.setFirstRemove(Boolean.TRUE);
|
|
|
+ logger.info("第一条数据移除>>>>>>>>>>>>>>>" + deviceId);
|
|
|
+ }
|
|
|
} else {
|
|
|
logger.warn("当前上报数据为V 时间不更新!!>>>>>>>>>>>>>>>");
|
|
|
}
|
|
|
switchMap.put(deviceId, swm);
|
|
|
logger.warn("更新A 活跃时间》》》》》》》》》》" + JSON.toJSONString(swm));
|
|
|
+ return swm;
|
|
|
}
|
|
|
|
|
|
protected void printAcceptanceData(ByteBuf dataByteBuf, ChannelHandlerContext ctx) {
|
|
@@ -253,10 +301,10 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
if (secondsSinceLogin < 5L) {
|
|
|
TimeUnit.SECONDS.sleep(5 - secondsSinceLogin);
|
|
|
}
|
|
|
- normalReplyModel(factory, deviceId, channel, 3);
|
|
|
+ normalReplyModel(factory, deviceId, channel, URGENCY);
|
|
|
WorkModel wm = new WorkModel();
|
|
|
wm.setUrgentTime(currentTime.getTime());// 设置紧急模式时间
|
|
|
- wm.setUrgentType(2);// 默认指令模式为正常模式
|
|
|
+ wm.setUrgentType(OTHER);// 默认指令模式为正常模式
|
|
|
modelMap.put(deviceId, wm);
|
|
|
} catch (InterruptedException e) {
|
|
|
logger.error(e.getMessage());
|
|
@@ -406,6 +454,11 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
*/
|
|
|
private Long gpsUpTime;
|
|
|
|
|
|
+ /**
|
|
|
+ * 模式切换第一条是否移除 true 不需要移除, false 需要移除
|
|
|
+ */
|
|
|
+ private Boolean firstRemove = Boolean.TRUE;
|
|
|
+
|
|
|
public Long getActiveTime() {
|
|
|
return activeTime;
|
|
|
}
|
|
@@ -437,6 +490,14 @@ public class WatchJWServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
public void setGpsUpTime(Long gpsUpTime) {
|
|
|
this.gpsUpTime = gpsUpTime;
|
|
|
}
|
|
|
+
|
|
|
+ public Boolean getFirstRemove() {
|
|
|
+ return firstRemove;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setFirstRemove(Boolean firstRemove) {
|
|
|
+ this.firstRemove = firstRemove;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|