feat: 环境监测相关接口
This commit is contained in:
parent
a031a25de0
commit
6030eed880
@ -132,3 +132,20 @@ func (a *EnvMonitorDevice) Delete(c *gin.Context) {
|
||||
}
|
||||
util.ResOK(c)
|
||||
}
|
||||
|
||||
// @Tags EnvMonitorDeviceAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Get device statistics
|
||||
// @Success 200 {object} util.ResponseResult{data=[]schema.DeviceStatistics}
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/devices/statistics [get]
|
||||
func (a *EnvMonitorDevice) Statistics(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
statistics, err := a.EnvMonitorDeviceBIZ.Statistics(ctx)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResSuccess(c, statistics)
|
||||
}
|
||||
|
@ -106,7 +106,8 @@ func (a *EnvMonitorDeviceTypeInfo) Update(c *gin.Context) {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
user := util.FromLoginUser(ctx)
|
||||
item.TenantID = user.TenantID
|
||||
err := a.EnvMonitorDeviceTypeInfoBIZ.Update(ctx, c.Param("id"), item)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
|
@ -177,3 +177,11 @@ func (a *EnvMonitorDevice) Delete(ctx context.Context, id string) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (a *EnvMonitorDevice) Statistics(ctx context.Context) ([]schema.DeviceStatistics, error) {
|
||||
statistics, err := a.EnvMonitorDeviceDAL.Statistics(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return statistics, nil
|
||||
}
|
||||
|
@ -53,8 +53,15 @@ func (a *EnvMonitorDeviceWarningConfig) Create(ctx context.Context, formItem *sc
|
||||
if err := formItem.FillTo(envMonitorDeviceWarningConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 如果该租户已经存在报警范围配置,则不允许新增
|
||||
exists, err := a.EnvMonitorDeviceWarningConfigDAL.ExistsByTenantID(ctx, envMonitorDeviceWarningConfig.TenantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if exists {
|
||||
return nil, errors.BadRequest("", "已经存在报警范围配置,不允许新增")
|
||||
}
|
||||
|
||||
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
|
||||
err = a.Trans.Exec(ctx, func(ctx context.Context) error {
|
||||
if err := a.EnvMonitorDeviceWarningConfigDAL.Create(ctx, envMonitorDeviceWarningConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -32,8 +32,8 @@ func (a *EnvMonitorDevice) Query(ctx context.Context, params schema.EnvMonitorDe
|
||||
|
||||
db := GetEnvMonitorDeviceDB(ctx, a.DB)
|
||||
db = db.Preload("EnvMonitorDeviceFishPond").Preload("EnvMonitorDeviceSensor")
|
||||
if params.Name != "" {
|
||||
db = db.Where("name like ?", "%"+params.Name+"%")
|
||||
if params.DeviceName != "" {
|
||||
db = db.Where("name like ?", "%"+params.DeviceName+"%")
|
||||
}
|
||||
if params.Type != nil && *params.Type != 0 {
|
||||
db = db.Where("type=?", *params.Type)
|
||||
@ -92,3 +92,36 @@ func (a *EnvMonitorDevice) Delete(ctx context.Context, id string) error {
|
||||
result := GetEnvMonitorDeviceDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.EnvMonitorDevice))
|
||||
return errors.WithStack(result.Error)
|
||||
}
|
||||
|
||||
func (a *EnvMonitorDevice) Statistics(ctx context.Context) ([]schema.DeviceStatistics, error) {
|
||||
ctx = gormx.WithTenantID(ctx, consts.DefaultTenantID)
|
||||
|
||||
// 统计不同的设备类型数量以及在线数量(status=true)
|
||||
var statistics []schema.DeviceStatistics
|
||||
//
|
||||
// EnvMonitorDeviceSensorTypeWaterTemperature = 1 // 水温传感器
|
||||
// EnvMonitorDeviceSensorTypeDissolvedOxygen = 2 // 溶解氧传感器
|
||||
// EnvMonitorDeviceSensorTypeAmmonia = 3 // 氨氮传感器
|
||||
// EnvMonitorDeviceSensorTypePH = 4 // PH传感器
|
||||
// EnvMonitorDeviceSensorTypeConductivity = 5 // 电导率传感器
|
||||
// EnvMonitorDeviceSensorTypeVideo = 6 // 视频监控
|
||||
types := []int{schema.EnvMonitorDeviceSensorTypeWaterTemperature, schema.EnvMonitorDeviceSensorTypeDissolvedOxygen, schema.EnvMonitorDeviceSensorTypeAmmonia, schema.EnvMonitorDeviceSensorTypePH, schema.EnvMonitorDeviceSensorTypeConductivity, schema.EnvMonitorDeviceSensorTypeVideo}
|
||||
for _, v := range types {
|
||||
// 统计不同设备类型总数
|
||||
total := int64(0)
|
||||
GetEnvMonitorDeviceDB(ctx, a.DB).Model(&schema.EnvMonitorDeviceSensor{}).Where("type=?", v).Count(&total)
|
||||
// 统计不同设备类型在线数量
|
||||
online := int64(0)
|
||||
GetEnvMonitorDeviceDB(ctx, a.DB).Model(&schema.EnvMonitorDeviceSensor{}).Where("type=?", v).Where("status=?", true).Count(&online)
|
||||
statistics = append(statistics, schema.DeviceStatistics{
|
||||
Type: v,
|
||||
Total: int(total),
|
||||
Online: int(online),
|
||||
Offline: int(total - online),
|
||||
})
|
||||
if total > 0 {
|
||||
statistics[len(statistics)-1].Rate = int(online * 100 / total)
|
||||
}
|
||||
}
|
||||
return statistics, nil
|
||||
}
|
||||
|
@ -67,6 +67,11 @@ func (a *EnvMonitorDeviceWarningConfig) Exists(ctx context.Context, id string) (
|
||||
return ok, errors.WithStack(err)
|
||||
}
|
||||
|
||||
func (a *EnvMonitorDeviceWarningConfig) ExistsByTenantID(ctx context.Context, tenantID string) (bool, error) {
|
||||
ok, err := util.Exists(ctx, GetEnvMonitorDeviceWarningConfigDB(ctx, a.DB).Where("tenant_id=?", tenantID))
|
||||
return ok, errors.WithStack(err)
|
||||
}
|
||||
|
||||
// Create a new env monitor device warning config.
|
||||
func (a *EnvMonitorDeviceWarningConfig) Create(ctx context.Context, item *schema.EnvMonitorDeviceWarningConfig) error {
|
||||
result := GetEnvMonitorDeviceWarningConfigDB(ctx, a.DB).Create(item)
|
||||
|
@ -63,6 +63,9 @@ func (a *EnvMonitorDeviceWarningLog) Query(ctx context.Context, params schema.En
|
||||
if params.EndTime != "" {
|
||||
db = db.Where("created_at <= ?", params.EndTime)
|
||||
}
|
||||
if params.IsOK != nil {
|
||||
db = db.Where("is_ok = ?", *params.IsOK)
|
||||
}
|
||||
|
||||
var list schema.EnvMonitorDeviceWarningLogs
|
||||
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
|
||||
|
@ -42,7 +42,7 @@ func (a *EnvMonitor) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup)
|
||||
envMonitorDeviceTypeInfo.GET(":id", a.EnvMonitorDeviceTypeInfoAPI.Get)
|
||||
envMonitorDeviceTypeInfo.POST("", a.EnvMonitorDeviceTypeInfoAPI.Create)
|
||||
envMonitorDeviceTypeInfo.PUT(":id", a.EnvMonitorDeviceTypeInfoAPI.Update)
|
||||
// envMonitorDeviceTypeInfo.DELETE(":id", a.EnvMonitorDeviceTypeInfoAPI.Delete)
|
||||
envMonitorDeviceTypeInfo.DELETE(":id", a.EnvMonitorDeviceTypeInfoAPI.Delete)
|
||||
}
|
||||
envMonitorDevice := v1.Group("env-monitor-devices")
|
||||
{
|
||||
@ -50,7 +50,10 @@ func (a *EnvMonitor) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup)
|
||||
envMonitorDevice.GET(":id", a.EnvMonitorDeviceAPI.Get)
|
||||
envMonitorDevice.POST("", a.EnvMonitorDeviceAPI.Create)
|
||||
envMonitorDevice.PUT(":id", a.EnvMonitorDeviceAPI.Update)
|
||||
envMonitorDevice.DELETE(":id", a.EnvMonitorDeviceAPI.Delete)
|
||||
// envMonitorDevice.DELETE(":id", a.EnvMonitorDeviceAPI.Delete)
|
||||
|
||||
// 统计设备数量以及状态
|
||||
envMonitorDevice.GET("statistics", a.EnvMonitorDeviceAPI.Statistics)
|
||||
}
|
||||
envMonitorDeviceWarningConfig :=
|
||||
// envMonitorDeviceFishPond := v1.Group("env-monitor-device-fish-ponds")
|
||||
|
@ -6,6 +6,13 @@ import (
|
||||
"ifms/pkg/util"
|
||||
)
|
||||
|
||||
// 设备类型(1水质监测仪、2水下视频监控、3车间视频监控)
|
||||
const (
|
||||
EnvMonitorDeviceTypeWaterQuality = 1 // 水质监测仪
|
||||
EnvMonitorDeviceTypeWaterVideo = 2 // 水下视频监控
|
||||
EnvMonitorDeviceTypeWorkshopVideo = 3 // 车间视频监控
|
||||
)
|
||||
|
||||
// 环境监测设备管理
|
||||
type EnvMonitorDevice struct {
|
||||
ID int64 `json:"id" gorm:"size:20;primaryKey;comment:Unique ID;"` // Unique ID
|
||||
@ -26,8 +33,8 @@ type EnvMonitorDevice struct {
|
||||
// Defining the query parameters for the `EnvMonitorDevice` struct.
|
||||
type EnvMonitorDeviceQueryParam struct {
|
||||
util.PaginationParam
|
||||
Name string `form:"name"`
|
||||
Type *int `form:"type"`
|
||||
DeviceName string `form:"device_name"`
|
||||
Type *int `form:"type"`
|
||||
}
|
||||
|
||||
// Defining the query options for the `EnvMonitorDevice` struct.
|
||||
@ -75,3 +82,11 @@ func (a *EnvMonitorDeviceForm) FillTo(envMonitorDevice *EnvMonitorDevice) error
|
||||
envMonitorDevice.WorkshopName = a.WorkshopName
|
||||
return nil
|
||||
}
|
||||
|
||||
type DeviceStatistics struct {
|
||||
Type int `json:"type"`
|
||||
Total int `json:"total"`
|
||||
Online int `json:"online"`
|
||||
Offline int `json:"offline"`
|
||||
Rate int `json:"rate"`
|
||||
}
|
||||
|
@ -6,6 +6,16 @@ import (
|
||||
"ifms/pkg/util"
|
||||
)
|
||||
|
||||
// 设备类型(传感器类型 1水温传感器、2溶解氧传感器、3氨氮传感器、4PH传感器、5电导率传感器、6视频监控)
|
||||
const (
|
||||
EnvMonitorDeviceSensorTypeWaterTemperature = 1 // 水温传感器
|
||||
EnvMonitorDeviceSensorTypeDissolvedOxygen = 2 // 溶解氧传感器
|
||||
EnvMonitorDeviceSensorTypeAmmonia = 3 // 氨氮传感器
|
||||
EnvMonitorDeviceSensorTypePH = 4 // PH传感器
|
||||
EnvMonitorDeviceSensorTypeConductivity = 5 // 电导率传感器
|
||||
EnvMonitorDeviceSensorTypeVideo = 6 // 视频监控
|
||||
)
|
||||
|
||||
// 设备传感器表
|
||||
type EnvMonitorDeviceSensor struct {
|
||||
ID int64 `json:"id" gorm:"size:20;primaryKey;comment:Unique ID;"` // Unique ID
|
||||
|
@ -40,7 +40,7 @@ type EnvMonitorDeviceTypeInfos []*EnvMonitorDeviceTypeInfo
|
||||
|
||||
// Defining the data structure for creating a `EnvMonitorDeviceTypeInfo` struct.
|
||||
type EnvMonitorDeviceTypeInfoForm struct {
|
||||
TenantID string `json:"tenant_id" binding:"required"` // 租户ID
|
||||
TenantID string `json:"tenant_id"` // 租户ID
|
||||
Type int `json:"type" binding:"required"` // 环境监测设备类型(1水质监测仪、2水下视频监控、3车间视频监控)
|
||||
Model string `json:"model" binding:"required,max=255"` // 设备型号
|
||||
Supplier string `json:"supplier" binding:"required,max=255"` // 供应商
|
||||
|
@ -31,6 +31,7 @@ type EnvMonitorDeviceWarningLogQueryParam struct {
|
||||
DeviceName string `form:"device_name"` // 设备名称
|
||||
StartTime string `form:"start_time"` // 开始时间
|
||||
EndTime string `form:"end_time"` // 结束时间
|
||||
IsOK *bool `form:"is_ok"` // 是否正常
|
||||
}
|
||||
|
||||
// Defining the query options for the `EnvMonitorDeviceWarningLog` struct.
|
||||
|
Loading…
x
Reference in New Issue
Block a user