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

This commit is contained in:
Charalampos Doukas 2013-09-30 14:50:56 +02:00
parent 5da75b4943
commit e9c3e7fd26
4 changed files with 323 additions and 0 deletions

61
hardware/101-scanBLE.html Normal file
View File

@ -0,0 +1,61 @@
<!--
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.
-->
<script type="text/x-red" data-template-name="scanBLE">
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> BLE Device name</label>
<input type="text" id="node-input-ble_name" placeholder="XXX">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> UUID</label>
<input type="text" id="node-input-ble_uuid" placeholder="UUID">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="scanBLE">
<p>Scans for a specific BLE Device</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('scanBLE',{
category: 'advanced-input', // the palette category
color:"#0076d6",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
ble_name: {value:"", required:true},
ble_uuid: {value: "", required:true}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
icon: "arrow-in.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name||this.topic||"scanBLE";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
}
});
</script>

74
hardware/101-scanBLE.js Normal file
View File

@ -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() {
}

95
io/102-HTTP_generic.html Normal file
View File

@ -0,0 +1,95 @@
<!--
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.
-->
<script type="text/x-red" data-template-name="HTTP_generic">
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Host</label>
<input type="text" id="node-input-host" placeholder="127.0.0.1">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Port</label>
<input type="text" id="node-input-port" placeholder="80">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Path</label>
<input type="text" id="node-input-path" placeholder="">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Method</label>
<select id="node-input-method">
<option value="GET" selected="selected">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Content-Type</label>
<input type="text" id="node-input-contenttype" placeholder="">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Header</label>
<input type="textarea" id="node-input-header" placeholder="">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Data</label>
<input type="textarea" id="node-input-data" placeholder="">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="HTTP_generic">
<p>Makes a generic HTTP Request</p>
<p>You can define host, port, path, header, method and data</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('HTTP_generic',{
category: 'advanced-function', // the palette category
color:"#66bc46",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
host: {value:"", required:true},
port: {value: "", required:true},
path: {value:""},
method: {value:"", required:true},
header: {value:""},
data: {value:""},
contenttype: {value:""}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
icon: "arrow-in.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name||this.topic||"HTTP_generic";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
}
});
</script>

93
io/102-HTTP_generic.js Normal file
View File

@ -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();
}