Skip to content

Commit

Permalink
Merge pull request #475 from brown-ccv/fix-serialport
Browse files Browse the repository at this point in the history
fix: Add serialport code as its own file again
  • Loading branch information
RobertGemmaJr authored May 16, 2024
2 parents 4aeb7bd + 35360c9 commit 420a8fc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 72 deletions.
3 changes: 1 addition & 2 deletions forge.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ export default {
config: {
build: [
// Build files that use the main config
// TODO: Add serialPort.js here
{ entry: "src/electron/main.js", config: "vite.main.config.js" },
// Build files that use the preload config
{ entry: "src/electron/preload.js", config: "vite.preload.config.js" },
{ entry: "src/Electron/preload.js", config: "vite.preload.config.js" },
],
renderer: [{ name: "main_window", config: "vite.renderer.config.js" }],
},
Expand Down
64 changes: 64 additions & 0 deletions src/Electron/lib/serialport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import SerialPort from "serialport";

// TODO @brown-ccv #460: Test connections with MockBindings (e.g. CONTINUE_ANYWAY) https://serialport.io/docs/api-binding-mock

/**
* Retrieve's a serial port device based on either the COM name or product identifier
* If productID is undefined then comVendorName is the COM name, otherwise it's the vendorID
* @param {Array} portList A list of available serial port devices
* @param {string} comVendorName EITHER a com name or the vendor identifier of the desired device
* @param {string | undefined} productId The product identifier of the desired device
* @returns The SerialPort device
*/
function getDevice(portList, comVendorName, productId) {
if (productId === undefined) {
const comName = comVendorName;
return portList.filter(
// Find the device with the matching comName
(device) => device.comName === comName.toUpperCase() || device.comName === comName
);
} else {
const vendorId = comVendorName;
return portList.filter(
// Find the device with the matching vendorId and productId
(device) =>
(device.vendorId === vendorId.toUpperCase() || device.vendorId === vendorId) &&
device.productId === productId
);
}
}

/**
* Retrieve's a serial port device based on either the COM name or product identifier
* Returns false if the desired device was not found
* @param {string} comVendorName EITHER a com name or the vendor identifier of the desired device
* @param {string | undefined} productId The product identifier of the desired device
* @returns The SerialPort device
*/
// TODO @brown-ccv #460: This should fail, not return false
export async function getPort(comVendorName, productId) {
let portList;
try {
portList = await SerialPort.list();
} catch {
return false;
}

const device = getDevice(portList, comVendorName, productId);
try {
const path = device[0].comName;
const port = new SerialPort(path);
return port;
} catch {
return false;
}
}

/**
* Sends event code data to a serial port device
* @param {SerialPort} port A SerialPort device
* @param {number} event_code The numeric code to write to the device
*/
export async function sendToPort(port, event_code) {
port.write(Buffer.from([event_code]));
}
71 changes: 1 addition & 70 deletions src/Electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { BrowserWindow, app, dialog, ipcMain } from "electron";
import log from "electron-log";
import _ from "lodash";

// TODO @RobertGemmaJr: Figure out how to install the dev tools

// const { getPort, sendToPort } = require("./serialPort");
import { getPort, sendToPort } from "./lib/serialport";

// TODO @RobertGemmaJr: Do more testing with the environment variables - are home/clinic being built correctly?
// TODO @brown-ccv #460: Add serialport's MockBinding for the "Continue Anyway": https://serialport.io/docs/guide-testing
Expand Down Expand Up @@ -405,70 +403,3 @@ function handleEventSend(code) {
}
}
}

// TODO: SERPATE SERIAL PORT FILE

const SerialPort = require("serialport");

// TODO @brown-ccv #460: Test connections with MockBindings (e.g. CONTINUE_ANYWAY) https://serialport.io/docs/api-binding-mock

/**
* Retrieve's a serial port device based on either the COM name or product identifier
* If productID is undefined then comVendorName is the COM name, otherwise it's the vendorID
* @param {Array} portList A list of available serial port devices
* @param {string} comVendorName EITHER a com name or the vendor identifier of the desired device
* @param {string | undefined} productId The product identifier of the desired device
* @returns The SerialPort device
*/
function getDevice(portList, comVendorName, productId) {
if (productId === undefined) {
const comName = comVendorName;
return portList.filter(
// Find the device with the matching comName
(device) => device.comName === comName.toUpperCase() || device.comName === comName
);
} else {
const vendorId = comVendorName;
return portList.filter(
// Find the device with the matching vendorId and productId
(device) =>
(device.vendorId === vendorId.toUpperCase() || device.vendorId === vendorId) &&
device.productId === productId
);
}
}

/**
* Retrieve's a serial port device based on either the COM name or product identifier
* Returns false if the desired device was not found
* @param {string} comVendorName EITHER a com name or the vendor identifier of the desired device
* @param {string | undefined} productId The product identifier of the desired device
* @returns The SerialPort device
*/
// TODO @brown-ccv #460: This should fail, not return false
async function getPort(comVendorName, productId) {
let portList;
try {
portList = await SerialPort.list();
} catch {
return false;
}

const device = getDevice(portList, comVendorName, productId);
try {
const path = device[0].comName;
const port = new SerialPort(path);
return port;
} catch {
return false;
}
}

/**
* Sends event code data to a serial port device
* @param {SerialPort} port A SerialPort device
* @param {number} event_code The numeric code to write to the device
*/
async function sendToPort(port, event_code) {
port.write(Buffer.from([event_code]));
}

0 comments on commit 420a8fc

Please sign in to comment.