mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-12-27 15:24:28 +01:00
Move Firmata into Arduino node so we don't rely on their version of serialport
This commit is contained in:
67
hardware/Arduino/lib/com.js
Normal file
67
hardware/Arduino/lib/com.js
Normal file
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
|
||||
const Emitter = require("events");
|
||||
|
||||
class TransportStub extends Emitter {
|
||||
constructor(path /*, options, openCallback*/) {
|
||||
super();
|
||||
this.isOpen = true;
|
||||
this.baudRate = 0;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
write(buffer) {
|
||||
// Tests are written to work with arrays not buffers
|
||||
// this shouldn't impact the data, just the container
|
||||
// This also should be changed in future test rewrites
|
||||
/* istanbul ignore else */
|
||||
if (Buffer.isBuffer(buffer)) {
|
||||
buffer = Array.from(buffer);
|
||||
}
|
||||
|
||||
this.lastWrite = buffer;
|
||||
this.emit("write", buffer);
|
||||
}
|
||||
|
||||
static list() {
|
||||
/* istanbul ignore next */
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
}
|
||||
|
||||
// This trash is necessary for stubbing with sinon.
|
||||
TransportStub.SerialPort = TransportStub;
|
||||
|
||||
let com;
|
||||
let error;
|
||||
let SerialPort;
|
||||
|
||||
try {
|
||||
/* istanbul ignore else */
|
||||
if (process.env.IS_TEST_MODE) {
|
||||
com = TransportStub;
|
||||
} else {
|
||||
SerialPort = require("serialport").SerialPort;
|
||||
com = SerialPort;
|
||||
}
|
||||
} catch (err) {
|
||||
/* istanbul ignore next */
|
||||
error = err;
|
||||
}
|
||||
|
||||
/* istanbul ignore if */
|
||||
if (com == null) {
|
||||
if (process.env.IS_TEST_MODE) {
|
||||
com = TransportStub;
|
||||
} else {
|
||||
console.log("It looks like serialport didn't install properly.");
|
||||
console.log(
|
||||
"More information can be found here https://serialport.io/docs/guide-installation"
|
||||
);
|
||||
console.log(`The result of requiring the package is: ${SerialPort}`);
|
||||
console.log(error);
|
||||
throw "Missing serialport dependency";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = com;
|
||||
Reference in New Issue
Block a user