| | |
| | | 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(10000); |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | List<Short> paramlist = S7control.getinstance().ReadWord("DB100.DBW", 12); |
| | | this.readValue(); |
| | | String PlcAddress = this.name; |
| | | Integer Plccount = this.count; |
| | | // System.out.println(PlcAddress); |
| | | |
| | | // JSONObject jsonObject = new JSONObject(); |
| | | // // jsonObject.append("params", paramlist); |
| | | // jsonObject.append("params", new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| | | // 11, }); |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | List<Boolean> plclist = S7control.getinstance().ReadBits(PlcAddress, Plccount); |
| | | // List<Boolean> plclist2 = S7control.getinstance().ReadBits("DB102.5.5", 13); |
| | | |
| | | JSONObject jsonObject3 = new JSONObject(); |
| | | jsonObject3.append("sig", |
| | | new short[] { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, |
| | | 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, }); |
| | | WebSocketServer sendwServer3 = WebSocketServer.sessionMap.get("Sign"); |
| | | if (sendwServer3 != null) { |
| | | sendwServer3.sendMessage(jsonObject3.toString()); |
| | | } |
| | | // 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)); |
| | | |
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Sign"); |
| | | if (webSocketServer != null) { |
| | | List<String> messages = webSocketServer.getMessages(); |
| | | |
| | | String addressList = "DB100.DBW0"; |
| | | // addressList.add("DB100.DBW0"); |
| | | // addressList.add("DB100.DBW2"); |
| | | // addressList.add("DB100.DBW4"); |
| | | // addressList.add("DB100.DBW6"); |
| | | // addressList.add("DB100.DBW8"); |
| | | // addressList.add("DB100.DBW10"); |
| | | // addressList.add("DB100.DBW12"); |
| | | // addressList.add("DB100.DBW14"); |
| | | // addressList.add("DB100.DBW16"); |
| | | // addressList.add("DB100.DBW18"); |
| | | // addressList.add("DB100.DBW20"); |
| | | // addressList.add("DB100.DBW22"); |
| | | |
| | | 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(); |
| | | } |
| | | if (plclist != null) { |
| | | // 将获取的布尔类型转换为整数类型 |
| | | List<Integer> Intlist = new ArrayList<>(); |
| | | for (Boolean value : plclist) { |
| | | if (value != null) { |
| | | Intlist.add(value == true ? 0 : 1); |
| | | } |
| | | |
| | | // 将消息值写入 PLC |
| | | // S7control.getinstance().WriteWord(addressList, messageValues); |
| | | System.out.println("messageValues:" + messageValues); |
| | | System.out.println("addressList:" + addressList); |
| | | // 清空消息列表 |
| | | webSocketServer.clearMessages(); |
| | | } |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.append("sig", Intlist); |
| | | WebSocketServer sendwServer3 = WebSocketServer.sessionMap.get("Sign"); |
| | | if (sendwServer3 != null) { |
| | | sendwServer3.sendMessage(jsonObject.toString()); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |