|
|
|
@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.inspect.tcpserver.constant.Constant; |
|
|
|
import com.inspect.tcpserver.controller.ClientController; |
|
|
|
import com.inspect.tcpserver.domain.DeviceServerProperties; |
|
|
|
import com.inspect.tcpserver.util.Color; |
|
|
|
import com.thoughtworks.xstream.XStream; |
|
|
|
import com.thoughtworks.xstream.io.naming.NoNameCoder; |
|
|
|
import com.thoughtworks.xstream.io.xml.Xpp3Driver; |
|
|
|
@ -139,8 +140,8 @@ public class NettyServer { |
|
|
|
} |
|
|
|
|
|
|
|
//发送消息 |
|
|
|
public void sendMsg(String uuid, String client, boolean request, String xml) { |
|
|
|
if (tcpClientMap.containsKey(client) && !StringUtil.isNullOrEmpty(xml)) { |
|
|
|
public void sendMsg(String uuid, String clientKey, boolean request, String xml) { |
|
|
|
if (tcpClientMap.containsKey(clientKey) && !StringUtil.isNullOrEmpty(xml)) { |
|
|
|
ByteBuf byteBuf = Unpooled.copiedBuffer(xml, CharsetUtil.UTF_8); |
|
|
|
int length = byteBuf.readableBytes(); |
|
|
|
ByteBuf allBuf = Unpooled.buffer(length + ConfigType.dataLength); |
|
|
|
@ -154,11 +155,11 @@ public class NettyServer { |
|
|
|
allBuf.writeByte(0xEB); |
|
|
|
allBuf.writeByte(0x90); |
|
|
|
redisTemplate.opsForValue().set(String.valueOf(sendIndex), allBuf.toString(CharsetUtil.US_ASCII), 60L, TimeUnit.SECONDS); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 向设备端机器人回送消息序列号: {}, 接收端序列号: {}, 消息体: ################\n{}", uuid, client, sendIndex, receiveIndex, xml); |
|
|
|
nettyServerHandler.sendMsg(uuid, tcpClientMap.get(client), allBuf); |
|
|
|
logger.info("######## 会话:{}, 客户:{}, 向设备回送序列:{}, 接收端序列:{}, 消息########\n{}", uuid, clientKey, sendIndex, receiveIndex, xml); |
|
|
|
nettyServerHandler.sendMsg(uuid, clientKey, tcpClientMap.get(clientKey), allBuf); |
|
|
|
sendIndex++; |
|
|
|
} else { |
|
|
|
logger.warn("################ 会话: {}, 设备端机器人: [{}] 离线!!! ################", uuid, client); |
|
|
|
logger.warn("######## 会话: {}, 设备端机器人: [{}] 离线!!! ########", uuid, clientKey); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -201,7 +202,7 @@ public class NettyServer { |
|
|
|
command = Integer.parseInt(root.element("Command").getText()); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("################ 会话: {}, 客户: {}, 消息类型: {}, 命令:{}, 消息体: ################ \n{}", binaryModel.uuid, binaryModel.id, type, command, xml); |
|
|
|
logger.info(Color.RED + "######## 会话: {}, 客户: {}, 消息类型: {}, 命令:{}, 消息体: ########\n{}" + Color.END, binaryModel.uuid, binaryModel.id, type, command, xml); |
|
|
|
//判断是否重发 |
|
|
|
if (type == SystemType.system) { |
|
|
|
if (command == SystemType.has_response || command == SystemType.no_response) { |
|
|
|
@ -218,12 +219,12 @@ public class NettyServer { |
|
|
|
switch (command) { |
|
|
|
case SystemType.register_request: |
|
|
|
// 收到接入侧注册信息 |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端注册信息 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端注册信息 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
dealRegister(binaryModel.uuid, xml); |
|
|
|
break; |
|
|
|
case SystemType.heart_request: |
|
|
|
// 处理心跳请求响应 |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端心跳 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端心跳 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
sendHeartBeat(binaryModel.uuid, xml); |
|
|
|
break; |
|
|
|
case SystemType.has_response: |
|
|
|
@ -232,22 +233,23 @@ public class NettyServer { |
|
|
|
// 处理设备上报的模型同步响应 |
|
|
|
if (null != root.element("Items").element("Item").attribute("device_file_path")) { |
|
|
|
// 收到接入侧模型同步数据 |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端模型同步数据 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
logger.info(Color.YELLOW + "######## 会话: {}, 客户: {}, 客户端模型同步数据 ########" + Color.END, binaryModel.uuid, binaryModel.id); |
|
|
|
// json = downXml2Json.ModelControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, ModelControl.class); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, ModelControl.class); |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(json); |
|
|
|
jsonObject.put("uuid", binaryModel.uuid); |
|
|
|
jsonObject.put("Type", Constant.MODEL_UP_TYPE); |
|
|
|
rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
// 任务控制响应任务执行ID |
|
|
|
if (null != root.element("Items").element("Item").attribute("task_patrolled_id")) { |
|
|
|
// 收到接入侧任务下发或控制回复数据 |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 设备端任务下发或控制回复数据 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 设备端任务下发或控制回复数据 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
// json = downXml2Json.ModelControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, ModelControl.class); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, ModelControl.class); |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(json); |
|
|
|
jsonObject.put("SendCode", ""); |
|
|
|
clientController.sendMsg(jsonObject.toJSONString()); |
|
|
|
clientController.sendMsg(binaryModel.uuid, jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 接收到的系统类信息报文中,root:{},中不包含items或items中没有item,不予处理 |
|
|
|
@ -256,65 +258,68 @@ public class NettyServer { |
|
|
|
break; |
|
|
|
default: |
|
|
|
// 接收到的系统类信息报文中,command:{},不在处理范围内,不予处理 |
|
|
|
logger.warn("################ 非法的消息不予处理, 客户: {}, 命令: {}, 消息体 ################ \n{}", binaryModel.id, command, xml); |
|
|
|
logger.warn("######## 非法的消息不予处理, 客户: {}, 命令: {}, 消息体 ########\n{}", binaryModel.id, command, xml); |
|
|
|
} |
|
|
|
break; |
|
|
|
case PushType.patrolDeviceState: |
|
|
|
// json = downXml2Json.PatrolDeviceStateControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, PatrolDeviceStateControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端设备状态数据 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, PatrolDeviceStateControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端设备状态数据 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.patrolDeviceRunning: |
|
|
|
// json = downXml2Json.PatrolDeviceRunningControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, PatrolDeviceRuningControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端设备运行数据 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, PatrolDeviceRuningControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端设备运行数据 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.nestState: |
|
|
|
// json = downXml2Json.NestStateControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, NestStateControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端机巢状态数据 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, NestStateControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端机巢状态数据 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.nestRunning: |
|
|
|
// json = downXml2Json.NestRunningControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, NestRuningControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端机巢运行数据 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, NestRuningControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端机巢运行数据 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.location: |
|
|
|
// json = downXml2Json.LocationControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, LocationControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端设备坐标 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, LocationControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端设备坐标 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.route: |
|
|
|
// json = downXml2Json.RouteControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, RouteControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端设备路线 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, RouteControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端设备路线 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.alarm: |
|
|
|
// json = down.AlarmControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, AlarmControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端设备异常告警 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, AlarmControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端设备异常告警 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.environment: |
|
|
|
// json = downXml2Json.EnvironmentControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, EnvironmentControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端设备上报环境数据 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, EnvironmentControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端设备上报环境数据 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.taskState: |
|
|
|
// json = downXml2Json.TaskStateControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, TaskStateControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端设备任务状态 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, TaskStateControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端设备任务状态 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
case PushType.result: |
|
|
|
// json = downXml2Json.TaskResultControlXml2Json(xml); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.id, xml, TaskResultControl.class); |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 客户端巡视结果 ################", binaryModel.uuid, binaryModel.id); |
|
|
|
json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, TaskResultControl.class); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 客户端巡视结果 ########", binaryModel.uuid, binaryModel.id); |
|
|
|
break; |
|
|
|
default: |
|
|
|
// server-handle-接收到的type:{},不在处理范围内,不予处理 |
|
|
|
logger.info("################ 会话: {}, 客户: {}, 非法的消息不予处理, 类型{}, 消息体 ################", binaryModel.id, type, xml); |
|
|
|
logger.info("######## 会话: {}, 客户: {}, 非法的消息不予处理, 类型{}, 消息体 {} ########", binaryModel.uuid, binaryModel.id, type, xml); |
|
|
|
} |
|
|
|
if (type != SystemType.system && !StringUtil.isNullOrEmpty(json)) { |
|
|
|
//rabbitmq推送到消息队列中基于springboot_xggd |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(json); |
|
|
|
jsonObject.put("uuid", binaryModel.uuid); |
|
|
|
json = jsonObject.toJSONString(); |
|
|
|
rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, json); |
|
|
|
String receiveCode = root.element("ReceiveCode").getText(); |
|
|
|
feedbackToDevice(binaryModel.uuid, receiveCode, sendCode); |
|
|
|
@ -348,7 +353,7 @@ public class NettyServer { |
|
|
|
xStream.addPermission(AnyTypePermission.ANY); |
|
|
|
obj = (BaseControl) xStream.fromXML(xml); |
|
|
|
} catch (com.thoughtworks.xstream.XStreamException e) { |
|
|
|
logger.error("################ 注册解析失败, 异常堆栈[PatrolHost] {} ################", e.getMessage()); |
|
|
|
logger.error("######## 注册异常堆栈[PatrolHost] 会话: {}, {} ########", uuid, e.getMessage()); |
|
|
|
try { |
|
|
|
XStream xStreamEx = getXmlStreamInstance(); |
|
|
|
xStreamEx.alias(aliasDevice, BaseControl.class); |
|
|
|
@ -357,8 +362,7 @@ public class NettyServer { |
|
|
|
xStreamEx.addPermission(AnyTypePermission.ANY); |
|
|
|
obj = (BaseControl) xStreamEx.fromXML(xml); |
|
|
|
} catch (com.thoughtworks.xstream.XStreamException e2) { |
|
|
|
logger.error("################ 解析失败[PatrolDevice] ################"); |
|
|
|
logger.error("################ 异常堆栈 ################\n{}", ExceptionUtils.getStackTrace(e2)); |
|
|
|
logger.error("######## 解析失败[PatrolDevice] {} ########", e2.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -390,9 +394,10 @@ public class NettyServer { |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put("uuid", uuid); |
|
|
|
jsonObject.put("patroldevice_code", obj.SendCode); |
|
|
|
jsonObject.put("SendCode", obj.SendCode); |
|
|
|
jsonObject.put("Type", "heartbeat"); |
|
|
|
jsonObject.put("eventType", "connect"); |
|
|
|
jsonObject.put("heart_beat_interval", ConfigType.heart_beat_interval); |
|
|
|
jsonObject.put("HeartBeatInterval", ConfigType.heart_beat_interval); |
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, jsonObject.toJSONString()); |
|
|
|
} else { |
|
|
|
@ -441,15 +446,14 @@ public class NettyServer { |
|
|
|
xStream.autodetectAnnotations(true); |
|
|
|
xml = xStream.toXML(responseControl); |
|
|
|
} catch (com.thoughtworks.xstream.XStreamException e) { |
|
|
|
logger.error("################ DOWN REGISTER 解析失败, 异常堆栈 [PatrolHost]: {} ################", ExceptionUtils.getMessage(e)); |
|
|
|
logger.error("######## DOWN REGISTER 解析失败, 异常堆栈 [PatrolHost]: {} ########", e.getMessage()); |
|
|
|
try { |
|
|
|
XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); |
|
|
|
xStream.alias(aliasDevice, RegisterResponseControl.class); |
|
|
|
xStream.autodetectAnnotations(true); |
|
|
|
xml = xStream.toXML(responseControl); |
|
|
|
} catch (com.thoughtworks.xstream.XStreamException e2) { |
|
|
|
logger.error("################ DOWN REGISTER 解析失败[PatrolDevice] ################"); |
|
|
|
logger.error("################ DOWN REGISTER 异常堆栈[PatrolDevice] ################\n{}", ExceptionUtils.getStackTrace(e2)); |
|
|
|
logger.error("################ DOWN REGISTER 解析失败[PatrolDevice] {} ################", e2.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
sendMsg(uuid, sendCode, false, xml); |
|
|
|
@ -466,7 +470,7 @@ public class NettyServer { |
|
|
|
xStream.addPermission(AnyTypePermission.ANY); |
|
|
|
obj = (BaseControl) xStream.fromXML(xml); |
|
|
|
} catch (com.thoughtworks.xstream.XStreamException e) { |
|
|
|
logger.error("################ HEARTBEAT TO MQ 解析失败, 异常堆栈[PatrolHost]: {} ################", e.getMessage()); |
|
|
|
logger.error("######## HEARTBEAT TO MQ 解析失败, 异常堆栈[PatrolHost]: {} ########", e.getMessage()); |
|
|
|
try { |
|
|
|
XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); |
|
|
|
xStream.alias(aliasDevice, BaseControl.class); |
|
|
|
@ -475,8 +479,7 @@ public class NettyServer { |
|
|
|
xStream.addPermission(AnyTypePermission.ANY); |
|
|
|
obj = (BaseControl) xStream.fromXML(xml); |
|
|
|
} catch (com.thoughtworks.xstream.XStreamException e2) { |
|
|
|
logger.error("################ HEARTBEAT TO MQ 解析失败[PatrolDevice] ################"); |
|
|
|
logger.error("################ HEARTBEAT TO MQ 异常堆栈[PatrolDevice] ################\n{}", ExceptionUtils.getStackTrace(e2)); |
|
|
|
logger.error("######## HEARTBEAT TO MQ 解析失败[PatrolDevice] {} ########", e2.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -488,7 +491,7 @@ public class NettyServer { |
|
|
|
jsonObject.put("SendCode", obj.SendCode); |
|
|
|
jsonObject.put("Type", "heartbeat"); |
|
|
|
jsonObject.put("eventType", "heart"); |
|
|
|
jsonObject.put("heart_beat_interval", ConfigType.heart_beat_interval); |
|
|
|
jsonObject.put("HeartBeatInterval", ConfigType.heart_beat_interval); |
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
@ -549,7 +552,7 @@ public class NettyServer { |
|
|
|
|
|
|
|
} |
|
|
|
if (!StringUtils.isEmpty(xml)) { |
|
|
|
logger.info("################ 向设备端下发命令 ################\n{}", xml); |
|
|
|
//logger.info("################ 向设备端下发命令 ################\n{}", xml); |
|
|
|
sendMsg("", receiveCode, true, xml); |
|
|
|
} |
|
|
|
} |
|
|
|
|