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<Short> arraylist = S7control.getinstance().ReadWord("DB100.0", 12);
|
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());
|
}
|
|
WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter1");
|
if (webSocketServer != null) {
|
List<String> messages = webSocketServer.getMessages();
|
String addressList1 = "DB100.0";
|
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(addressList1, Arrays.asList(sValue));
|
System.out.println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList1);
|
} 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(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 (!mergedList.isEmpty()) {
|
S7control.getinstance().WriteWord(addressList3, mergedList);
|
System.out.println("messageValue:" + mergedList + " written to PLC at address " + addressList3);
|
}
|
|
// 清空消息列表
|
webSocketServer.clearMessages();
|
}
|
|
|
}
|
|
}
|
}
|
}
|