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()
|
}
|