mqtt_handle.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package mqtt
  2. import (
  3. "encoding/json"
  4. "kpt-pasture/model"
  5. "kpt-pasture/util"
  6. "strconv"
  7. "strings"
  8. "github.com/jinzhu/copier"
  9. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. "go.uber.org/zap"
  12. "gorm.io/gorm"
  13. )
  14. type DataInsertNeckRingLog struct {
  15. NeckRingOriginalData []*model.NeckRingOriginal
  16. NeckRingErrorData []*model.NeckRingError
  17. }
  18. var (
  19. DSMLog = &DataInsertNeckRingLog{
  20. NeckRingOriginalData: make([]*model.NeckRingOriginal, 0),
  21. NeckRingErrorData: make([]*model.NeckRingError, 0),
  22. }
  23. pastureMqttMap = make(map[string]int64)
  24. isFindPastureMqttMap bool
  25. )
  26. func (e *Entry) NeckRingHandle(data []byte) {
  27. newData := e.MsgDataFormat2(data)
  28. if newData == nil {
  29. return
  30. }
  31. if len(newData.NeckRingErrorData) > 0 || len(newData.NeckRingOriginalData) > 0 {
  32. // 写入数据
  33. if err := e.CreatedData(newData); err != nil {
  34. zaplog.Error("Failed to create data", zap.Any("err", err), zap.Any("dataList", newData))
  35. }
  36. }
  37. return
  38. }
  39. func (e *Entry) FindPastureMqttMap() map[string]int64 {
  40. if isFindPastureMqttMap {
  41. return pastureMqttMap
  42. }
  43. appMqttList := make([]*model.AppMqtt, 0)
  44. if err := e.DB.Model(new(model.AppMqtt)).Find(&appMqttList).Error; err != nil {
  45. zaplog.Error("FindPastureMqttMap", zap.Any("err", err))
  46. }
  47. for _, v := range appMqttList {
  48. pastureMqttMap[v.ReceiveNumber] = v.PastureId
  49. }
  50. isFindPastureMqttMap = true
  51. return pastureMqttMap
  52. }
  53. // 处理批量数据
  54. func (e *Entry) processBatch(batchList []*model.NeckRingOriginal) {
  55. // 初始化分类数据
  56. var (
  57. errorData []*model.NeckRingError
  58. originalData []*model.NeckRingOriginal
  59. )
  60. // 分类数据
  61. for _, batch := range batchList {
  62. // 异常脖环数据
  63. if ok := util.IsValidFrameId(batch.Frameid); !ok {
  64. var ed model.NeckRingError
  65. err := copier.Copy(&ed, &batch)
  66. if err != nil {
  67. zaplog.Error("processBatch", zap.Any("copier", err), zap.Any("data", batch))
  68. continue
  69. }
  70. errorData = append(errorData, &ed)
  71. } else {
  72. originalData = append(originalData, batch)
  73. }
  74. }
  75. // 更新日志
  76. DSMLog.NeckRingErrorData = append(DSMLog.NeckRingErrorData, errorData...)
  77. DSMLog.NeckRingOriginalData = append(DSMLog.NeckRingOriginalData, originalData...)
  78. // 写入数据
  79. if err := e.CreatedData(DSMLog); err != nil {
  80. zaplog.Error("Failed to create data", zap.Any("err", err), zap.Any("dataList", DSMLog))
  81. }
  82. // 清空日志
  83. DSMLog.NeckRingErrorData = DSMLog.NeckRingErrorData[:0]
  84. DSMLog.NeckRingOriginalData = DSMLog.NeckRingOriginalData[:0]
  85. }
  86. func (e *Entry) CreatedData(DSMLog *DataInsertNeckRingLog) error {
  87. if err := e.DB.Transaction(func(tx *gorm.DB) error {
  88. if len(DSMLog.NeckRingErrorData) > 0 {
  89. if err := e.DB.Model(new(model.NeckRingError)).Create(DSMLog.NeckRingErrorData).Error; err != nil {
  90. return xerr.WithStack(err)
  91. }
  92. }
  93. if len(DSMLog.NeckRingOriginalData) > 0 {
  94. if err := e.DB.Model(new(model.NeckRingOriginal)).Create(DSMLog.NeckRingOriginalData).Error; err != nil {
  95. return xerr.WithStack(err)
  96. }
  97. }
  98. return nil
  99. }); err != nil {
  100. return xerr.WithStack(err)
  101. }
  102. return nil
  103. }
  104. func (e *Entry) MsgDataFormat2(msg []byte) *DataInsertNeckRingLog {
  105. neckLogList := &model.NeckRingWrapper{}
  106. if err := json.Unmarshal(msg, neckLogList); err != nil {
  107. zaplog.Error("MsgDataFormat", zap.Any("err", err), zap.Any("msg", string(msg)))
  108. return nil
  109. }
  110. if neckLogList.Type == "heartbeat" {
  111. return nil
  112. }
  113. normalOriginal := make([]*model.NeckRingOriginal, 0)
  114. errorOriginal := make([]*model.NeckRingError, 0)
  115. pastureMqttMap = e.FindPastureMqttMap()
  116. for _, neckLog := range neckLogList.NeckRing.NeckPck {
  117. newOriginal := model.NewNeckRingOriginal(neckLog, pastureMqttMap)
  118. if ok := util.IsValidFrameId(neckLog.Frameid); !ok {
  119. var ed model.NeckRingError
  120. if err := copier.Copy(&ed, &newOriginal); err != nil {
  121. zaplog.Error("MsgDataFormat2", zap.Any("copier", err), zap.Any("neckLog", neckLog))
  122. continue
  123. }
  124. errorOriginal = append(errorOriginal, &ed)
  125. } else {
  126. activeDate, hours := util.GetNeckRingActiveTimer(neckLog.Frameid)
  127. newOriginal.ActiveDate = activeDate
  128. newOriginal.Hours = int32(hours)
  129. normalOriginal = append(normalOriginal, newOriginal)
  130. }
  131. }
  132. return &DataInsertNeckRingLog{
  133. NeckRingErrorData: errorOriginal,
  134. NeckRingOriginalData: normalOriginal,
  135. }
  136. }
  137. func (e *Entry) MsgDataFormat(msg []byte) []*model.NeckRingOriginal {
  138. msgData := make(map[string]interface{})
  139. pairs := strings.Split(util.MsgFormat(string(msg)), " ")
  140. for _, pair := range pairs {
  141. parts := strings.SplitN(pair, ":", 2)
  142. if len(parts) != 2 {
  143. continue
  144. }
  145. key, value := parts[0], parts[1]
  146. if len(key) == 0 {
  147. continue
  148. }
  149. msgData[key] = value
  150. }
  151. softVer := int64(0)
  152. if softVerInter, ok := msgData["SOFT_VER"]; ok {
  153. if softVerstr, ok := softVerInter.(string); ok {
  154. softVer, _ = strconv.ParseInt(softVerstr, 10, 64)
  155. }
  156. }
  157. if softVer <= 0 {
  158. if softVerInter, ok := msgData["soft_ver"]; ok {
  159. if softVerstr, ok := softVerInter.(string); ok {
  160. softVer, _ = strconv.ParseInt(softVerstr, 10, 64)
  161. }
  162. }
  163. }
  164. uuid := ""
  165. if uuidInter, ok := msgData["uuid"]; ok {
  166. if uuidStr, ok := uuidInter.(string); ok {
  167. uuid = uuidStr
  168. }
  169. }
  170. frameId := int64(0)
  171. if frameIdInter, ok := msgData["frameid"]; ok {
  172. if frameId64, ok := frameIdInter.(string); ok {
  173. frameId, _ = strconv.ParseInt(frameId64, 10, 64)
  174. }
  175. }
  176. temp := float64(0)
  177. if tempInter, ok := msgData["Temp"]; ok {
  178. if tempFloat, ok := tempInter.(string); ok {
  179. temp, _ = strconv.ParseFloat(tempFloat, 64)
  180. }
  181. }
  182. if temp <= 0 {
  183. if tempInter, ok := msgData["temp"]; ok {
  184. if tempFloat, ok := tempInter.(string); ok {
  185. temp, _ = strconv.ParseFloat(tempFloat, 64)
  186. }
  187. }
  188. }
  189. imei := ""
  190. if imeiInter, ok := msgData["imei"]; ok {
  191. if imeiStr, ok := imeiInter.(string); ok {
  192. imei = imeiStr
  193. }
  194. }
  195. active := int64(0)
  196. if activeInter, ok := msgData["active"]; ok {
  197. if active32, ok := activeInter.(string); ok {
  198. active, _ = strconv.ParseInt(active32, 10, 64)
  199. }
  200. }
  201. inAction := int64(0)
  202. if inActionInter, ok := msgData["inactive"]; ok {
  203. if inAction32, ok := inActionInter.(string); ok {
  204. inAction, _ = strconv.ParseInt(inAction32, 10, 64)
  205. }
  206. }
  207. ruMina := int64(0)
  208. if ruMinaInter, ok := msgData["Rumina"]; ok {
  209. if ruMina32, ok := ruMinaInter.(string); ok {
  210. ruMina, _ = strconv.ParseInt(ruMina32, 10, 64)
  211. }
  212. }
  213. if ruMina <= 0 {
  214. if ruMinaInter, ok := msgData["rumina"]; ok {
  215. if ruMina32, ok := ruMinaInter.(string); ok {
  216. ruMina, _ = strconv.ParseInt(ruMina32, 10, 64)
  217. }
  218. }
  219. }
  220. intake := int64(0)
  221. if intakeInter, ok := msgData["Intake"]; ok {
  222. if intake32, ok := intakeInter.(string); ok {
  223. intake, _ = strconv.ParseInt(intake32, 10, 64)
  224. }
  225. }
  226. if intake <= 0 {
  227. if intakeInter, ok := msgData["intake"]; ok {
  228. if intake32, ok := intakeInter.(string); ok {
  229. intake, _ = strconv.ParseInt(intake32, 10, 64)
  230. }
  231. }
  232. }
  233. gasp := int64(0)
  234. if gaspInter, ok := msgData["gasp"]; ok {
  235. if gasp32, ok := gaspInter.(string); ok {
  236. gasp, _ = strconv.ParseInt(gasp32, 10, 64)
  237. }
  238. }
  239. reMain := int64(0)
  240. if reMainInter, ok := msgData["Remain"]; ok {
  241. if reMain32, ok := reMainInter.(string); ok {
  242. reMain, _ = strconv.ParseInt(reMain32, 10, 64)
  243. }
  244. }
  245. if ruMina <= 0 {
  246. if reMainInter, ok := msgData["remain"]; ok {
  247. if reMain32, ok := reMainInter.(string); ok {
  248. reMain, _ = strconv.ParseInt(reMain32, 10, 64)
  249. }
  250. }
  251. }
  252. /*cowId := ""
  253. if cowIdInter, ok := msgData["cowid"]; ok {
  254. if cowIdStr, ok := cowIdInter.(string); ok {
  255. cowId = cowIdStr
  256. }
  257. }
  258. csq := int64(0)
  259. if csqInter, ok := msgData["csq"]; ok {
  260. if csq32, ok := csqInter.(string); ok {
  261. csq, _ = strconv.ParseInt(csq32, 10, 64)
  262. }
  263. }
  264. other := int64(0)
  265. if otherInter, ok := msgData["other"]; ok {
  266. if other32, ok := otherInter.(string); ok {
  267. other, _ = strconv.ParseInt(other32, 10, 64)
  268. }
  269. }
  270. nccId := ""
  271. if nccIdInter, ok := msgData["nccid"]; ok {
  272. if nccIdStr, ok := nccIdInter.(string); ok {
  273. nccId = nccIdStr
  274. }
  275. }
  276. */
  277. return []*model.NeckRingOriginal{
  278. {
  279. FirmwareVersion: int32(softVer),
  280. Uuid: uuid,
  281. Frameid: int32(frameId),
  282. ReceiveNumber: imei,
  283. Active: int32(active),
  284. Inactive: int32(inAction),
  285. Rumina: int32(ruMina),
  286. Intake: int32(intake),
  287. Gasp: int32(gasp),
  288. Remain: int32(reMain),
  289. },
  290. }
  291. }