Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix]: fixed setting negative numbers via state set cli command #3003

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 28 additions & 16 deletions packages/cli/src/lib/cli/cliStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CLIStatesOptions> {
constructor(options: CLIStatesOptions) {
super(options);
}

Expand Down Expand Up @@ -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<boolean> {
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')));
}
Expand Down Expand Up @@ -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) => {
Expand All @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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);
},
);
Expand All @@ -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 =
Expand All @@ -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);
});
});
Expand Down
13 changes: 12 additions & 1 deletion packages/controller/test/lib/testConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
Loading