In our first article about the Peakboard Hub API, we discussed how to get an API key, establish a connection to the Hub, and get information about the boxes that are connected to the hub. In today’s article, we will discuss how to call shared, public functions on a box from the outside.
Let’s assume we’re running a third party application, and we need to notify people inside our factory about something. In this case, we can call the Peakboard Hub API to have the Hub run a function on a Peakboard Box.
In this article, we will run an alarm on the box and send a parameter that specifies how many seconds the alarm should run for. We will also use a second function to check if an alarm is currently displayed. That way, we can use it to confirm that the alarm is working properly.
Build the Peakboard application
The Peakboard application is simple. In the center of the screen, we have a text box:
- If there’s an alarm, it shows the alarm message in a red color.
- If there’s no alarm, it shows “N/A.”
We use the integer variable SecondsLeft
to track the number of seconds the alarm will continue running for.
Let’s take a look at the first function, SubmitAlarm
. It takes two parameters:
AlarmTime
, the number of seconds to display the alarm message.AlarmMessage
, a string that contains the alarm message itself.
When SubmitAlarm
is called, the text box shows the alarm message, and the number of seconds is stored in the SecondsLeft
variable.
The second function, IsAlarmActive
, doesn’t take any parameters. It returns a boolean that specifies if the number of seconds remaining on the alarm is greater than 0 (alarm is active) or not (alarm is inactive).
The function in the timer event is used to count the time that’s left, and to set the text back to “N/A” once the time is up.
Call the API
Let’s assume the application is actively running on a box and that the box is connected to the Hub. This screenshot shows the box in the Hub portal:
The first thing we need to do is connect to the Hub and exchange the API key with an access token that we will need for the real calls later.
As an additional exercise, we will get the functions that are available on our box with the /public-api/v1/box/functions
endpoint. The response body contains comprehensive information about the functions and their parameters. In our case, we only need the names of the available functions.
The output of the code looks like this:
Here’s the entire JSON response with all the metadata. You can see the two functions with their corresponding in and out parameters:
Call a function
We call the shared function with the /public-api/v1/box/function
endpoint. You can see how a dedicated box is addressed by its name (boxId
). We also submit the name of the function to be called along with the parameters.
The second function we need to call is IsAlarmActive
. It has a return parameter that can be read in the response body. It’s not embedded in any kind of JSON, but rather printed directly in the body.
Here’s the result in the console output:
And here’s what the actual alarm looks like on the box, as seen from the Peakboard Hub portal: