From e9c3e7fd26886609315c4c4eacbaf16fc5b3e4b0 Mon Sep 17 00:00:00 2001 From: Charalampos Doukas Date: Mon, 30 Sep 2013 14:50:56 +0200 Subject: [PATCH 1/3] Added new nodes to the node repository. Although current HTTP node will be updated to include more functionality, still added the HTTP_generic node just in case --- hardware/101-scanBLE.html | 61 +++++++++++++++++++++++++ hardware/101-scanBLE.js | 74 ++++++++++++++++++++++++++++++ io/102-HTTP_generic.html | 95 +++++++++++++++++++++++++++++++++++++++ io/102-HTTP_generic.js | 93 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 323 insertions(+) create mode 100644 hardware/101-scanBLE.html create mode 100644 hardware/101-scanBLE.js create mode 100644 io/102-HTTP_generic.html create mode 100644 io/102-HTTP_generic.js diff --git a/hardware/101-scanBLE.html b/hardware/101-scanBLE.html new file mode 100644 index 00000000..cd602574 --- /dev/null +++ b/hardware/101-scanBLE.html @@ -0,0 +1,61 @@ + + + + + + + + + diff --git a/hardware/101-scanBLE.js b/hardware/101-scanBLE.js new file mode 100644 index 00000000..7f01385e --- /dev/null +++ b/hardware/101-scanBLE.js @@ -0,0 +1,74 @@ +/** + * scanBLE.js + * Scans for a specific Bluetooth 4 (BLE) Device (by Name and UUID) + * Returns the Name the of Device when found and stops scanning + * Requires Noble: https://github.com/sandeepmistry/noble + * Copyright 2013 Charalampos Doukas + * + * 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"); + +//import noble +var noble = require('noble'); + +// The main node definition - most things happen in here +function Scan(n) { + // Create a RED node + RED.nodes.createNode(this,n); + + var msg = {}; + var ble_name; + var node = this; + + //get name and uuid from user + this.ble_name = n.ble_name; + this.ble_uuid = n.ble_uuid; + + this.on("input", function(msg){ + noble.startScanning(); + }); + noble.on('scanStart', function(msg) { + var msg = {}; + msg.topic = node.topic; + msg.payload = "Scanning initiated..." //debugging + //console.log('scanning initiated...'); + node.send(msg); + }); + + noble.on('discover', function(peripheral) { + + var msg = {}; + msg.topic = node.topic; + msg.payload = "not found"; + + if(peripheral.advertisement.localName==node.ble_name && peripheral.advertisement.serviceUuids[0]==node.ble_uuid) { + msg.payload=peripheral.advertisement.localName; + noble.stopScanning(); } + node.send(msg); +}); + + +} + +// Register the node by name. This must be called before overriding any of the +// Node functions. +RED.nodes.registerType("scanBLE", Scan); + + +Scan.prototype.close = function() { + +} + diff --git a/io/102-HTTP_generic.html b/io/102-HTTP_generic.html new file mode 100644 index 00000000..690212bf --- /dev/null +++ b/io/102-HTTP_generic.html @@ -0,0 +1,95 @@ + + + + + + + + + diff --git a/io/102-HTTP_generic.js b/io/102-HTTP_generic.js new file mode 100644 index 00000000..9c42bf0f --- /dev/null +++ b/io/102-HTTP_generic.js @@ -0,0 +1,93 @@ +/* + + HTTP_generic.js + Performs a generic HTTP Request (PUT, GET, POST, DELETE) + User can set method, host, port, path, content-type and header/data request. + Example of header parameters:'foo:bar,param2:value,param3:value' + + Copyright 2013 Charalampos Doukas + + 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 http = require("http"); + + +// The main node definition - most things happen in here +function HTTP_Request(n) { + // Create a RED node + RED.nodes.createNode(this,n); + var msg = {}; + this.method = n.method; + this.header = n.header; + this.data = n.data; + this.host = n.host; + this.port = n.port; + this.path = n.path; + this.contenttype = n.contenttype; + var node = this; + + + this.on("input", function(msg){ + var options = { + host: node.host, + port: node.port, + path: node.path, + method: node.method, + headers: { + 'Content-Type': node.contenttype + } + }; + + var req = http.request(options, function(res) { + res.setEncoding('utf8'); + res.on('data', function (chunk) { + var msg = {}; + msg.payload = chunk; + node.send(msg); + }); + }); + + req.on('error', function(e) { + console.log('problem with request: ' + e.message); + }); + + var tmp_head = node.header.split(','); + for(var i = 0; i < tmp_head.length; i++) + { + var head_part = tmp_head[i].split(':'); + req.setHeader(head_part[0], head_part[1]); + } + + // write data to request body + req.write(node.data); + req.end(); + + + }); + +} + +// Register the node by name. This must be called before overriding any of the +// Node functions. +RED.nodes.registerType("HTTP_generic", HTTP_Request); + + +HTTP_Request.prototype.close = function() { + // Called when the node is shutdown - eg on redeploy. + // Allows ports to be closed, connections dropped etc. + // eg: this.client.disconnect(); +} + From 7d41db377f609d034b656fbd8c1cf2bf6680b685 Mon Sep 17 00:00:00 2001 From: Charalampos Doukas Date: Wed, 2 Oct 2013 21:48:54 +0200 Subject: [PATCH 2/3] removed HTTP_generic moved scanBLE into a subfolder --- hardware/{ => scanBLE}/101-scanBLE.html | 0 hardware/{ => scanBLE}/101-scanBLE.js | 0 io/102-HTTP_generic.html | 95 ------------------------- io/102-HTTP_generic.js | 93 ------------------------ 4 files changed, 188 deletions(-) rename hardware/{ => scanBLE}/101-scanBLE.html (100%) rename hardware/{ => scanBLE}/101-scanBLE.js (100%) delete mode 100644 io/102-HTTP_generic.html delete mode 100644 io/102-HTTP_generic.js diff --git a/hardware/101-scanBLE.html b/hardware/scanBLE/101-scanBLE.html similarity index 100% rename from hardware/101-scanBLE.html rename to hardware/scanBLE/101-scanBLE.html diff --git a/hardware/101-scanBLE.js b/hardware/scanBLE/101-scanBLE.js similarity index 100% rename from hardware/101-scanBLE.js rename to hardware/scanBLE/101-scanBLE.js diff --git a/io/102-HTTP_generic.html b/io/102-HTTP_generic.html deleted file mode 100644 index 690212bf..00000000 --- a/io/102-HTTP_generic.html +++ /dev/null @@ -1,95 +0,0 @@ - - - - - - - - - diff --git a/io/102-HTTP_generic.js b/io/102-HTTP_generic.js deleted file mode 100644 index 9c42bf0f..00000000 --- a/io/102-HTTP_generic.js +++ /dev/null @@ -1,93 +0,0 @@ -/* - - HTTP_generic.js - Performs a generic HTTP Request (PUT, GET, POST, DELETE) - User can set method, host, port, path, content-type and header/data request. - Example of header parameters:'foo:bar,param2:value,param3:value' - - Copyright 2013 Charalampos Doukas - - 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 http = require("http"); - - -// The main node definition - most things happen in here -function HTTP_Request(n) { - // Create a RED node - RED.nodes.createNode(this,n); - var msg = {}; - this.method = n.method; - this.header = n.header; - this.data = n.data; - this.host = n.host; - this.port = n.port; - this.path = n.path; - this.contenttype = n.contenttype; - var node = this; - - - this.on("input", function(msg){ - var options = { - host: node.host, - port: node.port, - path: node.path, - method: node.method, - headers: { - 'Content-Type': node.contenttype - } - }; - - var req = http.request(options, function(res) { - res.setEncoding('utf8'); - res.on('data', function (chunk) { - var msg = {}; - msg.payload = chunk; - node.send(msg); - }); - }); - - req.on('error', function(e) { - console.log('problem with request: ' + e.message); - }); - - var tmp_head = node.header.split(','); - for(var i = 0; i < tmp_head.length; i++) - { - var head_part = tmp_head[i].split(':'); - req.setHeader(head_part[0], head_part[1]); - } - - // write data to request body - req.write(node.data); - req.end(); - - - }); - -} - -// Register the node by name. This must be called before overriding any of the -// Node functions. -RED.nodes.registerType("HTTP_generic", HTTP_Request); - - -HTTP_Request.prototype.close = function() { - // Called when the node is shutdown - eg on redeploy. - // Allows ports to be closed, connections dropped etc. - // eg: this.client.disconnect(); -} - From 7ceeae38b1144fb12f6c5fbec6f1fa5b4afe611b Mon Sep 17 00:00:00 2001 From: Charalampos Doukas Date: Thu, 3 Oct 2013 12:51:48 +0200 Subject: [PATCH 3/3] added some comments --- hardware/scanBLE/101-scanBLE.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hardware/scanBLE/101-scanBLE.js b/hardware/scanBLE/101-scanBLE.js index 7f01385e..328077f7 100644 --- a/hardware/scanBLE/101-scanBLE.js +++ b/hardware/scanBLE/101-scanBLE.js @@ -3,7 +3,7 @@ * Scans for a specific Bluetooth 4 (BLE) Device (by Name and UUID) * Returns the Name the of Device when found and stops scanning * Requires Noble: https://github.com/sandeepmistry/noble - * Copyright 2013 Charalampos Doukas + * Copyright 2013 Charalampos Doukas - @BuildingIoT * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ **/ +//might need to modify accordingly var RED = require("../../red/red"); //import noble @@ -54,6 +55,7 @@ function Scan(n) { msg.topic = node.topic; msg.payload = "not found"; + //check for the device name and the UUID (first one from the UUID list) if(peripheral.advertisement.localName==node.ble_name && peripheral.advertisement.serviceUuids[0]==node.ble_uuid) { msg.payload=peripheral.advertisement.localName; noble.stopScanning(); }