Browse Source

增加设备更新接口

liuxiulin 6 months ago
parent
commit
b7bf45f65f

+ 7 - 0
pkg/models/device.go

@@ -28,6 +28,8 @@ type Device struct {
 	VendorID string `gorm:"column:vendor_id;size:32;index"`
 	//通讯模组名称
 	ModuleName string
+	// 备注
+	Memo string `sql:"type:varchar(50);"`
 }
 
 // DeviceQuery device query
@@ -42,6 +44,11 @@ type Devices struct {
 	Status int
 }
 
+// Validate 验证
+func (a *Devices) Validate() error {
+	return nil
+}
+
 // DeviceChartData 设备数据图表
 type DeviceChartData struct {
 	Dt    string

+ 15 - 0
services/knowoapi/controllers/device.go

@@ -477,3 +477,18 @@ func (a *DeviceController) PostMixedwaterlinkage() {
 	}
 	done(a.Ctx, params.DeviceId)
 }
+
+// Put 更新
+// PUT /device
+func (a *DeviceController) Put() {
+	params := new(models.Devices)
+	if err := parseBody(a.Ctx, params); err != nil {
+		badRequest(a.Ctx, err)
+		return
+	}
+	if err := a.Service.Update(params); err != nil {
+		responseError(a.Ctx, ErrDatabase, err.Error())
+		return
+	}
+	done(a.Ctx, "已保存")
+}

+ 28 - 0
services/knowoapi/model/device.go

@@ -1,6 +1,7 @@
 package model
 
 import (
+	"fmt"
 	"sparrow/pkg/models"
 	"time"
 
@@ -165,3 +166,30 @@ func (a *Device) Query(vendorId, productId string, deviceIDs []string) (datas []
 
 	return
 }
+
+// Get 获取数据内容
+func (a *Device) Get(vendorId string, recordId string) (data models.Device, err error) {
+	cache := getCache()
+	key := fmt.Sprintf("OtaId:%s", recordId)
+	if v, ok := cache.Get(key); ok {
+		_d := v.(*models.Device)
+		data = *_d
+	} else {
+		err = a.db.Where("vendor_id = ? and record_id = ?", vendorId, recordId).First(&data).Error
+		if err == nil {
+			cache.Set(key, &data)
+		}
+	}
+	return
+}
+
+// Update update
+func (a *Device) Update(Device *models.Devices) (data models.Device, err error) {
+	cache := getCache()
+	key := fmt.Sprintf("Device:%d", Device.ID)
+	if _, ok := cache.Get(key); ok {
+		cache.Delete(key)
+	}
+	err = a.db.Model(&data).Update(Device).Error
+	return
+}

+ 9 - 1
services/knowoapi/services/device.go

@@ -25,7 +25,9 @@ type DeviceService interface {
 	GetDevices(vendorid, proid string, pi, ps int, deviceid string) ([]*models.Devices, int, error)
 	//获取用户下所有设备的数量,在线设备的数量,离线设备的数量
 	GetDevicesCountByVenderId(vendorid string) (map[string]interface{}, error)
-	// 发起设备OTA升级
+	// Update 更新设备信息
+	Update(Device *models.Devices) error
+	// Upgrade 发起设备OTA升级
 	Upgrade(params *models.UpgradeParams) error
 	// GetUpgradeProgress 获取ota升级进度
 	GetUpgradeProgress(deviceId string) (rpcs.ReplyOtaProgress, error)
@@ -67,6 +69,12 @@ func NewDeviceService(models *model.All) DeviceService {
 		models: models,
 	}
 }
+
+func (a deviceservice) Update(Device *models.Devices) error {
+	_, err := a.models.Device.Update(Device)
+	return err
+}
+
 func (a deviceservice) GetDevices(vendorid, proid string, pi, ps int, deviceid string) ([]*models.Devices, int, error) {
 
 	data, total, err := a.models.Device.GetDevices(vendorid, proid, pi, ps, deviceid)

+ 1 - 0
services/registry/registry.go

@@ -79,6 +79,7 @@ func setDevice(target *models.Device, src *models.Device) {
 	target.CreatedAt = src.CreatedAt
 	target.UpdatedAt = src.UpdatedAt
 	target.VendorID = src.VendorID
+	target.Memo = src.Memo
 }
 
 //func setSubDevice(target *models.SubDevice, src *models.SubDevice) {