dms/telemetry/main.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include "gos.h"
#include "example_app_util.h"
// Update this value anytime settings.ini changes
// This will cause Gecko OS to reload the settings
#define SETTINGS_MAGIC_NUMBER 0x02
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
GOS_LOG("\r\nDMS Telemetry example starting ...");
// Load the application settings if necessary
// NOTE: If the 'magic number' is different, then
// 1. The settings.ini is load
// 2. Settings are saved to NVM
// 3. The device is rebooted
//
// Upon reboot the device will automatically bringup the WLAN inteface and open the DMS websocket
if(GOS_FAILED(result, gos_load_app_settings_once("settings.ini", SETTINGS_MAGIC_NUMBER)))
{
GOS_LOG("Failed to load settings, err:%d", result);
}
// Initialize the button GPIOs
gos_gpio_init(PLATFORM_BUTTON1, GOS_GPIO_INPUT_HIGH_IMPEDANCE, false);
gos_gpio_init(PLATFORM_BUTTON2, GOS_GPIO_INPUT_HIGH_IMPEDANCE, false);
// Set the connection handler
gos_dms_set_message_stream_state_handler(message_stream_connection_handler);
// Set the telemetry callback
gos_dms_set_telemetry_callback(telemetry_callback);
// Bring up the network if it isn't already
// This also pretty prints an error if no network credentials are set
}
/*************************************************************************************************
* Here we add the following additional data:
*
* buttons: {
* btn1: <true/false>,
* btn2: <true/false>
* },
* utc: "<UTC time string>"
*/
static gos_result_t telemetry_callback(gos_msgpack_context_t *msgpack, uint32_t *pair_count_ptr)
{
char temp_str[128];
// NOTE: The msgpack already has the root dictionary marker written
// We just need to add to the dictionary
// Add: buttons [
gos_msgpack_write_dict_array(msgpack, "buttons", 2);
// Add: { btn1: <true/false> }
gos_msgpack_write_dict_bool(msgpack, "btn1", gos_gpio_get(PLATFORM_BUTTON1) == PLATFORM_BUTTON_ACTIVE_STATE);
// Add: { btn2: <true/false> }
gos_msgpack_write_dict_bool(msgpack, "btn2", gos_gpio_get(PLATFORM_BUTTON2) == PLATFORM_BUTTON_ACTIVE_STATE);
// Add: utc : "<UTC string>"
gos_msgpack_write_dict_str(msgpack, "utc", (const char*)temp_str);
*pair_count_ptr = 2; // we added 2 entries to the root dictionary
GOS_LOG("Posting telemetry to DMS for device: %s", gos_system_get_uuid_str(temp_str));
return GOS_SUCCESS;
}
/*************************************************************************************************/
static void message_stream_connection_handler(bool is_up)
{
if (is_up == true)
{
char buffer[32];
GOS_LOG("DMS connection up");
GOS_LOG("Posting telemetry every %s seconds", gos_settings_get_print_str("dms.telemetry.interval", buffer, sizeof(buffer)));
GOS_LOG("NOTE: You can also issue the command:");
GOS_LOG("> dms_telemetry");
GOS_LOG("To immediately post telemetry to the DMS");
}
else
{
GOS_LOG("DMS connection down");
}
}