How to set global from python plugin
-
What is the 'right ' way to set a global variable from a python routine running on the SBC?
At the moment I'm using something equivalent to
from dsf.connections import CommandConnection cmd_con = CommandConnection(debug=True) cmd_con.connect() newvalue = 37 res = cmd_con.perform_simple_code('set global.foobar = ' + newvalue, async_exec=True)
Which works, but I wondered if there's a better way to get to write to the object model directly.
I note that there is
patch_object_model(key: str, patch: str)
in the dsf-python source, but it "May be used only in non-SPI mode", and there'sset_object_model(property_path: str, value: str)
but it is to "Set an atomic property in the object model" and I both don't actually know what it means by that, and can't make it do what I want -cmd_con.lock_object_model() cmd_con.set_object_model('global.foobar','123') cmd_con.unlock_object_model()
for example gives me (inter alia)
send: {"command":"SetObjectModel","PropertyPath":"global:foobar","Value":"123"} recv: {"result":false,"success":true}
which I think means the command successfully sent, but didn't do anything (the variable doesn't get the value).
I should probably confess that I'm using dsf-python-3.5.0rc2 on a 3.5.1 with bookworm DuetPi, so I might possibly be jumping the gun a bit.
-
@achrn Your first approach is fine. Instead of a simple code you can send a regular DSF G-code as well.
SetObjectModel
is reserved for the DuetPiManagementPlugin and for non-SPI mode (which I haven't tested in a while), so don't use that.