规则链

This commit is contained in:
XM-GO
2023-04-18 16:29:26 +08:00
parent db5a45590a
commit 5dfa341083
46 changed files with 980 additions and 381 deletions

View File

@@ -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
}