UNPKG

shuttle-node

Version:

An npm module for interfacing with the Contour Shuttle devices in Node.js

153 lines 5.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.setupShuttle = setupShuttle; exports.listAllConnectedDevices = listAllConnectedDevices; exports.isAShuttleDevice = isAShuttleDevice; exports.isUSBDevice = isUSBDevice; const core_1 = require("@shuttle-lib/core"); const HID = __importStar(require("node-hid")); const node_hid_wrapper_js_1 = require("./node-hid-wrapper.js"); const lib_js_1 = require("./lib.js"); async function setupShuttle(devicePathOrHIDDevice) { let devicePath; let device; let deviceInfo; if (!devicePathOrHIDDevice) { // Device not provided, will then select any connected device: const connectedShuttle = await listAllConnectedDevices(); if (!connectedShuttle.length) { throw new Error('Could not find any connected Shuttle devices.'); } // Just select the first one: devicePath = connectedShuttle[0].path; device = await HID.HIDAsync.open(devicePath); deviceInfo = { product: connectedShuttle[0].product, vendorId: connectedShuttle[0].vendorId, productId: connectedShuttle[0].productId, interface: connectedShuttle[0].interface, }; } else if ((0, lib_js_1.isHID_Device)(devicePathOrHIDDevice)) { // is HID.Device if (!devicePathOrHIDDevice.path) throw new Error('HID.Device path not set!'); devicePath = devicePathOrHIDDevice.path; device = await HID.HIDAsync.open(devicePath); deviceInfo = { product: devicePathOrHIDDevice.product, vendorId: devicePathOrHIDDevice.vendorId, productId: devicePathOrHIDDevice.productId, interface: devicePathOrHIDDevice.interface, }; } else if ((0, lib_js_1.isHID_HID)(devicePathOrHIDDevice)) { // is HID.HID device = devicePathOrHIDDevice; devicePath = devicePathOrHIDDevice.devicePath; // deviceInfo is set later } else if (typeof devicePathOrHIDDevice === 'string') { // is string (path) devicePath = devicePathOrHIDDevice; device = await HID.HIDAsync.open(devicePath); // deviceInfo is set later } else { throw new Error(`setupShuttle: invalid arguments: ${JSON.stringify(devicePathOrHIDDevice)}`); } if (!deviceInfo) { // Look through HID.devices(), because HID.Device contains the productId for (const hidDevice of HID.devices()) { if (hidDevice.path === devicePath) { deviceInfo = { product: hidDevice.product, vendorId: hidDevice.vendorId, productId: hidDevice.productId, interface: hidDevice.interface, }; break; } } } if (!device) throw new Error('Error setting up Shuttle: device not found'); if (!devicePath) throw new Error('Error setting up Shuttle: devicePath not found'); if (!deviceInfo) throw new Error('Error setting up Shuttle: deviceInfo not found'); const deviceWrap = new node_hid_wrapper_js_1.NodeHIDDevice(device); const shuttle = new core_1.Shuttle(deviceWrap, deviceInfo, devicePath); // Wait for the device to initialize: await shuttle.init(); return shuttle; } /** Returns a list of all connected Shuttle-HID-devices */ async function listAllConnectedDevices() { const hidDevices = await HID.devicesAsync(); const connectedShuttle = hidDevices.filter((device) => { // Filter to only return the supported devices: return isAShuttleDevice(device); }); return connectedShuttle; } /** Returns a list of all connected Shuttle-HID-devices */ function isAShuttleDevice(device) { let vendorId; let productId; if (isUSBDevice(device)) { vendorId = device.deviceDescriptor.idVendor; productId = device.deviceDescriptor.idProduct; } else { vendorId = device.vendorId; productId = device.productId; if (!device.path) return false; } if (!core_1.VENDOR_IDS.includes(vendorId)) return false; for (const product of Object.values(core_1.PRODUCTS)) { if (product.productId === productId && product.vendorId === vendorId) { return true; // break and return } } return false; } function isUSBDevice(device) { return 'deviceDescriptor' in device; } //# sourceMappingURL=methods.js.map