Merge branch 'master' of http://10.153.19.25:10101/r/CanadaMes
# Conflicts:
# springboot-vue3/src/main/java/com/example/springboot/component/PlcHold.java
# springboot-vue3/src/main/java/com/example/springboot/config/AppRunnerConfig.java
| | |
| | | this.$refs.loginFormRef.validate(async valid => { |
| | | if (!valid) return; |
| | | login(this.loginForm).then(res => { |
| | | document.cookie="Authorizationssss="+this.loginForm.username+"; expires=Thu, 18 Dec 2203 12:00:00 GMT"; |
| | | |
| | | // 设置token |
| | | setToken(res.data.Authorization); |
| | | this.$router.push('/layout') |
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import org.apache.commons.io.FileUtils; |
| | | |
| | | import java.io.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | public class PLCAutomaticParameterSetting extends Thread { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public static String readFileToString(String filePath) throws IOException { |
| | | File file = new File(filePath); |
| | | return FileUtils.readFileToString(file, "UTF-8"); |
| | | } |
| | | |
| | | public void readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/address.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("address"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | |
| | | // System.out.println(stt); |
| | | List<Short> arraylist = S7control.getinstance().ReadWord("DB100.120", 8); |
| | | List<Short> dache1 = S7control.getinstance().ReadWord("DB100.18", 1); |
| | | List<Short> xiaoche1 = S7control.getinstance().ReadWord("DB100.22", 1); |
| | | List<Short> fanzhuan1 = S7control.getinstance().ReadWord("DB100.14", 1); |
| | | // Short[] values1 = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1)); |
| | | // Short[] fanzhuan = { 4 }; |
| | | // List<Short> fanzhuan1 = new ArrayList<>(Arrays.asList(fanzhuan)); |
| | | // Short[] dache = { 5 }; |
| | | // List<Short> dache1 = new ArrayList<>(Arrays.asList(dache)); |
| | | // Short[] xiaoche = { 6 }; |
| | | // List<Short> xiaoche1 = new ArrayList<>(Arrays.asList(xiaoche)); |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | // new short[]{1,1, 1, 1, 1, 1, 2, 33, 2,3, 4, 5} |
| | | // new short[]{0,0, 0, 0, 0, 0, 0, 0, 0, 0} |
| | | |
| | | jsonObject.append("params", arraylist); |
| | | jsonObject.append("dache1", dache1); |
| | | jsonObject.append("xiaoche", xiaoche1); |
| | | jsonObject.append("fanzhuan", fanzhuan1); |
| | | // |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("AutomaticParameterSetting"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("AutomaticParameterSetting"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("AutomaticParameterSetting"); |
| | | if (webserver != null) { |
| | | List<String> messages = webserver.getMessages(); |
| | | String addressList1 = "DB100.120"; |
| | | String addressList2 = "DB100.18"; |
| | | String addressList3 = "DB100.22"; |
| | | String addressList4 = "DB100.14"; |
| | | |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为 short 类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | // System.out.println("lastMessage:" + lastMessage); |
| | | JSONArray messageArray = new JSONArray(lastMessage); |
| | | |
| | | // 整合第 1 到 2 个数组并去掉 null 元素 |
| | | List<Short> mergedList = new ArrayList<>(); |
| | | for (int i = 0; i < 2; i++) { |
| | | JSONArray sublist = messageArray.getJSONArray(i); |
| | | for (int j = 0; j < sublist.size(); j++) { |
| | | Object value = sublist.get(j); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short sValue = Short.parseShort(cleanedValue.trim()); |
| | | mergedList.add(sValue); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | System.out.println(messageArray); |
| | | // 写入第一个地址 |
| | | if (messageArray.getJSONArray(2).size() > 0) { |
| | | Object value = messageArray.getJSONArray(2).get(0); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short sValue = Short.parseShort(cleanedValue.trim()); |
| | | S7control.getinstance().WriteWord(addressList2, Arrays.asList(sValue)); |
| | | System.out |
| | | .println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList2); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 写入第二个地址 |
| | | if (messageArray.getJSONArray(3).size() > 0) { |
| | | Object value = messageArray.getJSONArray(3).get(0); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short sValue = Short.parseShort(cleanedValue.trim()); |
| | | S7control.getinstance().WriteWord(addressList3, Arrays.asList(sValue)); |
| | | System.out |
| | | .println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList3); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 写入第三个地址 |
| | | if (messageArray.getJSONArray(4).size() > 0) { |
| | | Object value = messageArray.getJSONArray(4).get(0); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short sValue = Short.parseShort(cleanedValue.trim()); |
| | | S7control.getinstance().WriteWord(addressList4, Arrays.asList(sValue)); |
| | | System.out |
| | | .println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList4); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 写入第四个地址 |
| | | if (!mergedList.isEmpty()) { |
| | | S7control.getinstance().WriteWord(addressList1, mergedList); |
| | | System.out.println("messageValue:" + mergedList + " written to PLC at address " + addressList1); |
| | | } |
| | | |
| | | // 清空消息列表 |
| | | webserver.clearMessages(); |
| | | } |
| | | }} |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import cn.hutool.json.JSONArray;
|
| | | import cn.hutool.json.JSONObject;
|
| | | import org.apache.commons.io.FileUtils;
|
| | |
|
| | | import java.io.*;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.List;
|
| | |
|
| | | public class PLCManualJog extends Thread {
|
| | | String name = "";
|
| | | Integer count = 0;
|
| | |
|
| | | public static String readFileToString(String filePath) throws IOException {
|
| | | File file = new File(filePath);
|
| | | return FileUtils.readFileToString(file, "UTF-8");
|
| | | }
|
| | |
|
| | | public void readValue() {
|
| | | String str = "";
|
| | | BufferedReader bufferedReader = null;
|
| | | FileInputStream fileInputStream;
|
| | | try {
|
| | | // 从文件中读取字节数据存入 fileInputStream
|
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/address.json");
|
| | | // 读取 fileInputStream 中字节并将其解码为字符
|
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
|
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader
|
| | | bufferedReader = new BufferedReader(inputStreamReader);
|
| | | String line = null;
|
| | | // 将 bufferedReader 内容一行一行赋值给str
|
| | | while ((line = bufferedReader.readLine()) != null) {
|
| | | str += line;
|
| | | }
|
| | |
|
| | | // 将str字符串格式转为json
|
| | | JSONObject jsonObject = new JSONObject(str);
|
| | |
|
| | | // 获取json中的值
|
| | | JSONArray address = jsonObject.getJSONArray("address");
|
| | | for (int i = 0; i < address.size(); i++) {
|
| | | JSONObject ress = (JSONObject) address.get(i);
|
| | |
|
| | | this.name = ress.getStr("name");
|
| | | this.count = ress.getInt("count");
|
| | |
|
| | | }
|
| | |
|
| | | } catch (FileNotFoundException e) {
|
| | | e.printStackTrace();
|
| | | } catch (UnsupportedEncodingException e) {
|
| | | e.printStackTrace();
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void run() {
|
| | | while (this != null) {
|
| | | try {
|
| | | Thread.sleep(500);
|
| | |
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | // this.readValue();
|
| | | // String PlcAddress = this.name;
|
| | | // Integer Plccount = this.count;
|
| | |
|
| | | // System.out.println(stt);
|
| | |
|
| | | List<String> addressList0 = new ArrayList<>();
|
| | | addressList0.add("DB101.2.6");
|
| | | addressList0.add("DB101.2.7");
|
| | | addressList0.add("DB101.3.0");
|
| | | addressList0.add("DB101.3.1");
|
| | | addressList0.add("DB101.3.2");
|
| | | addressList0.add("DB101.3.3");
|
| | | addressList0.add("DB101.3.4");
|
| | | addressList0.add("DB101.3.5");
|
| | |
|
| | | List<String> addressList21 = new ArrayList<>();
|
| | | addressList21.add("DB101.0.0");
|
| | | addressList21.add("DB101.0.1");
|
| | | addressList21.add("DB101.0.2");
|
| | | addressList21.add("DB101.0.3");
|
| | | addressList21.add("DB101.0.4");
|
| | | addressList21.add("DB101.0.5");
|
| | | addressList21.add("DB101.0.6");
|
| | | addressList21.add("DB101.0.7");
|
| | | addressList21.add("DB101.1.0");
|
| | | addressList21.add("DB101.1.1");
|
| | | addressList21.add("DB101.1.2");
|
| | | addressList21.add("DB101.1.3");
|
| | | addressList21.add("DB101.1.4");
|
| | | addressList21.add("DB101.1.5");
|
| | | addressList21.add("DB101.1.6");
|
| | | addressList21.add("DB101.1.7");
|
| | | List<Boolean> arraylist = S7control.getinstance().readBits(addressList21);
|
| | | List<Boolean> arraylist2 = S7control.getinstance().readBits(addressList0);
|
| | | List<Boolean> arraylist3 = S7control.getinstance().ReadBits("DB101.5.2", 2);
|
| | | // Boolean[] values1 = { false, true, true, true, false, false, true, false,
|
| | | // false, true, true, true, false, false,
|
| | | // true, false };
|
| | | // List<Boolean> arraylist = new ArrayList<>(Arrays.asList(values1));
|
| | | // Boolean[] values2 = { false, true, true, true, false, false, true, false };
|
| | | // List<Boolean> arraylist2 = new ArrayList<>(Arrays.asList(values2));
|
| | | // Boolean[] values3 = { false, false };
|
| | | // List<Boolean> arraylist3 = new ArrayList<>(Arrays.asList(values3));
|
| | |
|
| | | arraylist.addAll(arraylist2);
|
| | | arraylist.addAll(arraylist3);
|
| | |
|
| | | JSONObject jsonObject = new JSONObject();
|
| | |
|
| | | short[] params = new short[arraylist.size()];
|
| | | for (int i = 0; i < arraylist.size(); i++) {
|
| | | boolean value = arraylist.get(i);
|
| | | params[i] = value ? (short) 1 : (short) 0;
|
| | | }
|
| | |
|
| | | jsonObject.append("params", params);
|
| | |
|
| | | // System.out.println("messageValue:" + arraylist +"messageValue2:" + arraylist2 );
|
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | | // if (sendwServer != null) {
|
| | | // sendwServer.sendMessage(jsonObject.toString());
|
| | | // }
|
| | |
|
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | | if (sendwServer != null) {
|
| | | for (WebSocketServer webserver : sendwServer) {
|
| | | webserver.sendMessage(jsonObject.toString());
|
| | |
|
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | |
|
| | | if (webserver != null) {
|
| | | // String addressList1 = "DB101.0.0";
|
| | | // String addressList2 = "DB101.2.6";
|
| | | String addressList3 = "DB101.5.2";
|
| | |
|
| | |
|
| | | List<String> addressList2 = new ArrayList<>();
|
| | | addressList2.add("DB101.2.6");
|
| | | addressList2.add("DB101.2.7");
|
| | | addressList2.add("DB101.3.0");
|
| | | addressList2.add("DB101.3.1");
|
| | | addressList2.add("DB101.3.2");
|
| | | addressList2.add("DB101.3.3");
|
| | | addressList2.add("DB101.3.4");
|
| | | addressList2.add("DB101.3.5");
|
| | |
|
| | |
|
| | | List<String> addressList211 = new ArrayList<>();
|
| | | addressList211.add("DB101.0.0");
|
| | | addressList211.add("DB101.0.1");
|
| | | addressList211.add("DB101.0.2");
|
| | | addressList211.add("DB101.0.3");
|
| | | addressList211.add("DB101.0.4");
|
| | | addressList211.add("DB101.0.5");
|
| | | addressList211.add("DB101.0.6");
|
| | | addressList211.add("DB101.0.7");
|
| | | addressList211.add("DB101.1.0");
|
| | | addressList211.add("DB101.1.1");
|
| | | addressList211.add("DB101.1.2");
|
| | | addressList211.add("DB101.1.3");
|
| | | addressList211.add("DB101.1.4");
|
| | | addressList211.add("DB101.1.5");
|
| | | addressList211.add("DB101.1.6");
|
| | | addressList211.add("DB101.1.7");
|
| | |
|
| | | List<String> addressList311 = new ArrayList<>();
|
| | | addressList311.add("DB101.5.2");
|
| | | addressList311.add("DB101.5.3");
|
| | |
|
| | | List<String> messages = webserver.getMessages();
|
| | | if (!messages.isEmpty()) {
|
| | | // 将最后一个消息转换为整数类型的列表
|
| | | String lastMessage = messages.get(messages.size() - 1);
|
| | | // System.out.println("messages:" + messages);
|
| | | String[] parts = lastMessage.split(",");
|
| | | List<Integer> messageValues = new ArrayList<>();
|
| | | for (String part : parts) {
|
| | | try {
|
| | | // 使用正则表达式清除非数字字符
|
| | | String cleanedPart = part.replaceAll("[^0-9-]", "");
|
| | | Integer value = Integer.parseInt(cleanedPart.trim());
|
| | | messageValues.add(value);
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为整数类型,则忽略该部分
|
| | | // e.printStackTrace();
|
| | | }
|
| | |
|
| | | }
|
| | | // 将消息值转换为布尔列表
|
| | | List<Boolean> messageBooleans = new ArrayList<>();
|
| | | for (Integer value : messageValues) {
|
| | | messageBooleans.add(value == 1 ? true : false);
|
| | | }
|
| | | // System.out.println(messageBooleans);
|
| | | List<Boolean> bolList = new ArrayList<>();
|
| | | List<Boolean> bolList2 = new ArrayList<>();
|
| | | List<Boolean> bolList3 = new ArrayList<>();
|
| | |
|
| | | for (int i = 0; i < 16 && i < messageBooleans.size(); i++) {
|
| | | bolList.add(messageBooleans.get(i));
|
| | | }
|
| | |
|
| | | for (int i = 16; i < 24 && i < messageBooleans.size(); i++) {
|
| | | bolList2.add(messageBooleans.get(i));
|
| | | }
|
| | |
|
| | | for (int i = 24; i < 26 && i < messageBooleans.size(); i++) {
|
| | | bolList3.add(messageBooleans.get(i));
|
| | | }
|
| | |
|
| | | // System.out.println(bolList);
|
| | | // System.out.println(bolList2);
|
| | | // System.out.println(bolList3);
|
| | | if (!bolList.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(addressList211, bolList);
|
| | | System.out.println("messageValue:" + bolList + " written to PLC at address " + addressList211);
|
| | | }
|
| | | if (!bolList2.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(addressList2, bolList2);
|
| | | System.out.println("messageValue:" + bolList2 + " written to PLC at address " + addressList2);
|
| | | }
|
| | | if (!bolList3.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(addressList311, bolList3);
|
| | | System.out.println("messageValue:" + bolList3 + " written to PLC at address " + addressList311);
|
| | | }
|
| | | webserver.clearMessages();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | }
|
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import org.apache.commons.io.FileUtils; |
| | | |
| | | import java.io.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | public class PlcManualonePosition extends Thread { |
| | | private Configuration config; |
| | | |
| | | public PlcManualonePosition() throws IOException { |
| | | config = new Configuration("config.properties"); |
| | | } |
| | | |
| | | public static class PLCBooleanConverter { |
| | | |
| | | public static short[] convertListsToBinaryShortArray(List<Boolean>... lists) { |
| | | // 计算所有列表中元素的总数 |
| | | int totalSize = 0; |
| | | for (List<Boolean> list : lists) { |
| | | totalSize += list.size(); |
| | | } |
| | | |
| | | // 创建一个足够大的数组来存放所有的二进制值 |
| | | short[] binaryShortArray = new short[totalSize]; |
| | | |
| | | // 当前数组填充位置指针 |
| | | int currentIndex = 0; |
| | | |
| | | // 遍历所有的列表 |
| | | for (List<Boolean> list : lists) { |
| | | // 将每个布尔值转换为 0 或 1 并填入数组 |
| | | for (Boolean value : list) { |
| | | binaryShortArray[currentIndex++] = (short) (value ? 1 : 0); |
| | | } |
| | | } |
| | | |
| | | return binaryShortArray; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(500); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | |
| | | List<String> addresses = new ArrayList<>(); |
| | | addresses.add("DB100.108"); |
| | | addresses.add("DB100.20"); |
| | | addresses.add("DB103.32"); |
| | | List<Short> arraylist = S7control.getinstance().readWords(addresses); |
| | | // System.out.println(arraylist); |
| | | |
| | | List<String> addresses2 = new ArrayList<>(); |
| | | addresses2.add("DB100.110"); |
| | | addresses2.add("DB100.176"); |
| | | addresses2.add("DB103.34"); |
| | | List<Short> arraylist2 = S7control.getinstance().readWords(addresses2); |
| | | |
| | | List<String> addresses3 = new ArrayList<>(); |
| | | addresses3.add("DB100.112"); |
| | | addresses3.add("DB100.24"); |
| | | addresses3.add("DB103.36"); |
| | | List<Short> arraylist3 = S7control.getinstance().readWords(addresses3); |
| | | |
| | | List<String> addresses4 = new ArrayList<>(); |
| | | addresses4.add("DB100.114"); |
| | | addresses4.add("DB100.178"); |
| | | addresses4.add("DB103.40"); |
| | | List<Short> arraylist4 = S7control.getinstance().readWords(addresses4); |
| | | |
| | | List<Short> arraylist5 = S7control.getinstance().ReadWord("DB106.0", 1); |
| | | List<Short> arraylist6 = S7control.getinstance().ReadWord("DB106.12", 1); |
| | | List<Short> arraylist7 = S7control.getinstance().ReadWord("DB106.40", 1); |
| | | List<Short> arraylist8 = S7control.getinstance().ReadWord("DB106.50", 1); |
| | | List<String> addresses9 = new ArrayList<>(); |
| | | addresses9.add("DB104.1.2"); |
| | | addresses9.add("DB104.1.3"); |
| | | addresses9.add("DB104.1.6"); |
| | | addresses9.add("DB104.1.7"); |
| | | List<Boolean> arraylist9 = S7control.getinstance().readBits(addresses9); |
| | | |
| | | List<String> addresses10 = new ArrayList<>(); |
| | | addresses10.add("DB104.9.4"); |
| | | addresses10.add("DB104.9.5"); |
| | | addresses10.add("DB104.9.4"); |
| | | addresses10.add("DB104.9.5"); |
| | | List<Boolean> arraylist10 = S7control.getinstance().readBits(addresses10); |
| | | |
| | | //A01启动地址 |
| | | List<String> A01start = Arrays.asList(config.getProperty("mlp.A01start")); |
| | | //A02启动地址 |
| | | List<String> A02start = Arrays.asList(config.getProperty("mlp.A02start")); |
| | | |
| | | List<String> B01start = Arrays.asList(config.getProperty("mlp.B01start")); |
| | | |
| | | List<String> B02start = Arrays.asList(config.getProperty("mlp.B02start")); |
| | | |
| | | List<Boolean> A01readstart = S7control.getinstance().readBits(A01start); |
| | | List<Boolean> A02readstart = S7control.getinstance().readBits(A02start); |
| | | List<Boolean> B01readstart = S7control.getinstance().readBits(B01start); |
| | | List<Boolean> B02readstart = S7control.getinstance().readBits(B02start); |
| | | |
| | | short[] combinedBinaryShortArray = PLCBooleanConverter.convertListsToBinaryShortArray( |
| | | A01readstart, A02readstart,B01readstart, B02readstart |
| | | ); |
| | | |
| | | // Short[] values1 = { 1, 2, 3 }; |
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1)); |
| | | // Short[] values2 = { 1, 2, 3 }; |
| | | // List<Short> arraylist2 = new ArrayList<>(Arrays.asList(values2)); |
| | | // Short[] values3 = { 1, 2, 3 }; |
| | | // List<Short> arraylist3 = new ArrayList<>(Arrays.asList(values3)); |
| | | // Short[] values4 = { 1, 2, 3 }; |
| | | // List<Short> arraylist4 = new ArrayList<>(Arrays.asList(values4)); |
| | | // Short[] values5 = { 5 }; |
| | | // List<Short> arraylist5 = new ArrayList<>(Arrays.asList(values5)); |
| | | // Short[] values6 = { 6 }; |
| | | // List<Short> arraylist6 = new ArrayList<>(Arrays.asList(values6)); |
| | | // Short[] values7 = { 7 }; |
| | | // List<Short> arraylist7 = new ArrayList<>(Arrays.asList(values7)); |
| | | // Short[] values8 = { 8 }; |
| | | // List<Short> arraylist8 = new ArrayList<>(Arrays.asList(values8)); |
| | | // Boolean[] values9 = { false, false, true, true }; |
| | | // List<Boolean> arraylist9 = new ArrayList<>(Arrays.asList(values9)); |
| | | short[] params2 = new short[arraylist9.size()]; |
| | | for (int i = 0; i < arraylist9.size(); i++) { |
| | | boolean value = arraylist9.get(i); |
| | | params2[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | |
| | | // Boolean[] values10 = { false, false, true, true }; |
| | | // List<Boolean> arraylist10 = new ArrayList<>(Arrays.asList(values10)); |
| | | short[] params3 = new short[arraylist10.size()]; |
| | | for (int i = 0; i < arraylist10.size(); i++) { |
| | | boolean value = arraylist10.get(i); |
| | | params3[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | // List<Short> arraylist9 = S7control.getinstance().readWords(addresses9); |
| | | arraylist5.addAll(arraylist6); |
| | | arraylist5.addAll(arraylist7); |
| | | arraylist5.addAll(arraylist8); |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | // System.out.println(arraylist6); |
| | | jsonObject.append("zuhe1", arraylist); |
| | | jsonObject.append("zuhe2", arraylist2); |
| | | jsonObject.append("zuhe3", arraylist3); |
| | | jsonObject.append("zuhe4", arraylist4); |
| | | jsonObject.append("shishi1", arraylist5); |
| | | jsonObject.append("guzhang", params2); |
| | | jsonObject.append("weihuiling", params3); |
| | | jsonObject.append("qidong", combinedBinaryShortArray); |
| | | |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualonePosition"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("ManualonePosition"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("ManualonePosition"); |
| | | if (webserver != null) { |
| | | List<String> messages = webserver.getMessages(); |
| | | List<String> waddresses1 = new ArrayList<>(); |
| | | waddresses1.add("DB100.108"); |
| | | waddresses1.add("DB100.20"); |
| | | waddresses1.add("DB103.32"); |
| | | |
| | | List<String> waddresses2 = new ArrayList<>(); |
| | | waddresses2.add("DB100.110"); |
| | | waddresses2.add("DB100.176"); |
| | | waddresses2.add("DB103.34"); |
| | | |
| | | List<String> waddresses3 = new ArrayList<>(); |
| | | waddresses3.add("DB100.112"); |
| | | waddresses3.add("DB100.24"); |
| | | waddresses3.add("DB103.36"); |
| | | |
| | | List<String> waddresses4 = new ArrayList<>(); |
| | | waddresses4.add("DB100.114"); |
| | | waddresses4.add("DB100.178"); |
| | | waddresses4.add("DB103.40"); |
| | | |
| | | // 故障地址 |
| | | List<String> waddresses5 = new ArrayList<>(); |
| | | // waddresses5.add("DB103.244"); |
| | | // waddresses5.add("DB103.250"); |
| | | waddresses5.add("DB103.252"); |
| | | waddresses5.add("DB103.254"); |
| | | |
| | | // 复位地址 |
| | | List<String> waddresses6 = new ArrayList<>(); |
| | | waddresses6.add("DB101.4.0"); |
| | | waddresses6.add("DB101.4.3"); |
| | | waddresses6.add("DB101.4.4"); |
| | | waddresses6.add("DB101.4.6"); |
| | | |
| | | // 回零地址 |
| | | List<String> waddresses7 = new ArrayList<>(); |
| | | |
| | | waddresses7.add("DB101.4.5"); |
| | | waddresses7.add("DB101.4.7"); |
| | | |
| | | |
| | | // System.out.println(messages.isEmpty()); |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为 short 类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | // System.out.println("lastMessage:" + lastMessage); |
| | | JSONArray messageArray = new JSONArray(lastMessage); |
| | | // 写入A01 |
| | | if (messageArray.getJSONArray(0).size() > 0) { |
| | | JSONArray jsonArray = messageArray.getJSONArray(0); |
| | | List<Short> 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-]", ""); |
| | | short val = Short.parseShort(cleanedValue.trim()); |
| | | sValue.add(val); |
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list"); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses1, sValue); |
| | | if (A01readstart != null && !A01readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> a01startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : A01readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | a01startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(A01start, a01startval); |
| | | System.out.println("a01startval " + a01startval + " written to PLC at address " + A01start); |
| | | } |
| | | |
| | | |
| | | System.out.println("Values " + sValue + " written to PLC at address " + waddresses1); |
| | | } |
| | | } |
| | | |
| | | // 写入A02 |
| | | |
| | | if (messageArray.getJSONArray(1).size() > 0) { |
| | | JSONArray jsonArray = messageArray.getJSONArray(1); |
| | | List<Short> sValue2 = 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-]", ""); |
| | | short val = Short.parseShort(cleanedValue.trim()); |
| | | sValue2.add(val); |
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list"); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses2, sValue2); |
| | | if (A02readstart != null && !A02readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> a02startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : A02readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | a02startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(A02start, a02startval); |
| | | } |
| | | |
| | | |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses2); |
| | | } |
| | | } |
| | | |
| | | // 写入B01 |
| | | if (messageArray.getJSONArray(2).size() > 0) { |
| | | JSONArray jsonArray = messageArray.getJSONArray(2); |
| | | List<Short> sValue2 = 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-]", ""); |
| | | short val = Short.parseShort(cleanedValue.trim()); |
| | | sValue2.add(val); |
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list"); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses3, sValue2); |
| | | if (B01readstart != null && !B01readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> b01startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : B01readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | b01startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(B01start, b01startval); |
| | | } |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses3); |
| | | } |
| | | } |
| | | |
| | | // 写入B02 |
| | | if (messageArray.getJSONArray(3).size() > 0) { |
| | | JSONArray jsonArray = messageArray.getJSONArray(3); |
| | | List<Short> sValue2 = 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-]", ""); |
| | | short val = Short.parseShort(cleanedValue.trim()); |
| | | sValue2.add(val); |
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list"); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses4, sValue2); |
| | | if (B02readstart != null && !B02readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> b02startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : B02readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | b02startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(B02start, b02startval); |
| | | System.out.println("Values " + b02startval + " written to PLC at address " + B02start); |
| | | } |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses4); |
| | | } |
| | | } |
| | | |
| | | // 写入故障地址 |
| | | if (messageArray.getJSONArray(4).size() > 0) { |
| | | JSONArray jsonArray = messageArray.getJSONArray(4); |
| | | List<Short> sValue2 = 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-]", ""); |
| | | short val = Short.parseShort(cleanedValue.trim()); |
| | | sValue2.add(val); |
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list"); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses5, sValue2); |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses5); |
| | | } |
| | | } |
| | | |
| | | // 写入复位地址 |
| | | if (messageArray.getJSONArray(5).size() > 0) { |
| | | JSONArray jsonArray3 = messageArray.getJSONArray(5); |
| | | List<Boolean> sValue3 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray3.size(); i++) { |
| | | Object value = jsonArray3.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValue3.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue3.isEmpty()) { |
| | | S7control.getinstance().WriteBit(waddresses6, sValue3); |
| | | System.out.println("Values " + sValue3 + " written to PLC at address " + waddresses6); |
| | | } |
| | | } |
| | | |
| | | // 写入回零地址 |
| | | if (messageArray.getJSONArray(6).size() > 0) { |
| | | JSONArray jsonArray3 = messageArray.getJSONArray(6); |
| | | List<Boolean> sValue3 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray3.size(); i++) { |
| | | Object value = jsonArray3.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValue3.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue3.isEmpty()) { |
| | | S7control.getinstance().WriteBit(waddresses7, sValue3); |
| | | System.out.println("Values " + sValue3 + " written to PLC at address " + waddresses7); |
| | | } |
| | | } |
| | | |
| | | // 清空消息列表 |
| | | webserver.clearMessages(); |
| | | } |
| | | }} |
| | | } |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import cn.hutool.json.JSONArray;
|
| | | import cn.hutool.json.JSONObject;
|
| | | import org.apache.commons.io.FileUtils;
|
| | |
|
| | | import java.io.*;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.List;
|
| | |
|
| | | public class PlcManualonePosition2 extends Thread {
|
| | | private Configuration config;
|
| | |
|
| | | public PlcManualonePosition2() throws IOException {
|
| | | config = new Configuration("config.properties");
|
| | | }
|
| | |
|
| | | public static class PLCBooleanConverter {
|
| | |
|
| | | public static short[] convertListsToBinaryShortArray(List<Boolean>... lists) {
|
| | | // 计算所有列表中元素的总数
|
| | | int totalSize = 0;
|
| | | for (List<Boolean> list : lists) {
|
| | | totalSize += list.size();
|
| | | }
|
| | |
|
| | | // 创建一个足够大的数组来存放所有的二进制值
|
| | | short[] binaryShortArray = new short[totalSize];
|
| | |
|
| | | // 当前数组填充位置指针
|
| | | int currentIndex = 0;
|
| | |
|
| | | // 遍历所有的列表
|
| | | for (List<Boolean> list : lists) {
|
| | | // 将每个布尔值转换为 0 或 1 并填入数组
|
| | | for (Boolean value : list) {
|
| | | binaryShortArray[currentIndex++] = (short) (value ? 1 : 0);
|
| | | }
|
| | | }
|
| | |
|
| | | return binaryShortArray;
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | |
|
| | | @Override
|
| | | public void run() {
|
| | | while (this != null) {
|
| | | try {
|
| | | Thread.sleep(500);
|
| | |
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | // this.readValue();
|
| | | // String PlcAddress = this.name;
|
| | | // Integer Plccount = this.count;
|
| | |
|
| | | List<String> addresses = new ArrayList<>();
|
| | | addresses.add("DB100.116");
|
| | | addresses.add("DB100.16");
|
| | |
|
| | | addresses.add("DB103.20");
|
| | | List<Short> arraylist = S7control.getinstance().readWords(addresses);
|
| | | // System.out.println(arraylist);
|
| | |
|
| | | List<String> addresses2 = new ArrayList<>();
|
| | | addresses2.add("DB100.118");
|
| | | addresses2.add("DB100.192");
|
| | |
|
| | | addresses2.add("DB103.22");
|
| | | List<Short> arraylist2 = S7control.getinstance().readWords(addresses2);
|
| | |
|
| | | List<String> addresses9 = new ArrayList<>();
|
| | |
|
| | | addresses9.add("DB104.1.2");
|
| | | addresses9.add("DB104.1.3");
|
| | | List<Boolean> arraylist9 = S7control.getinstance().readBits(addresses9);
|
| | | short[] params2 = new short[arraylist9.size()];
|
| | | for (int i = 0; i < arraylist9.size(); i++) {
|
| | | boolean value = arraylist9.get(i);
|
| | | params2[i] = value ? (short) 1 : (short) 0;
|
| | | }
|
| | |
|
| | | // Short[] values1 = { 1, 2, 3 };
|
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1));
|
| | | // Short[] values2 = { 1, 2, 3 };
|
| | | // List<Short> arraylist2 = new ArrayList<>(Arrays.asList(values2));
|
| | | // Boolean[] values9 = { false, true };
|
| | | // List<Boolean> arraylist9 = new ArrayList<>(Arrays.asList(values9));
|
| | | // short[] params2 = new short[arraylist9.size()];
|
| | | // for (int i = 0; i < arraylist9.size(); i++) {
|
| | | // boolean value = arraylist9.get(i);
|
| | | // params2[i] = value ? (short) 1 : (short) 0;
|
| | | // }
|
| | |
|
| | | // Boolean[] values10 = { false, true };
|
| | | List<String> addresses10 = new ArrayList<>();
|
| | |
|
| | | addresses10.add("DB104.9.0");
|
| | | addresses10.add("DB104.9.1");
|
| | | List<Boolean> arraylist10 = S7control.getinstance().readBits(addresses10);
|
| | | short[] params3 = new short[arraylist10.size()];
|
| | | for (int i = 0; i < arraylist10.size(); i++) {
|
| | | boolean value = arraylist10.get(i);
|
| | | params3[i] = value ? (short) 1 : (short) 0;
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | //A01启动地址
|
| | | List<String> A01start = Arrays.asList(config.getProperty("mlp2.A01start"));
|
| | | //A02启动地址
|
| | | List<String> A02start = Arrays.asList(config.getProperty("mlp2.A02start"));
|
| | |
|
| | |
|
| | |
|
| | | List<Boolean> A01readstart = S7control.getinstance().readBits(A01start);
|
| | | List<Boolean> A02readstart = S7control.getinstance().readBits(A02start);
|
| | |
|
| | |
|
| | | short[] combinedBinaryShortArray = PLCBooleanConverter.convertListsToBinaryShortArray(
|
| | | A01readstart, A02readstart
|
| | | );
|
| | |
|
| | | JSONObject jsonObject = new JSONObject();
|
| | |
|
| | | jsonObject.append("zuhe1", arraylist);
|
| | | jsonObject.append("zuhe2", arraylist2);
|
| | | jsonObject.append("guzhang", params2);
|
| | | jsonObject.append("weihuiling", params3);
|
| | | jsonObject.append("qidong", combinedBinaryShortArray);
|
| | | // System.out.println(jsonObject);
|
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualonePosition2");
|
| | | // if (sendwServer != null) {
|
| | | // sendwServer.sendMessage(jsonObject.toString());
|
| | | // }
|
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("ManualonePosition2");
|
| | | if (sendwServer != null) {
|
| | | for (WebSocketServer webserver : sendwServer) {
|
| | | webserver.sendMessage(jsonObject.toString());
|
| | |
|
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("ManualonePosition2");
|
| | | if (webserver != null) {
|
| | | List<String> messages = webserver.getMessages();
|
| | | List<String> waddresses1 = new ArrayList<>();
|
| | | waddresses1.add("DB100.116");
|
| | | waddresses1.add("DB100.16");
|
| | |
|
| | |
|
| | | List<String> waddresses2 = new ArrayList<>();
|
| | | waddresses2.add("DB100.118");
|
| | | waddresses2.add("DB100.192");
|
| | |
|
| | |
|
| | | // 故障地址
|
| | | List<String> waddresses3 = new ArrayList<>();
|
| | | waddresses3.add("DB103.242");
|
| | | waddresses3.add("DB103.248");
|
| | |
|
| | | // 复位地址
|
| | | List<String> waddresses4 = new ArrayList<>();
|
| | | waddresses4.add("DB101.3.6");
|
| | | waddresses4.add("DB101.4.1");
|
| | |
|
| | | // 回零地址
|
| | | List<String> waddresses5 = new ArrayList<>();
|
| | | waddresses5.add("DB101.3.7");
|
| | | waddresses5.add("DB101.4.2");
|
| | |
|
| | | if (!messages.isEmpty()) {
|
| | | // 将最后一个消息转换为 short 类型的列表
|
| | | String lastMessage = messages.get(messages.size() - 1);
|
| | | // System.out.println("lastMessage:" + lastMessage);
|
| | | JSONArray messageArray = new JSONArray(lastMessage);
|
| | |
|
| | | // A01下发
|
| | | if (messageArray.getJSONArray(0).size() > 0) {
|
| | | JSONArray jsonArray = messageArray.getJSONArray(0);
|
| | | List<Short> 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-]", "");
|
| | | short val = Short.parseShort(cleanedValue.trim());
|
| | | sValue.add(val);
|
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list");
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为 short 类型,则忽略该部分
|
| | | System.err.println("Could not parse value: " + value);
|
| | | }
|
| | | }
|
| | | }
|
| | | if (!sValue.isEmpty()) {
|
| | | S7control.getinstance().WriteWord(waddresses1, sValue);
|
| | | if (A01readstart != null && !A01readstart.isEmpty()) {
|
| | | // 创建一个新的列表用于写入新值
|
| | | List<Boolean> a01startval = new ArrayList<>();
|
| | | // 遍历所有读取到的位值
|
| | | for (Boolean bit : A01readstart) {
|
| | | // 取反当前位的值并添加到新的列表中
|
| | | a01startval.add(!bit);
|
| | | }
|
| | | // 写入新的位值列表
|
| | | S7control.getinstance().WriteBit(A01start, a01startval);
|
| | | }
|
| | | System.out.println("Values " + sValue + " written to PLC at address " + waddresses1);
|
| | | }
|
| | | }
|
| | |
|
| | | // A02下发
|
| | | if (messageArray.getJSONArray(1).size() > 0) {
|
| | | JSONArray jsonArray = messageArray.getJSONArray(1);
|
| | | List<Short> sValue2 = 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-]", "");
|
| | | short val = Short.parseShort(cleanedValue.trim());
|
| | | sValue2.add(val);
|
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list");
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为 short 类型,则忽略该部分
|
| | | System.err.println("Could not parse value: " + value);
|
| | | }
|
| | | }
|
| | | }
|
| | | if (!sValue2.isEmpty()) {
|
| | | S7control.getinstance().WriteWord(waddresses2, sValue2);
|
| | | if (A02readstart != null && !A02readstart.isEmpty()) {
|
| | | // 创建一个新的列表用于写入新值
|
| | | List<Boolean> a02startval = new ArrayList<>();
|
| | | // 遍历所有读取到的位值
|
| | | for (Boolean bit : A02readstart) {
|
| | | // 取反当前位的值并添加到新的列表中
|
| | | a02startval.add(!bit);
|
| | | }
|
| | | // 写入新的位值列表
|
| | | S7control.getinstance().WriteBit(A02start, a02startval);
|
| | | }
|
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses2);
|
| | | }
|
| | | }
|
| | |
|
| | | // 写入故障地址
|
| | | if (messageArray.getJSONArray(2).size() > 0) {
|
| | | JSONArray jsonArray = messageArray.getJSONArray(2);
|
| | | List<Short> sValue2 = 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-]", "");
|
| | | short val = Short.parseShort(cleanedValue.trim());
|
| | | sValue2.add(val);
|
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list");
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为 short 类型,则忽略该部分
|
| | | System.err.println("Could not parse value: " + value);
|
| | | }
|
| | | }
|
| | | }
|
| | | if (!sValue2.isEmpty()) {
|
| | | S7control.getinstance().WriteWord(waddresses3, sValue2);
|
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses3);
|
| | | }
|
| | | }
|
| | |
|
| | | // 写入复位地址
|
| | | if (messageArray.getJSONArray(3).size() > 0) {
|
| | | JSONArray jsonArray3 = messageArray.getJSONArray(3);
|
| | | List<Boolean> sValue3 = new ArrayList<>();
|
| | | for (int i = 0; i < jsonArray3.size(); i++) {
|
| | | Object value = jsonArray3.get(i);
|
| | | if (value != null && !value.toString().equals("null")) {
|
| | | try {
|
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
|
| | | boolean val = "1".equals(cleanedValue.trim());
|
| | | sValue3.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);
|
| | | }
|
| | | }
|
| | | }
|
| | | if (!sValue3.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(waddresses4, sValue3);
|
| | | System.out.println("Values " + sValue3 + " written to PLC at address " + waddresses4);
|
| | | }
|
| | | }
|
| | |
|
| | | // 写入回零地址
|
| | | if (messageArray.getJSONArray(4).size() > 0) {
|
| | | JSONArray jsonArray3 = messageArray.getJSONArray(4);
|
| | | List<Boolean> sValue3 = new ArrayList<>();
|
| | | for (int i = 0; i < jsonArray3.size(); i++) {
|
| | | Object value = jsonArray3.get(i);
|
| | | if (value != null && !value.toString().equals("null")) {
|
| | | try {
|
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
|
| | | boolean val = "1".equals(cleanedValue.trim());
|
| | | sValue3.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);
|
| | | }
|
| | | }
|
| | | }
|
| | | if (!sValue3.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(waddresses5, sValue3);
|
| | | System.out.println("Values " + sValue3 + " written to PLC at address " + waddresses5);
|
| | | }
|
| | | }
|
| | |
|
| | | // 清空消息列表
|
| | | webserver.clearMessages();
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Arrays; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.UnsupportedEncodingException; |
| | | |
| | | import org.apache.commons.io.FileUtils; |
| | | |
| | | public class PlcParameter extends Thread { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public static String readFileToString(String filePath) throws IOException { |
| | | File file = new File(filePath); |
| | | return FileUtils.readFileToString(file, "UTF-8"); |
| | | } |
| | | |
| | | public void readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/Parameter.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("address"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(500); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | |
| | | // System.out.println(stt); |
| | | List<Short> arraylist = S7control.getinstance().ReadWord("DB100.0", 60); |
| | | List<Short> state = S7control.getinstance().ReadWord("DB103.0", 10); |
| | | // Short[] values1 = { 1231, 1, 1, 1, 1, 1, 2, 33, 2, 3, 4, 5 ,1231, 1, 1, 1, 1, 1, 2, 33, 2, 3, 4, 5 }; |
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1)); |
| | | |
| | | // Short[] values2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| | | // List<Short> state = new ArrayList<>(Arrays.asList(values2)); |
| | | |
| | | // Boolean[] values = { false, true, true, true, true, true, true, true, true, |
| | | // true, true, true, true, true, true, true, true, true, true, true, true, true, |
| | | // true, |
| | | // true, false, true }; |
| | | // List<Boolean> paramlist = new ArrayList<>(Arrays.asList(values)); |
| | | |
| | | // short[] sholist = new short[paramlist.size()]; |
| | | |
| | | // for (int i = 0; i < paramlist.size(); i++) { |
| | | // boolean value = paramlist.get(i); |
| | | // sholist[i] = value ? (short) 1 : (short) 0; |
| | | // } |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | // new short[]{1,1, 1, 1, 1, 1, 2, 33, 2,3, 4, 5} |
| | | // new short[]{0,0, 0, 0, 0, 0, 0, 0, 0, 0} |
| | | |
| | | jsonObject.append("params", arraylist); |
| | | jsonObject.append("state", state); |
| | | // jsonObject.append("action", sholist); |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (webserver != null) { |
| | | List<String> messages = webserver.getMessages(); |
| | | |
| | | String addressList = "DB100.0"; |
| | | |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为 short 类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | System.out.println("messages:" + messages); |
| | | String[] parts = lastMessage.split(","); |
| | | List<Short> messageValues = new ArrayList<>(); |
| | | for (String part : parts) { |
| | | try { |
| | | // 使用正则表达式清除非数字字符 |
| | | String cleanedPart = part.replaceAll("[^0-9-]", ""); |
| | | short value = Short.parseShort(cleanedPart.trim()); |
| | | messageValues.add(value); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 将消息值写入 PLC |
| | | S7control.getinstance().WriteWord(addressList, messageValues); |
| | | System.out.println("messageValues:" + messageValues); |
| | | System.out.println("addressList:" + addressList); |
| | | // 清空消息列表 |
| | | webserver.clearMessages(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import org.apache.commons.io.FileUtils; |
| | | |
| | | import java.io.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | public class PlcParameter1 extends Thread { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public static String readFileToString(String filePath) throws IOException { |
| | | File file = new File(filePath); |
| | | return FileUtils.readFileToString(file, "UTF-8"); |
| | | } |
| | | |
| | | public void readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/address.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("address"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | |
| | | // System.out.println(stt); |
| | | List<String> addresses = new ArrayList<>(); |
| | | addresses.add("DB100.0"); |
| | | addresses.add("DB100.2"); |
| | | addresses.add("DB100.4"); |
| | | addresses.add("DB100.6"); |
| | | addresses.add("DB100.26"); |
| | | addresses.add("DB100.28"); |
| | | addresses.add("DB100.30"); |
| | | addresses.add("DB100.32"); |
| | | addresses.add("DB100.34"); |
| | | addresses.add("DB100.36"); |
| | | addresses.add("DB100.38"); |
| | | addresses.add("DB100.40"); |
| | | List<Short> arraylist = S7control.getinstance().readWords(addresses); |
| | | //翻转 |
| | | List<Short> fanzhuan1 = S7control.getinstance().ReadWord("DB100.8", 1); |
| | | List<Short> xiaoche1 = S7control.getinstance().ReadWord("DB100.12", 1); |
| | | // Short[] values1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; |
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1)); |
| | | // Short[] fanzhuan = { 4 }; |
| | | // List<Short> fanzhuan1 = new ArrayList<>(Arrays.asList(fanzhuan)); |
| | | // Short[] xiaoche = {5}; |
| | | // List<Short> xiaoche1 = new ArrayList<>(Arrays.asList(xiaoche)); |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | // new short[]{1,1, 1, 1, 1, 1, 2, 33, 2,3, 4, 5} |
| | | // new short[]{0,0, 0, 0, 0, 0, 0, 0, 0, 0} |
| | | |
| | | jsonObject.append("params", arraylist); |
| | | jsonObject.append("fanzhuan", fanzhuan1); |
| | | jsonObject.append("xiaoche", xiaoche1); |
| | | |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter1"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Parameter1"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | |
| | | if (webserver != null) { |
| | | List<String> messages = webserver.getMessages(); |
| | | List<String> waddresses1 = new ArrayList<>(); |
| | | waddresses1.add("DB100.0"); |
| | | waddresses1.add("DB100.2"); |
| | | waddresses1.add("DB100.4"); |
| | | waddresses1.add("DB100.6"); |
| | | waddresses1.add("DB100.26"); |
| | | waddresses1.add("DB100.28"); |
| | | waddresses1.add("DB100.30"); |
| | | waddresses1.add("DB100.32"); |
| | | waddresses1.add("DB100.34"); |
| | | waddresses1.add("DB100.36"); |
| | | waddresses1.add("DB100.38"); |
| | | waddresses1.add("DB100.40"); |
| | | |
| | | String addressList2 = "DB100.8"; |
| | | String addressList3 = "DB100.12"; |
| | | |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为 short 类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | // System.out.println("lastMessage:" + lastMessage); |
| | | JSONArray messageArray = new JSONArray(lastMessage); |
| | | |
| | | // 整合第 1 到 3 个数组并去掉 null 元素 |
| | | List<Short> mergedList = new ArrayList<>(); |
| | | for (int i = 0; i < 3; i++) { |
| | | JSONArray sublist = messageArray.getJSONArray(i); |
| | | for (int j = 0; j < sublist.size(); j++) { |
| | | Object value = sublist.get(j); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short sValue = Short.parseShort(cleanedValue.trim()); |
| | | mergedList.add(sValue); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 写入第一个地址 |
| | | if (messageArray.getJSONArray(3).size() > 0) { |
| | | Object value = messageArray.getJSONArray(3).get(0); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short sValue = Short.parseShort(cleanedValue.trim()); |
| | | S7control.getinstance().WriteWord(addressList2, Arrays.asList(sValue)); |
| | | System.out |
| | | .println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList2); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 写入第二个地址 |
| | | if (messageArray.getJSONArray(4).size() > 0) { |
| | | Object value = messageArray.getJSONArray(4).get(0); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short sValue = Short.parseShort(cleanedValue.trim()); |
| | | S7control.getinstance().WriteWord(addressList3, Arrays.asList(sValue)); |
| | | System.out |
| | | .println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList3); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 写入第三个地址 |
| | | if (!mergedList.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses1, mergedList); |
| | | System.out.println("messageValue:" + mergedList + " written to PLC at address " + waddresses1); |
| | | } |
| | | |
| | | // 清空消息列表 |
| | | webserver.clearMessages(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import org.apache.commons.io.FileUtils; |
| | | |
| | | import java.io.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | |
| | | |
| | | public class PlcParameter2 extends Thread { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public static String readFileToString(String filePath) throws IOException { |
| | | File file = new File(filePath); |
| | | return FileUtils.readFileToString(file, "UTF-8"); |
| | | } |
| | | |
| | | public void readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/address.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("Positioning1"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // 手动状态地址 |
| | | List<String> addressList1 = new ArrayList<>(); |
| | | |
| | | addressList1.add("DB103.44");//D01 |
| | | addressList1.add("DB103.58");//D02 |
| | | addressList1.add("DB103.72");//D03 |
| | | addressList1.add("DB103.86");//D04 |
| | | addressList1.add("DB103.100");//D05 |
| | | addressList1.add("DB103.114");//D06 |
| | | addressList1.add("DB103.128");//A01 |
| | | addressList1.add("DB103.142");//A01 2 |
| | | addressList1.add("DB103.156");//A02 |
| | | addressList1.add("DB103.184");//B01 |
| | | addressList1.add("DB103.212");//B02 |
| | | |
| | | |
| | | List<String> paramlist = S7control.getinstance().readStrings(addressList1); |
| | | |
| | | |
| | | //清除ID地址 |
| | | List<String> addresses = new ArrayList<>(); |
| | | addresses.add("DB103.0"); |
| | | addresses.add("DB103.2"); |
| | | addresses.add("DB103.12"); |
| | | addresses.add("DB103.14"); |
| | | addresses.add("DB103.16"); |
| | | addresses.add("DB103.18"); |
| | | addresses.add("DB103.8"); |
| | | addresses.add("DB103.10"); |
| | | addresses.add("DB103.4"); |
| | | addresses.add("DB103.6"); |
| | | List<Short> data = S7control.getinstance().readWords(addresses); |
| | | |
| | | |
| | | //id |
| | | List<String> addressList0 = new ArrayList<>(); |
| | | |
| | | addressList0.add("DB101.9.2"); |
| | | addressList0.add("DB101.9.3"); |
| | | addressList0.add("DB101.9.4"); |
| | | addressList0.add("DB101.9.5"); |
| | | addressList0.add("DB101.9.6"); |
| | | addressList0.add("DB101.9.7"); |
| | | |
| | | addressList0.add("DB101.10.0"); |
| | | addressList0.add("DB101.10.1"); |
| | | addressList0.add("DB101.10.2"); |
| | | addressList0.add("DB101.10.3"); |
| | | |
| | | // 扫码枪 |
| | | List<Boolean> data2 = S7control.getinstance().readBits(addressList0); |
| | | List<String> addressList3 = new ArrayList<>(); |
| | | addressList3.add("DB103.256"); |
| | | addressList3.add("DB103.270"); |
| | | |
| | | |
| | | |
| | | |
| | | List<String> niuanaddressList3 = new ArrayList<>(); |
| | | |
| | | niuanaddressList3.add("DB101.10.4"); |
| | | List<Boolean> anniuread = S7control.getinstance().readBits(niuanaddressList3); |
| | | |
| | | |
| | | |
| | | List<String> data3 = S7control.getinstance().readStrings(addressList3); |
| | | // A01 A02 B01 B02 |
| | | List<String> addresses4 = new ArrayList<>(); |
| | | addresses4.add("DB103.32"); |
| | | addresses4.add("DB103.34"); |
| | | addresses4.add("DB103.36"); |
| | | addresses4.add("DB103.40"); |
| | | |
| | | List<Short> data4 = S7control.getinstance().readWords(addresses4); |
| | | |
| | | // List<Long> TIME2 = Collections.singletonList(S7control.getinstance().readtime("DB100.194")); |
| | | // System.out.println("addressList:" + TIME2); |
| | | |
| | | |
| | | // |
| | | |
| | | |
| | | |
| | | |
| | | // String[] values6 = { ".x1 ","x2 ","x3",";;x4","x5","x6","x7","x8","x9","x10","x11"}; |
| | | // List<String> paramlist = new ArrayList<>(Arrays.asList(values6)); |
| | | // Short[] values1 = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| | | // List<Short> data = new ArrayList<>(Arrays.asList(values1)); |
| | | // Boolean[] value4 = { false, false, false, false, false, false, false, false, false, |
| | | // false}; |
| | | // List<Boolean> data2 = new ArrayList<>(Arrays.asList(value4)); |
| | | // String[] values3 = { ".x11 ","x21 "}; |
| | | // List<String> data3 = new ArrayList<>(Arrays.asList(values3)); |
| | | // Short[] values4 = { 2,2,3,4}; |
| | | // List<Short> data4 = new ArrayList<>(Arrays.asList(values4)); |
| | | // |
| | | // |
| | | JSONObject jsonObject = new JSONObject(); |
| | | short[] params = new short[data2.size()]; |
| | | for (int i = 0; i < data2.size(); i++) { |
| | | boolean value = data2.get(i); |
| | | params[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | |
| | | short[] anniuparams = new short[anniuread.size()]; |
| | | for (int i = 0; i < anniuread.size(); i++) { |
| | | boolean value = anniuread.get(i); |
| | | anniuparams[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | |
| | | jsonObject.append("params", params); |
| | | jsonObject.append("params", paramlist); |
| | | jsonObject.append("params", data); |
| | | jsonObject.append("params", data3); |
| | | jsonObject.append("params", data4); |
| | | jsonObject.append("jiting", anniuparams); |
| | | |
| | | |
| | | |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Parameter2"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | |
| | | |
| | | |
| | | List<String> messages = webserver.getMessages(); |
| | | List<String> addressList = new ArrayList<>(); |
| | | |
| | | addressList.add("DB101.9.2"); |
| | | addressList.add("DB101.9.3"); |
| | | addressList.add("DB101.9.4"); |
| | | addressList.add("DB101.9.5"); |
| | | addressList.add("DB101.9.6"); |
| | | addressList.add("DB101.9.7"); |
| | | addressList.add("DB101.10.0"); |
| | | addressList.add("DB101.10.1"); |
| | | addressList.add("DB101.10.2"); |
| | | addressList.add("DB101.10.3"); |
| | | |
| | | |
| | | |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为整数类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | // System.out.println("lastMessage:" + lastMessage); |
| | | JSONArray messageArray = new JSONArray(lastMessage); |
| | | |
| | | if (messageArray.getJSONArray(0).size() > 0) { |
| | | JSONArray jsonArray3 = messageArray.getJSONArray(0); |
| | | List<Boolean> sValue3 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray3.size(); i++) { |
| | | Object value = jsonArray3.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValue3.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue3.isEmpty()) { |
| | | S7control.getinstance().WriteBit(addressList, sValue3); |
| | | System.out.println("Values " + sValue3 + " written to PLC at address " + sValue3); |
| | | } |
| | | } |
| | | // 将布尔列表写入 PLC |
| | | if (messageArray.getJSONArray(1).size() > 0) { |
| | | JSONArray jsonArray3 = messageArray.getJSONArray(1); |
| | | List<Boolean> sValue3 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray3.size(); i++) { |
| | | Object value = jsonArray3.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValue3.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue3.isEmpty()) { |
| | | S7control.getinstance().WriteBit(niuanaddressList3, sValue3); |
| | | |
| | | System.out.println("Values " + sValue3 + " written to PLC at address " + sValue3); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 清空消息列表 |
| | | webserver.clearMessages(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import cn.hutool.json.JSONArray;
|
| | | import cn.hutool.json.JSONObject;
|
| | |
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | | import java.util.Arrays;
|
| | | import java.io.BufferedReader;
|
| | | import java.io.File;
|
| | | import java.io.FileInputStream;
|
| | | import java.io.FileNotFoundException;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStreamReader;
|
| | | import java.io.UnsupportedEncodingException;
|
| | |
|
| | | import org.apache.commons.io.FileUtils;
|
| | |
|
| | | public class PlcPositioning1 extends Thread {
|
| | | String name = "";
|
| | | Integer count = 0;
|
| | |
|
| | | public static String readFileToString(String filePath) throws IOException {
|
| | | File file = new File(filePath);
|
| | | return FileUtils.readFileToString(file, "UTF-8");
|
| | | }
|
| | |
|
| | | public void readValue() {
|
| | | String str = "";
|
| | | BufferedReader bufferedReader = null;
|
| | | FileInputStream fileInputStream;
|
| | | try {
|
| | | // 从文件中读取字节数据存入 fileInputStream
|
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/address.json");
|
| | | // 读取 fileInputStream 中字节并将其解码为字符
|
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
|
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader
|
| | | bufferedReader = new BufferedReader(inputStreamReader);
|
| | | String line = null;
|
| | | // 将 bufferedReader 内容一行一行赋值给str
|
| | | while ((line = bufferedReader.readLine()) != null) {
|
| | | str += line;
|
| | | }
|
| | |
|
| | | // 将str字符串格式转为json
|
| | | JSONObject jsonObject = new JSONObject(str);
|
| | |
|
| | | // 获取json中的值
|
| | | JSONArray address = jsonObject.getJSONArray("Positioning1");
|
| | | for (int i = 0; i < address.size(); i++) {
|
| | | JSONObject ress = (JSONObject) address.get(i);
|
| | |
|
| | | this.name = ress.getStr("name");
|
| | | this.count = ress.getInt("count");
|
| | | System.out.println("messageValues:" + this.name);
|
| | | System.out.println("messageValues:" + this.count);
|
| | | }
|
| | |
|
| | | } catch (FileNotFoundException e) {
|
| | | e.printStackTrace();
|
| | | } catch (UnsupportedEncodingException e) {
|
| | | e.printStackTrace();
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void run() {
|
| | | while (this != null) {
|
| | | try {
|
| | | Thread.sleep(1000);
|
| | |
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | // this.readValue();
|
| | | // String PlcAddress = this.name;
|
| | | // Integer Plccount = this.count;
|
| | |
|
| | | // System.out.println(stt);
|
| | | //存片格 出片格读取
|
| | | List<Short> arraylist = S7control.getinstance().ReadWord("DB100.42", 23);
|
| | | // System.out.println(arraylist);
|
| | | // Short[] 存片 = { 1 };
|
| | | // List<Short> 存片1 = new ArrayList<>(Arrays.asList(存片));
|
| | | // Short[] 接片 = { 1 };
|
| | | // List<Short> 接片1 = new ArrayList<>(Arrays.asList(接片));
|
| | | // Short[] 间距 = { 1 };
|
| | | // List<Short> 间距1 = new ArrayList<>(Arrays.asList(间距));
|
| | | // Short[] values1 = {1,1,1, 1,2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12,13,14,15,16,17,18,19,20 };
|
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1));
|
| | |
|
| | |
|
| | | JSONObject jsonObject = new JSONObject();
|
| | |
|
| | |
|
| | | jsonObject.append("params", arraylist);
|
| | |
|
| | |
|
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Positioning1");
|
| | | // if (sendwServer != null) {
|
| | | // sendwServer.sendMessage(jsonObject.toString());
|
| | | // }
|
| | |
|
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Positioning1");
|
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Positioning1");
|
| | | if (sendwServer != null) {
|
| | | for (WebSocketServer webserver : sendwServer) {
|
| | | webserver.sendMessage(jsonObject.toString());
|
| | |
|
| | | if (webserver != null) {
|
| | | List<String> messages = webserver.getMessages();
|
| | | //存片格 出片格地址
|
| | | String addressList3 = "DB100.42";
|
| | |
|
| | | if (!messages.isEmpty()) {
|
| | | // 将最后一个消息转换为 short 类型的列表
|
| | | String lastMessage = messages.get(messages.size() - 1);
|
| | | // System.out.println("lastMessage:" + lastMessage);
|
| | | JSONArray messageArray = new JSONArray(lastMessage);
|
| | |
|
| | | // 整合第 1 到 3 个数组并去掉 null 元素
|
| | | List<Short> mergedList = new ArrayList<>();
|
| | | for (int i = 0; i < 5; i++) {
|
| | | JSONArray sublist = messageArray.getJSONArray(i);
|
| | | for (int j = 0; j < sublist.size(); j++) {
|
| | | Object value = sublist.get(j);
|
| | | if (value != null && !value.toString().equals("null")) {
|
| | | try {
|
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
|
| | | short sValue = Short.parseShort(cleanedValue.trim());
|
| | | mergedList.add(sValue);
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为 short 类型,则忽略该部分
|
| | | System.err.println("Could not parse value: " + value);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | // 写入存片格 出片格位置设定数据
|
| | | if (!mergedList.isEmpty()) {
|
| | | S7control.getinstance().WriteWord(addressList3, mergedList);
|
| | | System.out.println("messageValue:" + mergedList + " written to PLC at address " + addressList3);
|
| | | }
|
| | |
|
| | | // 清空消息列表
|
| | | webserver.clearMessages();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import cn.hutool.json.JSONArray;
|
| | | import cn.hutool.json.JSONObject;
|
| | | import org.apache.commons.io.FileUtils;
|
| | |
|
| | | import java.io.*;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.List;
|
| | |
|
| | | public class PlcPositioning2 extends Thread {
|
| | | String name = "";
|
| | | Integer count = 0;
|
| | |
|
| | | public static String readFileToString(String filePath) throws IOException {
|
| | | File file = new File(filePath);
|
| | | return FileUtils.readFileToString(file, "UTF-8");
|
| | | }
|
| | |
|
| | | public void readValue() {
|
| | | String str = "";
|
| | | BufferedReader bufferedReader = null;
|
| | | FileInputStream fileInputStream;
|
| | | try {
|
| | | // 从文件中读取字节数据存入 fileInputStream
|
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/address.json");
|
| | | // 读取 fileInputStream 中字节并将其解码为字符
|
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
|
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader
|
| | | bufferedReader = new BufferedReader(inputStreamReader);
|
| | | String line = null;
|
| | | // 将 bufferedReader 内容一行一行赋值给str
|
| | | while ((line = bufferedReader.readLine()) != null) {
|
| | | str += line;
|
| | | }
|
| | |
|
| | | // 将str字符串格式转为json
|
| | | JSONObject jsonObject = new JSONObject(str);
|
| | |
|
| | | // 获取json中的值
|
| | | JSONArray address = jsonObject.getJSONArray("Positioning2");
|
| | | for (int i = 0; i < address.size(); i++) {
|
| | | JSONObject ress = (JSONObject) address.get(i);
|
| | |
|
| | | this.name = ress.getStr("name");
|
| | | this.count = ress.getInt("count");
|
| | | System.out.println("messageValues:" + this.name);
|
| | | System.out.println("messageValues:" + this.count);
|
| | | }
|
| | |
|
| | | } catch (FileNotFoundException e) {
|
| | | e.printStackTrace();
|
| | | } catch (UnsupportedEncodingException e) {
|
| | | e.printStackTrace();
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void run() {
|
| | | while (this != null) {
|
| | | try {
|
| | | Thread.sleep(1000);
|
| | |
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | // this.readValue();
|
| | | // String PlcAddress = this.name;
|
| | | // Integer Plccount = this.count;
|
| | |
|
| | | // System.out.println(stt);
|
| | | //B01格位置 地址读取
|
| | | List<Short> arraylist = S7control.getinstance().ReadWord("DB100.88", 10);
|
| | |
|
| | | // Short[] values1 = { 1,2, 3, 4, 5 , 11, 12,13,14,15};
|
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1));
|
| | |
|
| | |
|
| | | JSONObject jsonObject = new JSONObject();
|
| | | // new short[]{1,1, 1, 1, 1, 1, 2, 33, 2,3, 4, 5}
|
| | | // new short[]{0,0, 0, 0, 0, 0, 0, 0, 0, 0}
|
| | |
|
| | | jsonObject.append("params", arraylist);
|
| | |
|
| | |
|
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Positioning2");
|
| | | // if (sendwServer != null) {
|
| | | // sendwServer.sendMessage(jsonObject.toString());
|
| | | // }
|
| | |
|
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Positioning2");
|
| | | if (sendwServer != null) {
|
| | | for (WebSocketServer webserver : sendwServer) {
|
| | | webserver.sendMessage(jsonObject.toString());
|
| | |
|
| | |
|
| | | if (webserver != null) {
|
| | | List<String> messages = webserver.getMessages();
|
| | | //B01 B02 地址
|
| | | String addressList3 = "DB100.88";
|
| | |
|
| | | if (!messages.isEmpty()) {
|
| | | // 将最后一个消息转换为 short 类型的列表
|
| | | String lastMessage = messages.get(messages.size() - 1);
|
| | | // System.out.println("lastMessage:" + lastMessage);
|
| | | JSONArray messageArray = new JSONArray(lastMessage);
|
| | |
|
| | | // 整合第 1 到 3 个数组并去掉 null 元素
|
| | | List<Short> mergedList = new ArrayList<>();
|
| | | for (int i = 0; i < 2; i++) {
|
| | | JSONArray sublist = messageArray.getJSONArray(i);
|
| | | for (int j = 0; j < sublist.size(); j++) {
|
| | | Object value = sublist.get(j);
|
| | | if (value != null && !value.toString().equals("null")) {
|
| | | try {
|
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
|
| | | short sValue = Short.parseShort(cleanedValue.trim());
|
| | | mergedList.add(sValue);
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为 short 类型,则忽略该部分
|
| | | System.err.println("Could not parse value: " + value);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | // 写入b01 b02地址
|
| | | if (!mergedList.isEmpty()) {
|
| | | S7control.getinstance().WriteWord(addressList3, mergedList);
|
| | | System.out.println("messageValue:" + mergedList + " written to PLC at address " + addressList3);
|
| | | }
|
| | |
|
| | | // 清空消息列表
|
| | | webserver.clearMessages();
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import com.google.common.primitives.Bytes; |
| | | import org.apache.commons.io.FileUtils; |
| | | |
| | | import java.io.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | public class PlcServoManualone extends Thread { |
| | | |
| | | |
| | | |
| | | private Configuration config; |
| | | |
| | | public PlcServoManualone() throws IOException { |
| | | config = new Configuration("config.properties"); |
| | | } |
| | | |
| | | public static class PLCBooleanConverter { |
| | | |
| | | public static short[] convertListsToBinaryShortArray(List<Boolean>... lists) { |
| | | // 计算所有列表中元素的总数 |
| | | int totalSize = 0; |
| | | for (List<Boolean> list : lists) { |
| | | totalSize += list.size(); |
| | | } |
| | | |
| | | // 创建一个足够大的数组来存放所有的二进制值 |
| | | short[] binaryShortArray = new short[totalSize]; |
| | | |
| | | // 当前数组填充位置指针 |
| | | int currentIndex = 0; |
| | | |
| | | // 遍历所有的列表 |
| | | for (List<Boolean> list : lists) { |
| | | // 将每个布尔值转换为 0 或 1 并填入数组 |
| | | for (Boolean value : list) { |
| | | binaryShortArray[currentIndex++] = (short) (value ? 1 : 0); |
| | | } |
| | | } |
| | | |
| | | return binaryShortArray; |
| | | } |
| | | } |
| | | |
| | | //读取id |
| | | public StringBuilder queGlassid(String address,int count) { |
| | | StringBuilder writedstrIdOut = new StringBuilder(); |
| | | byte[] writedglassidbytesOut = S7control.getinstance().ReadByte(address, count); |
| | | if (writedglassidbytesOut != null) { |
| | | // 获取玻璃id |
| | | for (byte iditem : writedglassidbytesOut) { |
| | | writedstrIdOut.append((char) iditem); |
| | | } |
| | | } |
| | | return writedstrIdOut; |
| | | } |
| | | //写入id |
| | | public void outmesid(String glassid,String address) { |
| | | //System.out.println("outmesid:" + glassid); |
| | | List<Byte> glassidlist = new ArrayList(); |
| | | char ds[]=glassid.toCharArray(); |
| | | for (char iditem : ds) { |
| | | glassidlist.add((byte)iditem); |
| | | } |
| | | byte[] bytes = Bytes.toArray(glassidlist); |
| | | System.out.println("outmesidbytes:" + bytes.length); |
| | | S7control.getinstance().WriteByte(address, bytes); |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | //A01组合中地址表集合 |
| | | List<String> addresses = Arrays.asList(config.getProperty("A01.addresses").split(",")); |
| | | //System.out.println(addresses); |
| | | |
| | | List<Short> arraylist = S7control.getinstance().readWords(addresses); |
| | | |
| | | //A02组合中地址表集合 |
| | | List<String> addresses2 = Arrays.asList(config.getProperty("A02.addresses").split(",")); |
| | | List<Short> arraylist2 = S7control.getinstance().readWords(addresses2); |
| | | |
| | | //A01翻转地址 |
| | | List<String> addresses3 = Arrays.asList(config.getProperty("A01.flipAddress")); |
| | | List<Short> arraylist3 = S7control.getinstance().readWords(addresses3); |
| | | |
| | | //A02翻转地址 |
| | | List<String> addresses4 = Arrays.asList(config.getProperty("A02.flipAddress")); |
| | | |
| | | List<Short> arraylist4 = S7control.getinstance().readWords(addresses4); |
| | | //B01组合中地址 |
| | | String b01Address = config.getProperty("B01.address"); |
| | | List<Short> arraylist5 = S7control.getinstance().ReadWord(b01Address, 2); |
| | | //B02组合地址 |
| | | String b02Address = config.getProperty("B02.address"); |
| | | List<Short> arraylist6 = S7control.getinstance().ReadWord(b02Address, 2); |
| | | // System.out.println(b02Address); |
| | | // 所有故障按钮地址 |
| | | List<String> addresses7 = Arrays.asList(config.getProperty("faultButtonAddresses").split(",")); |
| | | List<Boolean> arraylist7 = S7control.getinstance().readBits(addresses7); |
| | | |
| | | //A01启动地址 |
| | | List<String> A01start = Arrays.asList(config.getProperty("A01start")); |
| | | //A02启动地址 |
| | | List<String> A02start = Arrays.asList(config.getProperty("A02start")); |
| | | //A01半自动地址 |
| | | List<String> A01Bstart = Arrays.asList(config.getProperty("A01Bstart")); |
| | | //A02半自动地址 |
| | | List<String> A02Bstart = Arrays.asList(config.getProperty("A02Bstart")); |
| | | // |
| | | List<String> abortresumeTasks = Arrays.asList(config.getProperty("abortresumeTasks")); |
| | | //回零按钮地址 |
| | | List<String> waddresses4 = Arrays.asList(config.getProperty("HomedButtonAddresses").split(",")); |
| | | // 所有复位按钮地址 |
| | | List<String> Reset = Arrays.asList(config.getProperty("resetButtonAddresses").split(",")); |
| | | // System.out.println(Reset); |
| | | // 所有未回零按钮地址 |
| | | List<String> addresses8 = Arrays.asList(config.getProperty("notHomedButtonAddresses").split(",")); |
| | | // 进片id地址 |
| | | String inputGlassIdAddress = config.getProperty("inputGlassIdAddress"); |
| | | String outputGlassIdAddress = config.getProperty("outputGlassIdAddress"); |
| | | StringBuilder queueid1 = queGlassid(inputGlassIdAddress, 14); |
| | | // 存片id地址 |
| | | StringBuilder queueid2 = queGlassid(outputGlassIdAddress, 14); |
| | | |
| | | List<Boolean> A01readstart = S7control.getinstance().readBits(A01start); |
| | | List<Boolean> A02readstart = S7control.getinstance().readBits(A02start); |
| | | List<Boolean> A01readBstart = S7control.getinstance().readBits(A01Bstart); |
| | | List<Boolean> A02readsBtart = S7control.getinstance().readBits(A02Bstart); |
| | | |
| | | List<Boolean> resumeTasks = S7control.getinstance().readBits(abortresumeTasks); |
| | | |
| | | short[] combinedBinaryShortArray = PLCBooleanConverter.convertListsToBinaryShortArray( |
| | | A01readstart, A01readBstart, A02readstart, A02readsBtart |
| | | ); |
| | | |
| | | |
| | | // 所有故障按钮转换为1和0 |
| | | short[] params2 = new short[arraylist7.size()]; |
| | | for (int i = 0; i < arraylist7.size(); i++) { |
| | | boolean value = arraylist7.get(i); |
| | | params2[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | |
| | | //急停中止按钮 |
| | | short[] resumeTask = new short[resumeTasks.size()]; |
| | | for (int i = 0; i < resumeTasks.size(); i++) { |
| | | boolean value = resumeTasks.get(i); |
| | | resumeTask[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | |
| | | List<Boolean> Reset1 = S7control.getinstance().readBits(Reset); |
| | | // System.out.println(Reset1); |
| | | // 所有故障按钮转换为1和0 |
| | | short[] rReset = new short[Reset1.size()]; |
| | | for (int i = 0; i < Reset1.size(); i++) { |
| | | boolean value = Reset1.get(i); |
| | | rReset[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | |
| | | |
| | | |
| | | List<Boolean> arraylist8 = S7control.getinstance().readBits(addresses8); |
| | | short[] params = new short[arraylist8.size()]; |
| | | for (int i = 0; i < arraylist8.size(); i++) { |
| | | boolean value = arraylist8.get(i); |
| | | params[i] = value ? (short) 1 : (short) 0; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | // Short[] values1 = { 1, 2, 3, 4, 5, 6, 7}; |
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1)); |
| | | // Short[] values2 = { 1, 2, 3, 4, 5}; |
| | | // List<Short> arraylist2 = new ArrayList<>(Arrays.asList(values2)); |
| | | // |
| | | // Short[] values3 = {1}; |
| | | // List<Short> arraylist3 = new ArrayList<>(Arrays.asList(values3)); |
| | | // Short[] values4 = {2}; |
| | | // List<Short> arraylist4 = new ArrayList<>(Arrays.asList(values4)); |
| | | // Short[] values5 = {3,3}; |
| | | // List<Short> arraylist5 = new ArrayList<>(Arrays.asList(values5)); |
| | | // Short[] values6 = { 4,4}; |
| | | // List<Short> arraylist6 = new ArrayList<>(Arrays.asList(values6)); |
| | | // Boolean[] values7 = { false, true, true, true, true, true}; |
| | | // List<Boolean> arraylist7 = new ArrayList<>(Arrays.asList(values7)); |
| | | // Boolean[] values8 = { false, true, true, true, true, true}; |
| | | // List<Boolean> arraylist8 = new ArrayList<>(Arrays.asList(values8)); |
| | | // |
| | | // String queueid1 = "x1234567890123"; |
| | | // String queueid2 = "x1234567890123"; |
| | | // |
| | | // short[] params2 = new short[arraylist7.size()]; |
| | | // for (int i = 0; i < arraylist7.size(); i++) { |
| | | // boolean value = arraylist7.get(i); |
| | | // params2[i] = value ? (short) 1 : (short) 0; |
| | | // } |
| | | // |
| | | // |
| | | // short[] params = new short[arraylist8.size()]; |
| | | // for (int i = 0; i < arraylist8.size(); i++) { |
| | | // boolean value = arraylist8.get(i); |
| | | // params[i] = value ? (short) 1 : (short) 0; |
| | | // } |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | |
| | | jsonObject.append("zuhe1", arraylist); |
| | | jsonObject.append("zuhe2", arraylist2); |
| | | jsonObject.append("zuhe3", arraylist3); |
| | | jsonObject.append("zuhe4", arraylist4); |
| | | jsonObject.append("zuhe5", arraylist5); |
| | | jsonObject.append("zuhe6", arraylist6); |
| | | jsonObject.append("guzhang", params2); |
| | | jsonObject.append("weihuiling", params); |
| | | jsonObject.append("jinpianid", queueid1); |
| | | jsonObject.append("qupianid", queueid2); |
| | | jsonObject.append("fuwei", rReset); |
| | | jsonObject.append("qidong", combinedBinaryShortArray); |
| | | jsonObject.append("resumeTasks", resumeTask); |
| | | |
| | | |
| | | |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ServoManualone"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("ServoManualone"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | |
| | | if (webserver != null) { |
| | | List<String> messages = webserver.getMessages(); |
| | | |
| | | |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为 short 类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | // System.out.println("lastMessage:" + lastMessage); |
| | | JSONArray messageArray = new JSONArray(lastMessage); |
| | | |
| | | |
| | | // 写入AO1组合 |
| | | if (messageArray.getJSONArray(0).size() > 0) { |
| | | JSONArray jsonArray = messageArray.getJSONArray(0); |
| | | List<Short> 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-]", ""); |
| | | short val = Short.parseShort(cleanedValue.trim()); |
| | | sValue.add(val); |
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list"); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue.isEmpty()) { |
| | | |
| | | S7control.getinstance().WriteWord(addresses, sValue); |
| | | // 检查是否读取到了位值,并且列表不为空 |
| | | if (A01readstart != null && !A01readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> a01startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : A01readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | a01startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(A01start, a01startval); |
| | | } |
| | | System.out.println("Values " + sValue + " written to PLC at address " + addresses); |
| | | } |
| | | } |
| | | |
| | | |
| | | //// 写入AO2组合 |
| | | if (messageArray.getJSONArray(1).size() > 0) { |
| | | JSONArray jsonArray2 = messageArray.getJSONArray(1); |
| | | List<Short> sValue2 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray2.size(); i++) { |
| | | Object value = jsonArray2.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | short val = Short.parseShort(cleanedValue.trim()); |
| | | sValue2.add(val); |
| | | System.out.println("messageValue:" + Arrays.asList(val) + " added to the list"); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | System.err.println("Could not parse value: " + value); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(addresses2, sValue2); |
| | | |
| | | if (A02readstart != null && !A02readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> a02startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : A02readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | a02startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(A02start, a02startval); |
| | | } |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + addresses2); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 复位按钮写入 |
| | | if (messageArray.getJSONArray(2).size() > 0) { |
| | | JSONArray jsonArray3 = messageArray.getJSONArray(2); |
| | | List<Boolean> sValue3 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray3.size(); i++) { |
| | | Object value = jsonArray3.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValue3.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue3.isEmpty()) { |
| | | S7control.getinstance().WriteBit(Reset, sValue3); |
| | | System.out.println("Values " + sValue3 + " written to PLC at address " + Reset); |
| | | } |
| | | } |
| | | |
| | | //回零按钮写入 |
| | | if (messageArray.getJSONArray(3).size() > 0) { |
| | | JSONArray jsonArray4 = messageArray.getJSONArray(3); |
| | | List<Boolean> sValue4 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray4.size(); i++) { |
| | | Object value = jsonArray4.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValue4.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValue4.isEmpty()) { |
| | | S7control.getinstance().WriteBit(waddresses4, sValue4); |
| | | System.out.println("Values " + sValue4 + " written to PLC at address " + waddresses4); |
| | | } |
| | | } |
| | | |
| | | |
| | | //取片ID写入 |
| | | if (messageArray.getJSONArray(5).size() > 0) { |
| | | // JSONArray jsonArray5 = messageArray.getJSONArray(4); |
| | | String jsonArray5 = String.valueOf(messageArray.getJSONArray(5)); |
| | | if (!jsonArray5.isEmpty()) { |
| | | // String value = (String) jsonArray5.get(0); |
| | | jsonArray5 = jsonArray5.replaceAll("[\\[\\]\"]", ""); // 去除方括号和双引号 |
| | | outmesid(jsonArray5, outputGlassIdAddress); |
| | | System.out.println("Values1 " + jsonArray5 + " written to PLC at address " + outputGlassIdAddress); |
| | | } |
| | | } |
| | | // 进片ID写入 |
| | | if (messageArray.getJSONArray(4).size() > 0) { |
| | | JSONArray jsonArray6 = messageArray.getJSONArray(4); |
| | | |
| | | if (!jsonArray6.isEmpty()) { |
| | | String value2 = (String) jsonArray6.get(0); |
| | | outmesid(value2.trim(), inputGlassIdAddress); |
| | | System.out.println("Values2 " + value2 + " written to PLC at address " + inputGlassIdAddress); |
| | | } |
| | | } |
| | | |
| | | |
| | | if (messageArray.getJSONArray(6).size() > 0) { |
| | | JSONArray jsonArray4 = messageArray.getJSONArray(6); |
| | | List<Boolean> sValueb4 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray4.size(); i++) { |
| | | Object value = jsonArray4.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValueb4.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValueb4.isEmpty()) { |
| | | |
| | | S7control.getinstance().WriteBit(A01Bstart, sValueb4); |
| | | System.out.println("Values " + sValueb4 + " written to PLC at address " + A01Bstart); |
| | | } |
| | | } |
| | | |
| | | |
| | | if (messageArray.getJSONArray(7).size() > 0 && !messageArray.isNull(7)) { |
| | | JSONArray jsonArray4 = messageArray.getJSONArray(7); |
| | | List<Boolean> sValueb5 = new ArrayList<>(); |
| | | for (int i = 0; i < jsonArray4.size(); i++) { |
| | | Object value = jsonArray4.get(i); |
| | | if (value != null && !value.toString().equals("null")) { |
| | | try { |
| | | String cleanedValue = value.toString().replaceAll("[^0-9-]", ""); |
| | | boolean val = "1".equals(cleanedValue.trim()); |
| | | sValueb5.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); |
| | | } |
| | | } |
| | | } |
| | | if (!sValueb5.isEmpty()) { |
| | | |
| | | S7control.getinstance().WriteBit(A02Bstart, sValueb5); |
| | | System.out.println("Values " + sValueb5 + " written to PLC at address " + A02Bstart); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | // 清空消息列表 |
| | | webserver.clearMessages(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | import com.example.springboot.mapper.AlarmMapper; |
| | | |
| | | public class Plcalarm extends Thread { |
| | | private AlarmMapper alarmMapper; |
| | | |
| | | String content = ""; |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public List<String> readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("D:/canadames/Alarm.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | List<String> arraylistss = new ArrayList<>(); |
| | | |
| | | // 获取json中报警信息的值 |
| | | JSONArray AlarmContent = jsonObject.getJSONArray("content"); |
| | | for (int i = 0; i < AlarmContent.size(); i++) { |
| | | JSONObject ress = (JSONObject) AlarmContent.get(i); |
| | | |
| | | this.content = ress.getStr("name"); |
| | | arraylistss.add(content); |
| | | } |
| | | // 获取json中地址的值 |
| | | JSONArray address = jsonObject.getJSONArray("address"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | return arraylistss; |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | |
| | | // 根据地址读取PCL数据 |
| | | List<Boolean> plclist = S7control.getinstance().ReadBits("DB104.0.0", 71); |
| | | // List<Boolean> plclist = S7control.getinstance().ReadBits(PlcAddress, Plccount); |
| | | // System.out.println(plclist); |
| | | // Boolean[] values = { false, false, true, false, true, false, true, false, |
| | | // true, false, true, false, true, false, |
| | | // true, false, true, false, true, false, true, false, true, false, true, false, |
| | | // true, false, true, false, true, |
| | | // false, true, false, true, false, |
| | | // true, false, true, false }; |
| | | // List<Boolean> plclist = new ArrayList<>(Arrays.asList(values)); |
| | | if (plclist != null) { |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.append("params", plclist); |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("alarm"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | |
| | | // 将获取的布尔类型转换为整数类型 |
| | | List<Integer> Intlist = new ArrayList<>(); |
| | | for (Boolean value : plclist) { |
| | | Intlist.add(value == true ? 1 : 0); |
| | | |
| | | } |
| | | |
| | | // 将Intlist转换为数组 |
| | | // System.out.println(Intlist); |
| | | Integer[] shuzu1 = Intlist.toArray(new Integer[0]); |
| | | |
| | | // 定义的报警内容数组,获取json返回的集合 |
| | | List<String> myCollection; |
| | | myCollection = readValue(); |
| | | String[] array1 = myCollection.toArray(new String[myCollection.size()]); |
| | | // System.out.println(array1.length); |
| | | alarmMapper = WebSocketServer.applicationContext.getBean(AlarmMapper.class); |
| | | for (short i = 0; i < array1.length; i++) { |
| | | // 查询对应报警信息结束时间为null的条数 |
| | | short result = alarmMapper.selectnullti(array1[i]); |
| | | // 读取到PLC的值为1并且对应报警信息结束时间为null的条数的条数为0 |
| | | // System.out.println(result); |
| | | if (shuzu1[i] == 1 && result == 0) { |
| | | // 填加一条报警信息,有开始时间 |
| | | alarmMapper.Insertalarm(array1[i]); |
| | | } else if (shuzu1[i] == 0 && result > 0) { |
| | | // 修改该条报警信息的结束时间 |
| | | alarmMapper.updatealarm(array1[i]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | |
| | | import com.example.springboot.entity.Out_slice; |
| | | import com.example.springboot.entity.Queue; |
| | | import com.example.springboot.entity.StorageCage; |
| | | 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; |
| | | |
| | | |
| | | public class Plchome extends Thread { |
| | | |
| | | @Autowired |
| | | private HomeMapper homeMapper; |
| | | private OutSliceServive outSliceServive; |
| | | private SpianMapper spianMapper; |
| | | private SpianService spianService; |
| | | private North_Glass_Buffer1Service north_Glass_Buffer1Service; |
| | | private JdbcConnections dbserve; |
| | | //出片队列是否允许出片 |
| | | public static Boolean isAllowQueue=true; |
| | | //铝框id |
| | | public static String FrameNo; |
| | | //是否需要手动确认玻璃 |
| | | public static Boolean isConfirm=false; |
| | | // public static Map b=new HashMap<>(); |
| | | // b.put() |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | JSONObject jsonObject = new JSONObject(); |
| | | |
| | | // 注入mapper |
| | | homeMapper = WebSocketServer.applicationContext.getBean(HomeMapper.class); |
| | | spianMapper = WebSocketServer.applicationContext.getBean(SpianMapper.class); |
| | | spianService = WebSocketServer.applicationContext.getBean(SpianService.class); |
| | | outSliceServive = WebSocketServer.applicationContext.getBean(OutSliceServive.class); |
| | | north_Glass_Buffer1Service = WebSocketServer.applicationContext.getBean(North_Glass_Buffer1Service.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(3); |
| | | List<StorageCage> tasklist2 = homeMapper.selectinout(2); |
| | | jsonObject.append("tasklist1", tasklist1); |
| | | jsonObject.append("tasklist2", tasklist2); |
| | | // 查询报警信息 |
| | | List<alarmmg> alarmmg = homeMapper.SelectAlarmmgInfo(); |
| | | jsonObject.append("alarmmg", alarmmg); |
| | | // 读去Plc进片车与出片车位置W |
| | | List<String> addressList = new ArrayList<String>(); |
| | | addressList.add("DB106.12"); |
| | | addressList.add("DB106.0"); |
| | | List<Short> paramlist = S7control.getinstance().ReadWord(addressList); |
| | | if (paramlist != null) { |
| | | jsonObject.append("params", paramlist); |
| | | } |
| | | // // 获取进片车状态 |
| | | // List<Short> datas1ListState = S7control.getinstance().ReadWord("DB106.8", |
| | | // 1);// 获取进片车状态 |
| | | // boolean exist1 = datas1ListState.contains((short) 0); |
| | | // jsonObject.append("zhuangtai", exist1); |
| | | //获取进片玻璃信息 |
| | | List<Short> inglassInfo = S7control.getinstance().ReadWord("DB106.24", 1); |
| | | if (inglassInfo != null) { |
| | | if (inglassInfo.size() > 0) |
| | | jsonObject.append("loadglassheight", inglassInfo.get(0) * 70); |
| | | } |
| | | |
| | | // 伍 获取进是否有待确认的玻璃id |
| | | String queid = spianMapper.Selectqueueid(); |
| | | int state = spianMapper.Selectqueuestate(); |
| | | jsonObject.append("queid", queid); |
| | | jsonObject.append("state", state); |
| | | // 获取扫码位与上片位玻璃信息 |
| | | Queue form2 = homeMapper.GetQueueInfo(2); |
| | | Queue form3 = homeMapper.GetQueueInfo(1); |
| | | if(form2.getglassId()!=null&&form2.getglassId()!=""){ |
| | | jsonObject.append("form2", form2); |
| | | } |
| | | if(form3.getglassId()!=null&&form3.getglassId()!=""){ |
| | | jsonObject.append("form3", form3); |
| | | } |
| | | // 获取出片队列信息 |
| | | List<Out_slice> listoutslice=outSliceServive.SelectProductionqueue(); |
| | | // List<Out_slice> listoutslice = homeMapper.SelectProductionqueue(); |
| | | // for (Out_slice out_slice : listoutslice) { |
| | | // out_slice.setstorageCage(homeMapper.SelectStorageGlassById(out_slice.getGlassId())); |
| | | // } |
| | | jsonObject.append("listoutslice", listoutslice); |
| | | //获取当前出片队列状态 |
| | | jsonObject.append("isAllowQueue",Plchome.isAllowQueue); |
| | | |
| | | //获取铝框idDB106.DBW64 |
| | | // String frameno=spianService.queGlassid("DB106.24",14).toString(); |
| | | // if(frameno!=Plchome.FrameNo){ |
| | | // Plchome.FrameNo=frameno; |
| | | // } |
| | | // Plchome.FrameNo="123"; |
| | | // try { |
| | | // north_Glass_Buffer1Service.AddOutSliceS(Plchome.FrameNo); |
| | | // } catch (SQLException e) { |
| | | // // TODO Auto-generated catch block |
| | | // e.printStackTrace(); |
| | | // } |
| | | //是否需要人工确认上片玻璃 |
| | | jsonObject.append("isConfirm", isConfirm); |
| | | |
| | | //铝框线交互 |
| | | |
| | | // //铝框id请求字 |
| | | // List<Short> framerequest = S7control.getinstance().ReadWord("铝框id请求字地址", 1); |
| | | // if(framerequest!=null){ |
| | | // //铝框id |
| | | // String franmeno= spianService.queGlassid("铝框id地址", 14).toString(); |
| | | // String position; |
| | | // try { |
| | | // position=dbserve.SelectPositionByFrameBarcode(franmeno); |
| | | // } catch (SQLException e) { |
| | | // // TODO Auto-generated catch block |
| | | // position="0"; |
| | | // e.printStackTrace(); |
| | | // } |
| | | // Short send; |
| | | // if(position=="0"){ |
| | | // send=0; |
| | | // }else{ |
| | | // send=1; |
| | | // } |
| | | // S7control.getinstance().WriteWord("铝框翻转发送地址", (short)send); |
| | | // } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | dbserve = WebSocketServer.applicationContext.getBean(JdbcConnections.class); |
| | | boolean dbconnected = false; |
| | | try { |
| | | dbserve.getConn(); |
| | | dbconnected = true; |
| | | } catch (Exception e) { |
| | | // TODO: handle exception |
| | | dbconnected = false; |
| | | } |
| | | jsonObject.append("dbconnected", dbconnected); |
| | | // jsonObject.append("params", new short[] { 30, 40, }); |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Home"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Arrays; |
| | | |
| | | public class Plcsign extends Thread { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public void readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/Sign.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("address"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | // System.out.println(PlcAddress); |
| | | |
| | | // List<Boolean> plclist = S7control.getinstance().ReadBits(PlcAddress, Plccount); |
| | | List<Boolean> plclist = S7control.getinstance().ReadBits("DB102.0.0", 64); |
| | | |
| | | // Boolean[] values = { true, true, true, true, true, false, true, false, |
| | | // true, false, true, false, true, false, |
| | | // true, false, true, false, true, false, true, false, true, false, true, false, |
| | | // true, false, true, false, true, |
| | | // false, true, false, true, false, |
| | | // true, false, true, false, true, false, true, false, null, |
| | | // true, false, |
| | | // true, false, true, false, true, false, |
| | | // true, false, true, false, true, }; |
| | | // List<Boolean> plclist = new ArrayList<>(Arrays.asList(values)); |
| | | |
| | | if (plclist != null) { |
| | | // 将获取的布尔类型转换为整数类型 |
| | | List<Integer> Intlist = new ArrayList<>(); |
| | | for (Boolean value : plclist) { |
| | | if (value != null) { |
| | | Intlist.add(value == true ? 0 : 1); |
| | | } |
| | | |
| | | } |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.append("sig", Intlist); |
| | | // WebSocketServer sendwServer3 = WebSocketServer.sessionMap.get("Sign"); |
| | | // if (sendwServer3 != null) { |
| | | // sendwServer3.sendMessage(jsonObject.toString()); |
| | | // } |
| | | |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Sign"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Arrays; |
| | | |
| | | public class Plcstate extends Thread { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public void readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/State.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("address"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | // System.out.println(PlcAddress); |
| | | |
| | | //读取状态数据 |
| | | List<Short> plclist = S7control.getinstance().ReadWord("DB103.0", 44); |
| | | // Short[] values = { 0, 1, 1, 0, 1, 0, 1, 0, |
| | | // 1, 0, }; |
| | | // List<Short> plclist = new ArrayList<>(Arrays.asList(values)); |
| | | if (plclist != null) { |
| | | |
| | | List<String> Intlist = new ArrayList<>(); |
| | | for (Short value : plclist) { |
| | | Intlist.add(value == 1 ? "AUTO" : "Manual"); |
| | | |
| | | } |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.append("sta", Intlist); |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("State"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("State"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.concurrent.ConcurrentHashMap;
|
| | |
|
| | | import org.slf4j.Logger;
|
| | | import org.slf4j.LoggerFactory;
|
| | |
|
| | | import javax.websocket.OnClose;
|
| | | import javax.websocket.OnError;
|
| | | import javax.websocket.OnMessage;
|
| | | import javax.websocket.OnOpen;
|
| | | import javax.websocket.Session;
|
| | | 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;
|
| | |
|
| | | @ServerEndpoint(value = "/api/talk/{username}")
|
| | | @Component
|
| | | public class WebSocketServer {
|
| | |
|
| | | // @Autowired
|
| | | // HomeMapper homeMapper;
|
| | |
|
| | | static ConfigurableApplicationContext applicationContext;
|
| | |
|
| | | // 解决无法注入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;
|
| | | /**
|
| | | * 记录当前在线连接数
|
| | | */
|
| | | public static final Map<String, ArrayList<WebSocketServer>> sessionMap = new ConcurrentHashMap<>();
|
| | |
|
| | | String username;
|
| | | Session session;
|
| | |
|
| | | public WebSocketServer() {
|
| | | this.messages = new ArrayList<>();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 连接建立成功调用的方法
|
| | | */
|
| | | @OnOpen
|
| | | public void onOpen(Session session, @PathParam("username") String username) {
|
| | | this.username = username;
|
| | | this.session = session;
|
| | | List<WebSocketServer> webSocketServers = sessionMap.get(username);
|
| | | if (webSocketServers == null) {
|
| | | ArrayList<WebSocketServer> arrayListwebserver = new ArrayList<WebSocketServer>();
|
| | | arrayListwebserver.add(this);
|
| | | sessionMap.put(username, arrayListwebserver);
|
| | | } else {
|
| | | webSocketServers.add(this);
|
| | | }
|
| | | |
| | | log.info("有新用户加入,username={}, 当前在线人数为:{}", username, sessionMap.get(username).size());
|
| | |
|
| | | // JSONObject result = new JSONObject();
|
| | | // JSONArray array = new JSONArray();
|
| | | // result.set("users", array);
|
| | | // for (Object key : sessionMap.keySet()) {
|
| | | // JSONObject jsonObject = new JSONObject();
|
| | | // jsonObject.set("username", key);
|
| | | // array.add(jsonObject);
|
| | | // }
|
| | |
|
| | | // sendAllMessage(JSONUtil.toJsonStr(result)); // 后台发送消息给所有的客户端
|
| | | }
|
| | |
|
| | | /**
|
| | | * 连接关闭调用的方法
|
| | | */
|
| | | @OnClose
|
| | | public void onClose(Session session, @PathParam("username") String username) {
|
| | | List<WebSocketServer> webSocketServers = sessionMap.get(username);
|
| | | ArrayList<WebSocketServer> arrayListwebserver = new ArrayList<WebSocketServer>();
|
| | | if (webSocketServers.size()>1) {
|
| | | for (WebSocketServer webSocketServer : webSocketServers) {
|
| | | if(webSocketServer!=this){
|
| | | arrayListwebserver.add(webSocketServer);
|
| | | }
|
| | | }
|
| | | sessionMap.put(username, arrayListwebserver);
|
| | | log.info("移除username={}一名用户session, {}的当前在线人数为:{}", username, username, sessionMap.get(username).size());
|
| | | }else{
|
| | | sessionMap.remove(username);
|
| | | log.info("移除username={}一名用户session, {}连接关闭, 当前连接数为:{}", username, username, sessionMap.size());
|
| | | }
|
| | | |
| | | }
|
| | |
|
| | | /**
|
| | | * 收到客户端消息后调用的方法
|
| | | * 后台收到客户端发送过来的消息
|
| | | * onMessage 是一个消息的中转站
|
| | | * 接受 浏览器端 socket.send 发送过来的 json数据
|
| | | * |
| | | * @param message 客户端发送过来的消息
|
| | | */
|
| | | @OnMessage
|
| | | public void onMessage(String message, Session session, @PathParam("username") String username) {
|
| | | log.info("服务端收到用户username={}的消息:{}", username, message);
|
| | | JSONObject obj = JSONUtil.parseObj(message);
|
| | | String text = obj.getStr("data");
|
| | |
|
| | | JSONObject jsonObject = new JSONObject();
|
| | | jsonObject.set("message", text);
|
| | | this.messages.add(text);
|
| | | this.sendMessage(jsonObject.toString()); // JSONUtil.toJsonStr(jsonObject)
|
| | |
|
| | | }
|
| | |
|
| | | @OnError
|
| | | public void onError(Session session, Throwable error) {
|
| | | log.error("发生错误");
|
| | | error.printStackTrace();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 服务端发送消息给客户端
|
| | | */
|
| | | public void sendMessage(String message) {
|
| | | try {
|
| | | // log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message);
|
| | | this.session.getBasicRemote().sendText(message);
|
| | | } catch (Exception e) {
|
| | | log.error("服务端发送消息给客户端失败", e);
|
| | | }
|
| | | }
|
| | |
|
| | | // /**
|
| | | // * 服务端发送消息给所有客户端
|
| | | // */
|
| | | // public void sendAllMessage(String message) {
|
| | | // try {
|
| | | // for (WebSocketServer webSocketServer : sessionMap.values()) {
|
| | | // // log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message);
|
| | | // webSocketServer.sendMessage(message);
|
| | | // }
|
| | | // } catch (Exception e) {
|
| | | // log.error("服务端发送消息给客户端失败", e);
|
| | | // }
|
| | | // }
|
| | |
|
| | | public List<String> getMessages() {
|
| | | return messages;
|
| | |
|
| | | }
|
| | |
|
| | | public void clearMessages() {
|
| | | messages.clear();
|
| | | }
|
| | | }
|
| | |
| | | @GetMapping("/all2") |
| | | public void selectdd(String orderid){ |
| | | SpianService service=new SpianService(); |
| | | service.selectout(orderid); |
| | | service.selectout2(orderid); |
| | | |
| | | } |
| | | |
| | |
| | | int selectGlassState(int cage,int cell); |
| | | |
| | | //判断该调拨的笼子 |
| | | @Select("select glass_id,cage,cell,max(state)as shu,ROUND(id/2)as prcid,id from storage_cage where width>=#{width} and cage>#{cage1} and cage<#{cage2} GROUP BY cage,cell HAVING shu=0 order by abs(#{cage} - cage) limit 1;") |
| | | @Select("select glass_id,cage,cell,max(state)as shu,ROUND(id/2)as prcid,id from storage_cage where width>=#{width} and cage>#{cage1} and cage<#{cage2} GROUP BY cage,cell HAVING shu=0 order by abs(#{cage} - cage),cell desc limit 1;") |
| | | StorageCage selectGlassCage(int cage,double width,int cage1,int cage2); |
| | | |
| | | //删除笼子信息(出片) |
| | |
| | | package com.example.springboot.service; |
| | | |
| | | import java.lang.reflect.Array; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import com.example.springboot.common.Result; |
| | | import com.example.springboot.component.S7control; |
| | | import com.example.springboot.entity.Glass; |
| | | import com.example.springboot.entity.StorageCage; |
| | | import com.example.springboot.entity.north_glass_buffer1; |
| | | import com.example.springboot.mapper.SpianMapper; |
| | |
| | | |
| | | } |
| | | return (400); |
| | | |
| | | } |
| | | |
| | | public Short selectAll2() { // 多片上片 |
| | | int cage1; |
| | | int cells; |
| | | int ids; |
| | | int prcid; |
| | | int tiers; |
| | | int prctier; |
| | | double width; |
| | | double minwidth = 0; |
| | | List<String> adddresslist = new ArrayList<>(); |
| | | adddresslist.add("DB105.0");// 进片车起始位置 |
| | | adddresslist.add("DB105.2");// 进片车目标位置 |
| | | adddresslist.add("DB105.4");// 进片笼玻璃数 |
| | | adddresslist.add("DB105.12");// 进片车启动 1为启动 |
| | | List<Short> datas = new ArrayList<>(); |
| | | |
| | | // 获取多片序列玻璃宽度 |
| | | StorageCage queGlass = spianMapper.SelectQueueWidth(); |
| | | double widths = queGlass.getWidth(); |
| | | String orderids = queGlass.getOrderId(); |
| | | int num = spianMapper.SelectQueOrder(); |
| | | if (num == 1) { |
| | | |
| | | // 获取订单相关度最高的笼子排序 |
| | | List<StorageCage> storageCage = spianMapper.selectAll(orderids); |
| | | if (storageCage == null) { |
| | | return (400); |
| | | } |
| | | for (StorageCage storageCage2 : storageCage) { |
| | | // 保存订单优先顺序笼子号 |
| | | cage1 = storageCage2.getCage(); |
| | | // 判断该笼子号相邻最大的空格数 |
| | | int cages = spianMapper.selectCage(cage1); |
| | | StorageCage cages1; |
| | | // 判断是否有合适完整空格 |
| | | cages1 = spianMapper.selectqueCage1(cage1, storageCage2.getCell(), widths); |
| | | // 判断选中笼子是否有合适宽度空格 |
| | | // 有合适空格时进片 |
| | | if (cages1 != null && cages >= 1) { |
| | | ids = cages1.getId();// 数据库ID |
| | | cells = cages1.getCell();// 格子号 |
| | | prcid = cages1.getPrcId();// 传给prc的目标地id |
| | | String glassid=""; |
| | | // 数据库修改笼子表新增一条玻璃 |
| | | for (int i = 2; i > 0; i--) { |
| | | north_glass_buffer1 qglass = spianMapper.SelectQueueGlass(i); |
| | | width = qglass.getglasslengthmm();// 格子剩余宽度 |
| | | glassid=qglass.getbarcode(); |
| | | spianMapper.UpdataAddqueCage(qglass.getordernumber(), qglass.getbarcode(), width, cage1, cells, i,1); |
| | | spianMapper.UpdataAddCage1(width, cage1, cells);// 减少格子宽度 |
| | | spianMapper.Inserttask(0, 0, 1000, ids, qglass.getbarcode().toString());// 新增任务 |
| | | } |
| | | // 执行进片 |
| | | datas.add((short) 1000); |
| | | datas.add((short) prcid); |
| | | datas.add((short) 0); |
| | | datas.add((short) 1); |
| | | S7control.getinstance().WriteWord(adddresslist, datas); |
| | | outmesid(glassid,"DB105.20"); |
| | | return (200); |
| | | |
| | | } |
| | | } |
| | | |
| | | } else { |
| | | for (int i = 2; i > 0; i--) { |
| | | north_glass_buffer1 qglass = spianMapper.SelectQueueGlass(i); |
| | | String orderid = qglass.getordernumber().toString(); |
| | | String glassid = qglass.getbarcode(); |
| | | widths = qglass.getglasslengthmm(); |
| | | List<StorageCage> storageCage = spianMapper.selectAll(orderid); |
| | | if (storageCage == null) { |
| | | return (400); |
| | | } |
| | | for (StorageCage storageCage2 : storageCage) { |
| | | // 保存订单优先顺序笼子号 |
| | | cage1 = storageCage2.getCage(); |
| | | // 判断该笼子号相邻最大的空格数 |
| | | int cages = spianMapper.selectCage(cage1); |
| | | StorageCage cages1; |
| | | // 判断是否是第二片需要加间隔物 |
| | | prctier = spianMapper.selectsum(cage1, storageCage2.getCell());// 传给prc的格子内玻璃数 |
| | | if (prctier > 0) { |
| | | cages1 = spianMapper.selectCage1(cage1, storageCage2.getCell(), widths + 400); |
| | | } else { |
| | | cages1 = spianMapper.selectCage1(cage1, storageCage2.getCell(), widths); |
| | | } |
| | | // 判断选中笼子是否有合适宽度空格 |
| | | // 有合适空格时进片 |
| | | if (cages1 != null && cages >= 1) { |
| | | ids = cages1.getId();// 数据库ID |
| | | tiers = cages1.getTier();// 内外片 |
| | | cells = cages1.getCell();// 格子号 |
| | | prcid = cages1.getPrcId();// 传给prc的目标地id |
| | | width = cages1.getWidth();// 格子剩余宽度 |
| | | widths = qglass.getglasslengthmm(); |
| | | |
| | | // 执行进片 |
| | | datas.add((short) 1000); |
| | | datas.add((short) prcid); |
| | | datas.add((short) prctier); |
| | | datas.add((short) 1); |
| | | // 数据库修改笼子表新增一条玻璃 |
| | | spianMapper.UpdataAddCage(orderid, glassid, widths, cage1, cells, ids, 2); |
| | | spianMapper.UpdataAddCage1(widths, cage1, cells);// 减少格子宽度 |
| | | spianMapper.Inserttask(0, 0, 1000, ids, glassid.toString());// 新增任务 |
| | | outmesid(glassid,"DB105.20");//派发进片id |
| | | // S7control.getinstance().WriteWord(adddresslist, datas); |
| | | break; |
| | | // S7control.getinstance().WriteByte("DB105.16",glassid); |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | return (400); |
| | | |
| | | } |
| | | |
| | | // 按订单出片 |
| | | public Result selectout(String orderid) { |
| | | |
| | | // spianMapper.UpdatetaskOut(); //完成上一次出片或者调度任务,改到plchold里,出片或调度任务完成立即改变结束任务; |
| | | // 定义PRC数据传送数组 |
| | | List<String> adddresslist = new ArrayList<>(); |
| | | adddresslist.add("DB105.10");// 出片车起始位置 |
| | | adddresslist.add("DB105.12");// 出片车目标位置 |
| | | adddresslist.add("DB105.14");// 出片笼玻璃数 |
| | | adddresslist.add("DB105.18");// 出片车启动 1为启动 |
| | | List<Short> datas = new ArrayList<>(); |
| | | // 获取优先出片的位置 |
| | | StorageCage cageout = spianMapper.selectOut(orderid); |
| | | if (cageout == null) { |
| | | return Result.success("200"); |
| | | } |
| | | // 判断是否同一格内都为同一订单 |
| | | |
| | | int orders = Integer.valueOf(cageout.getOrderId()).intValue(); |
| | | |
| | | int cage = cageout.getCage(); // 储存出片位置,笼子格子几号玻璃 |
| | | int cell = cageout.getCell();// 出片格号 |
| | | int tier = cageout.getTier();// 出片内外片 |
| | | int prcid = cageout.getPrcId();// prcid |
| | | String glassid = cageout.getGlassId(); |
| | | int prcid2; |
| | | int ids; |
| | | int cages; |
| | | int cells; |
| | | double glasswidth = cageout.getGlassWidth(); |
| | | // 当一格内都为同一订单时 |
| | | |
| | | if (orders == 2) { |
| | | datas.add((short) prcid); |
| | | datas.add((short) 1000); |
| | | datas.add((short) 2); |
| | | datas.add((short) 1); |
| | | spianMapper.UpdatequeOut(cage, cell); |
| | | return Result.success("200"); |
| | | } else { |
| | | // 判断是否除此格子内还有其他格子有单独的订单玻璃 |
| | | StorageCage pd = spianMapper.SelectQueout(orderid,cage, cell); |
| | | // 判断玻璃内外片 |
| | | if (tier == 2) { |
| | | |
| | | // 判断玻璃可直接出片时 |
| | | // 修改数据库笼子表(出片) |
| | | datas.add((short) prcid); |
| | | datas.add((short) 1000); |
| | | datas.add((short) 1); |
| | | datas.add((short) 1); |
| | | |
| | | spianMapper.UpdataOutCage1(glasswidth, cage, cell);// 增加原格子宽度 |
| | | spianMapper.UpdataGlassCageState(glassid,3);//更改笼子表的玻璃状态 |
| | | spianMapper.Inserttask(1, 0, cageout.getId(), 1000, orderid);// 新增任务 |
| | | S7control.getinstance().WriteWord(adddresslist, datas);// 派发出片车任务 |
| | | outmesid(glassid,"DB105.34");// 派发出片ID |
| | | if (pd != null) { |
| | | cage = pd.getCage(); |
| | | cell = pd.getCage(); |
| | | // 清除本格数据出片 |
| | | spianMapper.UpdatequeOut(cage, cell); |
| | | return Result.success("200"); |
| | | } |
| | | return Result.success(datas);// 结束 |
| | | } else { |
| | | // 获取格子的玻璃数量 |
| | | int state = spianMapper.selectGlassState(cage, cell); |
| | | // 判断内片是否需要调拨 |
| | | if (state == 1) { |
| | | // spianMapper.UpdataGlassCage("", "", glasswidth, cage, cell, tier, 0); |
| | | datas.add((short) prcid); |
| | | datas.add((short) 1000); |
| | | datas.add((short) 1); |
| | | datas.add((short) 1); |
| | | spianMapper.Inserttask(1, 0,prcid, 1000, orderid);// 新增任务 |
| | | spianMapper.UpdataOutCage1(glasswidth, cage, cell);// 增加原格子宽度 |
| | | spianMapper.UpdataGlassCageState(glassid,3);//更改笼子表的玻璃状态 |
| | | //S7control.getinstance().WriteWord(adddresslist, datas);// 派发出片车任务 |
| | | //outmesid(glassid);// 派发出片ID |
| | | if (pd != null) { |
| | | cage = pd.getCage(); |
| | | cell = pd.getCage(); |
| | | // 清除本格数据出片 |
| | | spianMapper.UpdatequeOut(cage, cell); |
| | | return Result.success("200"); |
| | | } |
| | | return Result.success(datas);// 结束 |
| | | |
| | | } else { |
| | | // 玻璃需要调拨时,判断属于哪个半区的笼子 |
| | | if (cage < 6) { |
| | | StorageCage cagecell = spianMapper.selectGlassCage(cage, glasswidth, 0, 6); |
| | | if (cagecell == null) { |
| | | return Result.success(cagecell); |
| | | } |
| | | // 获取调拨位置进行调拨 |
| | | prcid2 = cagecell.getPrcId();// 调拨目标位PRCID |
| | | ids = cagecell.getId();// 调拨目标位ID |
| | | cages = cagecell.getCage();// 调拨目标位笼子 |
| | | cells = cagecell.getCell();// 调拨目标位格子 |
| | | |
| | | // 填入调拨数据 |
| | | datas.add((short) prcid);// 调拨外片起始位置 |
| | | datas.add((short) prcid2); |
| | | datas.add((short) 2); |
| | | datas.add((short) 1); |
| | | // 更换玻璃的笼子 |
| | | String glassids = spianMapper.SelectGlassid(cage, cell); // 获取被调拨的玻璃id |
| | | spianMapper.UpdateDBCage(ids, cage, cell,2);// 将原格子数据更新到新格子里 |
| | | spianMapper.UpdataAddCage1(glasswidth, cages, cells);// 减少新格子宽度 |
| | | spianMapper.UpdataOutCage1(glasswidth, cage, cell);// 增加原格子宽度 |
| | | spianMapper.UpdataGlassCageState(glassid,3);//更改笼子表的玻璃状态 |
| | | spianMapper.Inserttask(2, 0, cageout.getId(), ids, orderid);// 新增调度任务 |
| | | // 传输mes数据,坐标,数据 |
| | | S7control.getinstance().WriteWord(adddresslist, datas);// 派发出片车任务 |
| | | System.out.println(datas); |
| | | outmesid(glassids,"DB105.34");// 派发调拨玻璃ID |
| | | return Result.success(cagecell); // 结束 |
| | | |
| | | } else {// 笼子号大于5时 |
| | | StorageCage cagecell = spianMapper.selectGlassCage(cage, glasswidth, 5, 11); |
| | | if (cagecell == null) { |
| | | return Result.success(cagecell); |
| | | } |
| | | // 获取调拨位置进行调拨 |
| | | prcid2 = cagecell.getPrcId();// 调拨目标位prcID |
| | | ids = cagecell.getId();// 调拨目标位ID |
| | | cages = cagecell.getCage();// 调拨目标位笼子 |
| | | cells = cagecell.getCell();// 调拨目标位格子 |
| | | // 填入调拨数据 |
| | | datas.add((short) prcid); |
| | | datas.add((short) prcid2); |
| | | datas.add((short) 2); |
| | | datas.add((short) 1); |
| | | String glassids = spianMapper.SelectGlassid(cage, cell); // 获取被调拨的玻璃id |
| | | spianMapper.UpdateDBCage(ids, cage, cell,2);// 将原格子数据更新到新格子里 |
| | | spianMapper.Inserttask(2, 0, cageout.getId(), ids, orderid);// 新增调度任务 |
| | | spianMapper.UpdataAddCage1(glasswidth, cages, cells);// 减少新格子宽度 |
| | | spianMapper.UpdataOutCage1(glasswidth, cage, cell);// 增加原格子宽度 |
| | | spianMapper.UpdataGlassCageState(glassid,3);//更改笼子表的玻璃状态 |
| | | // 传输mes数据,坐标,数据 |
| | | S7control.getinstance().WriteWord(adddresslist, datas);// 派发出片车任务 |
| | | |
| | | outmesid(glassids,"DB105.34");// 派发调拨玻璃ID |
| | | return Result.success(cagecell); // 结束 |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | |