Example #1
0
/* This task publishes messages to the Evrythng cloud when button is pressed. */
static void button_task(os_thread_arg_t arg)
{
    int led_state = 0;
    int button = (int)arg;
    int presses = 0;

    char json_str[256];
    char led_action_json_fmt[] = "{\"type\":\"%s\",\"customFields\":{\"status\":\"%d\"}}";
    char button_json_fmt[] = "[{\"value\": \"%d\"}]";

    char* button_property = button == button_1 ? "button_1" : "button_2";
    char* led_action = button == button_1 ? "_led1" : "_led2";
    os_semaphore_t sem = button == button_1 ? button1_sem : button2_sem;

    while(1)
    {
        if (os_semaphore_get(&sem, OS_WAIT_FOREVER) == WM_SUCCESS)
        {
            sprintf(json_str, button_json_fmt, ++presses);
            EvrythngPubThngProperty(evt_handle, thng_id, button_property, json_str);

            led_state = !led_state;
            sprintf(json_str, led_action_json_fmt, led_action, led_state);

            EvrythngPubThngAction(evt_handle, thng_id, led_action, json_str);
        }
    }
}
Example #2
0
void test_pubsub_thng_action(CuTest* tc)
{
    START_SINGLE_CONNECTION
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngAction(h1, THNG_1, ACTION_1, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngPubThngAction(h1, THNG_1, ACTION_1, ACTION_JSON));
    END_SINGLE_CONNECTION
}