zhangqian
2023-12-20 061c52572b3099cf5fee70245981804b9ca4bc6a
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package problem
 
import (
    "apsClient/conf"
    "apsClient/constvar"
    "apsClient/model"
    "apsClient/pkg/sqlitex"
    "apsClient/service"
    "fmt"
    "net"
    "sync"
)
 
var checkItems = []constvar.ProblemCode{
    constvar.ProblemCodeService,
    constvar.ProblemCodeNetwork,
    constvar.ProblemCodeDB,
    constvar.ProblemCodeSerf,
    constvar.ProblemCodeCloud,
    constvar.ProblemCodeDevice,
    constvar.ProblemCodePlcConfig,
    constvar.ProblemCodePlcAddressList,
    constvar.ProblemCodePlcProcessModelAddressList,
    constvar.ProblemCodePlcConnect,
}
var itemNameMap = map[constvar.ProblemCode]string{
    constvar.ProblemCodeService:                    "服务",
    constvar.ProblemCodeNetwork:                    "网络连接",
    constvar.ProblemCodeDB:                         "数据库",
    constvar.ProblemCodeSerf:                       "serf集群",
    constvar.ProblemCodeCloud:                      "消息队列",
    constvar.ProblemCodeDevice:                     "生产设备绑定",
    constvar.ProblemCodePlcConfig:                  "plc配置",
    constvar.ProblemCodePlcAddressList:             "plc地址表",
    constvar.ProblemCodePlcProcessModelAddressList: "plc工艺参数地址表",
    constvar.ProblemCodePlcConnect:                 "plc连接",
}
 
type CheckResult struct {
    ItemCode    constvar.ProblemCode
    ItemName    string
    CheckResult bool
}
 
var checkResultList []*CheckResult
var mutex sync.RWMutex
 
func Check() {
    checkResultListTemp := make([]*CheckResult, 0)
    var checkObj check
    for _, item := range checkItems {
        switch item {
        case constvar.ProblemCodeService:
            checkObj = &Default{}
        case constvar.ProblemCodeNetwork:
            checkObj = &Network{}
        case constvar.ProblemCodeDB:
            checkObj = &DB{}
        case constvar.ProblemCodeSerf:
            checkObj = &Serf{}
        case constvar.ProblemCodeCloud:
            checkObj = &Nsq{}
        case constvar.ProblemCodeDevice:
            checkObj = &Device{}
        case constvar.ProblemCodePlcConfig:
            checkObj = &PlcConfig{}
        case constvar.ProblemCodePlcAddressList:
            checkObj = &PlcAddressList{}
        case constvar.ProblemCodePlcProcessModelAddressList:
            checkObj = &PlcProcessModelAddressList{}
        case constvar.ProblemCodePlcConnect:
            checkObj = &PlcConnect{}
        default:
            continue
        }
        checkResultListTemp = append(checkResultListTemp, &CheckResult{
            ItemCode:    item,
            ItemName:    itemNameMap[item],
            CheckResult: checkObj.Check(),
        })
    }
    mutex.Lock()
    defer mutex.Unlock()
    checkResultList = checkResultListTemp
 
}
 
func Get() []*CheckResult {
    mutex.RLock()
    defer mutex.RUnlock()
    return checkResultList
 
}
 
type check interface {
    Check() bool
}
 
type Default struct{}
 
func (slf *Default) Check() bool {
    return true
}
 
type Network struct{}
 
func (slf *Network) Check() bool {
    ifaces, err := net.Interfaces()
    if err != nil {
        fmt.Println("Error:", err)
        return false
    }
    var netAllFailed bool
    for _, iFace := range ifaces {
        if iFace.Flags == net.FlagUp {
            netAllFailed = false
            fmt.Printf("Interface %s is DOWN, indicating a network issue.\n", iFace.Name)
        }
    }
    return !netAllFailed
}
 
type DB struct{}
 
func (slf *DB) Check() bool {
    err := sqlitex.GetDB().DB().Ping()
    if err != nil {
        return false
    }
    return true
}
 
type Serf struct{}
 
func (slf *Serf) Check() bool {
    return conf.Conf.SerfClusterStatus != ""
}
 
type Nsq struct{}
 
func (slf *Nsq) Check() bool {
    old, err := model.NewSystemStatusSearch().SetKey(constvar.SystemStatusKeyNsq).First()
    if err != nil {
        return false
    }
    return old.Value == constvar.SystemStatusValueNormal
}
 
type Device struct{}
 
func (slf *Device) Check() bool {
    list, err := service.GetDeviceIDList()
    if err != nil {
        return false
    }
    return len(list) > 0
}
 
type ProcessModel struct {
}
 
func (slf *ProcessModel) Check() bool {
    return false
}
 
type PlcConfig struct{}
 
func (slf *PlcConfig) Check() bool {
    plcConfig, _ := service.NewDevicePlcService().GetDevicePlc()
    if plcConfig == nil {
        return false
    }
    if plcConfig.Method == "" {
        return false
    }
    if plcConfig.Method == constvar.PlcMethodModbusTCP && (plcConfig.Address == "" || plcConfig.Port == 0) {
        return false
    }
    if plcConfig.Method == constvar.PlcMethodSerial && (plcConfig.BaudRate == 0 || plcConfig.SerialName == "") {
        return false
    }
 
    if plcConfig.Method == constvar.PlcMethodModbusRTU && (plcConfig.DataBit == 0 || plcConfig.StopBit == 0 || plcConfig.Parity == 0) {
        return false
    }
    return true
}
 
type PlcAddressList struct{}
 
func (slf *PlcAddressList) Check() bool {
    plcConfig, _ := service.NewDevicePlcService().GetDevicePlc()
    return plcConfig != nil && len(plcConfig.Details) > 0
}
 
type PlcProcessModelAddressList struct{}
 
func (slf *PlcProcessModelAddressList) Check() bool {
    record, err := model.NewProcessModelPlcAddressSearch().SetDeviceID(conf.Conf.CurrentDeviceID).First()
    if err != nil {
        return false
    }
    if len(record.AddressList) == 0 {
        return false
    }
    return true
}
 
type PlcConnect struct{}
 
func (slf *PlcConnect) Check() bool {
    return service.PlcIsConnect()
}