Skip to content

Commit

Permalink
feat: modbus point excel api CRUD (hootrhino#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
wan9bo7 authored Jul 6, 2023
1 parent a704eee commit 7888352
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
24 changes: 23 additions & 1 deletion plugin/http_server/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package httpserver
import (
"errors"

model "github.com/hootrhino/rulex/plugin/http_server/model"
"github.com/hootrhino/rulex/plugin/http_server/model"

"gorm.io/gorm"
)
Expand Down Expand Up @@ -228,6 +228,10 @@ func (s *HttpApiServer) UpdateDevice(uuid string, o *model.MDevice) error {
}
}

// -------------------------------------------------------------------------------------
// ModbusPointPositions
// -------------------------------------------------------------------------------------

// InsertModbusPointPosition 插入modbus点位表
func (s *HttpApiServer) InsertModbusPointPosition(list []model.MModbusPointPosition) error {
m := model.MModbusPointPosition{}
Expand All @@ -251,6 +255,24 @@ func (s *HttpApiServer) DeleteModbusPointAndDevice(deviceUuid string) error {
})
}

// UpdateModbusPoint 更新modbus点位
func (s *HttpApiServer) UpdateModbusPoint(mm model.MModbusPointPosition) error {
m := model.MDevice{}
if err := s.sqliteDb.Where("id = ?", mm.ID).First(&m).Error; err != nil {
return err
} else {
s.sqliteDb.Model(m).Updates(&m)
return nil
}
}

// AllModbusPointByDeviceUuid 根据设备UUID查询设备点位
func (s *HttpApiServer) AllModbusPointByDeviceUuid(deviceUuid string) (list []model.MModbusPointPosition, err error) {

err = s.sqliteDb.Where("device_uuid = ?", deviceUuid).Find(&list).Error
return
}

// -------------------------------------------------------------------------------------
// Goods
// -------------------------------------------------------------------------------------
Expand Down
50 changes: 50 additions & 0 deletions plugin/http_server/device_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,56 @@ func UpdateDevice(c *gin.Context, hs *HttpApiServer) {
c.JSON(common.HTTP_OK, common.Ok())
}

// ModbusPoints 获取modbus_excel类型的点位数据
func ModbusPoints(c *gin.Context, hs *HttpApiServer) {
deviceUuid := c.GetString("deviceUuid")
list, err := hs.AllModbusPointByDeviceUuid(deviceUuid)
if err != nil {
c.JSON(common.HTTP_OK, common.Error400(err))
return
}
c.JSON(common.HTTP_OK, common.OkWithData(list))
}

// UpdateModbusPoint 更新modbus_excel类型的点位数据
func UpdateModbusPoint(c *gin.Context, hs *HttpApiServer) {
type Form struct {
Id uint
DeviceUuid string `json:"deviceUuid" gorm:"not null"`
Tag string `json:"tag" gorm:"not null"`
Function int `json:"function" gorm:"not null"`
SlaverId byte `json:"slaverId" gorm:"not null"`
StartAddress uint16 `json:"startAddress" gorm:"not null"`
Quality uint16 `json:"quality" gorm:"not null"`
}

form := Form{}
if err := c.ShouldBindJSON(&form); err != nil {
c.JSON(common.HTTP_OK, common.Error400(err))
return
}

err := hs.UpdateModbusPoint(model.MModbusPointPosition{
RulexModel: model.RulexModel{
ID: form.Id,
},
DeviceUuid: form.DeviceUuid,
Tag: form.Tag,
Function: form.Function,
SlaverId: form.SlaverId,
StartAddress: form.StartAddress,
Quality: form.Quality,
})

if err != nil {
c.JSON(common.HTTP_OK, common.Error400(err))
return
}

c.JSON(common.HTTP_OK, common.Ok())

}

// ModbusSheetImport 上传Excel文件
func ModbusSheetImport(c *gin.Context, hs *HttpApiServer) {
// 解析 multipart/form-data 类型的请求体
Expand Down
2 changes: 2 additions & 0 deletions plugin/http_server/http_api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ func (hs *HttpApiServer) LoadRoute() {
hs.ginEngine.PUT(url("devices"), hs.addRoute(UpdateDevice))
hs.ginEngine.DELETE(url("devices"), hs.addRoute(DeleteDevice))
hs.ginEngine.POST(url("devices/modbus/sheetImport"), hs.addRoute(ModbusSheetImport))
hs.ginEngine.PUT(url("devices/modbus/point"), hs.addRoute(UpdateModbusPoint))
hs.ginEngine.GET(url("devices/modbus"), hs.addRoute(ModbusPoints))

// 外挂管理
hs.ginEngine.GET(url("goods"), hs.addRoute(Goods))
Expand Down

0 comments on commit 7888352

Please sign in to comment.