|
@@ -7,7 +7,6 @@ import (
|
|
|
"go.uber.org/zap"
|
|
|
"kpt-temporary-mqtt/config"
|
|
|
"kpt-temporary-mqtt/model"
|
|
|
- "kpt-temporary-mqtt/store/kptstore"
|
|
|
"kpt-temporary-mqtt/util"
|
|
|
"strconv"
|
|
|
"strings"
|
|
@@ -25,7 +24,7 @@ var connectLostHandler golangMqtt.ConnectionLostHandler = func(client golangMqtt
|
|
|
zaplog.Info("connectLost", zap.Any("err", err.Error()))
|
|
|
}
|
|
|
|
|
|
-func NewMqtt(configOption *config.AppConfig) golangMqtt.Client {
|
|
|
+func (d *DataEventEntry) NewMqtt(configOption *config.AppConfig) golangMqtt.Client {
|
|
|
opts := golangMqtt.NewClientOptions()
|
|
|
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", configOption.Broker, configOption.Port))
|
|
|
opts.SetClientID(util.RandString(6))
|
|
@@ -41,7 +40,7 @@ func NewMqtt(configOption *config.AppConfig) golangMqtt.Client {
|
|
|
return client
|
|
|
}
|
|
|
|
|
|
-func SubMsg(configOption *config.AppConfig, client golangMqtt.Client) {
|
|
|
+func (d *DataEventEntry) SubMsgLog(configOption *config.AppConfig, client golangMqtt.Client) {
|
|
|
var subMsgChan = make(chan []byte, configOption.WorkNumber)
|
|
|
client.Subscribe(configOption.SubTopName, 1, func(client golangMqtt.Client, msg golangMqtt.Message) {
|
|
|
subMsgChan <- msg.Payload()
|
|
@@ -52,26 +51,27 @@ func SubMsg(configOption *config.AppConfig, client golangMqtt.Client) {
|
|
|
for {
|
|
|
select {
|
|
|
case msg := <-subMsgChan:
|
|
|
- CreatMsgLog(msg)
|
|
|
+ d.CreatMsgLog(msg)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
//}()
|
|
|
//}
|
|
|
}
|
|
|
|
|
|
-func CreatMsgLog(msg []byte) {
|
|
|
- subMsgLog, err := MsgDataFormat(msg)
|
|
|
+func (d *DataEventEntry) CreatMsgLog(msg []byte) {
|
|
|
+ subMsgLog, err := d.MsgDataFormat(msg)
|
|
|
if err != nil {
|
|
|
zaplog.Error("CreatMsgLog", zap.Any("err", err), zap.Any("msg", string(msg)))
|
|
|
}
|
|
|
|
|
|
- if err := &kptstore.DB.Table(new(model.SubMsgLog).TableName()).Create(subMsgLog).Error; err != nil {
|
|
|
+ if err := d.DB.Table(new(model.SubMsgLog).TableName()).Create(subMsgLog).Error; err != nil {
|
|
|
fmt.Println("msg", string(msg))
|
|
|
fmt.Println("subMsgLog", subMsgLog)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func MsgDataFormat(msg []byte) (*model.SubMsgLog, error) {
|
|
|
+func (d *DataEventEntry) MsgDataFormat(msg []byte) (*model.SubMsgLog, error) {
|
|
|
msgData := make(map[string]interface{})
|
|
|
pairs := strings.Split(util.MsgFormat(string(msg)), " ")
|
|
|
for _, pair := range pairs {
|