<<< Previous: Creating User Interfaces Part 1 - The web content files
Part 1 of the FreeRTOS+Nabto user interface guide described the XML, Javascript and web content files that together make up a web based FreeRTOS+Nabto user interface, and describe the queries that are understood by the remote FreeRTOS+Nabto device. This page provides an XML and Javascript implementation for two of the queries used in the live demo.
| Query ID #1: Get RTOS tick count | |
| Request Parameters: | None |
| Response Parameters: | The RTOS tick count value, transmitted as a 32-bit unsigned integer |
<!-- The bullet points directly above describe this XML code -->
<query name="rtos_tick.json" description="Get RTOS Tick Count" id="1">
<request>
</request>
<response format="json">
<parameter name="tick_value" type="uint32"/>
</response>
</query>
Listing 1: XML code that describes query #1
<h3>
1: Live data
</h3>
This incrementing number <b><span id="tick_count_span"> 0 </span></b> is the FreeRTOS
tick count. It equals the number of RTOS ticks that have passed
Listing 2: An extract of the HTML page showing the <span> tag
/*
* Parameters:
* request The query name, and the value of any request parameters. Request
* parameters are passed using standard URL notation (in name=value
* pairs following a ?).
* action A function to execute when a response to the query has been received.
*/
function queryDevice( request, action )
Listing 3: The Javascript queryDevice() prototype
It can be seen from Listing 1, listing 2 and listing 3 that the tick
count value can be updated by calling queryDevice() with:
Listing 4 is a worked example.
/* Writes the value received in the tick_value response parameter into the span
with id tick_count_span. */
function handleTickReply( response )
{
if( response[ "tick_value" ] != null )
{
$( "#tick_count_span" ).text( response[ "tick_value" ] );
}
}
/* The RTOS tick count is updated when queryTick() is called. */
function queryTick()
{
/* Send the rtos_tick.json query, and call handleTickReply() when the response
is received. */
queryDevice( "rtos_tick.json?", handleTickReply );
}
Listing 4: Javascript to update the RTOS tick count in the user interface
| Query ID #2: Get network statistic | |
| Request Parameters: | The ID of the statistic being queried, received as a 32-bit unsigned integer |
| Response Parameters: | The value of the network statistic that corresponds to the ID received as the request parameter, transmitted as a 32-bit unsigned integer |
From listing 5 it can be seen that:
<!-- See the bullet point notes above. -->
<query name="network_stats.json" description="Get network statistic" id="2">
<request>
<parameter name="stat_id" type="uint32"/>
</request>
<response format="json">
<parameter name="stat_value" type="uint32"/>
</response>
</query>
Listing 5: XML code that describes query #2
<input type="button" id="netstat_update" data-icon="refresh" value="Query Value"/>
Listing 6: An extract of the HTML page showing the button definition
It can be seen from Listing 3, listing 5 and listing 6 that the network
statistic specified by the selected radio button can be updated by calling
queryDevice() with:
Listing 7 is a worked example.
/* Writes the value received in the stat_value response parameter onto the
input button that has id netstat_update. */
function handleNetStatReply( response )
{
if( response[ "stat_value" ] != null )
{
/* Write "Stat value is: nnn" to the button. */
$("#netstat_update").val( "Stat value is: " + response[ "stat_value" ] );
$("#netstat_update").button( "refresh" );
}
}
/* The function to call to send the Get Network Statistic query, and handle the
query's response. */
function queryNetstat(input)
{
var stat_id;
/* Get the ID of the selected radio button. */
stat_id = getSelectedRadioButton();
if( stat_id >= 0 )
{
/* Add the id of the selected radio button to the query, so the query
is passed in the form "network_stats.json?stat_id=n", where n is the
value of stat_id. */
queryDevice( "network_stats.json?stat_id=" + stat_id, handleNetStatReply );
}
}
Listing 7: Javascript to update the text on the input button with the value of the queried network statistic