From d712a6532655e43ef1d23424b500ee6928cc1249 Mon Sep 17 00:00:00 2001 From: Pietro Brenna Date: Fri, 21 Feb 2020 13:14:00 +0100 Subject: [PATCH] Aggiunti nodi per parsare comandi tipo pidgin --- command-parse.html | 287 +++++++++++++++++++++++++++++++++++++++++++++ command-parse.js | 65 ++++++++++ package.json | 3 +- 3 files changed, 354 insertions(+), 1 deletion(-) create mode 100644 command-parse.html create mode 100644 command-parse.js diff --git a/command-parse.html b/command-parse.html new file mode 100644 index 0000000..31b7bd7 --- /dev/null +++ b/command-parse.html @@ -0,0 +1,287 @@ + + + + + + + + + + + + + + diff --git a/command-parse.js b/command-parse.js new file mode 100644 index 0000000..695242c --- /dev/null +++ b/command-parse.js @@ -0,0 +1,65 @@ + +module.exports = function (RED) { + "use strict"; + + function ParserEntryPoint(n) { + RED.nodes.createNode(this, n); + this.property = n.property; + this.help_message = n.help_message; + this.on("input", msg => { + RED.util.evaluateNodeProperty(this.property, "msg", this, msg, + (err, value) => { + if (err) { + this.error(`Can't evaluate msg.${this.property}`); + } else { + let tokens = value.trim().toLowerCase().split(" "); + msg.token_parser = { + new_tokens: tokens, + consumed_tokens: [], + help_message: this.help_message + }; + this.send(msg); + }; + }); + }); + } + + RED.nodes.registerType("parser-entry-point", ParserEntryPoint); + + function ConsumaToken(n) { + RED.nodes.createNode(this, n); + this.rules = n.rules; + this.behavior= n.behavior; + this.checkall= n.checkall; + function check_rule(r, token) { + switch(r.t){ + case "eq": + return token == r.v.toLowerCase(); + case "regex": + let reg = new RegExp(r.v); + return token.match(reg); + } + } + this.on("input", msg => { + let token = msg.token_parser.new_tokens.shift() || ""; + msg.token_parser.consumed_tokens.push(token); + let out = []; + let n_matches = 0; + for (let r of this.rules) { + if(check_rule(r, token)){ + out.push(msg); + n_matches += 1; + if (!this.checkall) { + break; + } + } else { + out.push(undefined); + } + } + //out.push(n_matches == 0 ? msg : undefined); + this.send(out); + }); + } + RED.nodes.registerType("parser-consume", ConsumaToken); + +} diff --git a/package.json b/package.json index c516ef2..3e32aa3 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "node-red" : { "nodes" : { "xmpp": "92-xmpp.js", - "my-debounce": "my-debounce.js" + "my-debounce": "my-debounce.js", + "command-parse": "command-parse.js" } }, "author": {