Read metadata from actual file printing

Hi,

I like to access file metadata available in filesViewsModel from printerStateViewModel. Is there a better solution as recursively go through the allItems() Array in filesViewModel? Actually I know the path of the file, have to split it into folders and filename and loop through the AllItems-Array from folders to the file itself. Looks tricky but should work...

filepath:

AllItems-Array:

Any better ideas?

Cheers,
Nils

Use the built-in js client?

https://docs.octoprint.org/en/master/jsclientlib/files.html#OctoPrintClient.files.get

Have to loop recursively also using js client but very nice that there is an example. Is it better to use js client instead of functions available in filesViewModel? Information in the results are the same...

PS. I hate JS and debugging in the browser...the additional file metadata stored via set_additional_metadata is returned using "list" for every file but if using "get" for a specific file it is not available. Is it possible to add this via options?

Done it now using OctoPrint.files.listForLocation("local/" + self.printerStateViewModel.filepath())
This function will return all metadata available and also only the file selected. So no recursive parsing and also I with async handling of the jquery request the performance of the GUI is a little better-

ok, but that seems to be the same as OctoPrint.files.get('local', self.printerStateViewModel.filepath()). It's also async I think as it returns a promise, which could then use .done, etc.

1 Like

Checked it with a GCODE file I used for testing without metadata. My fault, you are right. It behave the same and I changed to it to get-method as it better matches what I need in next version of my plugin.

Do you know why how to get the origin from according to self.printerStateViewModel.filepath? I could not find any variable holding the origin itself in printerStateView of the file selected. I checked SD attribute but it does not hold the location on filepath change...so it is false if I select a file from SD the first time and true if I move back to the local storage.

//get list of enabled metadata and filament change if a file is selected
self.printerStateViewModel.filepath.subscribe(function() {
  if (typeof self.printerStateViewModel.filepath() !== 'undefined' && self.printerStateViewModel.filepath() !== null) {

PS. Should I add a second watch that my function is also executed on change of "SD" attribute? Or is there a professional way that somewhere the origin is stored according the filepath attribute?

PPS. Ha. :slight_smile: Will change my coding to use a callback from OctoPrint client with OnEvent FileSelected and FileDeselected. I think this is the appropiate method. :slight_smile: Thanks for discussion. :wink:

Yeah, that's a good way to approach it too.