|
@@ -1,114 +1,114 @@
|
|
|
-package com.sooka.sponest.dataexchange.monitoringEquipment.shuicejia;
|
|
|
-
|
|
|
-import cn.hutool.core.io.BufferUtil;
|
|
|
-import cn.hutool.core.io.IORuntimeException;
|
|
|
-import cn.hutool.core.io.IoUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.socket.nio.NioServer;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
-import com.sooka.sponest.dataexchange.remoteapi.service.center.monitor.RemoteMonitorService;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.boot.CommandLineRunner;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.io.IOException;
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.nio.ByteBuffer;
|
|
|
-import java.nio.channels.SocketChannel;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-@Component
|
|
|
-public class TcpServer extends Thread implements CommandLineRunner {
|
|
|
-
|
|
|
- private static final Logger log = LoggerFactory.getLogger(TcpServer.class);
|
|
|
- private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
-
|
|
|
- /**服务器地址**/
|
|
|
- @Value("${monitoringEquipment.tcp.port:}")
|
|
|
- public Integer port;
|
|
|
-
|
|
|
- @Resource
|
|
|
- public RemoteMonitorService remoteMonitorService;
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run(String... args){
|
|
|
- NioServer server = new NioServer(port);
|
|
|
- server.setChannelHandler((sc)->{
|
|
|
- ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
|
|
- try{
|
|
|
- int readBytes = sc.read(readBuffer);
|
|
|
- if (readBytes > 0) {
|
|
|
- readBuffer.flip();
|
|
|
- byte[] bytes = new byte[readBuffer.remaining()];
|
|
|
- readBuffer.get(bytes);
|
|
|
- String body = StrUtil.utf8Str(bytes);
|
|
|
- readAndWrite(sc, body);
|
|
|
- } else if (readBytes < 0) {
|
|
|
- IoUtil.close(sc);
|
|
|
- }
|
|
|
- } catch (IOException e){
|
|
|
- throw new IORuntimeException(e);
|
|
|
- }
|
|
|
- });
|
|
|
- server.listen();
|
|
|
- }
|
|
|
-
|
|
|
- public void readAndWrite(SocketChannel channel, String response) throws IOException {
|
|
|
- JSONObject object;
|
|
|
- AjaxResult result;
|
|
|
- log.info("TCPServer 收到数据 ==> {}", response);
|
|
|
- try {
|
|
|
- object = JSONObject.parseObject(response);
|
|
|
- //推送数据到监控中心
|
|
|
- result = remoteMonitorService.saveWaterMonitoringData(object);
|
|
|
- log.info("TCPServer 数据推送成功! result ===> {}", result);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.info("TCPServer 数据推送失败! result ===> {}", e.getMessage());
|
|
|
- return;
|
|
|
- }
|
|
|
- //返回客户端数据
|
|
|
- String st = object.get("ST").toString();
|
|
|
- Integer serial = Integer.parseInt(object.get("SERIAL").toString());
|
|
|
- JSONObject res = new JSONObject();
|
|
|
- res.put("ST",st);
|
|
|
- res.put("SERIAL",serial);
|
|
|
- res.put("MARK",1);
|
|
|
- res.put("TM", sdf.format(new Date()));
|
|
|
- //将缓冲数据写入渠道,返回给客户端
|
|
|
- channel.write(BufferUtil.createUtf8(res.toJSONString()));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 对象转Map
|
|
|
- * @param object
|
|
|
- * @return
|
|
|
- * @throws IllegalAccessException
|
|
|
- */
|
|
|
- public static Map beanToMap(Object object){
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- Field[] fields = object.getClass().getDeclaredFields();
|
|
|
- for (Field field : fields) {
|
|
|
- field.setAccessible(true);
|
|
|
- try {
|
|
|
- map.put(field.getName(), field.get(object));
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- return map;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
+//package com.sooka.sponest.dataexchange.monitoringEquipment.shuicejia;
|
|
|
+//
|
|
|
+//import cn.hutool.core.io.BufferUtil;
|
|
|
+//import cn.hutool.core.io.IORuntimeException;
|
|
|
+//import cn.hutool.core.io.IoUtil;
|
|
|
+//import cn.hutool.core.util.StrUtil;
|
|
|
+//import cn.hutool.socket.nio.NioServer;
|
|
|
+//import com.alibaba.fastjson.JSONObject;
|
|
|
+//import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
+//import com.sooka.sponest.dataexchange.remoteapi.service.center.monitor.RemoteMonitorService;
|
|
|
+//import org.slf4j.Logger;
|
|
|
+//import org.slf4j.LoggerFactory;
|
|
|
+//import org.springframework.beans.factory.annotation.Value;
|
|
|
+//import org.springframework.boot.CommandLineRunner;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//
|
|
|
+//import javax.annotation.Resource;
|
|
|
+//import java.io.IOException;
|
|
|
+//import java.lang.reflect.Field;
|
|
|
+//import java.nio.ByteBuffer;
|
|
|
+//import java.nio.channels.SocketChannel;
|
|
|
+//import java.text.SimpleDateFormat;
|
|
|
+//import java.util.Date;
|
|
|
+//import java.util.HashMap;
|
|
|
+//import java.util.Map;
|
|
|
+//
|
|
|
+//@Component
|
|
|
+//public class TcpServer extends Thread implements CommandLineRunner {
|
|
|
+//
|
|
|
+// private static final Logger log = LoggerFactory.getLogger(TcpServer.class);
|
|
|
+// private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+//
|
|
|
+// /**服务器地址**/
|
|
|
+// @Value("${monitoringEquipment.tcp.port:}")
|
|
|
+// public Integer port;
|
|
|
+//
|
|
|
+// @Resource
|
|
|
+// public RemoteMonitorService remoteMonitorService;
|
|
|
+//
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void run(String... args){
|
|
|
+// NioServer server = new NioServer(port);
|
|
|
+// server.setChannelHandler((sc)->{
|
|
|
+// ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
|
|
+// try{
|
|
|
+// int readBytes = sc.read(readBuffer);
|
|
|
+// if (readBytes > 0) {
|
|
|
+// readBuffer.flip();
|
|
|
+// byte[] bytes = new byte[readBuffer.remaining()];
|
|
|
+// readBuffer.get(bytes);
|
|
|
+// String body = StrUtil.utf8Str(bytes);
|
|
|
+// readAndWrite(sc, body);
|
|
|
+// } else if (readBytes < 0) {
|
|
|
+// IoUtil.close(sc);
|
|
|
+// }
|
|
|
+// } catch (IOException e){
|
|
|
+// throw new IORuntimeException(e);
|
|
|
+// }
|
|
|
+// });
|
|
|
+// server.listen();
|
|
|
+// }
|
|
|
+//
|
|
|
+// public void readAndWrite(SocketChannel channel, String response) throws IOException {
|
|
|
+// JSONObject object;
|
|
|
+// AjaxResult result;
|
|
|
+// log.info("TCPServer 收到数据 ==> {}", response);
|
|
|
+// try {
|
|
|
+// object = JSONObject.parseObject(response);
|
|
|
+// //推送数据到监控中心
|
|
|
+// result = remoteMonitorService.saveWaterMonitoringData(object);
|
|
|
+// log.info("TCPServer 数据推送成功! result ===> {}", result);
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// log.info("TCPServer 数据推送失败! result ===> {}", e.getMessage());
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// //返回客户端数据
|
|
|
+// String st = object.get("ST").toString();
|
|
|
+// Integer serial = Integer.parseInt(object.get("SERIAL").toString());
|
|
|
+// JSONObject res = new JSONObject();
|
|
|
+// res.put("ST",st);
|
|
|
+// res.put("SERIAL",serial);
|
|
|
+// res.put("MARK",1);
|
|
|
+// res.put("TM", sdf.format(new Date()));
|
|
|
+// //将缓冲数据写入渠道,返回给客户端
|
|
|
+// channel.write(BufferUtil.createUtf8(res.toJSONString()));
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 对象转Map
|
|
|
+// * @param object
|
|
|
+// * @return
|
|
|
+// * @throws IllegalAccessException
|
|
|
+// */
|
|
|
+// public static Map beanToMap(Object object){
|
|
|
+// Map<String, Object> map = new HashMap<>();
|
|
|
+// Field[] fields = object.getClass().getDeclaredFields();
|
|
|
+// for (Field field : fields) {
|
|
|
+// field.setAccessible(true);
|
|
|
+// try {
|
|
|
+// map.put(field.getName(), field.get(object));
|
|
|
+// } catch (IllegalAccessException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return map;
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+//}
|
|
|
+//
|
|
|
+//
|