| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- 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"`
- Protocol string `form:"protocolOrder" search:"type:order;column:protocol;table:iot_device"`
- Type string `form:"typeOrder" search:"type:order;column:type;table:iot_device"`
- Mode string `form:"modeOrder" search:"type:order;column:mode;table:iot_device"`
- Address string `form:"addressOrder" search:"type:order;column:address;table:iot_device"`
- Status string `form:"statusOrder" search:"type:order;column:status;table:iot_device"`
- OnlineStatus string `form:"onlineStatusOrder" search:"type:order;column:online_status;table:iot_device"`
- TimeOnline string `form:"timeOnlineOrder" search:"type:order;column:time_online;table:iot_device"`
- TimeOffline string `form:"timeOfflineOrder" search:"type:order;column:time_offline;table:iot_device"`
- CreatedAtOrder string `form:"createdAtOrder" search:"type:order;column:created_at;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
- }
- type IotDeviceSetConfigReq struct {
- Id int `json:"id"`
- Config string `json:"config"`
- }
|