Skip to content

Instantly share code, notes, and snippets.

@anttikuuskoski
Last active September 24, 2023 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anttikuuskoski/ca6e4b70bc86786ce49d22abe4fac06a to your computer and use it in GitHub Desktop.
Save anttikuuskoski/ca6e4b70bc86786ce49d22abe4fac06a to your computer and use it in GitHub Desktop.
Code to demonstrate behaviour of Object.create (not working) vs cloning through json (working) in shelly gen2 firmware 1.0.0-beta6 - 1.0.3 running in Shelly Plug Plus S
function reportConfigError(res, ec, em) {
console.log("Config update error", ec, em);
}
function cloneObject(something) {
let str = JSON.stringify(something);
return JSON.parse(str);
}
let bluConfig = {
leds: {
mode: "switch",
colors: {
"switch:0": {
on: {
rgb: [ 0, 0, 100],
brightness: 80 },
off: {
rgb: [ 0, 50, 0 ],
brightness: 10 }
},
power: { brightness: 100 }
},
night_mode: {
enable: false,
brightness: 100,
active_between: [
"00:00",
"00:00"
]
}
},
controls: {
"switch:0": {
in_mode: "momentary"
}
}
};
// let clone = Object.create(bluConfig); // Results in error -103 Missing or bad argument 'leds or controls'! from PLUGS_UI.SetConfig
let clone = cloneObject(bluConfig); // Works as expected.
Shelly.call( "PLUGS_UI.SetConfig", { config: clone }, reportConfigError );
@anttikuuskoski
Copy link
Author

Debug log when using cloneObject:

shelly_user_script.:370 Running 'script_6.js'... 17:20:38
shelly_ejs_rpc.cpp:41 Shelly.call PLUGS_UI.SetConfig {"config":{"leds":{"mode":"switch","colors":{"switch:0":{"on":{"rgb":[0,0,100],"brightness":80},"off":{"rgb":[0,50,0],"brightness":10}},"power":{"brightness":100}},"night_mode":{"enable":false,"brightness":100,"active_between":["00:00","00:00"]}},"controls":{"switch:0":{"in_mode":"momentary"}}}} 17:20:38
shelly_user_script.:341 JS RAM stat: after user code: 105144 after: 101372, used: 3772 17:20:38
shelly_notification:163 Status change of script:6: {"id":6,"running":true} 17:20:38
shos_rpc_inst.c:230 PLUGS_UI.SetConfig via loopback 17:20:38
shos_sys_config.c:326 Saved to conf9.json 17:20:39
shos_sys_config.c:326 Saved to conf9.json 17:20:39
shelly_notification:208 Event from plugs_ui: {"component":"plugs_ui","event":"config_changed","restart_required":false,"ts":1691763639.10,"cfg_rev":13712} 17:20:39

@anttikuuskoski
Copy link
Author

anttikuuskoski commented Aug 11, 2023

Debug log when using Object.create

shelly_user_script.:370 Running 'script_6.js'... 17:22:05 
shelly_ejs_rpc.cpp:41 Shelly.call PLUGS_UI.SetConfig {"config":{}} 17:22:05
shelly_user_script.:341 JS RAM stat: after user code: 102084 after: 99592, used: 2492 17:22:05
shelly_notification:163 Status change of script:6: {"id":6,"running":true} 17:22:05
shos_rpc_inst.c:230 PLUGS_UI.SetConfig via loopback 17:22:05
Config update error -103 17:22:05

@anttikuuskoski
Copy link
Author

See https://gist.github.com/anttikuuskoski/08c246aba218d74f65c752d55f836dae which demonstrates that Object.create or something in the running environment is not entirely sane.

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