Browse Source

修改在线状态接口

baozhensong 6 years ago
parent
commit
91d44d66f1

+ 0 - 5
pkg/models/device.go

@@ -32,11 +32,6 @@ type DeviceQuery struct {
 	ProductName string
 }
 
-type Devices struct {
-	Device
-	Status int
-}
-
 // DeviceChartData 设备数据图表
 type DeviceChartData struct {
 	Dt    string

+ 7 - 3
services/knowoapi/controllers/device.go

@@ -66,12 +66,16 @@ func (a *DeviceController) GetDevicestatus() {
 	err = server.RPCCallByName(nil, "devicemanager", "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
 	if err != nil {
 		server.Log.Errorf("get devie online status error: %v", err)
-		responseError(a.Ctx, ErrDatabase, err.Error())
-		return
+	}
+
+	var status int
+
+	if int(onlinereply.HeartbeatInterval) > 0 && len(onlinereply.ClientIP) > 0 {
+		status = 1
 	}
 
 	done(a.Ctx, map[string]interface{}{
-		"status": onlinereply,
+		"status": status,
 	})
 
 }

+ 4 - 31
services/knowoapi/services/device.go

@@ -2,8 +2,6 @@ package services
 
 import (
 	"sparrow/pkg/models"
-	"sparrow/pkg/rpcs"
-	"sparrow/pkg/server"
 	"sparrow/services/knowoapi/model"
 )
 
@@ -20,7 +18,7 @@ type DeviceService interface {
 	//获取近N日活跃设备数据
 	GetLivelyOfNumDays(uint, int) ([]map[string]interface{}, error)
 	//获取已经激活的设备列表
-	GetDevices(vendorid uint, proid, pi, ps int, deviceid string) ([]*models.Devices, int, error)
+	GetDevices(vendorid uint, proid, pi, ps int, deviceid string) ([]models.Device, int, error)
 }
 
 type deviceservice struct {
@@ -33,36 +31,11 @@ func NewDeviceService(models *model.All) DeviceService {
 		models: models,
 	}
 }
-func (a deviceservice) GetDevices(vendorid uint, proid, pi, ps int, deviceid string) ([]*models.Devices, int, error) {
+func (a deviceservice) GetDevices(vendorid uint, proid, pi, ps int, deviceid string) ([]models.Device, int, error) {
 
-	datas, total, err := a.models.Device.GetDevices(vendorid, proid, pi, ps, deviceid)
-
-	devicedatas := make([]*models.Devices, 0)
-
-	for _, device := range datas {
-		onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
-			Id: uint64(device.ID),
-		}
-
-		onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
-		err = server.RPCCallByName(nil, "devicemanager", "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
-		if err != nil {
-			server.Log.Errorf("get devie online status error: %v", err)
-		}
-
-		devices := new(models.Devices)
-
-		devices.Device = device
-
-		if int(onlinereply.HeartbeatInterval) > 0 {
-
-			devices.Status = 1
-		}
-		devicedatas = append(devicedatas, devices)
-	}
-
-	return devicedatas, total, err
+	return a.models.Device.GetDevices(vendorid, proid, pi, ps, deviceid)
 }
+
 func (a deviceservice) GetDeviceCount(vendorid uint) (int, error) {
 	return a.models.Device.GetDeviceCount(vendorid)
 }