From 3879216b974b6a54232dd03017e0695186cea092 Mon Sep 17 00:00:00 2001
From: ZengTao <2773468879@qq.com>
Date: 星期三, 06 九月 2023 17:02:47 +0800
Subject: [PATCH] 更新从后台往前端推送数据

---
 springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java |   17 +++
 CanadaMes-ui/src/views/home/index.vue                                               |   89 +++++++++++++++--
 springboot-vue3/src/main/java/com/example/springboot/config/AppRunnerConfig.java    |    4 
 CanadaMes-ui/src/api/home.js                                                        |    8 +
 CanadaMes-ui/src/views/device/talkvue.vue                                           |   21 +--
 springboot-vue3/src/main/java/com/example/springboot/entity/StorageCage.java        |   48 ++++++---
 springboot-vue3/src/main/java/com/example/springboot/AuthorityApplication.java      |    9 +
 springboot-vue3/src/main/java/com/example/springboot/component/Plchome.java         |   70 ++++++++++++++
 springboot-vue3/src/main/java/com/example/springboot/entity/StorageTask.java        |    3 
 springboot-vue3/src/main/java/com/example/springboot/mapper/HomeMapper.java         |    8 +
 springboot-vue3/src/main/java/com/example/springboot/controller/HomeController.java |   11 ++
 11 files changed, 239 insertions(+), 49 deletions(-)

diff --git a/CanadaMes-ui/src/api/home.js b/CanadaMes-ui/src/api/home.js
index b63aef2..7d62fb2 100644
--- a/CanadaMes-ui/src/api/home.js
+++ b/CanadaMes-ui/src/api/home.js
@@ -42,3 +42,11 @@
         data :""
     })
 }
+
+export function UpdateTask(types,shelf_rack) {
+    return request({
+        url: '/home/UpdateTask?types=' + types + '&shelf_rack'+shelf_rack,
+        method: 'get',
+        data :""
+    })
+}
diff --git a/CanadaMes-ui/src/views/device/talkvue.vue b/CanadaMes-ui/src/views/device/talkvue.vue
index b1bf81f..a66b935 100644
--- a/CanadaMes-ui/src/views/device/talkvue.vue
+++ b/CanadaMes-ui/src/views/device/talkvue.vue
@@ -3,25 +3,22 @@
 
   <div>
     <button @click="send()">娴嬭瘯鍙戦��</button>
-  <button @click="ttt()">娴嬭瘯鍙戦��</button>
+    <button @click="ttt()">娴嬭瘯鍙戦��</button>
 
   </div>
-
-
-
 </template>
     
 <script>
 let socket;
 export default {
   name: "talkvue",
- 
+
   data() {
     return {
       messagepack: {
-        data: { taskname:"" }
+        data: { taskname: "" }
       },
-      qqqq:{},
+      qqqq: {},
       queryInfo: {
         data: "1",
         pageSize: 10
@@ -30,7 +27,7 @@
   },
   created() {
     this.init();
-    
+
   },
   methods:
   {
@@ -58,8 +55,8 @@
           console.log("鏀跺埌鏁版嵁====" + msg.data)
 
           // 瀵规敹鍒扮殑json鏁版嵁杩涜瑙f瀽锛� 绫讳技杩欐牱鐨勶細 {"users": [{"username": "zhang"},{ "username": "admin"}]}
-          this.qqqq= JSON.parse(msg.data);
-console.log(this.qqqq);
+          this.qqqq = JSON.parse(msg.data);
+          console.log(this.qqqq);
         };
         //鍏抽棴浜嬩欢
         socket.onclose = function () {
@@ -72,13 +69,13 @@
       }
     },
     send() {
-      this.messagepack.data =  {taskname:"鍓嶇鍒板悗鍙�"} ;
+      this.messagepack.data = { taskname: "鍓嶇鍒板悗鍙�" };
       socket?.send(JSON.stringify(this.messagepack));  // 灏嗙粍瑁呭ソ鐨刯son鍙戦�佺粰鏈嶅姟绔紝鐢辨湇鍔$杩涜杞彂
     },
     ttt() {
       console.log(this.qqqq)
     },
-    
+
   }
 }
 </script>
\ No newline at end of file
diff --git a/CanadaMes-ui/src/views/home/index.vue b/CanadaMes-ui/src/views/home/index.vue
index a6d1c92..f5ca385 100644
--- a/CanadaMes-ui/src/views/home/index.vue
+++ b/CanadaMes-ui/src/views/home/index.vue
@@ -91,7 +91,7 @@
     background-size: 32px 66px;
     background-attachment: local;
     width: 32px;
-    
+
     position: absolute;
     /* left: 116px; */
     /* left:1060px;18.88px */
@@ -102,10 +102,12 @@
 .el-table th {
     padding: 8px 0;
 }
-.el-table__empty-block{
+
+.el-table__empty-block {
     min-height: 41px;
 }
-.el-table__empty-text{
+
+.el-table__empty-text {
     line-height: 41px;
 }
 </style>
@@ -120,11 +122,11 @@
                 <el-button type="warning" @click="getOrder()">Exit the glass by order number</el-button>
             </div>
             <div>
-                <el-table :data="this.tasklist1" border style="width: 100%">
+                <el-table :data="this.tasklist1" border style="width: 100%" >
                     <el-table-column prop="glassId" label="Coming out glass ID"></el-table-column>
                     <el-table-column :width="250" prop="cage" label="The Grille number being used"></el-table-column>
                     <el-table-column prop="orderId" label="Order Nmuber"></el-table-column>
-                    <el-table-column prop="glasswidth" label="Length and width"></el-table-column>
+                    <el-table-column prop="lengthwidth" label="Length and width"></el-table-column>
                     <el-table-column prop="coating" label="coating"></el-table-column>
                     <el-table-column label="Operate">
                         <el-button type="primary" style="padding: 4px 10px;font-size: 12px;">end task</el-button>
@@ -134,7 +136,7 @@
                     <el-table-column prop="glassId" label="Incoming glass ID"></el-table-column>
                     <el-table-column :width="250" prop="cage" label="The Grille number being used"></el-table-column>
                     <el-table-column prop="orderId" label="Order Nmuber"></el-table-column>
-                    <el-table-column prop="glasswidth" label="Length and width"></el-table-column>
+                    <el-table-column prop="lengthwidth" label="Length and width"></el-table-column>
                     <el-table-column prop="coating" label="coating"></el-table-column>
                     <el-table-column label="Operate">
                         <el-button type="primary" style="padding: 4px 10px;font-size: 12px;">end task</el-button>
@@ -168,10 +170,10 @@
                 <div style="display:flex;position: absolute;float:left;z-index: 999;top:102px;left:682.5px;">
                     <div :class="getStatusClass(item.state)" v-for="item in cagelist4" :key="item['date']"></div>
                 </div>
-                <div class="blocks-img" :style="{ left: car1 + 'px' ,top: '10px'}">
+                <div class="blocks-img" :style="{ left: car1 + 'px', top: '10px' }">
 
                 </div>
-                <div class="blocks-img" :style="{ left: car2 + 'px' ,top: '129px'}">
+                <div class="blocks-img" :style="{ left: car2 + 'px', top: '129px' }">
 
                 </div>
             </div>
@@ -205,8 +207,9 @@
 
 import { home, home2, loadtask, InsertOrder, Addglassid } from "../../api/home";
 
-
+let socket;
 export default {
+    name: "Home",
     data() {
         return {
             dialogFormVisible: false,
@@ -234,8 +237,68 @@
     },
     created() {
         this.load();
+        this.init();
     },
     methods: {
+        init() {
+            let viewname = "Home";
+
+            if (typeof (WebSocket) == "undefined") {
+                console.log("鎮ㄧ殑娴忚鍣ㄤ笉鏀寔WebSocket");
+            } else {
+                //console.log("鎮ㄧ殑娴忚鍣ㄦ敮鎸乄ebSocket");
+
+                let socketUrl = "ws://" + "localhost:8888" + "/springboot-vue3/api/talk/" + viewname;
+                if (socket != null) {
+                    socket.close();
+                    socket = null;
+                }
+                // 寮�鍚竴涓獁ebsocket鏈嶅姟
+                socket = new WebSocket(socketUrl);
+                //鎵撳紑浜嬩欢
+                socket.onopen = function () {
+                    console.log("websocket宸叉墦寮�");
+                };
+                //  娴忚鍣ㄧ鏀舵秷鎭紝鑾峰緱浠庢湇鍔$鍙戦�佽繃鏉ョ殑鏂囨湰娑堟伅
+                socket.onmessage = function (msg) {
+
+                    //console.log("鏀跺埌鏁版嵁====" + msg.data);
+                    let obj = JSON.parse(msg.data);
+                    //this.$set(this.record.params, 0, obj.params[0]);
+
+                    this.car1 = 116 + 18.88 * obj.params[0][0];
+                    this.car2 = 187 + 18.8 * obj.params[0][1];
+                    this.tableData=obj.tableData[0];
+                    this.cagelist1=obj.cagelist1[0];
+                    this.cagelist2=obj.cagelist2[0];
+                    this.cagelist3=obj.cagelist3[0];
+                    this.cagelist4=obj.cagelist4[0];
+                    this.tasklist1=obj.tasklist1[0];
+                    this.tasklist2=obj.tasklist2[0];
+                
+                    // for (let a = 0; a <= this.record.params.length - 1; a++) {
+                    //     // this.record.xyData[a] = [this.record.canshu[a], this.record.params[0][a]];
+                    //     // this.record.xyData[a] = { name: this.record.canshu[a], value: this.record.params[0][a] };
+
+                    //     // this.record.xyData[a].value = this.record.params[0][a];
+                    //     this.car1=this.record.params[0][a];
+                    //     console.log(this.record.params[0][a]);
+
+
+                    // }
+                    this.$forceUpdate();
+
+                }.bind(this);
+                //鍏抽棴浜嬩欢
+                socket.onclose = function () {
+                    console.log("websocket宸插叧闂�");
+                };
+                //鍙戠敓浜嗛敊璇簨浠�
+                socket.onerror = function () {
+                    console.log("websocket鍙戠敓浜嗛敊璇�");
+                }
+            }
+        },
         load() {
             home().then(res => {
                 this.tableData = res.data.list;
@@ -255,8 +318,7 @@
             });
             loadtask(this.task2).then(res => {
                 this.tasklist2 = res.data.list;
-                this.car1 = 116 + 18.88 * 20;
-                this.car2 = 187 + 18.8 * 30;
+                
             });
         },
         //鏍规嵁鏍煎瓙鐘舵�佷慨鏀归鑹�
@@ -314,8 +376,9 @@
                 });
             }
         },
-        endtask(){
-            
+        endtask(type,shelfrack) {
+            alert(type);
+            alert(shelfrack);
         }
     }
 };
diff --git a/springboot-vue3/src/main/java/com/example/springboot/AuthorityApplication.java b/springboot-vue3/src/main/java/com/example/springboot/AuthorityApplication.java
index ac10b17..5d2cb9e 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/AuthorityApplication.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/AuthorityApplication.java
@@ -2,12 +2,19 @@
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ConfigurableApplicationContext;
+
+import com.example.springboot.component.WebSocketServer;
 
 @SpringBootApplication
 public class AuthorityApplication {
 
     public static void main(String[] args) {
-        SpringApplication.run(AuthorityApplication.class, args);
+        // SpringApplication.run(AuthorityApplication.class, args);
+
+        SpringApplication springApplication = new SpringApplication(AuthorityApplication.class);
+        ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
+        WebSocketServer.setApplicationContext(configurableApplicationContext);
     }
 
 }
diff --git a/springboot-vue3/src/main/java/com/example/springboot/component/Plchome.java b/springboot-vue3/src/main/java/com/example/springboot/component/Plchome.java
new file mode 100644
index 0000000..6450dc5
--- /dev/null
+++ b/springboot-vue3/src/main/java/com/example/springboot/component/Plchome.java
@@ -0,0 +1,70 @@
+package com.example.springboot.component;
+
+import cn.hutool.json.JSONObject;
+
+import java.util.List;
+
+import org.springframework.context.ConfigurableApplicationContext;
+
+import com.example.springboot.entity.StorageCage;
+import com.example.springboot.mapper.HomeMapper;
+
+public class Plchome extends Thread {
+
+    private HomeMapper homeMapper;
+
+    @Override
+    public void run() {
+        while (this != null) {
+            try {
+                Thread.sleep(10000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+
+            JSONObject jsonObject = new JSONObject();
+            
+            homeMapper = WebSocketServer.applicationContext.getBean(HomeMapper.class);
+            // 绗煎瓙浣跨敤鎯呭喌
+            List<StorageCage> tableData = homeMapper.selectAll();
+            jsonObject.append("tableData", tableData);
+            //鏍肩悊鐗囩鏍煎瓙鐘舵��
+            List<StorageCage> cagelist1 = homeMapper.selectRack1();
+            List<StorageCage> cagelist2 = homeMapper.selectRack2();
+            List<StorageCage> cagelist3 = homeMapper.selectRack3();
+            List<StorageCage> cagelist4 = homeMapper.selectRack4();
+            jsonObject.append("cagelist1", cagelist1);
+            jsonObject.append("cagelist2", cagelist2);
+            jsonObject.append("cagelist3", cagelist3);
+            jsonObject.append("cagelist4", cagelist4);
+            //杩涘嚭鐗囦换鍔�
+            List<StorageCage> tasklist1=homeMapper.selectinout(2);
+            List<StorageCage> tasklist2=homeMapper.selectinout(3);
+            jsonObject.append("tasklist1", tasklist1);
+            jsonObject.append("tasklist2", tasklist2);
+
+            //璇诲幓Plc杩涚墖杞︿笌鍑虹墖杞︿綅缃�
+            // List<String> addressList = new ArrayList<>();
+            // addressList.add("DB100.DBW0");
+            // addressList.add("DB100.DBW2");
+            // List<Short> paramlist = S7control.getinstance().ReadWord(addressList);
+            // jsonObject.append("params", paramlist);
+
+            jsonObject.append("params", new short[] { 30, 40, });
+            WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Home");
+
+            if (sendwServer != null) {
+                sendwServer.sendMessage(jsonObject.toString());
+            }
+
+            // JSONObject jsonObject = new JSONObject();
+            // jsonObject.append("params", new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
+            // 11, });
+            // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Home");
+            // if (sendwServer != null) {
+            // sendwServer.sendMessage(jsonObject.toString());
+            // }
+
+        }
+    }
+}
diff --git a/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java b/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
index 697d697..8749b5e 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
@@ -4,6 +4,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.catalina.core.ApplicationContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -16,8 +18,8 @@
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 
+import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.stereotype.Component;
-
 
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
@@ -25,6 +27,17 @@
 @ServerEndpoint(value = "/api/talk/{username}")
 @Component
 public class WebSocketServer {
+
+    // @Autowired
+    // HomeMapper homeMapper;
+
+    static ConfigurableApplicationContext applicationContext;
+ 
+    //瑙e喅鏃犳硶娉ㄥ叆mapper闂  //浣跨敤鏂规硶 homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class);
+    public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
+        WebSocketServer.applicationContext = configurableApplicationContext;
+    }
+
 
    private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
     private List<String> messages;
@@ -86,6 +99,7 @@
          jsonObject.set("message", text);
         this.messages.add(text);
          this.sendMessage(jsonObject.toString()); //JSONUtil.toJsonStr(jsonObject)
+         
     }
 
     @OnError
@@ -122,6 +136,7 @@
 
     public List<String> getMessages() {
         return messages;
+        
     }
 
     public void clearMessages() {
diff --git a/springboot-vue3/src/main/java/com/example/springboot/config/AppRunnerConfig.java b/springboot-vue3/src/main/java/com/example/springboot/config/AppRunnerConfig.java
index 3e48672..6b80ebd 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/config/AppRunnerConfig.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/config/AppRunnerConfig.java
@@ -7,6 +7,7 @@
 
 import com.example.springboot.component.PlcHold;
 import com.example.springboot.component.Plcaction;
+import com.example.springboot.component.Plchome;
 
 @Component
 @Order(1)
@@ -20,5 +21,8 @@
     new PlcHold().start();
 
     new Plcaction().start();
+
+    new Plchome().start();
+    
   }
 }
diff --git a/springboot-vue3/src/main/java/com/example/springboot/controller/HomeController.java b/springboot-vue3/src/main/java/com/example/springboot/controller/HomeController.java
index 73b3e33..f21eb5f 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/controller/HomeController.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/controller/HomeController.java
@@ -76,8 +76,17 @@
         }else{
             map.put("message2","200");
             //璋冪敤浼嶄笂鐗囧嚱鏁�
-
         }
         return Result.success(map);
     }
+
+    @GetMapping("/UpdateTask")
+    public Result UpdateTask(Integer types,Integer shelf_rack){
+        Map<String, Object> map = new HashMap<>();
+        homeMapper.UpdateTask(types,shelf_rack);
+        map.put("message2","200");
+        return Result.success(map);
+    }
+
+    
 }
diff --git a/springboot-vue3/src/main/java/com/example/springboot/entity/StorageCage.java b/springboot-vue3/src/main/java/com/example/springboot/entity/StorageCage.java
index 4a05577..1a394d7 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/entity/StorageCage.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/entity/StorageCage.java
@@ -9,15 +9,19 @@
     private Double glasswidth;
     private Double glassheight;
     private String state;
-    private Integer tier;  
+    private Integer tier;
     private String orderId;
+    private String coating;
+    private String lengthwidth;
 
     public Integer id() {
         return id;
     }
+
     public void setId(Integer id) {
         this.id = id;
     }
+
     public Integer getId() {
         return id;
     }
@@ -29,14 +33,15 @@
     public void setGlassId(String glassId) {
         this.glassId = glassId;
     }
-     public String getOrderId() {
+
+    public String getOrderId() {
         return orderId;
     }
 
     public void setOrderId(String orderId) {
         this.orderId = orderId;
     }
-    
+
     public Integer getCage() {
         return cage;
     }
@@ -53,16 +58,6 @@
         return cell;
     }
 
-    
-
-    // public String getHeighth() {
-    //     return height;
-    // }
-
-    // public void setHeight(String height) {
-    //     this.height = height;
-    // }
-
     public Double getWidth() {
         return width;
     }
@@ -71,7 +66,7 @@
         this.width = width;
     }
 
-     public String getState() {
+    public String getState() {
         return state;
     }
 
@@ -79,26 +74,43 @@
         this.state = state;
     }
 
-      public Integer getTier() {
+    public Integer getTier() {
         return tier;
     }
 
     public void setTier(Integer tier) {
         this.tier = tier;
     }
-     public Double getGlassWidth() {
+
+    public Double getGlassWidth() {
         return glasswidth;
     }
 
     public void setGlassWidth(Double glasswidth) {
         this.glasswidth = glasswidth;
     }
+
     public Double getGlassHeight() {
         return glassheight;
     }
-  
+
     public void setGlassHeight(Double glassheight) {
         this.glassheight = glassheight;
     }
-  
+
+    public String getCoating() {
+        return coating;
+    }
+
+    public void setCoating(String coating) {
+        this.coating = coating;
+    }
+
+    public String getLengthWidth() {
+        return lengthwidth;
+    }
+
+    public void setLengthWidth(String lengthwidth) {
+        this.lengthwidth = lengthwidth;
+    }
 }
diff --git a/springboot-vue3/src/main/java/com/example/springboot/entity/StorageTask.java b/springboot-vue3/src/main/java/com/example/springboot/entity/StorageTask.java
index ec9a836..1fbb577 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/entity/StorageTask.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/entity/StorageTask.java
@@ -6,6 +6,7 @@
     private String cataskStatege;
     private String shelfRack;
     private String loadrack;
+    
 
     public Integer id() {
         return id;
@@ -49,6 +50,6 @@
         this.loadrack = loadrack;
     }
 
-
+    
 
 }
\ No newline at end of file
diff --git a/springboot-vue3/src/main/java/com/example/springboot/mapper/HomeMapper.java b/springboot-vue3/src/main/java/com/example/springboot/mapper/HomeMapper.java
index 85d809a..c0ce979 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/mapper/HomeMapper.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/mapper/HomeMapper.java
@@ -28,7 +28,7 @@
     List<StorageCage> selectRack4();
 
     // 鏍规嵁浠诲姟绫诲瀷鏌ヨ褰撳墠姝e湪鍑虹墖锛岃繘鐗囩殑鐜荤拑淇℃伅
-    @Select("select * from storage_cage where state=#{task_type} limit 1")
+    @Select("select *,concat(glasswidth,' x ',glassheight) as lengthwidth from storage_cage where state=#{task_type} limit 1")
     List<StorageCage> selectinout(@Param("task_type") Integer task_type);
 
     // 鏌ヨ鏄惁瀛樺湪姝よ鍗�
@@ -36,7 +36,7 @@
     short SelectOrder(@Param("orderid") String orderid);
 
     // @Insert("insert into order_out(orderid) values('#{orderid}')")
-    @Insert("INSERT INTO `canadames`.`order_out`( `order_id`,`mod_time`) VALUES ( #{orderid},now())")
+    @Insert("INSERT INTO `canadames`.`order_out`( `orderid`,`mod_time`) VALUES ( #{orderid},now())")
     void InsertOrder(String orderid);
 
     //鍋滄鎸夊綋鍓嶈鍗曞嚭鐗�
@@ -46,4 +46,8 @@
     //鏌ヨ鏄惁瀛樺湪姝e湪涓婄墖鐨勪换鍔�
     @Select("select count(*) from storage_task where task_state=0 and task_type=1")
     short SelectOutTask();
+
+    //鎵嬪姩瀹屾垚浠诲姟
+    @Select("update storage_task set state=1 where shelf_rack=#{shelf_rack} and task_type=#{task_type} and task_state=0")
+    short SelectOutTask(@Param("task_type") Integer task_type,@Param("shelf_rack") Integer shelf_rack);
 }

--
Gitblit v1.8.0