|
@@ -8,6 +8,7 @@ import (
|
|
"github.com/emqx/kuiper/common"
|
|
"github.com/emqx/kuiper/common"
|
|
"github.com/emqx/kuiper/xstream/api"
|
|
"github.com/emqx/kuiper/xstream/api"
|
|
"github.com/google/uuid"
|
|
"github.com/google/uuid"
|
|
|
|
+ "path"
|
|
"strconv"
|
|
"strconv"
|
|
"strings"
|
|
"strings"
|
|
)
|
|
)
|
|
@@ -22,6 +23,7 @@ type MQTTSource struct {
|
|
certPath string
|
|
certPath string
|
|
pkeyPath string
|
|
pkeyPath string
|
|
|
|
|
|
|
|
+ model modelVersion
|
|
schema map[string]interface{}
|
|
schema map[string]interface{}
|
|
conn MQTT.Client
|
|
conn MQTT.Client
|
|
}
|
|
}
|
|
@@ -36,6 +38,8 @@ type MQTTConfig struct {
|
|
Password string `json:"password"`
|
|
Password string `json:"password"`
|
|
Certification string `json:"certificationPath"`
|
|
Certification string `json:"certificationPath"`
|
|
PrivateKPath string `json:"privateKeyPath"`
|
|
PrivateKPath string `json:"privateKeyPath"`
|
|
|
|
+ KubeedgeModelFile string `json:"kubeedgeModelFile"`
|
|
|
|
+ KubeedgeVersion string `json:"kubeedgeVersion"`
|
|
}
|
|
}
|
|
|
|
|
|
func (ms *MQTTSource) WithSchema(schema string) *MQTTSource {
|
|
func (ms *MQTTSource) WithSchema(schema string) *MQTTSource {
|
|
@@ -66,6 +70,18 @@ func (ms *MQTTSource) Configure(topic string, props map[string]interface{}) erro
|
|
ms.password = strings.Trim(cfg.Password, " ")
|
|
ms.password = strings.Trim(cfg.Password, " ")
|
|
ms.certPath = cfg.Certification
|
|
ms.certPath = cfg.Certification
|
|
ms.pkeyPath = cfg.PrivateKPath
|
|
ms.pkeyPath = cfg.PrivateKPath
|
|
|
|
+
|
|
|
|
+ if 0 != len(cfg.KubeedgeModelFile) {
|
|
|
|
+ conf, err := common.LoadConf(path.Join("sources", cfg.KubeedgeModelFile))
|
|
|
|
+ if nil != err {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ ms.model = modelFactory(cfg.KubeedgeVersion)
|
|
|
|
+ if err = json.Unmarshal(conf, ms.model); err != nil {
|
|
|
|
+ ms.model = nil
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -120,7 +136,7 @@ func (ms *MQTTSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTupl
|
|
opts.SetConnectionLostHandler(func(client MQTT.Client, e error) {
|
|
opts.SetConnectionLostHandler(func(client MQTT.Client, e error) {
|
|
log.Errorf("The connection %s is disconnected due to error %s, will try to re-connect later.", ms.srv+": "+ms.clientid, e)
|
|
log.Errorf("The connection %s is disconnected due to error %s, will try to re-connect later.", ms.srv+": "+ms.clientid, e)
|
|
reconn = true
|
|
reconn = true
|
|
- subscribe(ms.tpc, client, ctx, consumer)
|
|
|
|
|
|
+ subscribe(ms.tpc, client, ctx, consumer, ms.model)
|
|
})
|
|
})
|
|
|
|
|
|
opts.SetOnConnectHandler(func(client MQTT.Client) {
|
|
opts.SetOnConnectHandler(func(client MQTT.Client) {
|
|
@@ -135,11 +151,11 @@ func (ms *MQTTSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTupl
|
|
}
|
|
}
|
|
log.Infof("The connection to server %s was established successfully", ms.srv)
|
|
log.Infof("The connection to server %s was established successfully", ms.srv)
|
|
ms.conn = c
|
|
ms.conn = c
|
|
- subscribe(ms.tpc, c, ctx, consumer)
|
|
|
|
|
|
+ subscribe(ms.tpc, c, ctx, consumer, ms.model)
|
|
log.Infof("Successfully subscribe to topic %s", ms.srv+": "+ms.clientid)
|
|
log.Infof("Successfully subscribe to topic %s", ms.srv+": "+ms.clientid)
|
|
}
|
|
}
|
|
|
|
|
|
-func subscribe(topic string, client MQTT.Client, ctx api.StreamContext, consumer chan<- api.SourceTuple) {
|
|
|
|
|
|
+func subscribe(topic string, client MQTT.Client, ctx api.StreamContext, consumer chan<- api.SourceTuple, model modelVersion) {
|
|
log := ctx.GetLogger()
|
|
log := ctx.GetLogger()
|
|
h := func(client MQTT.Client, msg MQTT.Message) {
|
|
h := func(client MQTT.Client, msg MQTT.Message) {
|
|
log.Debugf("instance %d received %s", ctx.GetInstanceId(), msg.Payload())
|
|
log.Debugf("instance %d received %s", ctx.GetInstanceId(), msg.Payload())
|
|
@@ -154,6 +170,13 @@ func subscribe(topic string, client MQTT.Client, ctx api.StreamContext, consumer
|
|
meta["topic"] = msg.Topic()
|
|
meta["topic"] = msg.Topic()
|
|
meta["messageid"] = strconv.Itoa(int(msg.MessageID()))
|
|
meta["messageid"] = strconv.Itoa(int(msg.MessageID()))
|
|
|
|
|
|
|
|
+ if nil != model {
|
|
|
|
+ sliErr := model.checkType(result, msg.Topic())
|
|
|
|
+ for _, v := range sliErr {
|
|
|
|
+ log.Errorf(v)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
select {
|
|
select {
|
|
case consumer <- api.NewDefaultSourceTuple(result, meta):
|
|
case consumer <- api.NewDefaultSourceTuple(result, meta):
|
|
log.Debugf("send data to source node")
|
|
log.Debugf("send data to source node")
|