sub.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package mqtt
  2. import (
  3. "fmt"
  4. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  5. golangMqtt "github.com/eclipse/paho.mqtt.golang"
  6. "go.uber.org/zap"
  7. "kpt-temporary-mqtt/config"
  8. "kpt-temporary-mqtt/model"
  9. "kpt-temporary-mqtt/util"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. var messagePubHandler golangMqtt.MessageHandler = func(client golangMqtt.Client, msg golangMqtt.Message) {
  15. zaplog.Info("messagePubHandlerReceived", zap.Any("message", string(msg.Payload())), zap.Any("topic", msg.Topic()))
  16. }
  17. var connectHandler golangMqtt.OnConnectHandler = func(client golangMqtt.Client) {
  18. zaplog.Info("connectedClient", zap.Any("client", client))
  19. }
  20. var connectLostHandler golangMqtt.ConnectionLostHandler = func(client golangMqtt.Client, err error) {
  21. zaplog.Info("connectLost", zap.Any("err", err.Error()))
  22. }
  23. func (d *DataEventEntry) NewMqtt(configOption *config.AppConfig) golangMqtt.Client {
  24. opts := golangMqtt.NewClientOptions()
  25. opts.AddBroker(fmt.Sprintf("tcp://%s:%d", configOption.Broker, configOption.Port))
  26. opts.SetClientID(util.RandString(6))
  27. opts.SetUsername(configOption.UserName)
  28. opts.SetPassword(configOption.Password)
  29. opts.SetDefaultPublishHandler(messagePubHandler)
  30. opts.OnConnect = connectHandler
  31. opts.OnConnectionLost = connectLostHandler
  32. client := golangMqtt.NewClient(opts)
  33. if token := client.Connect(); token.Wait() && token.Error() != nil {
  34. panic(token.Error())
  35. }
  36. return client
  37. }
  38. func (d *DataEventEntry) SubMsgLog(configOption *config.AppConfig, client golangMqtt.Client) {
  39. var subMsgChan = make(chan []byte, configOption.WorkNumber)
  40. client.Subscribe(configOption.SubTopName, 1, func(client golangMqtt.Client, msg golangMqtt.Message) {
  41. subMsgChan <- msg.Payload()
  42. })
  43. //for i := 0; i < int(configOption.WorkNumber); i++ {
  44. //go func() {
  45. for {
  46. select {
  47. case msg := <-subMsgChan:
  48. d.CreatMsgLog(msg)
  49. }
  50. }
  51. //}()
  52. //}
  53. }
  54. func (d *DataEventEntry) CreatMsgLog(msg []byte) {
  55. subMsgLog, err := d.MsgDataFormat(msg)
  56. if err != nil {
  57. zaplog.Error("CreatMsgLog", zap.Any("err", err), zap.Any("msg", string(msg)))
  58. }
  59. if subMsgLog == nil {
  60. return
  61. }
  62. if subMsgLog.Imei == "" {
  63. zaplog.Info("CreatMsgLog", zap.Any("msg", string(msg)), zap.Any("subMsgLog", subMsgLog))
  64. return
  65. }
  66. defer func() {
  67. if time.Now().Day()%15 == 0 {
  68. d.DB.Model(new(model.SubMsgLog)).Where("created_at < ?", time.Now().AddDate(0, 0, -15).Unix()).Delete(new(model.SubMsgLog))
  69. return
  70. }
  71. }()
  72. if err = d.DB.Table(new(model.SubMsgLog).TableName()).Create(subMsgLog).Error; err != nil {
  73. zaplog.Error("CreatMsgLog", zap.Any("err", err), zap.Any("msg", string(msg)), zap.Any("subMsgLog", subMsgLog))
  74. }
  75. }
  76. func (d *DataEventEntry) MsgDataFormat(msg []byte) (*model.SubMsgLog, error) {
  77. msgData := make(map[string]interface{})
  78. pairs := strings.Split(util.MsgFormat(string(msg)), " ")
  79. for _, pair := range pairs {
  80. parts := strings.SplitN(pair, ":", 2)
  81. if len(parts) != 2 {
  82. continue
  83. }
  84. key, value := parts[0], parts[1]
  85. if len(key) == 0 {
  86. continue
  87. }
  88. msgData[key] = value
  89. }
  90. softVer := int64(0)
  91. if softVerInter, ok := msgData["SOFT_VER"]; ok {
  92. if softVerstr, ok := softVerInter.(string); ok {
  93. softVer, _ = strconv.ParseInt(softVerstr, 10, 64)
  94. }
  95. }
  96. uuid := ""
  97. if uuidInter, ok := msgData["uuid"]; ok {
  98. if uuidStr, ok := uuidInter.(string); ok {
  99. uuid = uuidStr
  100. }
  101. }
  102. frameId := int64(0)
  103. if frameIdInter, ok := msgData["frameid"]; ok {
  104. if frameId64, ok := frameIdInter.(string); ok {
  105. frameId, _ = strconv.ParseInt(frameId64, 10, 64)
  106. }
  107. }
  108. cowId := ""
  109. if cowIdInter, ok := msgData["cowid"]; ok {
  110. if cowIdStr, ok := cowIdInter.(string); ok {
  111. cowId = cowIdStr
  112. }
  113. }
  114. csq := int64(0)
  115. if csqInter, ok := msgData["csq"]; ok {
  116. if csq32, ok := csqInter.(string); ok {
  117. csq, _ = strconv.ParseInt(csq32, 10, 64)
  118. }
  119. }
  120. temp := float64(0)
  121. if tempInter, ok := msgData["Temp"]; ok {
  122. if tempFloat, ok := tempInter.(string); ok {
  123. temp, _ = strconv.ParseFloat(tempFloat, 64)
  124. }
  125. }
  126. imei := ""
  127. if imeiInter, ok := msgData["imei"]; ok {
  128. if imeiStr, ok := imeiInter.(string); ok {
  129. imei = imeiStr
  130. }
  131. }
  132. active := int64(0)
  133. if activeInter, ok := msgData["active"]; ok {
  134. if active32, ok := activeInter.(string); ok {
  135. active, _ = strconv.ParseInt(active32, 10, 64)
  136. }
  137. }
  138. inAction := int64(0)
  139. if inActionInter, ok := msgData["inactive"]; ok {
  140. if inAction32, ok := inActionInter.(string); ok {
  141. inAction, _ = strconv.ParseInt(inAction32, 10, 64)
  142. }
  143. }
  144. ruMina := int64(0)
  145. if ruMinaInter, ok := msgData["Rumina"]; ok {
  146. if ruMina32, ok := ruMinaInter.(string); ok {
  147. ruMina, _ = strconv.ParseInt(ruMina32, 10, 64)
  148. }
  149. }
  150. intake := int64(0)
  151. if intakeInter, ok := msgData["Intake"]; ok {
  152. if intake32, ok := intakeInter.(string); ok {
  153. intake, _ = strconv.ParseInt(intake32, 10, 64)
  154. }
  155. }
  156. gasp := int64(0)
  157. if gaspInter, ok := msgData["gasp"]; ok {
  158. if gasp32, ok := gaspInter.(string); ok {
  159. gasp, _ = strconv.ParseInt(gasp32, 10, 64)
  160. }
  161. }
  162. other := int64(0)
  163. if otherInter, ok := msgData["other"]; ok {
  164. if other32, ok := otherInter.(string); ok {
  165. other, _ = strconv.ParseInt(other32, 10, 64)
  166. }
  167. }
  168. reMain := int64(0)
  169. if reMainInter, ok := msgData["Remain"]; ok {
  170. if reMain32, ok := reMainInter.(string); ok {
  171. reMain, _ = strconv.ParseInt(reMain32, 10, 64)
  172. }
  173. }
  174. return &model.SubMsgLog{
  175. SoftVer: softVer,
  176. Uuid: uuid,
  177. FrameId: frameId,
  178. CowId: cowId,
  179. Csq: csq,
  180. Temp: int64(temp * 100),
  181. Imei: imei,
  182. Active: int32(active),
  183. InActive: int32(inAction),
  184. RuMina: int32(ruMina),
  185. Intake: int32(intake),
  186. Gasp: int32(gasp),
  187. Other: int32(other),
  188. ReMain: int32(reMain),
  189. }, nil
  190. }