From 3c393c472ca2e697d347e791b92bbe24c11cf069 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Sun, 20 Apr 2014 18:01:38 +0100 Subject: [PATCH] Add Notify My Android Node - with credentials support --- social/nma/57-nma.html | 88 +++++++++++++++++++++++++++++++++++++++ social/nma/57-nma.js | 81 +++++++++++++++++++++++++++++++++++ social/nma/icons/nma.png | Bin 0 -> 1625 bytes 3 files changed, 169 insertions(+) create mode 100644 social/nma/57-nma.html create mode 100644 social/nma/57-nma.js create mode 100644 social/nma/icons/nma.png diff --git a/social/nma/57-nma.html b/social/nma/57-nma.html new file mode 100644 index 00000000..c6f88d7b --- /dev/null +++ b/social/nma/57-nma.html @@ -0,0 +1,88 @@ + + + + + + + diff --git a/social/nma/57-nma.js b/social/nma/57-nma.js new file mode 100644 index 00000000..2c1e517e --- /dev/null +++ b/social/nma/57-nma.js @@ -0,0 +1,81 @@ +/** + * Copyright 2014 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 nma = require('nma'); +var util = require('util'); + +function NMANode(n) { + RED.nodes.createNode(this,n); + this.title = n.title; + var credentials = RED.nodes.getCredentials(n.id); + if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; } + else { this.error("No NMA API key set"); } + var node = this; + this.on("input",function(msg) { + var titl = this.title||msg.topic||"Node-RED"; + if (typeof(msg.payload) === 'object') { + msg.payload = JSON.stringify(msg.payload); + } + else { msg.payload = msg.payload.toString(); } + if (node.pushkey) { + try { + nma(node.pushkey, "Node-RED", titl, msg.payload, 0 ); + } catch (e) { + node.warn("NMA error: "+ e); + } + } + else { + node.warn("NMA credentials not set."); + } + }); +} + +RED.nodes.registerType("nma",NMANode); + +var querystring = require('querystring'); + +RED.httpAdmin.get('/nma/:id',function(req,res) { + var credentials = RED.nodes.getCredentials(req.params.id); + if (credentials) { + res.send(JSON.stringify({hasPassword:(credentials.pushkey&&credentials.pushkey!="")})); + } else { + res.send(JSON.stringify({})); + } +}); + +RED.httpAdmin.delete('/nma/:id',function(req,res) { + RED.nodes.deleteCredentials(req.params.id); + res.send(200); +}); + +RED.httpAdmin.post('/nma/:id',function(req,res) { + var body = ""; + req.on('data', function(chunk) { + body+=chunk; + }); + req.on('end', function(){ + var newCreds = querystring.parse(body); + var credentials = RED.nodes.getCredentials(req.params.id)||{}; + if (newCreds.pushkey == "") { + delete credentials.pushkey; + } else { + credentials.pushkey = newCreds.pushkey||credentials.pushkey; + } + RED.nodes.addCredentials(req.params.id,credentials); + res.send(200); + }); +}); diff --git a/social/nma/icons/nma.png b/social/nma/icons/nma.png new file mode 100644 index 0000000000000000000000000000000000000000..902f90f46411cdb4e49f7ec9c18f1ea1de97cb35 GIT binary patch literal 1625 zcmV-f2B!ImP)Px#24YJ`L;wH)0002_L%V+f000SaNLh0L01FcU01FcV0GgZ_000H-Nkl#n;q?YPdiJ0iQRx~9|Cg#CjMGZ9LKz&jNZ=^|_~>LLn)BBYBD zA}BD53egH7y)oS`bp6+r7eZSaii&G1YRc~H&fnkpcCqJ~ednFuKX;ppU=F;z|DNYO z=Q-zlzURCzY{-Ue$cFqMNkptVuZL(*L#nC*z;;X0azxBXy43b&N$1+q07X zF6l^f{y7%axTPTQXae%2wqJB8mW zsJ3sHbj0>C;3O~(?2vQ_SOoS1CvA_)_N6sF6;Rn81r`$An}W;(!yz~S7vvost7uCYA~Yy*Z)+p)sx0@{+^ zPXhb}@THm#0bjKJPHteuvjCMOmm{sLU;kUu135{yZ?t_auwByDG;kI;1uTY+|0C)mEcU$w7E+x8`r4&?^j#6ca08M?cHzorMf>0ObQ?daEn)KYp0xF_$6 zCmsS`wf$#c+V)o@-IVVu+hdXzf%EH8>Jl*&5qtYKO=ou222M!2AGig$K6{>RiMOzQ zJc;hGq_=>pZEwxc_ixZ5V$XmE&1R*)E7N)scqco^5{81+HZUsbIPf3ak4w5OsiLQ} zGR}jPf;KRb3(ebpFKMG)(l$A$O7Jh(K2Op!z$2yjmlmWKsHX-r2uU-xAC|N;@wW^N z10%V)r7SmXU?|T=fi1SjyR^A$4BGu63|h2(oup~tU%<__KV3L0hGu5a#u zlYxFv)BvvzVBeScY#QWNRaHd9eD*B$&tKc#+^aI5)$f+m`X^Gq8#tU-?OVQdcSA{p zg+tckXV6G~V(AQB0dP{?$S_&FHE7##7G83Lprfj)IraHDMbiCnR`B(dB}vN~w6G?| zhMIWl@^Lt@)aEWoIt}Cj-MpMsu#&;)YUymPsRgE}(Eq~M=kgPD)kk8r-l*7+b&&r8 XcFFQO^xs~%00000NkvXXu0mjfDef2C literal 0 HcmV?d00001