wuyouming666
2023-12-26 e7eaa7bb402aae3bb88d9219e50f0e1179f3257f
springboot-vue3/src/main/java/com/example/springboot/component/Plchome.java
@@ -1,5 +1,6 @@
package com.example.springboot.component;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import java.io.IOException;
@@ -8,6 +9,7 @@
import java.util.Arrays;
import java.util.List;
import com.example.springboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.springboot.entity.Out_slice;
@@ -16,10 +18,7 @@
import com.example.springboot.entity.alarmmg;
import com.example.springboot.mapper.HomeMapper;
import com.example.springboot.mapper.SpianMapper;
import com.example.springboot.service.JdbcConnections;
import com.example.springboot.service.North_Glass_Buffer1Service;
import com.example.springboot.service.OutSliceServive;
import com.example.springboot.service.SpianService;
import com.example.springboot.service.StorageCageService;
public class Plchome extends Thread {
@@ -42,6 +41,8 @@
    private  StorageCageService storageCageService;
    public Plchome() throws IOException {
        config = new Configuration("config.properties");
@@ -61,6 +62,7 @@
            JSONObject jsonObject = new JSONObject();
            // 注入mapper
            homeMapper = WebSocketServer.applicationContext.getBean(HomeMapper.class);
            spianMapper = WebSocketServer.applicationContext.getBean(SpianMapper.class);
            spianService = WebSocketServer.applicationContext.getBean(SpianService.class);
@@ -178,7 +180,26 @@
                boolean value = anniuread.get(i);
                anniuparams[i] = value ? (short) 1 : (short) 0;
            }
            short[] resets2 = new short[resets.size()];
            for (int i = 0; i < resets.size(); i++) {
                boolean value = resets.get(i);
                resets2[i] = value ? (short) 1 : (short) 0;
            }
//            System.out.println(resets);
            for (short number : resets2) {
                if (number == 1) {
                    System.out.println(resets);
            jsonObject.append("Abort", anniuparams);
                }
            }
            // 铝框线交互
            // //铝框id请求字
@@ -202,7 +223,7 @@
            // }
            // S7control.getinstance().WriteWord("铝框翻转发送地址", (short)send);
            // }
            storageCageService = WebSocketServer.applicationContext.getBean(StorageCageService.class);
            dbserve = WebSocketServer.applicationContext.getBean(JdbcConnections.class);
            boolean dbconnected = false;
            try {
@@ -218,9 +239,79 @@
            if (sendwServer != null) {
                for (WebSocketServer webserver : sendwServer) {
                    webserver.sendMessage(jsonObject.toString());
                    if (webserver != null) {
                        List<String> messages = webserver.getMessages();
                        if (!messages.isEmpty()) {
                            // 将最后一个消息转换为整数类型的列表
                            String lastMessage = messages.get(messages.size() - 1);
//        System.out.println("lastMessage:" + lastMessage);
                            JSONArray messageArray = new JSONArray(lastMessage);
                            try {
                                // 检查索引是否有效
                                if (messageArray.getJSONArray(0).size() > 0) {
                                    // 获取消息数组
                                    JSONArray jsonArray = messageArray.getJSONArray(0);
                                    // 创建一个布尔值列表
                                    List<Boolean> sValue = new ArrayList<>();
                                    // 遍历消息数组
                                    for (int i = 0; i < jsonArray.size(); i++) {
                                        // 获取消息数组中的值
                                        Object value = jsonArray.get(i);
                                        // 检查值是否有效
                                        if (value != null && !value.toString().equals("null")) {
                                            try {
                                                // 移除非数字和数字字符
                                                String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                                                // 解析为布尔值
                                                boolean val = "1".equals(cleanedValue.trim());
                                                // 将布尔值添加到布尔值列表中
                                                sValue.add(val);
                                                System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                                            } catch (NumberFormatException e) {
                                                // 如果无法解析为 boolean 类型,则忽略该部分
                                                System.err.println("Could not parse value: " + value);
                }
                                        }
                                    }
// 终止进片任务
                                    boolean Value1 = sValue.get(0);
                                    boolean Value2 = sValue.get(1);
                                    if (Value1) {
                                        storageCageService.EndTask(0);
                                    }
                                    if (Value2) {
                                        storageCageService.EndTask(1);
                                    }
                                    // 检查布尔值列表是否为空
                                    if (!sValue.isEmpty()) {
                                        // 调用 S7control.getinstance().WriteBit 方法将布尔值列表写入地址列表
                                        S7control.getinstance().WriteBit(niuanaddressList3, sValue);
                                        System.out.println("Values " + sValue + " written to PLC at address " + niuanaddressList3);
                                    }
                                }
                            } catch (Exception e) {
                                System.err.println("An error occurred while writing bit to PLC: " + e.getMessage());
                            }
                            webserver.clearMessages();
            }
                    }
                }
            }
        }
    }
}