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

[Bug]: getState(ID).val nach Sortierung kuriose Rückwirkung wenn ID ein Array ist #1758

Open
3 tasks done
ETGermany opened this issue Nov 4, 2024 · 1 comment
Open
3 tasks done

Comments

@ETGermany
Copy link

ETGermany commented Nov 4, 2024

I'm sure that

  • This issue is still present in the current beta version of this adapter
  • There is no other (open) issue with the same topic (use the search!)
  • This issue is not described in the adapter documentation / FAQ (read the docs!)

Script type

JavaScript

The problem

In einem Datenpunkt ist eine Array enthalten.
Mit einem getState(ID).val wird das Array in eine Variable1 übernommen, in eine Variable2 wird eine Kopie der Variable1 abgelegt.
Variable2 wird sortiert. Danach haben Variable1 und Variable2 das sortierte Array als Wert.
Ein erneutes getState(ID).val für Variable1 ist ohne Wirkung, in Variable1 bleibt das sortierte Array, selbst wenn dem vor dem erneuten getState() ein leeres Array zugewiesen wird.
Skript stoppen und neu starten ist ohne Änderung.
Nach Stoppen und Starten der JS-Instanz funktioniert getState(ID).val einmalig wieder.

var testArray = '0_userdata.0.TestArray'; // LISTE: [9,5,1,7,0]

for (let i=1;i<3;i++) {
    var arrayValue = getState(testArray).val;
    log("ArrayValue: " + arrayValue);
    var arrayValueSort = arrayValue;
    arrayValueSort.sort();
    log("ArrayValueSort: " + arrayValueSort);
    arrayValue = [];
}

JavaScriptLog Erklärung

iobroker.current.log (in debug mode!)

javascript.1
2024-11-04 13:03:49.001	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions

javascript.1
2024-11-04 13:03:49.001	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValueSort: 0,1,5,7,9

javascript.1
2024-11-04 13:03:49.000	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValue: 0,1,5,7,9

javascript.1
2024-11-04 13:03:49.000	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValueSort: 0,1,5,7,9

javascript.1
2024-11-04 13:03:49.000	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValue: 9,5,1,7,0

javascript.1
2024-11-04 13:03:48.990	info	Start JavaScript script.js.common.ZUMTESTEN.ARRAY_TEST_2 (Javascript/js)

javascript.1
2024-11-04 13:03:43.020	debug	[sunTimeSchedules] Next: 2024-11-04T23:00:01.000Z

javascript.1
2024-11-04 13:03:42.847	info	Start JavaScript script.js.common.ZUMTESTEN.ZAHLEN_TEST (Javascript/js)

javascript.1
2024-11-04 13:03:42.727	debug	[sunTimeSchedules] Times: {"solarNoon":"2024-11-04T11:11:05.839Z","nadir":"2024-11-04T23:11:05.839Z","sunrise":"2024-11-04T06:16:15.115Z","sunset":"2024-11-04T16:05:56.563Z","sunriseEnd":"2024-11-04T06:19:40.515Z","sunsetStart":"2024-11-04T16:02:31.163Z","dawn":"2024-11-04T05:43:46.215Z","dusk":"2024-11-04T16:38:25.463Z","nauticalDawn":"2024-11-04T05:07:13.256Z","nauticalDusk":"2024-11-04T17:14:58.422Z","nightEnd":"2024-11-04T04:31:23.222Z","night":"2024-11-04T17:50:48.456Z","goldenHourEnd":"2024-11-04T07:01:34.388Z","goldenHour":"2024-11-04T15:20:37.290Z"}

javascript.1
2024-11-04 13:03:42.724	info	received all states

javascript.1
2024-11-04 13:03:42.641	info	received all objects

javascript.1
2024-11-04 13:03:42.122	info	requesting all objects

javascript.1
2024-11-04 13:03:42.120	info	requesting all states

javascript.1
2024-11-04 13:03:41.779	debug	Loaded TypeScript definitions for request: ["node_modules/@types/request/package.json","node_modules/@types/request/index.d.ts"]

javascript.1
2024-11-04 13:03:41.771	debug	Loaded TypeScript definitions for node: ["node_modules/@types/node/package.json","node_modules/@types/node/index.d.ts","node_modules/@types/node/globals.typedarray.d.ts","node_modules/@types/node/buffer.buffer.d.ts","node_modules/@types/node/assert.d.ts","node_modules/@types/node/assert/strict.d.ts","node_modules/@types/node/globals.d.ts","node_modules/@types/node/async_hooks.d.ts","node_modules/@types/node/buffer.d.ts","node_modules/@types/node/child_process.d.ts","node_modules/@types/node/cluster.d.ts","node_modules/@types/node/console.d.ts","node_modules/@types/node/constants.d.ts","node_modules/@types/node/crypto.d.ts","node_modules/@types/node/dgram.d.ts","node_modules/@types/node/diagnostics_channel.d.ts","node_modules/@types/node/dns.d.ts","node_modules/@types/node/dns/promises.d.ts","node_modules/@types/node/domain.d.ts","node_modules/@types/node/dom-events.d.ts","node_modules/@types/node/events.d.ts","node_modules/@types/node/fs.d.ts","node_modules/@types/node/fs/promises.d.ts","node_modules/@types/node/http.d.ts","node_modules/@types/node/http2.d.ts","node_modules/@types/node/https.d.ts","node_modules/@types/node/inspector.d.ts","node_modules/@types/node/module.d.ts","node_modules/@types/node/net.d.ts","node_modules/@types/node/os.d.ts","node_modules/@types/node/path.d.ts","node_modules/@types/node/perf_hooks.d.ts","node_modules/@types/node/process.d.ts","node_modules/@types/node/punycode.d.ts","node_modules/@types/node/querystring.d.ts","node_modules/@types/node/readline.d.ts","node_modules/@types/node/readline/promises.d.ts","node_modules/@types/node/repl.d.ts","node_modules/@types/node/sea.d.ts","node_modules/@types/node/sqlite.d.ts","node_modules/@types/node/stream.d.ts","node_modules/@types/node/stream/promises.d.ts","node_modules/@types/node/stream/consumers.d.ts","node_modules/@types/node/stream/web.d.ts","node_modules/@types/node/string_decoder.d.ts","node_modules/@types/node/test.d.ts","node_modules/@types/node/timers.d.ts","node_modules/@types/node/timers/promises.d.ts","node_modules/@types/node/tls.d.ts","node_modules/@types/node/trace_events.d.ts","node_modules/@types/node/tty.d.ts","node_modules/@types/node/url.d.ts","node_modules/@types/node/util.d.ts","node_modules/@types/node/v8.d.ts","node_modules/@types/node/vm.d.ts","node_modules/@types/node/wasi.d.ts","node_modules/@types/node/worker_threads.d.ts","node_modules/@types/node/zlib.d.ts","node_modules/@types/node/globals.global.d.ts"]

javascript.1
2024-11-04 13:03:41.740	debug	Installed custom library: "protobufjs@latest"

javascript.1
2024-11-04 13:03:39.502	debug	Found custom library in config: "protobufjs@latest"

javascript.1
2024-11-04 13:03:39.450	debug	Installed custom library: "mqtt@latest"

javascript.1
2024-11-04 13:03:37.222	debug	Found custom library in config: "mqtt@latest"

javascript.1
2024-11-04 13:03:37.220	debug	Installed custom library: "pi-controller@latest"

javascript.1
2024-11-04 13:03:34.995	debug	Found custom library in config: "pi-controller@latest"

javascript.1
2024-11-04 13:03:34.994	debug	Found installed libraries: ["mqtt","pi-controller","protobufjs"]

javascript.1
2024-11-04 13:03:34.984	debug	Custom libraries in config: "pi-controller mqtt protobufjs": ["pi-controller","mqtt","protobufjs"]

javascript.1
2024-11-04 13:03:34.981	debug	config.subscribe (Do not subscribe all states on start): false

javascript.1
2024-11-04 13:03:34.967	info	starting. Version 8.8.3 (non-npm: ioBroker/ioBroker.javascript) in /opt/iobroker/node_modules/iobroker.javascript, node: v20.18.0, js-controller: 6.0.11

javascript.1
2024-11-04 13:03:34.795	debug	Plugin sentry Initialize Plugin (enabled=true)

javascript.1
2024-11-04 13:03:34.759	debug	States connected to redis: 127.0.0.1:9000

javascript.1
2024-11-04 13:03:34.704	debug	States create User PubSub Client

javascript.1
2024-11-04 13:03:34.703	debug	States create System PubSub Client

javascript.1
2024-11-04 13:03:34.697	debug	Redis States: Use Redis connection: 127.0.0.1:9000

javascript.1
2024-11-04 13:03:34.675	debug	Objects connected to redis: 127.0.0.1:9001

javascript.1
2024-11-04 13:03:34.671	debug	Objects client initialize lua scripts

javascript.1
2024-11-04 13:03:34.649	debug	Objects create User PubSub Client

javascript.1
2024-11-04 13:03:34.648	debug	Objects create System PubSub Client

javascript.1
2024-11-04 13:03:34.647	debug	Objects client ready ... initialize now

javascript.1
2024-11-04 13:03:34.630	debug	Redis Objects: Use Redis connection: 127.0.0.1:9001

host.n51-ubuntu
2024-11-04 13:03:33.757	info	instance system.adapter.javascript.1 in version "8.8.3" (non-npm: ioBroker/ioBroker.javascript) started with pid 2522

host.n51-ubuntu
2024-11-04 13:03:33.549	info	"system.adapter.javascript.1" enabled

host.n51-ubuntu
2024-11-04 13:03:29.344	info	instance system.adapter.javascript.1 terminated with code 11 (ADAPTER_REQUESTED_TERMINATION)

javascript.1
2024-11-04 13:03:29.296	info	terminating

host.n51-ubuntu
2024-11-04 13:03:28.856	info	stopInstance system.adapter.javascript.1 send kill signal

javascript.1
2024-11-04 13:03:28.797	info	Terminated (ADAPTER_REQUESTED_TERMINATION): Without reason

javascript.1
2024-11-04 13:03:28.796	debug	Plugin sentry destroyed

javascript.1
2024-11-04 13:03:28.796	info	terminating

javascript.1
2024-11-04 13:03:28.795	info	Stopping script script.js.common.ZUMTESTEN.ZAHLEN_TEST

javascript.1
2024-11-04 13:03:28.794	info	Got terminate signal TERMINATE_YOURSELF

host.n51-ubuntu
2024-11-04 13:03:28.791	info	stopInstance system.adapter.javascript.1 (force=false, process=true)

host.n51-ubuntu
2024-11-04 13:03:28.790	info	"system.adapter.javascript.1" disabled

javascript.1
2024-11-04 13:03:24.348	info	Stopping script script.js.common.ZUMTESTEN.ARRAY_TEST_2

javascript.1
2024-11-04 13:03:21.729	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions

javascript.1
2024-11-04 13:03:21.729	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValueSort: 0,1,5,7,9

javascript.1
2024-11-04 13:03:21.728	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValue: 0,1,5,7,9

javascript.1
2024-11-04 13:03:21.728	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValueSort: 0,1,5,7,9

javascript.1
2024-11-04 13:03:21.728	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValue: 0,1,5,7,9

javascript.1
2024-11-04 13:03:21.702	info	Start JavaScript script.js.common.ZUMTESTEN.ARRAY_TEST_2 (Javascript/js)

javascript.1
2024-11-04 13:03:12.899	info	Stopping script script.js.common.ZUMTESTEN.ARRAY_TEST_2

javascript.1
2024-11-04 13:03:09.780	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions

javascript.1
2024-11-04 13:03:09.780	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValueSort: 0,1,5,7,9

javascript.1
2024-11-04 13:03:09.780	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValue: 0,1,5,7,9

javascript.1
2024-11-04 13:03:09.780	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValueSort: 0,1,5,7,9

javascript.1
2024-11-04 13:03:09.779	info	script.js.common.ZUMTESTEN.ARRAY_TEST_2: ArrayValue: 9,5,1,7,0

javascript.1
2024-11-04 13:03:09.776	info	Start JavaScript script.js.common.ZUMTESTEN.ARRAY_TEST_2 (Javascript/js)

javascript.1
2024-11-04 13:03:07.881	debug	[sunTimeSchedules] Next: 2024-11-04T23:00:01.000Z

Version of nodejs

20.18.0

Version of ioBroker js-controller

6.0.11

Version of adapter

8.8.3

@klein0r
Copy link
Collaborator

klein0r commented Nov 8, 2024

Das ist ein generelles Problem. .sort() sortiert ja die originale Referenz um (und gibt kein neues Array zurück). Daher wird im Cache des JavaScript-Adapters der Wert umsortiert und dann immer wieder so zurückgegeben.

Das gleiche Problem kann man erreichen, wenn man einen State holt, .val auf einen anderen Wert setzt und dann den State nochmal liest. Das wurde nicht berücksichtigt und es sollte immer eine Kopie des State zurückgeliefert werden.

Danke für den Report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants