diff --git a/forge.config.js b/forge.config.js index de9056cdd..474f7ccbb 100644 --- a/forge.config.js +++ b/forge.config.js @@ -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" }], }, diff --git a/src/Electron/lib/serialport.js b/src/Electron/lib/serialport.js new file mode 100644 index 000000000..55610d35e --- /dev/null +++ b/src/Electron/lib/serialport.js @@ -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])); +} diff --git a/src/Electron/main.js b/src/Electron/main.js index 726ba1515..7f2eece73 100644 --- a/src/Electron/main.js +++ b/src/Electron/main.js @@ -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 @@ -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])); -}