Пример #1
0
void test_subunsub_thng(CuTest* tc)
{
    PRINT_START_MEM_STATS 
    evrythng_handle_t h1;
    common_tcp_init_handle(&h1);

    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngConnect(h1));

    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngAction(h1, THNG_1, ACTION_1, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngAction(h1, THNG_1, ACTION_2, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngUnsubThngAction(h1, THNG_1, ACTION_2));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngProperty(h1, THNG_1, PROPERTY_1, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngUnsubThngProperty(h1, THNG_1, PROPERTY_1));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngProperty(h1, THNG_1, PROPERTY_1, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngProperty(h1, THNG_1, PROPERTY_2, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngUnsubThngProperty(h1, THNG_1, PROPERTY_1));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngProperties(h1, THNG_1, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngUnsubThngProperties(h1, THNG_1));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngActions(h1, THNG_1, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngUnsubThngActions(h1, THNG_1));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngLocation(h1, THNG_1, 0, test_sub_callback));
    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngUnsubThngLocation(h1, THNG_1));

    CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngDisconnect(h1));
    EvrythngDestroyHandle(h1);
    PRINT_END_MEM_STATS
}
Пример #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
}
Пример #3
0
/* This task configures Evrythng client and connects to the Evrythng cloud  */
static void evrythng_task()
{
    psm_handle_t handle;
    int rc;

    if ((rc = psm_open(&handle, "evrythng")) != 0)
    {
        wmprintf("psm_open failed with: %d (Is the module name registered?)\n\r", rc);
        goto exit;
    }

    char api_key[128];
    if (psm_get(&handle, "api_key", api_key, 128) == 0)
    {
        wmprintf("api_key: %s\n\r", api_key);
    }
    else
    {
        wmprintf("api_key doesn't exist\n\r");
        goto exit;
    }

    char thng_id_buf[64];
    if (psm_get(&handle, "thng_id", thng_id_buf, 64) == 0)
    {
        if (thng_id != NULL)
        {
            os_mem_free(thng_id);
        }
        thng_id = (char*)os_mem_alloc((strlen(thng_id_buf)+1)*sizeof(char));
        strcpy(thng_id, thng_id_buf);
        wmprintf("thng_id: %s\n\r", thng_id);
    }
    else
    {
        wmprintf("thng_id doesn't exist\n\r");
        goto exit;
    }

    char url_buf[64];
    if (psm_get(&handle, "url", url_buf, 64) == 0)
    {
        wmprintf("Evrythng URL: %s\n\r", url_buf);
    }
    else
    {
        wmprintf("Evrythng URL doesn't exist\n\r");
        goto exit;
    }
    psm_close(&handle);

    EvrythngInitHandle(&evt_handle);
    EvrythngSetUrl(evt_handle, url_buf);
    EvrythngSetKey(evt_handle, api_key);
    EvrythngSetLogCallback(evt_handle, log_callback);
    EvrythngSetConnectionCallbacks(evt_handle, on_connection_lost, on_connection_restored);

    while (EvrythngConnect(evt_handle) != EVRYTHNG_SUCCESS)
    {
        wmprintf("Retry\n\r");
        os_thread_sleep(os_msec_to_ticks(5000));
    }
    wmprintf("Connected\n\r");

    os_semaphore_create_counting(&button1_sem, "button1_sem", 1000, 0);
    os_semaphore_create_counting(&button2_sem, "button1_sem", 1000, 0);

    EvrythngSubThngAction(evt_handle, thng_id, "_led1", 0, action_led_callback);
    EvrythngSubThngAction(evt_handle, thng_id, "_led2", 0, action_led_callback);

    os_thread_create(&button1_thread, "button1_task", button_task, (void*)button_1, &button_stack, OS_PRIO_3);
    os_thread_create(&button2_thread, "button2_task", button_task, (void*)button_2, &button_stack, OS_PRIO_3);

exit:
    os_thread_self_complete(0);
}