فهرست منبع

传感器推送数据

lchao 2 سال پیش
والد
کامیت
8b23fa4fe3
2فایلهای تغییر یافته به همراه225 افزوده شده و 147 حذف شده
  1. 112 0
      ruoyi-admin/src/main/java/com/ruoyi/tcp/TcpServer.java
  2. 113 147
      ruoyi-admin/src/main/java/com/ruoyi/tcp/domain/InputLine.java

+ 112 - 0
ruoyi-admin/src/main/java/com/ruoyi/tcp/TcpServer.java

@@ -0,0 +1,112 @@
+package com.ruoyi.tcp;
+
+import cn.hutool.core.io.BufferUtil;
+import cn.hutool.core.io.IORuntimeException;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.lang.Console;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.socket.nio.NioServer;
+import com.alibaba.fastjson.JSONObject;
+import com.dahuatech.hutool.core.convert.Convert;
+import com.ruoyi.framework.web.service.ConfigService;
+import com.ruoyi.tcp.domain.InputLine;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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 {
+
+    @Resource
+    private ConfigService configService;
+
+    private static final Logger log = LoggerFactory.getLogger(TcpServer.class);
+    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+    @Override
+    public void run(String... args){
+        Integer port = Integer.parseInt(configService.getKey("port"));
+        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;
+        try {
+            object = JSONObject.parseObject(response);
+            //先推送
+            String result = HttpRequest.post(configService.getKey("centerMonitorURL")+"device/saveWaterMonitoringData")
+                    .header("Content-Type", "application/json")
+                    .body(object.toJSONString())
+                    .execute().body();
+            System.out.println(result);
+        } catch (Exception e) {
+            e.printStackTrace();
+            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;
+    }
+
+
+}
+
+

+ 113 - 147
ruoyi-admin/src/main/java/com/ruoyi/tcp/domain/InputLine.java

@@ -1,269 +1,235 @@
 package com.ruoyi.tcp.domain;
 
-import com.ruoyi.common.annotation.Excel;
+import com.alibaba.fastjson.JSONObject;
 import com.ruoyi.common.core.domain.BaseEntity;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
 
 public class InputLine extends BaseEntity {
 
-
     /** 测站唯一编码 */
     private String st;
 
     /** 功能码 */
-    @Excel(name = "功能码")
     private String fun;
 
     /** 数据采集时间 */
-    @Excel(name = "数据采集时间")
     private String tt;
 
     /** 通讯时间 */
-    @Excel(name = "通讯时间")
     private String tm;
 
     /** 版本 */
-    @Excel(name = "版本")
-    private Long ver;
+    private Float ver;
 
     /** 传输标志 */
-    @Excel(name = "传输标志")
-    private Long mark;
+    private Integer mark;
 
     /** 数据包流水号 */
-    @Excel(name = "数据包流水号")
-    private Long serial;
+    private Integer serial;
 
     /** 测站类型 */
-    @Excel(name = "测站类型")
     private String type;
 
     /** 设备温度 */
-    @Excel(name = "设备温度")
-    private Long dtemp;
+    private Float dtemp;
 
     /** 电池电量 */
-    @Excel(name = "电池电量")
-    private Long bat;
+    private Integer bat;
 
     /** 设备通讯信号强度 */
-    @Excel(name = "设备通讯信号强度")
-    private Long signal;
+    private Integer signal;
 
     /** 设备电池电压 */
-    @Excel(name = "设备电池电压")
-    private Long vt;
-
+    private Float vt;
 
+    /** 设备编码 */
+    private String deviceCode;
 
     private String q1;
 
-
-
     private String v1;
 
-
-
     private String z1;
 
+    private String q;
 
+    private String cq1;
 
-    private String q;
 
 
-//    @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
-    private String cq1;
 
-    /** 设备编码 */
-    @Excel(name = "设备编码")
-    private String deviceCode;
+    public String getSt() {
+        return st;
+    }
 
-    public void setSt(String st)
-    {
+    public void setSt(String st) {
         this.st = st;
     }
 
-    public String getSt()
-    {
-        return st;
+    public String getFun() {
+        return fun;
     }
-    public void setFun(String fun)
-    {
+
+    public void setFun(String fun) {
         this.fun = fun;
     }
 
-    public String getFun()
-    {
-        return fun;
+    public String getTt() {
+        return tt;
     }
-    public void setTt(String tt)
-    {
+
+    public void setTt(String tt) {
         this.tt = tt;
     }
 
-    public String getTt()
-    {
-        return tt;
+    public String getTm() {
+        return tm;
     }
-    public void setTm(String tm)
-    {
+
+    public void setTm(String tm) {
         this.tm = tm;
     }
 
-    public String getTm()
-    {
-        return tm;
+    public Float getVer() {
+        return ver;
     }
-    public void setVer(Long ver)
-    {
+
+    public void setVer(Float ver) {
         this.ver = ver;
     }
 
-    public Long getVer()
-    {
-        return ver;
+    public Integer getMark() {
+        return mark;
     }
-    public void setMark(Long mark)
-    {
+
+    public void setMark(Integer mark) {
         this.mark = mark;
     }
 
-    public Long getMark()
-    {
-        return mark;
+    public Integer getSerial() {
+        return serial;
     }
-    public void setSerial(Long serial)
-    {
+
+    public void setSerial(Integer serial) {
         this.serial = serial;
     }
 
-    public Long getSerial()
-    {
-        return serial;
+    public String getType() {
+        return type;
     }
-    public void setType(String type)
-    {
+
+    public void setType(String type) {
         this.type = type;
     }
 
-    public String getType()
-    {
-        return type;
+    public Float getDtemp() {
+        return dtemp;
     }
-    public void setDtemp(Long dtemp)
-    {
+
+    public void setDtemp(Float dtemp) {
         this.dtemp = dtemp;
     }
 
-    public Long getDtemp()
-    {
-        return dtemp;
+    public Integer getBat() {
+        return bat;
     }
-    public void setBat(Long bat)
-    {
+
+    public void setBat(Integer bat) {
         this.bat = bat;
     }
 
-    public Long getBat()
-    {
-        return bat;
+    public Integer getSignal() {
+        return signal;
     }
-    public void setSignal(Long signal)
-    {
+
+    public void setSignal(Integer signal) {
         this.signal = signal;
     }
 
-    public Long getSignal()
-    {
-        return signal;
+    public Float getVt() {
+        return vt;
     }
-    public void setVt(Long vt)
-    {
+
+    public void setVt(Float vt) {
         this.vt = vt;
     }
 
-    public Long getVt()
-    {
-        return vt;
+    public String getDeviceCode() {
+        return deviceCode;
     }
-    public void setQ1(String q1)
-    {
-        this.q1 = q1;
+
+    public void setDeviceCode(String deviceCode) {
+        this.deviceCode = deviceCode;
     }
 
-    public String getQ1()
-    {
+    public String getQ1() {
         return q1;
     }
-    public void setV1(String v1)
-    {
-        this.v1 = v1;
+
+    public void setQ1(String q1) {
+        this.q1 = q1;
     }
 
-    public String getV1()
-    {
+    public String getV1() {
         return v1;
     }
-    public void setZ1(String z1)
-    {
-        this.z1 = z1;
+
+    public void setV1(String v1) {
+        this.v1 = v1;
     }
 
-    public String getZ1()
-    {
+    public String getZ1() {
         return z1;
     }
-    public void setQ(String q)
-    {
-        this.q = q;
+
+    public void setZ1(String z1) {
+        this.z1 = z1;
     }
 
-    public String getQ()
-    {
+    public String getQ() {
         return q;
     }
-    public void setCq1(String cq1)
-    {
-        this.cq1 = cq1;
+
+    public void setQ(String q) {
+        this.q = q;
     }
 
-    public String getCq1()
-    {
+    public String getCq1() {
         return cq1;
     }
-    public void setDeviceCode(String deviceCode)
-    {
-        this.deviceCode = deviceCode;
-    }
 
-    public String getDeviceCode()
-    {
-        return deviceCode;
+    public void setCq1(String cq1) {
+        this.cq1 = cq1;
     }
 
-    @Override
-    public String toString() {
-        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
-                .append("st", getSt())
-                .append("fun", getFun())
-                .append("tt", getTt())
-                .append("tm", getTm())
-                .append("ver", getVer())
-                .append("mark", getMark())
-                .append("serial", getSerial())
-                .append("type", getType())
-                .append("dtemp", getDtemp())
-                .append("bat", getBat())
-                .append("signal", getSignal())
-                .append("vt", getVt())
-                .append("q1", getQ1())
-                .append("v1", getV1())
-                .append("z1", getZ1())
-                .append("q", getQ())
-                .append("cq1", getCq1())
-                .append("deviceCode", getDeviceCode())
-                .toString();
+    public void parseJSONObjectToDomain(JSONObject jsonObject){
+        this.st = jsonObject.get("ST").toString();
+        this.fun = jsonObject.get("FUN").toString();
+        this.tt = jsonObject.get("TT").toString();
+        this.tm = jsonObject.get("TM").toString();
+        this.ver = Float.parseFloat(jsonObject.get("VER").toString());
+        this.mark = Integer.parseInt(jsonObject.get("MARK").toString());
+        this.serial = Integer.parseInt(jsonObject.get("SERIAL").toString());
+        this.type = jsonObject.get("TYPE").toString();
+        this.dtemp = Float.parseFloat(jsonObject.get("DTEMP").toString());
+        this.bat = Integer.parseInt(jsonObject.get("BAT").toString());
+        this.signal = Integer.parseInt(jsonObject.get("SIGNAL").toString());
+        this.vt = Float.parseFloat(jsonObject.get("VT").toString());
+    }
+
+    public void parseJSONObjectToDomain(JSONObject jsonObject,String deviceCode){
+        this.st = jsonObject.get("ST").toString();
+        this.fun = jsonObject.get("FUN").toString();
+        this.tt = jsonObject.get("TT").toString();
+        this.tm = jsonObject.get("TM").toString();
+        this.ver = Float.parseFloat(jsonObject.get("VER").toString());
+        this.mark = Integer.parseInt(jsonObject.get("MARK").toString());
+        this.serial = Integer.parseInt(jsonObject.get("SERIAL").toString());
+        this.type = jsonObject.get("TYPE").toString();
+        this.dtemp = Float.parseFloat(jsonObject.get("DTEMP").toString());
+        this.bat = Integer.parseInt(jsonObject.get("BAT").toString());
+        this.signal = Integer.parseInt(jsonObject.get("SIGNAL").toString());
+        this.vt = Float.parseFloat(jsonObject.get("VT").toString());
+        this.deviceCode = deviceCode;
     }
 }