1
1
import fetch from "node-fetch" ;
2
- import { LightData } from "./lightData" ;
2
+ import { LightData , LightValues } from "./lightData" ;
3
3
import { Response } from "node-fetch" ;
4
4
5
5
export type LightType = "KeyLight" | "LightStrip" ;
@@ -48,16 +48,7 @@ export abstract class ElgatoLight {
48
48
* Helper method to call HTTP GET on the elgato light and ease the interpretation of the response.
49
49
* @returns the response of the elgato light or undefined
50
50
*/
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 > {
61
52
const response = await this . callGET ( ) ;
62
53
63
54
if ( response . status !== 200 ) {
@@ -79,15 +70,15 @@ export abstract class ElgatoLight {
79
70
* Switches the elgato light on.
80
71
*/
81
72
async turnLightOn ( ) : Promise < void > {
82
- const lightData = ElgatoLight . createLightData ( 1 ) ;
73
+ const lightData = ElgatoLight . createLightData ( { on : 1 } ) ;
83
74
await this . callPUT ( lightData ) ;
84
75
}
85
76
86
77
/**
87
78
* Switches the elgato light off.
88
79
*/
89
80
async turnLightOff ( ) : Promise < void > {
90
- const lightData = ElgatoLight . createLightData ( 0 ) ;
81
+ const lightData = ElgatoLight . createLightData ( { on : 0 } ) ;
91
82
await this . callPUT ( lightData ) ;
92
83
}
93
84
@@ -96,7 +87,7 @@ export abstract class ElgatoLight {
96
87
*/
97
88
async toggleLight ( ) : Promise < void > {
98
89
const state = await this . isLightOn ( ) ;
99
- const lightData = ElgatoLight . createLightData ( state ? 0 : 1 ) ;
90
+ const lightData = ElgatoLight . createLightData ( { on : state ? 0 : 1 } ) ;
100
91
await this . callPUT ( lightData ) ;
101
92
}
102
93
@@ -106,7 +97,7 @@ export abstract class ElgatoLight {
106
97
*/
107
98
async setBrightness ( brightness : number ) : Promise < void > {
108
99
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 } ) ;
110
101
await this . callPUT ( lightData ) ;
111
102
}
112
103
@@ -118,24 +109,10 @@ export abstract class ElgatoLight {
118
109
return ( await this . getLightData ( ) ) ?. brightness ?? - 1 ;
119
110
}
120
111
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 {
128
113
return {
129
114
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 ] ,
139
116
} ;
140
117
}
141
118
}
@@ -152,7 +129,7 @@ export class ElgatoKeyLight extends ElgatoLight {
152
129
*/
153
130
async setTemperature ( temperature : number ) : Promise < void > {
154
131
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 } ) ;
156
133
await this . callPUT ( lightData ) ;
157
134
}
158
135
@@ -181,7 +158,7 @@ export class ElgatoLightStrip extends ElgatoLight {
181
158
*/
182
159
async setHue ( hue : number ) : Promise < void > {
183
160
const sanitizedValue = Math . max ( 0 , Math . min ( 360 , hue ) ) ;
184
- const lightData = ElgatoLight . createLightData ( undefined , sanitizedValue ) ;
161
+ const lightData = ElgatoLight . createLightData ( { hue : sanitizedValue } ) ;
185
162
await this . callPUT ( lightData ) ;
186
163
}
187
164
@@ -199,7 +176,7 @@ export class ElgatoLightStrip extends ElgatoLight {
199
176
*/
200
177
async setSaturation ( saturation : number ) : Promise < void > {
201
178
const sanitizedValue = Math . max ( 0 , Math . min ( 100 , saturation ) ) ;
202
- const lightData = ElgatoLight . createLightData ( undefined , undefined , sanitizedValue ) ;
179
+ const lightData = ElgatoLight . createLightData ( { saturation : sanitizedValue } ) ;
203
180
await this . callPUT ( lightData ) ;
204
181
}
205
182
0 commit comments