device.go 9.4 KB

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