From 5e67f4111445ebe0150ea6bdc82b5564db01260f Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Sun, 3 Nov 2013 21:29:20 +0000 Subject: [PATCH] Add rawserial, mpd, mysql and swearfilter nodes. --- analysis/swearfilter/74-swearfilter.html | 46 +++++++ analysis/swearfilter/74-swearfilter.js | 28 +++++ io/rawserial/26-rawserial.html | 102 ++++++++++++++++ io/rawserial/26-rawserial.js | 112 +++++++++++++++++ social/music/69-mpd.html | 80 ++++++++++++ social/music/69-mpd.js | 84 +++++++++++++ social/music/icons/music.png | Bin 0 -> 1103 bytes storage/mysql/68-mysql.html | 147 +++++++++++++++++++++++ storage/mysql/68-mysql.js | 108 +++++++++++++++++ 9 files changed, 707 insertions(+) create mode 100644 analysis/swearfilter/74-swearfilter.html create mode 100644 analysis/swearfilter/74-swearfilter.js create mode 100644 io/rawserial/26-rawserial.html create mode 100644 io/rawserial/26-rawserial.js create mode 100644 social/music/69-mpd.html create mode 100644 social/music/69-mpd.js create mode 100644 social/music/icons/music.png create mode 100644 storage/mysql/68-mysql.html create mode 100644 storage/mysql/68-mysql.js diff --git a/analysis/swearfilter/74-swearfilter.html b/analysis/swearfilter/74-swearfilter.html new file mode 100644 index 00000000..fd0abeb5 --- /dev/null +++ b/analysis/swearfilter/74-swearfilter.html @@ -0,0 +1,46 @@ + + + + + + + diff --git a/analysis/swearfilter/74-swearfilter.js b/analysis/swearfilter/74-swearfilter.js new file mode 100644 index 00000000..e1a32568 --- /dev/null +++ b/analysis/swearfilter/74-swearfilter.js @@ -0,0 +1,28 @@ +/** + * Copyright 2013 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var RED = require("../../red/red"); +var badwords = require('badwords'); + +function BadwordsNode(n) { + RED.nodes.createNode(this,n); + var node = this; + this.on("input", function(msg) { + if (badwords.ok(msg.payload)) { node.send(msg); } + }); +} + +RED.nodes.registerType("badwords",BadwordsNode); diff --git a/io/rawserial/26-rawserial.html b/io/rawserial/26-rawserial.html new file mode 100644 index 00000000..55c622f9 --- /dev/null +++ b/io/rawserial/26-rawserial.html @@ -0,0 +1,102 @@ + + + + + + + + + + + + + diff --git a/io/rawserial/26-rawserial.js b/io/rawserial/26-rawserial.js new file mode 100644 index 00000000..9bccefac --- /dev/null +++ b/io/rawserial/26-rawserial.js @@ -0,0 +1,112 @@ +/** + * Copyright 2013 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var RED = require("../../red/red"); +var settings = RED.settings; +var util = require("util"); +var fs = require('fs'); +var plat = require('os').platform(); +var pre = "\\\\.\\"; + +if (! plat.match(/^win/)) { + util.log("[26-rawserial.js] Advise: Only really needed for Windows boxes without serialport npm module installed."); + pre = ""; +} + +function RawSerialInNode(n) { + RED.nodes.createNode(this,n); + this.port = n.port; + this.split = n.split||null; + if (this.split == '\\n') this.split = "\n"; + if (this.split == '\\r') this.split = "\r"; + var node = this; + + var setupSerial = function() { + node.inp = fs.createReadStream(pre+node.port); + node.inp.setEncoding('utf8'); + var line = ""; + node.inp.on('data', function (data) { + if (node.split != null) { + if (data == node.split) { + node.send({payload:line}); + line = ""; + } + else { line += data; } + } + else { node.send({payload:data}); } + }); + //node.inp.on('end', function (error) {console.log("End", error);}); + node.inp.on('close', function (error) { + util.log("[rawserial] "+node.port+" closed"); + node.tout = setTimeout(function() { + setupSerial(); + },settings.serialReconnectTime); + }); + node.inp.on('error', function(error) { + if (error.code == "ENOENT") { util.log("[rawserial] port "+node.port+" not found"); } + else { util.log("[rawserial] "+node.port+" error "+error); } + node.tout = setTimeout(function() { + setupSerial(); + },settings.serialReconnectTime); + }); + } + setupSerial(); + + node.on('close', function() { + if (node.tout) { clearTimeout(node.tout); } + if (node.inp) { node.inp.pause(); } + }); + +} +RED.nodes.registerType("rawserial in",RawSerialInNode); + + +function RawSerialOutNode(n) { + RED.nodes.createNode(this,n); + this.port = n.port; + var node = this; + + var setupSerial = function() { + node.oup = fs.createWriteStream(pre+node.port,{ flags:'w', encoding:'utf8', mode:0666 }); + node.on("input", function(msg) { + if (msg.payload != null) { + node.oup.write(msg.payload); + } + }); + node.oup.on('open', function (error) { util.log("[rawserial] opened "+node.port); }); + node.oup.on('end', function (error) { console.log("End",error); }); + node.oup.on('close', function (error) { + util.log("[rawserial] "+node.port+" closed"); + node.tout = setTimeout(function() { + setupSerial(); + },settings.serialReconnectTime); + }); + node.oup.on('error', function(error) { + if (error.code == "EACCES") { util.log("[rawserial] can't access port "+node.port); } + else if (error.code == "EIO") { util.log("[rawserial] can't write to port "+node.port); } + else { util.log("[rawserial] "+node.port+" error "+error); } + node.tout = setTimeout(function() { + setupSerial(); + },settings.serialReconnectTime); + }); + } + setupSerial(); + + node.on('close', function() { + if (node.tout) { clearTimeout(node.tout); } + }); +} +RED.nodes.registerType("rawserial out",RawSerialOutNode); diff --git a/social/music/69-mpd.html b/social/music/69-mpd.html new file mode 100644 index 00000000..5ca4f60b --- /dev/null +++ b/social/music/69-mpd.html @@ -0,0 +1,80 @@ + + + + + + + + + + + + + diff --git a/social/music/69-mpd.js b/social/music/69-mpd.js new file mode 100644 index 00000000..2afc91fd --- /dev/null +++ b/social/music/69-mpd.js @@ -0,0 +1,84 @@ +/** + * Copyright 2013 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var RED = require(process.env.NODE_RED_HOME+"/red/red"); +var komponist = require('komponist'); + +var mpc = ""; +komponist.createConnection(6600, 'localhost', function(err, client) { + if (err) node.error("MPD: Failed to connect to MPD server"); + mpc = client; +}); + +function MPDOut(n) { + RED.nodes.createNode(this,n); + var node = this; + node.mpc = mpc; + + this.on("input", function(msg) { + if (msg != null) { + console.log(msg); + try { + //node.mpc.command(msg.payload); + node.mpc.command(msg.payload, msg.param, function(err, results) { + if (err) { console.log("MPD: Error:",err); } + else { node.error(results); } + }); + } catch(err) { console.log("MPD: Error:",err); } + } + }); + + node.mpc.on('error', function(err) { + console.log("MPD: Error:",err); + }); +} +RED.nodes.registerType("mpd out",MPDOut); + +function MPDIn(n) { + RED.nodes.createNode(this,n); + var node = this; + node.mpc = mpc; + var oldMsg = ""; + + getSong(); + + function getSong() { + node.mpc.currentsong(function(err, info) { + if (err) console.log(err); + else { + var msg = {payload:{},topic:"music"}; + msg.payload.Artist = info.Artist; + msg.payload.Album = info.Album; + msg.payload.Title = info.Title; + msg.payload.Genre = info.Genre; + msg.payload.Date = info.Date; + if (JSON.stringify(msg) != oldMsg) { + node.send(msg); + oldMsg = JSON.stringify(msg); + } + } + }); + } + + node.mpc.on('changed', function(system) { + getSong(); + }); + + this.on("close", function() { + // node.mpc.command("stop"); + }); +} +RED.nodes.registerType("mpd in",MPDIn); diff --git a/social/music/icons/music.png b/social/music/icons/music.png new file mode 100644 index 0000000000000000000000000000000000000000..4e99a1bf3f678deb6ac950a03e10309d313a1d8e GIT binary patch literal 1103 zcmV-V1hD&wP)Px#24YJ`L;wH)0002_L%V+f000SaNLh0L01FcU01FcV0GgZ_00007bV*G`2i*$+ z2^S&jBV1wt00YlSL_t(o!|j+$h#XZEhQCu&-I*Z?f*40!ge;VR2w7+}32qd@5H%1) z%|=8a_}UoIfFXo{Ac8wr3Bu@ z_eeSo^!)>Bg}V)ahk!2}*k0frV76|tv;lBEa2IgKf8Xk~(@nyxal$<*z_JOJfevsV z@RGM(2`m8DT`Vs)Q6yz;888Pt2^{gZ^}t8K?od?OC@;1V;=;2lUcD!QeWCqz;E-3i z)*=T99?Jdt47d$gbWqPodcr@VGAFZ^yjP6762Sff+~PDO;5p!a5C2T)r?vrFl8c>W z;(fqh{(K6!I;=*6G_TtVu?6q-{I1}o1Hs#8Tm{9Isd0d5?wkj10QNg4ZgHS;8afX= z?SM7_&xem_R4l}tE5yQ^t_@TB5qK0h=akb0jsn{q=oKOKt%i8d5c=2xyan9u|95~B zA?F@%lV%1bIe5^kvkN>Dir{cqQO*KCc%KK%Y;Jw1xB z{si14X*N`JwVE-r9`FWm`J|@;$x4#}-v$t;J#7{poQ*0HxvL*`CDOF~>btVO7q|p? z0a%x4KpO^7KxSr3k{)YOVfTSo0;rx>?pS-0qP(wd7+{uAM{yKley?1e@)pq}Yy<{J_V-4UzI*cY?^ifA6(>kyInFxm19lhV0tW82!IX1ap3nM z@8C-LBybt002ovPDHLkV1jK|@Z + + + + + + + + + + + + + + diff --git a/storage/mysql/68-mysql.js b/storage/mysql/68-mysql.js new file mode 100644 index 00000000..01bf646b --- /dev/null +++ b/storage/mysql/68-mysql.js @@ -0,0 +1,108 @@ +/** + * Copyright 2013 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var RED = require(process.env.NODE_RED_HOME+"/red/red"); +var mysqldb = require('mysql'); + +function MySQLNode(n) { + RED.nodes.createNode(this,n); + this.host = n.host; + this.port = n.port; + this.user = n.user; + this.password = n.pass; + this.dbname = n.db; + var node = this; + + node.connection = mysqldb.createConnection({ + host : node.host, + port : node.port, + user : node.user, + password : node.password, + database : node.dbname + }); + + node.connection.connect(function(err) { + if (err) node.error(err); + }); + + node.on('close', function () { + node.connection.end(function(err) { + if (err) node.error(err); + }); + }); +} +RED.nodes.registerType("MySQLdatabase",MySQLNode); + + +function MysqlDBNodeIn(n) { + RED.nodes.createNode(this,n); + this.mydb = n.mydb; + this.mydbConfig = RED.nodes.getNode(this.mydb); + + if (this.mydbConfig) { + var node = this; + node.on("input", function(msg) { + if (typeof msg.topic === 'string') { + //console.log("query:",msg.topic); + node.mydbConfig.connection.query(msg.topic, function(err, rows) { + if (err) { node.warn(err); } + else { + msg.payload = rows; + node.send(msg); + } + }); + } + else { + if (typeof msg.topic !== 'string') node.error("msg.topic : the query is not defined as a string"); + } + }); + } + else { + this.error("MySQL database not configured"); + } +} +RED.nodes.registerType("mysql",MysqlDBNodeIn); + + +//function MysqlDBNodeOut(n) { + //RED.nodes.createNode(this,n); + //this.level = n.level; + //this.operation = n.operation; + //this.levelConfig = RED.nodes.getNode(this.level); + + //if (this.levelConfig) { + //var node = this; + //node.on("input", function(msg) { + //if (typeof msg.topic === 'string') { + //if (node.operation === "delete") { + //node.levelConfig.db.del(msg.topic); + //} + //else { + //node.levelConfig.db.put(msg.topic, msg.payload, function(err) { + //if (err) node.error(err); + //}); + //} + //} + //else { + //if (typeof msg.topic !== 'string') node.error("msg.topic : the key is not defined"); + //} + //}); + //} + //else { + //this.error("MySQL database not configured"); + //} +//} +//RED.nodes.registerType("mysql out",MysqlDBNodeOut);