package dto import ( "IotAdmin/app/iot/models" "IotAdmin/common/dto" comModels "IotAdmin/common/models" "IotAdmin/core/sdk/pkg" "IotAdmin/iot/constant" iotStruct "IotAdmin/iot/struct" "strconv" "strings" "time" ) type IotDeviceGetPageReq struct { dto.Pagination `search:"-"` OrgId int `form:"orgId" search:"-"` ParentId int `form:"parentId" search:"type:exact;column:parent_id;table:iot_device" comment:"父设备ID"` GroupId int `form:"groupId" search:"type:exact;column:group_id;table:iot_device" comment:"设备分组"` Sn string `form:"sn" search:"type:icontains;column:sn;table:iot_device" comment:"设备SN"` Name string `form:"name" search:"type:icontains;column:name;table:iot_device" comment:"设备名称"` Status int `form:"status" search:"type:exact;column:status;table:iot_device" comment:"启用状态"` Type int `form:"type" search:"type:exact;column:type;table:iot_device" comment:"设备类型"` BeginTime string `form:"beginTime" search:"type:gte;column:created_at;table:iot_device" comment:"创建时间"` EndTime string `form:"endTime" search:"type:lte;column:created_at;table:iot_device" comment:"创建时间"` IotDeviceOrder } type IotDeviceOrder struct { GroupId string `form:"groupIdOrder" search:"type:order;column:group_id;table:iot_device"` Sn string `form:"snOrder" search:"type:order;column:sn;table:iot_device"` Name string `form:"nameOrder" search:"type:order;column:name;table:iot_device"` Type int `form:"typeOrder" search:"type:order;column:type;table:iot_device"` } func (m *IotDeviceGetPageReq) GetNeedSearch() interface{} { return *m } // IotDeviceInsertReq 添加设备请求参数 type IotDeviceInsertReq struct { Id int `json:"-" comment:"Id"` // Id ParentId int `json:"parentId" comment:"父ID"` GroupId int `json:"groupId" comment:"分组ID"` Sn string `json:"sn" comment:"设备编码"` Name string `json:"name" comment:"设备名称"` Status int `json:"status" comment:"启用状态"` Type int `json:"type" comment:"设备类型 1:网关 2:表计"` Mode int `json:"mode" comment:"设备模式 1:下发指令查询 2:表计主动上报"` Cycle int `json:"cycle" comment:"上报周期"` Description string `json:"description" comment:"设备描述"` Protocol string `json:"protocol" comment:"表计协议"` Address int `json:"address" comment:"表计地址"` BmYz string `json:"bmYz" comment:"编码因子"` ReportConfig *[]iotStruct.ReportConfig `json:"reportConfig" comment:"上报配置"` comModels.ControlBy } func (s *IotDeviceInsertReq) Generate(model *models.IotDevice) { if s.Id == 0 { model.Model = comModels.Model{Id: s.Id} } model.ParentId = s.ParentId model.GroupId = s.GroupId model.Sn = GenSn(model.Last) model.Name = s.Name model.Type = s.Type model.Mode = s.Mode model.Cycle = s.Cycle model.Status = s.Status model.Description = s.Description model.Protocol = s.Protocol model.Address = s.Address model.BmYz = s.BmYz model.CreateBy = s.CreateBy // 添加这而,需要记录是被谁创建的 model.Dsn = "" if model.Type == constant.IotDeviceTypeMeter && s.ReportConfig != nil { model.Dsn = BuildDsnString(s.ReportConfig) } } func GenSn(model *models.IotDevice) string { str := "" if model.Type == constant.IotDeviceTypeGateway { today := time.Now().Format("0601") str += today snLen := len(model.Sn) // 截取sn的前4位 if model.Sn == "" || model.Sn[0:4] != today { str += "101" } else { // 截取sn第5位到后3位 nStr := model.Sn[4 : snLen-3] num, err := strconv.Atoi(nStr) if err != nil { return "" } num++ str += strconv.Itoa(num) } // 生成3位随机数 str += pkg.GenerateRandomNumber(3) } else { arr := strings.Split(model.Sn, "_") str += arr[0] if len(arr) > 1 { num, err := strconv.Atoi(arr[1]) if err != nil { return "" } num++ str += "_" + strconv.Itoa(num) } else { str += "_101" } } return str } func (s *IotDeviceInsertReq) GetId() interface{} { return s.Id } // IotDeviceUpdateReq 修改设备请求参数 type IotDeviceUpdateReq struct { Id int `uri:"id" comment:"Id"` // Id ParentId int `json:"parentId" comment:"父ID"` GroupId int `json:"groupId" comment:"分组ID"` Sn string `json:"sn" comment:"设备编码"` Name string `json:"name" comment:"设备名称"` Type int `json:"type" comment:"设备类型 1:网关 2:表计"` Status int `json:"status" comment:"启用状态"` Mode int `json:"mode" comment:"设备模式 1:下发指令查询 2:表计主动上报"` Cycle int `json:"cycle" comment:"上报周期"` Description string `json:"description" comment:"设备描述"` Protocol string `json:"protocol" comment:"表计协议"` Address int `json:"address" comment:"表计地址"` BmYz string `json:"bmYz" comment:"编码因子"` ReportConfig *[]iotStruct.ReportConfig `json:"reportConfig" comment:"上报配置"` comModels.ControlBy } func (s *IotDeviceUpdateReq) Generate(model *models.IotDevice) { if s.Id == 0 { model.Model = comModels.Model{Id: s.Id} } model.ParentId = s.ParentId model.GroupId = s.GroupId model.Sn = s.Sn model.Name = s.Name model.Type = s.Type model.Mode = s.Mode model.Cycle = s.Cycle model.Status = s.Status model.Description = s.Description model.Protocol = s.Protocol model.Address = s.Address model.BmYz = s.BmYz model.UpdateBy = s.UpdateBy // 添加这而,需要记录是被谁更新的 model.Dsn = "" if model.Type == constant.IotDeviceTypeMeter { model.Dsn = BuildDsnString(s.ReportConfig) } } func (s *IotDeviceUpdateReq) GetId() interface{} { return s.Id } func BuildDsnString(configs *[]iotStruct.ReportConfig) string { str := "" for i, cfg := range *configs { if i > 0 { str += constant.DsnSplitString } host := strings.Replace(cfg.Host, ":", ":", 1) if !strings.Contains(host, ":") || cfg.Protocol == "" || cfg.ST == "" || cfg.MN == "" { return "" } str += host + constant.DsnChildSplitString + cfg.Protocol + constant.DsnChildSplitString + cfg.ST + constant.DsnChildSplitString + cfg.MN if cfg.User != "" { str += constant.DsnChildSplitString + cfg.User + constant.DsnChildSplitString + cfg.Pwd } } return str } // IotDeviceGetReq 获取设备请求参数 type IotDeviceGetReq struct { Id int `uri:"id"` } func (s *IotDeviceGetReq) GetId() interface{} { return s.Id } // IotDeviceDeleteReq 删除设备请求参数 type IotDeviceDeleteReq struct { Ids []int `json:"ids"` } func (s *IotDeviceDeleteReq) GetId() interface{} { return s.Ids } // IotDeviceResp 获取设备响应参数 type IotDeviceResp struct { Id int `uri:"id" comment:"Id"` // Id ParentId int `json:"parentId" comment:"父ID"` GroupId int `json:"groupId" comment:"分组ID"` Sn string `json:"sn" comment:"设备编码"` Name string `json:"name" comment:"设备名称"` Type int `json:"type" comment:"设备类型 1:网关 2:表计"` Mode int `json:"mode" comment:"设备模式 1:下发指令查询 2:表计主动上报"` Cycle int `json:"cycle" comment:"上报周期"` Description string `json:"description" comment:"设备描述"` Protocol string `json:"protocol" comment:"表计协议"` Address int `json:"address" comment:"表计地址"` CreateBy int `json:"createBy" comment:"创建者"` CreatedAt time.Time `json:"createdAt" comment:"创建时间"` } func (s *IotDeviceResp) Generate(model *models.IotDevice) { s.Id = model.Id s.ParentId = model.ParentId s.GroupId = model.GroupId s.Sn = model.Sn s.Name = model.Name s.Type = model.Type s.Mode = model.Mode s.Cycle = model.Cycle s.Description = model.Description s.Protocol = model.Protocol s.Address = model.Address s.CreateBy = model.CreateBy s.CreatedAt = model.CreatedAt }