mqtt_handle.go 8.4 KB

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