@@ -9,9 +9,15 @@ const ALIAS_STARTS_WITH = 'alias.';
9
9
10
10
type ResultTransform = ( input : ioBroker . State ) => string ;
11
11
12
+ interface CLIStatesOptions extends CLICommandOptions {
13
+ id ?: string ;
14
+ value ?: string | boolean | number ;
15
+ ack ?: 'true' | 'false' | 0 | 1 ;
16
+ }
17
+
12
18
/** Command iobroker state ... */
13
- export class CLIStates extends CLICommand {
14
- constructor ( options : CLICommandOptions ) {
19
+ export class CLIStates extends CLICommand < CLIStatesOptions > {
20
+ constructor ( options : CLIStatesOptions ) {
15
21
super ( options ) ;
16
22
}
17
23
@@ -115,7 +121,7 @@ export class CLIStates extends CLICommand {
115
121
* @param _obj cached object
116
122
*/
117
123
private async _isBinary ( id : string , objects : ObjectsClient , _obj ?: ioBroker . AnyObject | null ) : Promise < boolean > {
118
- const obj = _obj || ( await objects . getObjectAsync ( id ) ) ;
124
+ const obj = _obj || ( await objects . getObject ( id ) ) ;
119
125
120
126
return ! ! ( obj && ( 'binary' in obj || ( obj . common && 'type' in obj . common && obj . common . type === 'file' ) ) ) ;
121
127
}
@@ -205,24 +211,30 @@ export class CLIStates extends CLICommand {
205
211
* @param args parsed cli arguments
206
212
*/
207
213
set_ ( args : any [ ] ) : void {
208
- const { callback, dbConnect, showHelp } = this . options ;
209
- // eslint-disable-next-line prefer-const
210
- let [ id , val , ack ] = args . slice ( 1 ) as [ string , any , any ] ;
214
+ const { callback, dbConnect, showHelp, value, ack : ackArg , id } = this . options ;
211
215
const force = args . includes ( '--force' ) || args . includes ( '-f' ) ;
212
216
213
- if ( val === undefined ) {
217
+ if ( id === undefined ) {
218
+ CLI . error . requiredArgumentMissing ( 'id' ) ;
219
+ showHelp ( ) ;
220
+ return void callback ( 0 ) ;
221
+ }
222
+
223
+ if ( value === undefined ) {
214
224
CLI . error . requiredArgumentMissing ( 'value' ) ;
215
225
showHelp ( ) ;
216
226
return void callback ( 0 ) ;
217
227
}
218
228
219
- if ( ack !== undefined ) {
220
- ack = ack === 'true' || ack === '1' || ack === 1 || ack === true ;
229
+ let ack = false ;
230
+
231
+ if ( ackArg !== undefined ) {
232
+ ack = ackArg === 'true' || ackArg === 1 ;
221
233
}
222
234
223
235
dbConnect ( params => {
224
236
const { states, objects } = params ;
225
- const newVal = ack === undefined ? { val, ack : false } : { val , ack : ! ! ack } ;
237
+ const newVal = { val : value , ack : ! ! ack } ;
226
238
227
239
if ( id . startsWith ( ALIAS_STARTS_WITH ) ) {
228
240
objects . getObject ( id , async ( _err , obj ) => {
@@ -231,7 +243,7 @@ export class CLIStates extends CLICommand {
231
243
return void callback ( 1 ) ;
232
244
}
233
245
// alias
234
- if ( obj && obj . common && obj . common . alias && obj . common . alias . id ) {
246
+ if ( obj ? .common ?. alias ? .id ) {
235
247
const aliasId =
236
248
typeof obj . common . alias . id . write === 'string'
237
249
? obj . common . alias . id . write
@@ -251,7 +263,7 @@ export class CLIStates extends CLICommand {
251
263
if ( obj . common . type === 'string' ) {
252
264
newVal . val = newVal . val . toString ( ) ;
253
265
} else if ( obj . common . type === 'number' ) {
254
- newVal . val = parseFloat ( newVal . val ) ;
266
+ newVal . val = Number ( newVal . val ) ;
255
267
} else if ( obj . common . type === 'boolean' ) {
256
268
newVal . val = newVal . val . toString ( ) ;
257
269
newVal . val =
@@ -279,7 +291,7 @@ export class CLIStates extends CLICommand {
279
291
CLI . error . unknown ( err . message ) ;
280
292
return void callback ( 1 ) ; // ?
281
293
}
282
- CLI . success . stateUpdated ( id , val , ! ! ack ) ;
294
+ CLI . success . stateUpdated ( id , value , ! ! ack ) ;
283
295
return void callback ( 0 ) ;
284
296
} ,
285
297
) ;
@@ -306,11 +318,11 @@ export class CLIStates extends CLICommand {
306
318
return void callback ( 1 ) ; // object not exists
307
319
}
308
320
309
- if ( obj && obj . common && obj . common . type ) {
321
+ if ( obj ? .common ? .type ) {
310
322
if ( obj . common . type === 'string' ) {
311
323
newVal . val = newVal . val . toString ( ) ;
312
324
} else if ( obj . common . type === 'number' ) {
313
- newVal . val = parseFloat ( newVal . val ) ;
325
+ newVal . val = Number ( newVal . val ) ;
314
326
} else if ( obj . common . type === 'boolean' ) {
315
327
newVal . val = newVal . val . toString ( ) ;
316
328
newVal . val =
@@ -326,7 +338,7 @@ export class CLIStates extends CLICommand {
326
338
CLI . error . unknown ( err . message ) ;
327
339
return void callback ( 1 ) ; // ?
328
340
}
329
- CLI . success . stateUpdated ( id , val , ! ! ack ) ;
341
+ CLI . success . stateUpdated ( id , value , ! ! ack ) ;
330
342
return void callback ( 0 ) ;
331
343
} ) ;
332
344
} ) ;
0 commit comments