Skip to content

Commit

Permalink
feat: change the API of findDevice to return null if no device is f…
Browse files Browse the repository at this point in the history
…ound, rather than throwing an error

BREAKING CHANGE: This refactors the API of the `findDevice`
function to return `null` rather than throwing an error if a
Logitech Litra Glow device cannot be found attached to the
computer. If you're using this function, you should update any
call sites so they don't assume that a device has been returned -
for example with an `if (device)` `null` check.
  • Loading branch information
timrogers committed Nov 13, 2022
1 parent 22aa29b commit c8f3295
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 50 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ npm install --save litra-glow

The `findDevice` function checks your computer to find whether a Logitech Litra Glow is plugged in.

If it is, an object representing the device is returned, which you can pass into other function. If it isn't, an error is thrown.
If it is, it returns an object representing the device, which you can pass into other function. If it isn't, it returns `null`.

```js
import { findDevice } from 'litra-glow';

const device = findDevice();

if (device) {
// Do something
} else {
// Blow up
}
```

If you're a *huge* fan of the Litra Glow and you have multipled plugged in at the same time, it'll return whatever one it happens to find first.
If you're a *huge* fan of the Litra Glow and you have multiple plugged in at the same time, it'll return whatever one it happens to find first.

#### Turning your Litra Glow on or off

Expand All @@ -54,8 +60,10 @@ import { findDevice, turnOff, turnOn } from 'litra-glow';
const device = findDevice();

// Turn your light on, then turn it off again after 5 seconds
turnOn(device);
setTimeout(() => turnOff(device), 5000));
if (device) {
turnOn(device);
setTimeout(() => turnOff(device), 5000));
}
```

#### Setting the brightness of your Litra Glow
Expand All @@ -67,7 +75,9 @@ import { findDevice, setBrightnessInLumen } from 'litra-glow';

const device = findDevice();

setBrightnessInLumen(device, 150);
if (device) {
setBrightnessInLumen(device, 150);
}
```

You can also set brightness level to a percentage with `setBrightnessPercentage` if you don't want to think in Lumen:
Expand All @@ -77,7 +87,9 @@ import { findDevice, setBrightnessPercentage } from 'litra-glow';

const device = findDevice();

setBrightnessPercentage(device, 75);
if (device) {
setBrightnessPercentage(device, 75);
}
```

#### Setting the temperature of your Litra Glow
Expand All @@ -89,7 +101,9 @@ import { findDevice, setTemperatureInKelvin } from 'litra-glow';

const device = findDevice();

setTemperatureInKelvin(device, 4500);
if (device) {
setTemperatureInKelvin(device, 4500);
}
```

You can also set brightness level to a percentage with `setTemperaturePercentage` if you don't want to think in Lumen:
Expand All @@ -99,5 +113,7 @@ import { findDevice, setTemperaturePercentage } from 'litra-glow';

const device = findDevice();

setTemperaturePercentage(device, 75);
if (device) {
setTemperaturePercentage(device, 75);
}
```
7 changes: 6 additions & 1 deletion dist/commonjs/cli/litra-off.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
const driver_1 = require("./../driver");
try {
const device = (0, driver_1.findDevice)();
(0, driver_1.turnOff)(device);
if (device) {
(0, driver_1.turnOff)(device);
}
else {
throw 'Device not found';
}
process.exit(0);
}
catch (e) {
Expand Down
7 changes: 6 additions & 1 deletion dist/commonjs/cli/litra-on.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
const driver_1 = require("./../driver");
try {
const device = (0, driver_1.findDevice)();
(0, driver_1.turnOn)(device);
if (device) {
(0, driver_1.turnOn)(device);
}
else {
throw 'Device not found';
}
process.exit(0);
}
catch (e) {
Expand Down
14 changes: 7 additions & 7 deletions dist/commonjs/driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ export interface Device {
write: (values: number[] | Buffer) => number;
}
/**
* Finds your Logitech Litra Glow device and returns it. Throws an
* error if a matching device cannot be found connected to your
* computer.
* Finds your Logitech Litra Glow device and returns it. Returns `null`
* if a matching device cannot be found connected to your computer.
*
* @returns {Device} An object representing your Logitech Litra Glow
* device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin`
* @returns {Device, null} An object representing your Logitech Litra
* Glow device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin` - or `null` if a matching device cannot be
* found connected to your computer.
*/
export declare const findDevice: () => Device;
export declare const findDevice: () => Device | null;
/**
* Turns your Logitech Litra Glow device on.
*
Expand Down
14 changes: 7 additions & 7 deletions dist/commonjs/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const VENDOR_ID = 0x046d;
const PRODUCT_ID = 0xc900;
const USAGE_PAGE = 0xff43;
/**
* Finds your Logitech Litra Glow device and returns it. Throws an
* error if a matching device cannot be found connected to your
* computer.
* Finds your Logitech Litra Glow device and returns it. Returns `null`
* if a matching device cannot be found connected to your computer.
*
* @returns {Device} An object representing your Logitech Litra Glow
* device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin`
* @returns {Device, null} An object representing your Logitech Litra
* Glow device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin` - or `null` if a matching device cannot be
* found connected to your computer.
*/
const findDevice = () => {
const matchingDevice = node_hid_1.default.devices().find((device) => device.vendorId === VENDOR_ID &&
Expand All @@ -26,7 +26,7 @@ const findDevice = () => {
return new node_hid_1.default.HID(matchingDevice.path);
}
else {
throw 'Device not found';
return null;
}
};
exports.findDevice = findDevice;
Expand Down
7 changes: 6 additions & 1 deletion dist/esm/cli/litra-off.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import { findDevice, turnOff } from './../driver';
try {
const device = findDevice();
turnOff(device);
if (device) {
turnOff(device);
}
else {
throw 'Device not found';
}
process.exit(0);
}
catch (e) {
Expand Down
7 changes: 6 additions & 1 deletion dist/esm/cli/litra-on.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import { findDevice, turnOn } from './../driver';
try {
const device = findDevice();
turnOn(device);
if (device) {
turnOn(device);
}
else {
throw 'Device not found';
}
process.exit(0);
}
catch (e) {
Expand Down
14 changes: 7 additions & 7 deletions dist/esm/driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ export interface Device {
write: (values: number[] | Buffer) => number;
}
/**
* Finds your Logitech Litra Glow device and returns it. Throws an
* error if a matching device cannot be found connected to your
* computer.
* Finds your Logitech Litra Glow device and returns it. Returns `null`
* if a matching device cannot be found connected to your computer.
*
* @returns {Device} An object representing your Logitech Litra Glow
* device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin`
* @returns {Device, null} An object representing your Logitech Litra
* Glow device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin` - or `null` if a matching device cannot be
* found connected to your computer.
*/
export declare const findDevice: () => Device;
export declare const findDevice: () => Device | null;
/**
* Turns your Logitech Litra Glow device on.
*
Expand Down
14 changes: 7 additions & 7 deletions dist/esm/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const VENDOR_ID = 0x046d;
const PRODUCT_ID = 0xc900;
const USAGE_PAGE = 0xff43;
/**
* Finds your Logitech Litra Glow device and returns it. Throws an
* error if a matching device cannot be found connected to your
* computer.
* Finds your Logitech Litra Glow device and returns it. Returns `null`
* if a matching device cannot be found connected to your computer.
*
* @returns {Device} An object representing your Logitech Litra Glow
* device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin`
* @returns {Device, null} An object representing your Logitech Litra
* Glow device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin` - or `null` if a matching device cannot be
* found connected to your computer.
*/
export const findDevice = () => {
const matchingDevice = HID.devices().find((device) => device.vendorId === VENDOR_ID &&
Expand All @@ -20,7 +20,7 @@ export const findDevice = () => {
return new HID.HID(matchingDevice.path);
}
else {
throw 'Device not found';
return null;
}
};
/**
Expand Down
8 changes: 7 additions & 1 deletion src/cli/litra-off.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { findDevice, turnOff } from './../driver';

try {
const device = findDevice();
turnOff(device);

if (device) {
turnOff(device);
} else {
throw 'Device not found';
}

process.exit(0);
} catch (e) {
console.log(e);
Expand Down
8 changes: 7 additions & 1 deletion src/cli/litra-on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { findDevice, turnOn } from './../driver';

try {
const device = findDevice();
turnOn(device);

if (device) {
turnOn(device);
} else {
throw 'Device not found';
}

process.exit(0);
} catch (e) {
console.log(e);
Expand Down
16 changes: 8 additions & 8 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export interface Device {
}

/**
* Finds your Logitech Litra Glow device and returns it. Throws an
* error if a matching device cannot be found connected to your
* computer.
* Finds your Logitech Litra Glow device and returns it. Returns `null`
* if a matching device cannot be found connected to your computer.
*
* @returns {Device} An object representing your Logitech Litra Glow
* device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin`
* @returns {Device, null} An object representing your Logitech Litra
* Glow device, passed into other functions like `turnOn` and
* `setTemperatureInKelvin` - or `null` if a matching device cannot be
* found connected to your computer.
*/
export const findDevice = (): Device => {
export const findDevice = (): Device | null => {
const matchingDevice = HID.devices().find(
(device) =>
device.vendorId === VENDOR_ID &&
Expand All @@ -31,7 +31,7 @@ export const findDevice = (): Device => {
if (matchingDevice) {
return new HID.HID(matchingDevice.path as string);
} else {
throw 'Device not found';
return null;
}
};

Expand Down

0 comments on commit c8f3295

Please sign in to comment.