wuyouming666
2023-12-14 2a6d8a858ca7e41436548b35db147b5cdad4e4be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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]);
              }
            }
          }
        }
      }
    }