mqtt_handle.go 7.8 KB

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