device.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package dto
  2. import (
  3. "IotAdmin/app/iot/models"
  4. "IotAdmin/common/dto"
  5. comModels "IotAdmin/common/models"
  6. "IotAdmin/core/sdk/pkg"
  7. "IotAdmin/iot/constant"
  8. iotStruct "IotAdmin/iot/struct"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type IotDeviceGetPageReq struct {
  14. dto.Pagination `search:"-"`
  15. OrgId int `form:"orgId" search:"-"`
  16. ParentId int `form:"parentId" search:"type:exact;column:parent_id;table:iot_device" comment:"父设备ID"`
  17. GroupId int `form:"groupId" search:"type:exact;column:group_id;table:iot_device" comment:"设备分组"`
  18. Sn string `form:"sn" search:"type:icontains;column:sn;table:iot_device" comment:"设备SN"`
  19. Name string `form:"name" search:"type:icontains;column:name;table:iot_device" comment:"设备名称"`
  20. Status int `form:"status" search:"type:exact;column:status;table:iot_device" comment:"启用状态"`
  21. Type int `form:"type" search:"type:exact;column:type;table:iot_device" comment:"设备类型"`
  22. BeginTime string `form:"beginTime" search:"type:gte;column:created_at;table:iot_device" comment:"创建时间"`
  23. EndTime string `form:"endTime" search:"type:lte;column:created_at;table:iot_device" comment:"创建时间"`
  24. IotDeviceOrder
  25. }
  26. type IotDeviceOrder struct {
  27. GroupId string `form:"groupIdOrder" search:"type:order;column:group_id;table:iot_device"`
  28. Sn string `form:"snOrder" search:"type:order;column:sn;table:iot_device"`
  29. Name string `form:"nameOrder" search:"type:order;column:name;table:iot_device"`
  30. Type int `form:"typeOrder" search:"type:order;column:type;table:iot_device"`
  31. }
  32. func (m *IotDeviceGetPageReq) GetNeedSearch() interface{} {
  33. return *m
  34. }
  35. // IotDeviceInsertReq 添加设备请求参数
  36. type IotDeviceInsertReq struct {
  37. Id int `json:"-" comment:"Id"` // Id
  38. ParentId int `json:"parentId" comment:"父ID"`
  39. GroupId int `json:"groupId" comment:"分组ID"`
  40. Sn string `json:"sn" comment:"设备编码"`
  41. Name string `json:"name" comment:"设备名称"`
  42. Status int `json:"status" comment:"启用状态"`
  43. Type int `json:"type" comment:"设备类型 1:网关 2:表计"`
  44. Mode int `json:"mode" comment:"设备模式 1:下发指令查询 2:表计主动上报"`
  45. Cycle int `json:"cycle" comment:"上报周期"`
  46. Description string `json:"description" comment:"设备描述"`
  47. Protocol string `json:"protocol" comment:"表计协议"`
  48. Address int `json:"address" comment:"表计地址"`
  49. BmYz string `json:"bmYz" comment:"编码因子"`
  50. ReportConfig *[]iotStruct.ReportConfig `json:"reportConfig" comment:"上报配置"`
  51. comModels.ControlBy
  52. }
  53. func (s *IotDeviceInsertReq) Generate(model *models.IotDevice) {
  54. if s.Id == 0 {
  55. model.Model = comModels.Model{Id: s.Id}
  56. }
  57. model.ParentId = s.ParentId
  58. model.GroupId = s.GroupId
  59. model.Sn = GenSn(model.Last)
  60. model.Name = s.Name
  61. model.Type = s.Type
  62. model.Mode = s.Mode
  63. model.Cycle = s.Cycle
  64. model.Status = s.Status
  65. model.Description = s.Description
  66. model.Protocol = s.Protocol
  67. model.Address = s.Address
  68. model.BmYz = s.BmYz
  69. model.CreateBy = s.CreateBy // 添加这而,需要记录是被谁创建的
  70. model.Dsn = ""
  71. if model.Type == constant.IotDeviceTypeMeter && s.ReportConfig != nil {
  72. model.Dsn = BuildDsnString(s.ReportConfig)
  73. }
  74. }
  75. func GenSn(model *models.IotDevice) string {
  76. str := ""
  77. if model.Type == constant.IotDeviceTypeGateway {
  78. today := time.Now().Format("0601")
  79. str += today
  80. snLen := len(model.Sn)
  81. // 截取sn的前4位
  82. if model.Sn == "" || model.Sn[0:4] != today {
  83. str += "101"
  84. } else {
  85. // 截取sn第5位到后3位
  86. nStr := model.Sn[4 : snLen-3]
  87. num, err := strconv.Atoi(nStr)
  88. if err != nil {
  89. return ""
  90. }
  91. num++
  92. str += strconv.Itoa(num)
  93. }
  94. // 生成3位随机数
  95. str += pkg.GenerateRandomNumber(3)
  96. } else {
  97. arr := strings.Split(model.Sn, "_")
  98. str += arr[0]
  99. if len(arr) > 1 {
  100. num, err := strconv.Atoi(arr[1])
  101. if err != nil {
  102. return ""
  103. }
  104. num++
  105. str += "_" + strconv.Itoa(num)
  106. } else {
  107. str += "_101"
  108. }
  109. }
  110. return str
  111. }
  112. func (s *IotDeviceInsertReq) GetId() interface{} {
  113. return s.Id
  114. }
  115. // IotDeviceUpdateReq 修改设备请求参数
  116. type IotDeviceUpdateReq struct {
  117. Id int `uri:"id" comment:"Id"` // Id
  118. ParentId int `json:"parentId" comment:"父ID"`
  119. GroupId int `json:"groupId" comment:"分组ID"`
  120. Sn string `json:"sn" comment:"设备编码"`
  121. Name string `json:"name" comment:"设备名称"`
  122. Type int `json:"type" comment:"设备类型 1:网关 2:表计"`
  123. Status int `json:"status" comment:"启用状态"`
  124. Mode int `json:"mode" comment:"设备模式 1:下发指令查询 2:表计主动上报"`
  125. Cycle int `json:"cycle" comment:"上报周期"`
  126. Description string `json:"description" comment:"设备描述"`
  127. Protocol string `json:"protocol" comment:"表计协议"`
  128. Address int `json:"address" comment:"表计地址"`
  129. BmYz string `json:"bmYz" comment:"编码因子"`
  130. ReportConfig *[]iotStruct.ReportConfig `json:"reportConfig" comment:"上报配置"`
  131. comModels.ControlBy
  132. }
  133. func (s *IotDeviceUpdateReq) Generate(model *models.IotDevice) {
  134. if s.Id == 0 {
  135. model.Model = comModels.Model{Id: s.Id}
  136. }
  137. model.ParentId = s.ParentId
  138. model.GroupId = s.GroupId
  139. model.Sn = s.Sn
  140. model.Name = s.Name
  141. model.Type = s.Type
  142. model.Mode = s.Mode
  143. model.Cycle = s.Cycle
  144. model.Status = s.Status
  145. model.Description = s.Description
  146. model.Protocol = s.Protocol
  147. model.Address = s.Address
  148. model.BmYz = s.BmYz
  149. model.UpdateBy = s.UpdateBy // 添加这而,需要记录是被谁更新的
  150. model.Dsn = ""
  151. if model.Type == constant.IotDeviceTypeMeter {
  152. model.Dsn = BuildDsnString(s.ReportConfig)
  153. }
  154. }
  155. func (s *IotDeviceUpdateReq) GetId() interface{} {
  156. return s.Id
  157. }
  158. func BuildDsnString(configs *[]iotStruct.ReportConfig) string {
  159. str := ""
  160. for i, cfg := range *configs {
  161. if i > 0 {
  162. str += constant.DsnSplitString
  163. }
  164. host := strings.Replace(cfg.Host, ":", ":", 1)
  165. if !strings.Contains(host, ":") || cfg.Protocol == "" || cfg.ST == "" || cfg.MN == "" {
  166. return ""
  167. }
  168. str += host + constant.DsnChildSplitString + cfg.Protocol + constant.DsnChildSplitString + cfg.ST + constant.DsnChildSplitString + cfg.MN
  169. if cfg.User != "" {
  170. str += constant.DsnChildSplitString + cfg.User + constant.DsnChildSplitString + cfg.Pwd
  171. }
  172. }
  173. return str
  174. }
  175. // IotDeviceGetReq 获取设备请求参数
  176. type IotDeviceGetReq struct {
  177. Id int `uri:"id"`
  178. }
  179. func (s *IotDeviceGetReq) GetId() interface{} {
  180. return s.Id
  181. }
  182. // IotDeviceDeleteReq 删除设备请求参数
  183. type IotDeviceDeleteReq struct {
  184. Ids []int `json:"ids"`
  185. }
  186. func (s *IotDeviceDeleteReq) GetId() interface{} {
  187. return s.Ids
  188. }
  189. // IotDeviceResp 获取设备响应参数
  190. type IotDeviceResp struct {
  191. Id int `uri:"id" comment:"Id"` // Id
  192. ParentId int `json:"parentId" comment:"父ID"`
  193. GroupId int `json:"groupId" comment:"分组ID"`
  194. Sn string `json:"sn" comment:"设备编码"`
  195. Name string `json:"name" comment:"设备名称"`
  196. Type int `json:"type" comment:"设备类型 1:网关 2:表计"`
  197. Mode int `json:"mode" comment:"设备模式 1:下发指令查询 2:表计主动上报"`
  198. Cycle int `json:"cycle" comment:"上报周期"`
  199. Description string `json:"description" comment:"设备描述"`
  200. Protocol string `json:"protocol" comment:"表计协议"`
  201. Address int `json:"address" comment:"表计地址"`
  202. CreateBy int `json:"createBy" comment:"创建者"`
  203. CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
  204. }
  205. func (s *IotDeviceResp) Generate(model *models.IotDevice) {
  206. s.Id = model.Id
  207. s.ParentId = model.ParentId
  208. s.GroupId = model.GroupId
  209. s.Sn = model.Sn
  210. s.Name = model.Name
  211. s.Type = model.Type
  212. s.Mode = model.Mode
  213. s.Cycle = model.Cycle
  214. s.Description = model.Description
  215. s.Protocol = model.Protocol
  216. s.Address = model.Address
  217. s.CreateBy = model.CreateBy
  218. s.CreatedAt = model.CreatedAt
  219. }