Cloud API 🪁 - GeorgiaTech-DDI/makerspace_iot GitHub Wiki

API Gateway Access

Summary: In the Spring 2024 semester, we have created an API Gateway hosted on AWS that has the capability to store the information that is taken from the IoT box. The information gets sent to a DyanmoDB table from IoT Core, which is then queried when the API gateway is called.

DynamoDB Table

The table created is fairly simple. We simply created a DynamoDB table using AWS and set the partition key to 'assetID' and the sort key to 'time_stamp' (Note: the word 'timestamp' is a keyword, and you cannot be used to query a database when it is an attribute). Using time_stamp as the sort key allows for easy queries on large amounts of data which helps when large amounts of data are in the database. We then stored all of the data into the table using a python lambda function found here. We included all of the acceleration data, as well as another attribute that states how long the machine has been running (denoted as machine_time).

API Gateway

The API gateway is also a simple application. I followed the following tutorial to produce the main API gateway function (shoutout to Chido for the link) the tutorial can be found here. A main key point is that you will have to create layers for the lambda function to work. The link to the files I used can be found here. For the API gateway, I created an ANY method for the basic API call, and a /{proxy+} method with an ANY method attached as well. The link to the lambda function used is here.

Note: a sample call to the API may look like the following: https://tfoqg3fqx0.execute-api.us-east-1.amazonaws.com/dev/getByAssetID?limit=10&asset=IoT-1

Note: ignore if the link above does not work, as the link changes each time you deploy the api, simply review the format to see what a call typically looks like.

The main API functions are discussed below. The format for all of them are similar, and as the link changes or function names change ensure that the format remains the same. The format should be:

[API LINK].com/dev/[FUNCTION_NAME]?[parameter]=[parameter_value]&[parameter]=[parameter_value]

NOTE: AWS API Gateway has a hard 29 second timeout for API calls. What this means is that the function has 29 seconds to return something to the user (has 29 seconds to return something on the browser), and if nothing is returned, it is considered a timeout and the function stops. This is a large issue for our function, as each day contains a large amount of data points (86400) that represent each second of each day. As a result, our functions needed to altered to fit these demands. This is why there is no function to return the data from a single date, and if we want to get the data from a date we need to split it in half and get half of the date's data.

API Functions

/getAllDataByOneDateHalf

Requires:

  • asset
  • date
  • part

asset: The asset you are wanting to search for.

date: The date you want data from

part: The part of the date you want info from. Value should be 1 or 2.

Returns data from half of the date of a certain Asset ID. Part 1 returns all data from 12AM to 12PM of a date, and part 2 will return 12PM to 12AM (of the next day). Meaning, in order to get all of the data for an entire day, you would need to run the function with part 1 and part 2.

Example call: https://tfoqg3fqx0.execute-api.us-east-1.amazonaws.com/dev/getAllDataByOneDateHalf?asset=IoT-1&date=2024-11-15&part=1

/getAllDataByDate

Requires:

  • asset
  • date_min
  • date_max
  • last_evaluated_key (optional)

asset: the asset name you are looking for in the database

date_min: lower date on range

date_max: upper date on range (MUST BE GREATER THAN date_min)

last_evaluated_key: The last value that the query evaluated. Can be inserted as parameters to continue onto larger query. Parameter is optional.

This function returns all data from the DB for a certain date. Returns items, as well as a last_evaluated_key value that can be used as a query parameter to 'continue' the query through the date range. This allows for the function to continually be called so the user can get data across a range of dates.

Example call: https://tfoqg3fqx0.execute-api.us-east-1.amazonaws.com/dev/getAllDataByDate?asset=IoT-1&date_min=2024-11-14&date_max=2024-11-15

Example call: https://tfoqg3fqx0.execute-api.us-east-1.amazonaws.com/dev/getAllDataByDate?asset=IoT-1&date_min=2024-11-14&date_max=2024-11-15&last_evaluated_key=%7B%22time_stamp%22%3A%20%222024-11-14%2014%3A33%3A35%22%2C%20%22assetID%22%3A%20%22IoT-1%22%7D

The above call is a little weird because the last evaluated key needs to be JSON encoded to be used in an API call and to be used properly. Use the above formatting to call the function using the last evaluated key.

Fall 2024 Note: While I was trying to get the API up and running again, I was experiencing a lot of timeout issues within the API. When I tried debugging the lambda function and placed print statements in each function, the API suddenly worked. I'm not too sure why this worked, but it solved the timeout issues, so it might be the solution.