mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
规则链
This commit is contained in:
@@ -13,6 +13,8 @@ import (
|
||||
type externalMqttNode struct {
|
||||
bareNode
|
||||
TopicPattern string `json:"topicPattern"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
ConnectTimeoutSec int `json:"connectTimeoutSec"`
|
||||
@@ -28,38 +30,55 @@ func (f externalMqttNodeFactory) Name() string { return "MqttNode" }
|
||||
func (f externalMqttNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
|
||||
func (f externalMqttNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
|
||||
func (f externalMqttNodeFactory) Create(id string, meta Metadata) (Node, error) {
|
||||
|
||||
node := &externalMqttNode{
|
||||
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
|
||||
}
|
||||
_, err := decodePath(meta, node)
|
||||
if err != nil {
|
||||
return node, err
|
||||
}
|
||||
broker := fmt.Sprintf("tcp://%s:%s", node.Host, node.Port)
|
||||
opts := mqtt.NewClientOptions().AddBroker(broker)
|
||||
opts.SetClientID(node.ClientId)
|
||||
if node.ClientId != "" {
|
||||
opts.SetClientID(node.ClientId)
|
||||
}
|
||||
opts.SetCleanSession(node.CleanSession)
|
||||
opts.SetConnectTimeout(time.Duration(node.ConnectTimeoutSec) * time.Second)
|
||||
if node.Username != "" {
|
||||
opts.SetUsername(node.Username)
|
||||
}
|
||||
if node.Password != "" {
|
||||
opts.SetPassword(node.Password)
|
||||
}
|
||||
node.MqttCli = mqtt.NewClient(opts)
|
||||
|
||||
if token := node.MqttCli.Connect(); token.Wait() && token.Error() != nil {
|
||||
logrus.WithError(token.Error())
|
||||
return nil, token.Error()
|
||||
}
|
||||
return decodePath(meta, node)
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func (n *externalMqttNode) Handle(msg message.Message) error {
|
||||
logrus.Infof("%s handle message '%s'", n.Name(), msg.GetType())
|
||||
|
||||
defer n.MqttCli.Disconnect(1000)
|
||||
successLabelNode := n.GetLinkedNode("Success")
|
||||
failureLabelNode := n.GetLinkedNode("Failure")
|
||||
topic := n.TopicPattern //need fix add msg.metadata in it
|
||||
sendmqttmsg, err := json.Marshal(msg.GetMsg())
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
token := n.MqttCli.Publish(topic, 1, false, sendmqttmsg)
|
||||
if token.Wait() && token.Error() != nil {
|
||||
fmt.Println(token.Error())
|
||||
return failureLabelNode.Handle(msg)
|
||||
if failureLabelNode != nil {
|
||||
return failureLabelNode.Handle(msg)
|
||||
} else {
|
||||
return token.Error()
|
||||
}
|
||||
}
|
||||
return successLabelNode.Handle(msg)
|
||||
if successLabelNode != nil {
|
||||
return successLabelNode.Handle(msg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user