mirror of
https://github.com/lkmio/gb-cms.git
synced 2025-09-26 19:51:22 +08:00
fix: 错误计算设备总数问题
This commit is contained in:
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"gb-cms/common"
|
||||
"gb-cms/dao"
|
||||
"gb-cms/log"
|
||||
"gb-cms/stack"
|
||||
"net/http"
|
||||
@@ -132,7 +131,7 @@ func registerLiveGBSApi() {
|
||||
ChannelOnline: ChannelOnlineCount,
|
||||
ChannelTotal: ChannelTotalCount,
|
||||
DeviceOnline: stack.OnlineDeviceManager.Count(),
|
||||
DeviceTotal: dao.DeviceCount,
|
||||
DeviceTotal: DeviceCount,
|
||||
}
|
||||
|
||||
_ = common.HttpResponseSuccess(writer, response)
|
||||
|
@@ -8,10 +8,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
DeviceCount int
|
||||
)
|
||||
|
||||
type DeviceModel struct {
|
||||
GBModel
|
||||
DeviceID string `json:"device_id" gorm:"index"`
|
||||
@@ -64,7 +60,6 @@ func (d *daoDevice) LoadDevices() (map[string]*DeviceModel, error) {
|
||||
deviceMap[device.DeviceID] = device
|
||||
}
|
||||
|
||||
DeviceCount = len(devices)
|
||||
return deviceMap, nil
|
||||
}
|
||||
|
||||
@@ -77,11 +72,7 @@ func (d *daoDevice) SaveDevice(device *DeviceModel) error {
|
||||
|
||||
if device.ID == 0 {
|
||||
//return tx.Create(&old).Error
|
||||
err := tx.Save(device).Error
|
||||
if err == nil {
|
||||
DeviceCount++
|
||||
}
|
||||
return err
|
||||
return tx.Save(device).Error
|
||||
} else {
|
||||
return tx.Model(device).Select("Transport", "RemoteIP", "RemotePort", "Status", "RegisterTime", "LastHeartbeat").Updates(*device).Error
|
||||
}
|
||||
@@ -109,11 +100,6 @@ func (d *daoDevice) UpdateDeviceInfo(deviceId string, device *DeviceModel) error
|
||||
|
||||
func (d *daoDevice) UpdateDeviceStatus(deviceId string, status common.OnlineStatus) error {
|
||||
return DBTransaction(func(tx *gorm.DB) error {
|
||||
if status == common.ON {
|
||||
DeviceCount++
|
||||
} else if status == common.OFF {
|
||||
DeviceCount--
|
||||
}
|
||||
return tx.Model(&DeviceModel{}).Where("device_id =?", deviceId).Update("status", status).Error
|
||||
})
|
||||
}
|
||||
@@ -237,3 +223,9 @@ func (d *daoDevice) DeleteDevicesByUA(ua string) error {
|
||||
return tx.Where("user_agent =?", ua).Unscoped().Delete(&DeviceModel{}).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (d *daoDevice) Count() (int, error) {
|
||||
var count int64
|
||||
db.Model(&DeviceModel{}).Count(&count)
|
||||
return int(count), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user