sub.go 5.1 KB

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