AMI-system / ami_setup

AMI configuration files
MIT License
1 stars 2 forks source link

Recording metadata using the Bluetooth application #11

Open DylanCarbone opened 7 months ago

DylanCarbone commented 7 months ago

Hi @JonasBchrt,

I hope you are well,

I'm sorry this has taken me so long to post. As we discussed in our meeting last month, when you have the time, could you please add a 'Metadata' section to your Bluetooth app interface. This metadata section should include the following fields:

'Habitat name' - A simple text field asking the user to state the survey habitat. In future it would be useful to encourage choosing habitat names from a dropdown, with names obtained from a standard such as the IUCN Habitat Classification Scheme, but this scheme is still a draft version, and Darwin Core does not enforce a habitat standard, so a written habitat name for now will do.

'Location remarks' - A simple text field asking the user to describe any unexpected characteristics of the deployment environment that they feel may effect results. In most situations this field will be left blank. However, there may be corner cases when the field is needed. For example, the survey design may have strict placement coordinates for the AMI, but the placement is near a large light source and the surveyor wants to make a record of this.

At the end of the section, please add a 'Save metadata' button.

In a second corner case, I can imagine the user repeatedly pressing the 'Save metadata' button out of uncertainty over whether the metadata information has correctly synced. To prevent any crashes, I think the 'Save metadata' button should become locked after each press, until a short timer runs out, or until the Pi relays confirmation of an updated config.json file.

If you have any questions or comments please let me know, thanks.

JonasBchrt commented 7 months ago

Added the elements to the App.

image

It's currently a dummy box that doesn't modify anything on the pi yet. Do you also want me to implement writing to the config.json file on the pi @DylanCarbone now? If yes, can your remind me of the envisioned fixed path of the config.json on the raspi (and rockpi, if desired)? And the structure of the file? (At least to the extend that allows me to reliably modify these two fields.) A Python or JS snippet would also do the job, up to you. Thanks :)

DylanCarbone commented 7 months ago

Hi @JonasBchrt, that looks brilliant, thanks very much for your help!

It would be brilliant if the values could be written to the following fields in the config.json here.

The location of the config.json file varies slightly depending on AMI version, but I would like it to be located in the home/pi or home/bird-pi directory.

You can modify the fields in the config.json file using the following:

with open("/home/bird-pi/config.json", 'r') as file:
        json_config = json.load(file)

json_config["field_observations"]["location_remarks"] = input
json_config["field_observations"]["habitat"] = second_input

The config.json will be stored in the home directory of the raspberry Pi (and only the Raspberry Pi, as we transition away from the Rock Pi).

Please let me know if you have any other questions,

thanks again,

Dylan

JonasBchrt commented 7 months ago

Everything should now be implemented in the bluetooth-dev branch here after commit 19833b353c4d26dc2b12a51e3c1f269e704c2cbd @DylanCarbone

JonasBchrt commented 7 months ago

This uses a slightly more generic approach to setting the metadata, which also allows to set other fields (e.g., setting location via the BLE app should also work now)


    def set_metadata(self, metadata, path="/home/pi/config.json"):
        """
        Updates the metadata of the Ami-Trap in the config.json file.

        If the file doesn't exist, it is created.

        Args:
            metadata (dict): A dictionary containing the metadata.
        """
        # Read config.json, if it exists
        try:
            with open(path, "r") as f:
                config = json.load(f)
        except FileNotFoundError:
            config = {}
        # Go through all fields in metadata and update config
        for top_level_key, top_level_value in metadata.items():
            # Chdck if key exists already in conig.json
            if top_level_key in config:
                # If it's a dictionary, update only the keys that are present in the metadata
                if isinstance(top_level_value, dict):
                    for low_level_key, low_level_value in top_level_value.items():
                        config[top_level_key][low_level_key] = low_level_value
                else:
                    # If it's not a dictionary, update the value
                    config[top_level_key] = top_level_value
            else:
                # If it doesn't exist, create it
                config[top_level_key] = top_level_value
        # Write config to config.json. Format.
        with open(path, "w") as f:
            json.dump(config, f, indent=4)

It currently only checks the path /home/pi/config.json for an existing JSON file. (If there is none, it creates one.) Is that sufficient for now? Or is there a programmatic way to determine the path to config.json? I could also check at multiple locations until I have found it. Always having the same path would be the easiest way, ofc, though.

JonasBchrt commented 7 months ago

I will leave the issue open for you to ask for changes to my current implementation. If everything is fine, you can close it.

DylanCarbone commented 7 months ago

Thank you very much for this Jonas! I will run your implementation when I am next in the office, and close if there are no issues.

DylanCarbone commented 6 months ago

Hi @JonasBchrt,

Thanks again for all your help,

I transferred my config.json file to the "home/pi" directory, connected to it via the Bluetooth app, and attempted to transfer habitat name and location remarks metadata. Unfortunately, I was given an error saying it could not read the metadata to the AMI device. Am I taking the wrong approach? I was able to perform other actions, such as to shut down the AMI. If you can spare the time, could we have a quick meeting next Tuesday to sort out this remaining error?

Thanks,

Dylan

JonasBchrt commented 6 months ago

Hi @DylanCarbone,

I can't do Tuesday because I am at AIUK, but we can do another time - just find a slot in my calendar.

Can you try not putting a JSON file in the folder? Then, one should be created. I'd be curious if that works. Ultimately, we should read your config.json, of course.

JonasBchrt commented 6 months ago

You can also send me your specific config.json and I try. Don't wanna bother you too much.

JonasBchrt commented 6 months ago

I tried to reproduce the issue, but couldn't. Here's what I did:

Let's have a look together on your system.