tbnobody / OpenDTU

Software for ESP32 to talk to Hoymiles/TSUN/Solenso Inverters
GNU General Public License v2.0
1.81k stars 507 forks source link

[Request] Auto-Detect uploaded file type #2378

Open schlimmchen opened 3 hours ago

schlimmchen commented 3 hours ago

Is your feature request related to a problem? Please describe.

It happend to me again today that I was uploading a pin_mapping.json but forgot to set the drop-down menu to "pin_mapping.json", so I killed the config.json instead.

Describe the solution you'd like

Auto-detect the kind of file being uploaded and write the respective destination.

Parse the uploaded file as JSON using ArduinoJson, closing #1097 in the process. Then inspect the JSON to find a known property which allows to assume it is a config.json or a pin_mapping.json or garbage.

Describe alternatives you've considered

No response

Additional context

I have this on my radar for a long time now and I can implement it myself, however, I had to write this down now that it happened to me again...

tbnobody commented 3 hours ago

Duplicate of #1097

tbnobody commented 3 hours ago

No need for opening the same issue again.... or we are closing #1097.... but not both

tbnobody commented 3 hours ago

But this can be done in the webapp itself again... otherwise we would have to save the file using a temporary file name on the littlefs, parse it, and then either rename it to the right file or drop it.

schlimmchen commented 3 hours ago

No need for opening the same issue again.... or we are closing https://github.com/tbnobody/OpenDTU/issues/1097.... but not both

The requests are (slightly) different, as far as I can tell. That's why I opened a new issue. #1097 asks to check the uploaded file to be valid JSON. This issue is asking to drop the selection for the target file name from the UI and to detect automatically, which one should be overwritten, based on the uploaded file's contents.

otherwise we would have to save the file using a temporary file name on the littlefs, parse it, and then either rename it to the right file or drop it.

That sounds tedious. I did not look at the code, yet. I was assuming that the contents of the uploaded files can be streamed into ArduinoJson, so that we could inspect it in-flight, i.e., while it is in RAM.

stefan123t commented 1 hour ago

@schlimmchen yes you are right this is distinct from the other issue. Here it is about uploading the right file to overwrite the pin_mapping.json and not the config.json.

Could we simply use the filename prefix pin_mapping/config to distinguish the two ? I tend to have plenty of those files on my disk, but I try to keep the beginning unchanged.

Saving it in a new file does not sound too tedious from my perspective, we could store it and fall back in case we have to restart within several minutes (though not initiated from the Settings UI by saving). After all LittleFS is also a Flash / CopyOnWrite FS afair. Something like two boot-/config-environments, then you can double check validity of JSON before rejecting / activating it for the next boot.