ZengTao
2023-11-01 ce7da2e967f19e7e5543c25e91b7a759a6f360a5
springboot-vue3/src/main/java/com/example/springboot/component/Plcalarm.java
@@ -3,6 +3,7 @@
import cn.hutool.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.example.springboot.mapper.AlarmMapper;
@@ -14,31 +15,39 @@
  public void run() {
    while (this != null) {
      try {
        Thread.sleep(10000);
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      List<Boolean> plclist = S7control.getinstance().ReadBits("DB104.DBx0.0", 40);
      if (plclist == null) {
      } else {
      // 根据地址读取PCL数据
      List<Boolean> plclist = S7control.getinstance().ReadBits("DB104.0.0", 40);
      // Boolean[] values = { 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 };
      // List<Boolean> plclist = new ArrayList<>(Arrays.asList(values));
      if (plclist != null) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.append("params", plclist);
        WebSocketServer sendwServer = WebSocketServer.sessionMap.get("alarm");
        if (sendwServer != null) {
          sendwServer.sendMessage(jsonObject.toString());
        }
        // 将读取plc数据的plclist转换为数组
        Short[] shuzu1 = plclist.toArray(new Short[0]);
        // Short[] shuzu1 = {
        // 0, 1, 0, 1, 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,
        // 1, 0, 1, 0, 1, 0, 1, 0, 1,
        // };
        // 将获取的布尔类型转换为整数类型
        List<Integer> Intlist = new ArrayList<>();
        for (Boolean value : plclist) {
          Intlist.add(value == true ? 1 : 0);
        }
        // 将Intlist转换为数组
        Integer[] shuzu1 = Intlist.toArray(new Integer[0]);
        // 定义的报警内容数组
        String[] shuzu = {
            "D01 VFD error",
            "D02 VFD error",
@@ -82,11 +91,15 @@
            "B02 OUT pos error",
        };
        alarmMapper = WebSocketServer.applicationContext.getBean(AlarmMapper.class);
        for (short i = 0; i < shuzu1.length; i++) {
        for (short i = 0; i < shuzu.length; i++) {
          // 查询对应报警信息结束时间为null的条数
          short result = alarmMapper.selectnullti(shuzu[i]);
          // 读取到PLC的值为1并且对应报警信息结束时间为null的条数的条数为0
          if (shuzu1[i] == 1 && result == 0) {
            // 填加一条报警信息,有开始时间
            alarmMapper.Insertalarm(shuzu[i]);
          } else if (shuzu1[i] == 0 && result > 0) {
            // 修改该条报警信息的结束时间
            alarmMapper.updatealarm(shuzu[i]);
          }
        }