zhangqian
2023-11-01 443fb992699818305e3d7f6885ca7d1d36fbe7cd
增加系统运行时间,系统状态,集群状态,集群节点数
9个文件已修改
199 ■■■■■ 已修改文件
api/v1/device.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/config.go 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
crontask/cron_task.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/response/common.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
serf/sync.go 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/cmd.go 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/device.go
@@ -76,6 +76,10 @@
        SystemDeviceID:  conf.Conf.System.DeviceId,
        CurrentDeviceID: conf.Conf.CurrentDeviceID,
        DeviceIDList:    list,
        SystemDeviceStatus:   response.SystemDeviceStatusNormal,
        ClusterStatus:        conf.Conf.SerfClusterStatus,
        ClusterNodeQuantity:  conf.Conf.ClusterNodeQuantity,
        SystemDeviceRunSince: conf.Conf.SystemDeviceRunSince,
    }
    ctx.OkWithDetailed(resp)
}
conf/config.go
@@ -4,8 +4,11 @@
    "apsClient/pkg/convertx"
    "apsClient/pkg/logx"
    "apsClient/pkg/sqlitex"
    "apsClient/pkg/timex"
    "apsClient/utils"
    "github.com/spf13/viper"
    "log"
    "time"
)
var (
@@ -104,7 +107,11 @@
        CurrentDeviceID string //设置当前面板控制的设备
        SerfClusterStatus string
        SerfClusterStatus string //集群状态
        ClusterNodeQuantity int //集群节点数量
        SystemDeviceRunSince int64 //系统开始运行时间戳
    }
)
@@ -130,6 +137,19 @@
    if Conf.PLC.StandbyTime == 0 {
        Conf.PLC.StandbyTime = 300 //5分钟
    }
    uptimeStr, err := utils.Exec("uptime -s")
    if err == nil {
        t, err := timex.StringToTime(uptimeStr)
        if err == nil {
            Conf.SystemDeviceRunSince = t.Unix()
        }
    } else {
        Conf.SystemDeviceRunSince = time.Now().Unix()
    }
    SetUpTime()
    ShowConfig()
}
@@ -150,3 +170,15 @@
    log.Printf("   services :                  %+v", Conf.Services)
    log.Println("......................................................")
}
func SetUpTime() {
    uptimeStr, err := utils.Exec("uptime -s")
    if err == nil {
        t, err := timex.StringToTime(uptimeStr)
        if err == nil {
            Conf.SystemDeviceRunSince = t.Unix()
        }
    } else {
        Conf.SystemDeviceRunSince = time.Now().Unix()
    }
}
crontask/cron_task.go
@@ -7,6 +7,7 @@
    "apsClient/nsq"
    "apsClient/pkg/ecode"
    "apsClient/pkg/logx"
    "apsClient/serf"
    "apsClient/service"
    "fmt"
    "github.com/go-co-op/gocron"
@@ -79,6 +80,9 @@
        s.Every(60).Seconds().Do(SyncProductionProgress) //同步生产数据
        s.Every(30).Seconds().Do(SyncTaskStatus)         //同步任务状态
    }
    s.Every(10).Seconds().Do(QueryClusterStatus) //查询集群节点数量
    s.StartAsync()
    return nil
}
@@ -152,3 +156,9 @@
        logx.Errorf("send pull data msg error:%v, msg:%+v", err.Error(), msg)
    }
}
func QueryClusterStatus() {
    clusterStatus, nodeQuantity := serf.QueryClusterStatusAndNodeQuantity()
    conf.Conf.SerfClusterStatus = clusterStatus
    conf.Conf.ClusterNodeQuantity = nodeQuantity
}
docs/docs.go
@@ -1482,6 +1482,14 @@
        "response.DeviceListResponse": {
            "type": "object",
            "properties": {
                "clusterNodeQuantity": {
                    "description": "集群节点数量",
                    "type": "integer"
                },
                "clusterStatus": {
                    "description": "集群状态",
                    "type": "string"
                },
                "currentDeviceID": {
                    "description": "当前选定的生产设备",
                    "type": "string"
@@ -1496,6 +1504,18 @@
                "systemDeviceID": {
                    "description": "工控机设备ID",
                    "type": "string"
                },
                "systemDeviceRunSince": {
                    "description": "系统运行开始时间戳",
                    "type": "integer"
                },
                "systemDeviceStatus": {
                    "description": "设备状态",
                    "allOf": [
                        {
                            "$ref": "#/definitions/response.SystemDeviceStatus"
                        }
                    ]
                }
            }
        },
@@ -1551,6 +1571,21 @@
                }
            }
        },
        "response.SystemDeviceStatus": {
            "type": "integer",
            "enum": [
                1,
                2
            ],
            "x-enum-comments": {
                "SystemDeviceStatusNormal": "正常",
                "SystemDeviceStatusUnNormal": "异常"
            },
            "x-enum-varnames": [
                "SystemDeviceStatusNormal",
                "SystemDeviceStatusUnNormal"
            ]
        },
        "response.TaskCountdown": {
            "type": "object",
            "properties": {
docs/swagger.json
@@ -1470,6 +1470,14 @@
        "response.DeviceListResponse": {
            "type": "object",
            "properties": {
                "clusterNodeQuantity": {
                    "description": "集群节点数量",
                    "type": "integer"
                },
                "clusterStatus": {
                    "description": "集群状态",
                    "type": "string"
                },
                "currentDeviceID": {
                    "description": "当前选定的生产设备",
                    "type": "string"
@@ -1484,6 +1492,18 @@
                "systemDeviceID": {
                    "description": "工控机设备ID",
                    "type": "string"
                },
                "systemDeviceRunSince": {
                    "description": "系统运行开始时间戳",
                    "type": "integer"
                },
                "systemDeviceStatus": {
                    "description": "设备状态",
                    "allOf": [
                        {
                            "$ref": "#/definitions/response.SystemDeviceStatus"
                        }
                    ]
                }
            }
        },
@@ -1539,6 +1559,21 @@
                }
            }
        },
        "response.SystemDeviceStatus": {
            "type": "integer",
            "enum": [
                1,
                2
            ],
            "x-enum-comments": {
                "SystemDeviceStatusNormal": "正常",
                "SystemDeviceStatusUnNormal": "异常"
            },
            "x-enum-varnames": [
                "SystemDeviceStatusNormal",
                "SystemDeviceStatusUnNormal"
            ]
        },
        "response.TaskCountdown": {
            "type": "object",
            "properties": {
docs/swagger.yaml
@@ -491,6 +491,12 @@
    type: object
  response.DeviceListResponse:
    properties:
      clusterNodeQuantity:
        description: 集群节点数量
        type: integer
      clusterStatus:
        description: 集群状态
        type: string
      currentDeviceID:
        description: 当前选定的生产设备
        type: string
@@ -502,6 +508,13 @@
      systemDeviceID:
        description: 工控机设备ID
        type: string
      systemDeviceRunSince:
        description: 系统运行开始时间戳
        type: integer
      systemDeviceStatus:
        allOf:
        - $ref: '#/definitions/response.SystemDeviceStatus'
        description: 设备状态
    type: object
  response.ListResponse:
    properties:
@@ -537,6 +550,17 @@
      totalNumber:
        type: integer
    type: object
  response.SystemDeviceStatus:
    enum:
    - 1
    - 2
    type: integer
    x-enum-comments:
      SystemDeviceStatusNormal: 正常
      SystemDeviceStatusUnNormal: 异常
    x-enum-varnames:
    - SystemDeviceStatusNormal
    - SystemDeviceStatusUnNormal
  response.TaskCountdown:
    properties:
      countDownHour:
model/response/common.go
@@ -70,8 +70,19 @@
    Data  interface{}
}
type SystemDeviceStatus int
const (
    SystemDeviceStatusNormal   SystemDeviceStatus = 1 //正常
    SystemDeviceStatusUnNormal SystemDeviceStatus = 2 //异常
)
type DeviceListResponse struct {
    SystemDeviceID  string   `json:"systemDeviceID,omitempty"`  //工控机设备ID
    CurrentDeviceID string   `json:"currentDeviceID,omitempty"` //当前选定的生产设备
    DeviceIDList    []string `json:"deviceIDList,omitempty"`    //生产设备id列表
    SystemDeviceStatus   SystemDeviceStatus `json:"systemDeviceStatus"`        //设备状态
    ClusterStatus        string             `json:"clusterStatus"`             //集群状态
    ClusterNodeQuantity  int                `json:"clusterNodeQuantity"`       //集群节点数量
    SystemDeviceRunSince int64              `json:"systemDeviceRunSince"`      //系统运行开始时间戳
}
serf/sync.go
@@ -5,6 +5,7 @@
    "context"
    "encoding/json"
    "fmt"
    "github.com/mitchellh/mapstructure"
    "os"
    "os/signal"
    "regexp"
@@ -311,7 +312,7 @@
}
// 查询集群状态, 返回 master, slave, leave
func (ss *SyncServer) QueryClusterStat() string {
func (ss *SyncServer) QueryClusterStat() *bhomeclient.Reply {
    clusterStatTopic := "/data/api-v/cluster/status"
    req := bhomeclient.Request{
        Path:   clusterStatTopic,
@@ -322,14 +323,14 @@
    if err != nil {
        fmt.Println("RequestTopic error", err.Error())
        return ""
        return reply
    }
    ss.ClusterStatus = reply.Msg
    logx.Debugf("当前集群状态: %s", ss.ClusterStatus)
    return reply.Msg
    return reply
}
func (ss *SyncServer) handleDbLoggerPrint() {
@@ -519,3 +520,28 @@
    return ""
}
type NodeInfo struct {
    NodeID     string `json:"node_id,omitempty"`
    NodeIp     string `json:"node_ip,omitempty"`
    NodeName   string `json:"node_name,omitempty"`
    ClusterID  string `json:"cluster_id"`
    CreateTime string `json:"create_time"`
    DeviceType string `json:"device_type"`
    DriftState string `json:"drift_state"`
    Online     string `json:"online"`
}
func QueryClusterStatusAndNodeQuantity() (string, int) {
    reply := agent.QueryClusterStat()
    if reply == nil {
        return "", 0
    }
    var nodes []NodeInfo
    err := mapstructure.Decode(reply.Data, &nodes)
    if err != nil {
        logx.Errorf("mapstructure.Decode QueryClusterStat data err:%v", err)
        return reply.Msg, 0
    }
    return reply.Msg, len(nodes)
}
utils/cmd.go
@@ -26,3 +26,17 @@
    }
    return nil
}
func Exec(command string) (outputStr string, err error) {
    cmd := exec.Command(command)
    // 捕获命令的输出
    output, err := cmd.Output()
    if err != nil {
        return "", fmt.Errorf("命令执行失败: %v", err)
    }
    // 将输出转换为字符串
    outputStr = string(output)
    return
}