sub.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. }
  65. defer func() {
  66. if time.Now().Day()%15 == 0 {
  67. d.DB.Model(new(model.SubMsgLog)).Where("created_at < ?", time.Now().AddDate(0, 0, -10).Unix()).Delete(new(model.SubMsgLog))
  68. return
  69. }
  70. }()
  71. if err = d.DB.Table(new(model.SubMsgLog).TableName()).Create(subMsgLog).Error; err != nil {
  72. zaplog.Error("CreatMsgLog", zap.Any("err", err), zap.Any("msg", string(msg)), zap.Any("subMsgLog", subMsgLog))
  73. }
  74. }
  75. func (d *DataEventEntry) MsgDataFormat(msg []byte) (*model.SubMsgLog, error) {
  76. msgData := make(map[string]interface{})
  77. pairs := strings.Split(util.MsgFormat(string(msg)), " ")
  78. for _, pair := range pairs {
  79. parts := strings.SplitN(pair, ":", 2)
  80. if len(parts) != 2 {
  81. continue
  82. }
  83. key, value := parts[0], parts[1]
  84. if len(key) == 0 {
  85. continue
  86. }
  87. msgData[key] = value
  88. }
  89. softVer := int64(0)
  90. if softVerInter, ok := msgData["SOFT_VER"]; ok {
  91. if softVerstr, ok := softVerInter.(string); ok {
  92. softVer, _ = strconv.ParseInt(softVerstr, 10, 64)
  93. }
  94. }
  95. uuid := ""
  96. if uuidInter, ok := msgData["uuid"]; ok {
  97. if uuidStr, ok := uuidInter.(string); ok {
  98. uuid = uuidStr
  99. }
  100. }
  101. frameId := int64(0)
  102. if frameIdInter, ok := msgData["frameid"]; ok {
  103. if frameId64, ok := frameIdInter.(string); ok {
  104. frameId, _ = strconv.ParseInt(frameId64, 10, 64)
  105. }
  106. }
  107. cowId := ""
  108. if cowIdInter, ok := msgData["cowid"]; ok {
  109. if cowIdStr, ok := cowIdInter.(string); ok {
  110. cowId = cowIdStr
  111. }
  112. }
  113. csq := int64(0)
  114. if csqInter, ok := msgData["csq"]; ok {
  115. if csq32, ok := csqInter.(string); ok {
  116. csq, _ = strconv.ParseInt(csq32, 10, 64)
  117. }
  118. }
  119. temp := float64(0)
  120. if tempInter, ok := msgData["Temp"]; ok {
  121. if tempFloat, ok := tempInter.(string); ok {
  122. temp, _ = strconv.ParseFloat(tempFloat, 64)
  123. }
  124. }
  125. imei := ""
  126. if imeiInter, ok := msgData["imei"]; ok {
  127. if imeiStr, ok := imeiInter.(string); ok {
  128. imei = imeiStr
  129. }
  130. }
  131. active := int64(0)
  132. if activeInter, ok := msgData["active"]; ok {
  133. if active32, ok := activeInter.(string); ok {
  134. active, _ = strconv.ParseInt(active32, 10, 64)
  135. }
  136. }
  137. inAction := int64(0)
  138. if inActionInter, ok := msgData["inactive"]; ok {
  139. if inAction32, ok := inActionInter.(string); ok {
  140. inAction, _ = strconv.ParseInt(inAction32, 10, 64)
  141. }
  142. }
  143. ruMina := int64(0)
  144. if ruMinaInter, ok := msgData["Rumina"]; ok {
  145. if ruMina32, ok := ruMinaInter.(string); ok {
  146. ruMina, _ = strconv.ParseInt(ruMina32, 10, 64)
  147. }
  148. }
  149. intake := int64(0)
  150. if intakeInter, ok := msgData["Intake"]; ok {
  151. if intake32, ok := intakeInter.(string); ok {
  152. intake, _ = strconv.ParseInt(intake32, 10, 64)
  153. }
  154. }
  155. gasp := int64(0)
  156. if gaspInter, ok := msgData["gasp"]; ok {
  157. if gasp32, ok := gaspInter.(string); ok {
  158. gasp, _ = strconv.ParseInt(gasp32, 10, 64)
  159. }
  160. }
  161. other := int64(0)
  162. if otherInter, ok := msgData["other"]; ok {
  163. if other32, ok := otherInter.(string); ok {
  164. other, _ = strconv.ParseInt(other32, 10, 64)
  165. }
  166. }
  167. reMain := int64(0)
  168. if reMainInter, ok := msgData["Remain"]; ok {
  169. if reMain32, ok := reMainInter.(string); ok {
  170. reMain, _ = strconv.ParseInt(reMain32, 10, 64)
  171. }
  172. }
  173. return &model.SubMsgLog{
  174. SoftVer: softVer,
  175. Uuid: uuid,
  176. FrameId: frameId,
  177. CowId: cowId,
  178. Csq: csq,
  179. Temp: int64(temp * 100),
  180. Imei: imei,
  181. Active: int32(active),
  182. InActive: int32(inAction),
  183. RuMina: int32(ruMina),
  184. Intake: int32(intake),
  185. Gasp: int32(gasp),
  186. Other: int32(other),
  187. ReMain: int32(reMain),
  188. }, nil
  189. }