Using the Meridian API: Output Beacons info to CSV
02-18-2019 05:59 PM - edited 02-18-2019 06:31 PM
If you have a lot of beacons it can be useful to take a look at the information related to them over time in your own way. An easy way to manipulate and filter the data is within a spreadsheet. On writing this note there was no way to export beacons data within the GUI so an easy way to get at it is via the Meridian API. To do this you can use python.
This note assumes you have some basic knowledge of using python. Check out these resources to get started: Python and REST APIs 1: Building a work environment, Python and REST APIs 2: GET Data and Python and REST APIs 3: POST Data
NOTE: It's important to understand the API as much as possible before using it beyond the scope of this article. Data can be overwriten with the API and so caution should be taken. Also, Only documented APIs are supported by Aruba. https://docs.meridianapps.com/article/369-rest-api
The API to use is the Locations Beacons API Endpoint.
- Download the getBeacons_lesson.py attachment (it's zipped).
- Open the python file in your favourite editor (I use pyCharm).
- Let's step through it to see what it does. SpoilerSpoiler: it creates a .csv file with beacons data.
Break it Down:
- Lines 1 and 2 import modules that we need throughout the python.
- Lines 4 and 5 sets up the .csv file to be written.
- Lines 7 through 20 sets up the variables that get used for various jobs. Some are more important than others. You need to edit location and authtoken so that the variables reflect the unique codes assigned to your Meridian location and API access account.
The API documentation on Authenticating to the API discusses creating an Application token which is not always available. Instead you can browse https://edit.meridianapps.com/api/login (you must be logged in) and the token in there for your logged in user account. If your use case requires an Application Token and you are unable to generate one please contact Aruba Support.
The location id can be found within the Meridian editor. While in the editor (within the location) select Settings from the left hand navigation. Under the Location tab --> Location Details is the Location ID field. Copy this in to your getBeacons_lesson.py file in place of <your_location_id> being sure to leave the "quote marks".
- Lines 22 and 23 give some feedback about the API. You will get an output of Location Name and the URL for the Beacons API.
- Lines 26-63 are the functions used to get the data and write it to the .csv file. Each function is defined ("def") and then called upon later on.
- Line 66, 70 and 71 start the process by calling the functions get_beacons, table_header and write_beacons.
- Function get_beacons calls the API and parses the results. It incrementally counts the API call so you can see what is happening. This is because the Meridian API, by default, has a page sizing of 100 which means only 100 beacons will be returend. If more than 100 beacons exist then the 'next' will have a value showing the URL of the next set of results. We check this in the function check_for_more.
- After the first API call has been made we can generate the top row of the .csv file with headers. The function table_header will populate this row and print it on screen.
- The check_for_more function, as mentioned above, will check the value of 'next' to see if there is any more beacons data to collect from the API. If the value of 'next' is not 'None' then (hopefully) it should be a URL pointing to the next set of beacons data. The beacons_api variable will be overwritten with this URL and functions get_beacons and write_beacons will be executed. If the value is 'None' then the job is finished and the code exits.
- Finally, write_beacons is the function that actually puts beacon data in to the .csv file. It prints a message on screen indicating that it is working. Once written it will execute check_for_more just to see if there is any more data to collect.
This python could be adapted to gather information about other Meridian objects defined for the location such as Placemarks.