Skip to content

Commit 3f7dfd7

Browse files
authored
[fix]: fixed setting negative numbers via state set cli command (#3003)
* fixed setting negative numbers via state set cli command * fix test call
1 parent ea2722d commit 3f7dfd7

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
## __WORK IN PROGRESS__ - Lucy
88
* (@foxriver76) fix edge case problem on Windows (if adapter calls `readDir` on single file)
9+
* (@foxriver76) fixed setting negative numbers via `state set` cli command
910

1011
## 7.0.6 (2024-12-08) - Lucy
1112
* (@foxriver76) fixed UI upgrade if admin is running on privileged port (<1024)

packages/cli/src/lib/cli/cliStates.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ const ALIAS_STARTS_WITH = 'alias.';
99

1010
type ResultTransform = (input: ioBroker.State) => string;
1111

12+
interface CLIStatesOptions extends CLICommandOptions {
13+
id?: string;
14+
value?: string | boolean | number;
15+
ack?: 'true' | 'false' | 0 | 1;
16+
}
17+
1218
/** Command iobroker state ... */
13-
export class CLIStates extends CLICommand {
14-
constructor(options: CLICommandOptions) {
19+
export class CLIStates extends CLICommand<CLIStatesOptions> {
20+
constructor(options: CLIStatesOptions) {
1521
super(options);
1622
}
1723

@@ -115,7 +121,7 @@ export class CLIStates extends CLICommand {
115121
* @param _obj cached object
116122
*/
117123
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));
119125

120126
return !!(obj && ('binary' in obj || (obj.common && 'type' in obj.common && obj.common.type === 'file')));
121127
}
@@ -205,24 +211,30 @@ export class CLIStates extends CLICommand {
205211
* @param args parsed cli arguments
206212
*/
207213
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;
211215
const force = args.includes('--force') || args.includes('-f');
212216

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) {
214224
CLI.error.requiredArgumentMissing('value');
215225
showHelp();
216226
return void callback(0);
217227
}
218228

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;
221233
}
222234

223235
dbConnect(params => {
224236
const { states, objects } = params;
225-
const newVal = ack === undefined ? { val, ack: false } : { val, ack: !!ack };
237+
const newVal = { val: value, ack: !!ack };
226238

227239
if (id.startsWith(ALIAS_STARTS_WITH)) {
228240
objects.getObject(id, async (_err, obj) => {
@@ -231,7 +243,7 @@ export class CLIStates extends CLICommand {
231243
return void callback(1);
232244
}
233245
// alias
234-
if (obj && obj.common && obj.common.alias && obj.common.alias.id) {
246+
if (obj?.common?.alias?.id) {
235247
const aliasId =
236248
typeof obj.common.alias.id.write === 'string'
237249
? obj.common.alias.id.write
@@ -251,7 +263,7 @@ export class CLIStates extends CLICommand {
251263
if (obj.common.type === 'string') {
252264
newVal.val = newVal.val.toString();
253265
} else if (obj.common.type === 'number') {
254-
newVal.val = parseFloat(newVal.val);
266+
newVal.val = Number(newVal.val);
255267
} else if (obj.common.type === 'boolean') {
256268
newVal.val = newVal.val.toString();
257269
newVal.val =
@@ -279,7 +291,7 @@ export class CLIStates extends CLICommand {
279291
CLI.error.unknown(err.message);
280292
return void callback(1); // ?
281293
}
282-
CLI.success.stateUpdated(id, val, !!ack);
294+
CLI.success.stateUpdated(id, value, !!ack);
283295
return void callback(0);
284296
},
285297
);
@@ -306,11 +318,11 @@ export class CLIStates extends CLICommand {
306318
return void callback(1); // object not exists
307319
}
308320

309-
if (obj && obj.common && obj.common.type) {
321+
if (obj?.common?.type) {
310322
if (obj.common.type === 'string') {
311323
newVal.val = newVal.val.toString();
312324
} else if (obj.common.type === 'number') {
313-
newVal.val = parseFloat(newVal.val);
325+
newVal.val = Number(newVal.val);
314326
} else if (obj.common.type === 'boolean') {
315327
newVal.val = newVal.val.toString();
316328
newVal.val =
@@ -326,7 +338,7 @@ export class CLIStates extends CLICommand {
326338
CLI.error.unknown(err.message);
327339
return void callback(1); // ?
328340
}
329-
CLI.success.stateUpdated(id, val, !!ack);
341+
CLI.success.stateUpdated(id, value, !!ack);
330342
return void callback(0);
331343
});
332344
});

packages/controller/test/lib/testConsole.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,17 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
533533
// object o
534534

535535
// state s
536+
it(`${testName}state set with negative number`, async () => {
537+
const id = 'system.adapter.admin.upload';
538+
// check update
539+
const res = await execAsync(`"${process.execPath}" "${iobExecutable}" state set "${id}" "-1" 1`);
540+
expect(res.stderr).to.be.not.ok;
541+
542+
const state = await context.states.getState(id);
543+
544+
expect(state?.val).to.be.equal(-1);
545+
expect(state?.ack).to.be.equal(true);
546+
}).timeout(20_000);
536547

537548
// message
538549
// update
@@ -718,7 +729,7 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
718729
// ok
719730
}
720731

721-
await context.objects.setObjectAsync('system.adapter.vis.0', {
732+
await context.objects.setObject('system.adapter.vis.0', {
722733
common: {
723734
name: 'iobroker.vis-2',
724735
version: '1.0.0',

0 commit comments

Comments
 (0)