From 788835294fa3c0f86ba31ee033ca626ffaf3d2fb Mon Sep 17 00:00:00 2001 From: wan9bo7 <74997947+wan9bo7@users.noreply.github.com> Date: Thu, 6 Jul 2023 14:21:01 +0800 Subject: [PATCH] feat: modbus point excel api CRUD (#146) --- plugin/http_server/dao.go | 24 ++++++++++++- plugin/http_server/device_api.go | 50 +++++++++++++++++++++++++++ plugin/http_server/http_api_server.go | 2 ++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/plugin/http_server/dao.go b/plugin/http_server/dao.go index 59893aed6..f388634c8 100644 --- a/plugin/http_server/dao.go +++ b/plugin/http_server/dao.go @@ -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" ) @@ -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{} @@ -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 // ------------------------------------------------------------------------------------- diff --git a/plugin/http_server/device_api.go b/plugin/http_server/device_api.go index 50e6ede80..2478f4b84 100644 --- a/plugin/http_server/device_api.go +++ b/plugin/http_server/device_api.go @@ -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 类型的请求体 diff --git a/plugin/http_server/http_api_server.go b/plugin/http_server/http_api_server.go index 6c36f0875..29b1ec42f 100644 --- a/plugin/http_server/http_api_server.go +++ b/plugin/http_server/http_api_server.go @@ -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))