In my .node-red/node_modules directory I have a directory called node-red-contrib-barcode-scanner. In that are barcode-scanner.html, barcode-scanner.js and package.json. Also into here you’ll need to do npm install node-usb-barcode-scanner.
barcode-scanner.html:
<!--
Copyright 2016 Steampunk Professor
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="barcode-scanner">
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="Topic">
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-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="barcode-scanner">
<script type="text/x-red" data-help-name="barcode-scanner">
<p>Simple barcode scanner input node. Sends a single message when it scans a barcode</p>
<p>Outputs an object called <code>msg</code> containing <code>msg.topic</code> and
<code>msg.payload</code>. msg.payload is a String of characters corresponding to the barcode.</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<!-- The example below shows a small subset of the properties that can be set-->
<script type="text/javascript">
RED.nodes.registerType('barcode-scanner',{
category: 'advanced-input', // the palette category
defaults: { // defines the editable properties of the node
name: {value:"Barcode Scanner"}, // along with default values.
topic: {value:"barcode", required:true}
},
inputs:0, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
// set the icon (held in icons dir below where you save the node)
icon: "debug.png", // saved in icons/myicon.png
color: "#c0deed",
label: function() { // sets the default label contents
return this.name||"Barcode Scanner";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
}
});
</script>
barcode-scanner.js:
/**
* Copyright 2016 Steampunk Professor
*
* 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.
**/
module.exports = function(RED) {
hids = require('./node_modules/node-usb-barcode-scanner/usbscanner');
"use strict";
function BarcodeScannerNode(n) {
hids = require('./node_modules/node-usb-barcode-scanner/usbscanner');
"use strict";
function BarcodeScannerNode(n) {
// Create a RED node
RED.nodes.createNode(this,n);
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic;
// copy "this" object in case we need it in context of callbacks of other functions
var node = this;
var scan_device;
var tout;
var usbScanner = hids.usbScanner;
var scanner;
function findscanner() {
var devices = hids.getDevices();
for (var dev = 0; dev < devices.length; dev++) {
if (devices[dev].product.toLowerCase().indexOf('barcode') !== -1) {
scan_device = devices[dev];
break; // stop on first match
}
}
if (scan_device === null) {
tout = setTimeout( function () {
findscanner();
},15000);
}
}
findscanner();
if (scan_device != null) {
//initialize new usbScanner - takes optional parmeters
scanner = new usbScanner(scan_device);
this.status({fill:"green",shape:"dot",text:"connected"});
scanner.on("data", function(code){
var msg = {};
msg.topic = this.topic;
msg.payload = code;
// send out the message to the rest of the workspace
// ... this message will get sent at startup so you may not see it in a debug node.
node.send(msg);
});
scanner.on("close", function() {
scanner.stopScanning();
this.status({fill:"red",shape:"dot",text:"not connected"});
});
}
else {
findscanner();
}
}
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("barcode-scanner",BarcodeScannerNode);
}
package.json:
{
"name": "node-red-contrib-barcode-scanner",
"version": "0.0.2",
"description": "Barcode scanner node for Node-RED",
"main": "barcode-scanner.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"barcode",
"scanner"
],
"node-red": {
"nodes": {
"barcode-scanner": "barcode-scanner.js"
}
},
"author": "Steampunk Professor <steampunkprofessor@gmail.com>",
"license": "Apache-2.0",
"dependencies": {
"node-usb-barcode-scanner": "1.0.1"
}
}