diff --git a/CHANGELOG.md b/CHANGELOG.md index a87f557999..6d98c121d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ## __WORK IN PROGRESS__ - Lucy * (@foxriver76) fix edge case problem on Windows (if adapter calls `readDir` on single file) +* (@foxriver76) fixed setting negative numbers via `state set` cli command ## 7.0.6 (2024-12-08) - Lucy * (@foxriver76) fixed UI upgrade if admin is running on privileged port (<1024) diff --git a/packages/cli/src/lib/cli/cliStates.ts b/packages/cli/src/lib/cli/cliStates.ts index 0dfbea44b0..6260284d36 100644 --- a/packages/cli/src/lib/cli/cliStates.ts +++ b/packages/cli/src/lib/cli/cliStates.ts @@ -9,9 +9,15 @@ const ALIAS_STARTS_WITH = 'alias.'; type ResultTransform = (input: ioBroker.State) => string; +interface CLIStatesOptions extends CLICommandOptions { + id?: string; + value?: string | boolean | number; + ack?: 'true' | 'false' | 0 | 1; +} + /** Command iobroker state ... */ -export class CLIStates extends CLICommand { - constructor(options: CLICommandOptions) { +export class CLIStates extends CLICommand { + constructor(options: CLIStatesOptions) { super(options); } @@ -115,7 +121,7 @@ export class CLIStates extends CLICommand { * @param _obj cached object */ private async _isBinary(id: string, objects: ObjectsClient, _obj?: ioBroker.AnyObject | null): Promise { - const obj = _obj || (await objects.getObjectAsync(id)); + const obj = _obj || (await objects.getObject(id)); return !!(obj && ('binary' in obj || (obj.common && 'type' in obj.common && obj.common.type === 'file'))); } @@ -205,24 +211,30 @@ export class CLIStates extends CLICommand { * @param args parsed cli arguments */ set_(args: any[]): void { - const { callback, dbConnect, showHelp } = this.options; - // eslint-disable-next-line prefer-const - let [id, val, ack] = args.slice(1) as [string, any, any]; + const { callback, dbConnect, showHelp, value, ack: ackArg, id } = this.options; const force = args.includes('--force') || args.includes('-f'); - if (val === undefined) { + if (id === undefined) { + CLI.error.requiredArgumentMissing('id'); + showHelp(); + return void callback(0); + } + + if (value === undefined) { CLI.error.requiredArgumentMissing('value'); showHelp(); return void callback(0); } - if (ack !== undefined) { - ack = ack === 'true' || ack === '1' || ack === 1 || ack === true; + let ack = false; + + if (ackArg !== undefined) { + ack = ackArg === 'true' || ackArg === 1; } dbConnect(params => { const { states, objects } = params; - const newVal = ack === undefined ? { val, ack: false } : { val, ack: !!ack }; + const newVal = { val: value, ack: !!ack }; if (id.startsWith(ALIAS_STARTS_WITH)) { objects.getObject(id, async (_err, obj) => { @@ -231,7 +243,7 @@ export class CLIStates extends CLICommand { return void callback(1); } // alias - if (obj && obj.common && obj.common.alias && obj.common.alias.id) { + if (obj?.common?.alias?.id) { const aliasId = typeof obj.common.alias.id.write === 'string' ? obj.common.alias.id.write @@ -251,7 +263,7 @@ export class CLIStates extends CLICommand { if (obj.common.type === 'string') { newVal.val = newVal.val.toString(); } else if (obj.common.type === 'number') { - newVal.val = parseFloat(newVal.val); + newVal.val = Number(newVal.val); } else if (obj.common.type === 'boolean') { newVal.val = newVal.val.toString(); newVal.val = @@ -279,7 +291,7 @@ export class CLIStates extends CLICommand { CLI.error.unknown(err.message); return void callback(1); // ? } - CLI.success.stateUpdated(id, val, !!ack); + CLI.success.stateUpdated(id, value, !!ack); return void callback(0); }, ); @@ -306,11 +318,11 @@ export class CLIStates extends CLICommand { return void callback(1); // object not exists } - if (obj && obj.common && obj.common.type) { + if (obj?.common?.type) { if (obj.common.type === 'string') { newVal.val = newVal.val.toString(); } else if (obj.common.type === 'number') { - newVal.val = parseFloat(newVal.val); + newVal.val = Number(newVal.val); } else if (obj.common.type === 'boolean') { newVal.val = newVal.val.toString(); newVal.val = @@ -326,7 +338,7 @@ export class CLIStates extends CLICommand { CLI.error.unknown(err.message); return void callback(1); // ? } - CLI.success.stateUpdated(id, val, !!ack); + CLI.success.stateUpdated(id, value, !!ack); return void callback(0); }); }); diff --git a/packages/controller/test/lib/testConsole.ts b/packages/controller/test/lib/testConsole.ts index 45bdb6e4ae..50d17355be 100644 --- a/packages/controller/test/lib/testConsole.ts +++ b/packages/controller/test/lib/testConsole.ts @@ -533,6 +533,17 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont // object o // state s + it(`${testName}state set with negative number`, async () => { + const id = 'system.adapter.admin.upload'; + // check update + const res = await execAsync(`"${process.execPath}" "${iobExecutable}" state set "${id}" "-1" 1`); + expect(res.stderr).to.be.not.ok; + + const state = await context.states.getState(id); + + expect(state?.val).to.be.equal(-1); + expect(state?.ack).to.be.equal(true); + }).timeout(20_000); // message // update @@ -718,7 +729,7 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont // ok } - await context.objects.setObjectAsync('system.adapter.vis.0', { + await context.objects.setObject('system.adapter.vis.0', { common: { name: 'iobroker.vis-2', version: '1.0.0',