device.go 8.1 KB

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