Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 0a84989

Browse files
committed
Refactor elgato's createLightData to get values by object and fix some nitpicks
1 parent b8a807c commit 0a84989

File tree

5 files changed

+24
-48
lines changed

5 files changed

+24
-48
lines changed

nodecg-io-elgato-light/extension/elgatoLight.ts

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fetch from "node-fetch";
2-
import { LightData } from "./lightData";
2+
import { LightData, LightValues } from "./lightData";
33
import { Response } from "node-fetch";
44

55
export type LightType = "KeyLight" | "LightStrip";
@@ -48,16 +48,7 @@ export abstract class ElgatoLight {
4848
* Helper method to call HTTP GET on the elgato light and ease the interpretation of the response.
4949
* @returns the response of the elgato light or undefined
5050
*/
51-
protected async getLightData(): Promise<
52-
| {
53-
on?: number | undefined;
54-
hue?: number | undefined;
55-
saturation?: number | undefined;
56-
brightness?: number | undefined;
57-
temperature?: number | undefined;
58-
}
59-
| undefined
60-
> {
51+
protected async getLightData(): Promise<LightValues | undefined> {
6152
const response = await this.callGET();
6253

6354
if (response.status !== 200) {
@@ -79,15 +70,15 @@ export abstract class ElgatoLight {
7970
* Switches the elgato light on.
8071
*/
8172
async turnLightOn(): Promise<void> {
82-
const lightData = ElgatoLight.createLightData(1);
73+
const lightData = ElgatoLight.createLightData({ on: 1 });
8374
await this.callPUT(lightData);
8475
}
8576

8677
/**
8778
* Switches the elgato light off.
8879
*/
8980
async turnLightOff(): Promise<void> {
90-
const lightData = ElgatoLight.createLightData(0);
81+
const lightData = ElgatoLight.createLightData({ on: 0 });
9182
await this.callPUT(lightData);
9283
}
9384

@@ -96,7 +87,7 @@ export abstract class ElgatoLight {
9687
*/
9788
async toggleLight(): Promise<void> {
9889
const state = await this.isLightOn();
99-
const lightData = ElgatoLight.createLightData(state ? 0 : 1);
90+
const lightData = ElgatoLight.createLightData({ on: state ? 0 : 1 });
10091
await this.callPUT(lightData);
10192
}
10293

@@ -106,7 +97,7 @@ export abstract class ElgatoLight {
10697
*/
10798
async setBrightness(brightness: number): Promise<void> {
10899
const sanitizedValue = Math.max(0, Math.min(100, brightness));
109-
const lightData = ElgatoLight.createLightData(undefined, undefined, undefined, sanitizedValue);
100+
const lightData = ElgatoLight.createLightData({ brightness: sanitizedValue });
110101
await this.callPUT(lightData);
111102
}
112103

@@ -118,24 +109,10 @@ export abstract class ElgatoLight {
118109
return (await this.getLightData())?.brightness ?? -1;
119110
}
120111

121-
protected static createLightData(
122-
on?: number,
123-
hue?: number,
124-
saturation?: number,
125-
brightness?: number,
126-
temperature?: number,
127-
): LightData {
112+
protected static createLightData(data: LightValues): LightData {
128113
return {
129114
numberOfLights: 1,
130-
lights: [
131-
{
132-
on: on,
133-
hue: hue,
134-
saturation: saturation,
135-
brightness: brightness,
136-
temperature: temperature,
137-
},
138-
],
115+
lights: [data],
139116
};
140117
}
141118
}
@@ -152,7 +129,7 @@ export class ElgatoKeyLight extends ElgatoLight {
152129
*/
153130
async setTemperature(temperature: number): Promise<void> {
154131
const sanitizedValue = Math.max(143, Math.min(344, ElgatoKeyLight.temperatureFactor / temperature));
155-
const lightData = ElgatoLight.createLightData(undefined, undefined, undefined, undefined, sanitizedValue);
132+
const lightData = ElgatoLight.createLightData({ temperature: sanitizedValue });
156133
await this.callPUT(lightData);
157134
}
158135

@@ -181,7 +158,7 @@ export class ElgatoLightStrip extends ElgatoLight {
181158
*/
182159
async setHue(hue: number): Promise<void> {
183160
const sanitizedValue = Math.max(0, Math.min(360, hue));
184-
const lightData = ElgatoLight.createLightData(undefined, sanitizedValue);
161+
const lightData = ElgatoLight.createLightData({ hue: sanitizedValue });
185162
await this.callPUT(lightData);
186163
}
187164

@@ -199,7 +176,7 @@ export class ElgatoLightStrip extends ElgatoLight {
199176
*/
200177
async setSaturation(saturation: number): Promise<void> {
201178
const sanitizedValue = Math.max(0, Math.min(100, saturation));
202-
const lightData = ElgatoLight.createLightData(undefined, undefined, sanitizedValue);
179+
const lightData = ElgatoLight.createLightData({ saturation: sanitizedValue });
203180
await this.callPUT(lightData);
204181
}
205182

nodecg-io-elgato-light/extension/elgatoLightClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ElgatoLightClient {
3131

3232
/**
3333
* Tries to reach all elgato lights contained in the config provided in the constructor.
34-
* @returns an array of IP addresses of elgato lights that where configured but not reachable
34+
* @returns an array of IP addresses of elgato lights that where configured but not reachable
3535
*/
3636
async identifyNotReachableLights(): Promise<Array<string>> {
3737
const notReachableLights = [];

nodecg-io-elgato-light/extension/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class ElgatoLightService extends ServiceBundle<ElgatoLightConfig, ElgatoLightCli
1515
if (notReachableLights.length === 0) {
1616
return emptySuccess();
1717
}
18-
{
19-
return error(`Unable to connect to the lights with the following IPs: ${notReachableLights.join(", ")}`);
20-
}
18+
19+
return error(`Unable to connect to the lights with the following IPs: ${notReachableLights.join(", ")}`);
2120
}
2221

2322
async createClient(config: ElgatoLightConfig): Promise<Result<ElgatoLightClient>> {

nodecg-io-elgato-light/extension/lightData.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
*/
44
export interface LightData {
55
numberOfLights: 1;
6-
lights: [
7-
{
8-
on?: number;
9-
hue?: number;
10-
saturation?: number;
11-
brightness?: number;
12-
temperature?: number;
13-
},
14-
];
6+
lights: LightValues[];
7+
}
8+
9+
export interface LightValues {
10+
on?: number;
11+
hue?: number;
12+
saturation?: number;
13+
brightness?: number;
14+
temperature?: number;
1515
}

nodecg-io-elgato-light/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nodecg-io-elgato-light",
33
"version": "0.2.0",
4-
"description": "Control your Elgato lights, e.g., key lights and light stripes.",
4+
"description": "Control your Elgato lights, e.g. key lights and light stripes.",
55
"homepage": "https://nodecg.io/RELEASE/samples/elgato-light",
66
"author": {
77
"name": "CodeOverflow team",

0 commit comments

Comments
 (0)