/* 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); } } }
void test_pubsub_thng_prop(CuTest* tc) { START_SINGLE_CONNECTION CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSubThngProperty(h1, THNG_1, PROPERTY_1, 0, test_sub_callback)); CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngPubThngProperty(h1, THNG_1, PROPERTY_1, PROPERTY_VALUE_JSON)); END_SINGLE_CONNECTION }
static void evrythng_task(void* pvParameters) { cmd_opts* opts = (cmd_opts*)pvParameters; EvrythngInitHandle(&opts->evt_handle); EvrythngSetLogCallback(opts->evt_handle, log_callback); EvrythngSetConnectionCallbacks(opts->evt_handle, conlost_callback, conrestored_callback); EvrythngSetUrl(opts->evt_handle, opts->url); EvrythngSetKey(opts->evt_handle, opts->key); EvrythngSetThreadPriority(opts->evt_handle, 5); log("Connecting to %s", opts->url); while(EvrythngConnect(opts->evt_handle) != EVRYTHNG_SUCCESS) { log("Retrying"); platform_sleep(2000); } log("Evrythng client Connected"); if (opts->sub) { log("Subscribing to property %s", opts->prop); EvrythngSubThngProperty(opts->evt_handle, opts->thng, opts->prop, 1, print_property_callback); while (!stop) { platform_sleep(2000); } } else { while (!stop) { int value = rand() % 100; char msg[128]; sprintf(msg, "[{\"value\": %d}]", value); log("Publishing value %d to property %s", value, opts->prop); EvrythngPubThngProperty(opts->evt_handle, opts->thng, opts->prop, msg); platform_sleep(1000); } } EvrythngDisconnect(opts->evt_handle); EvrythngDestroyHandle(opts->evt_handle); vTaskEndScheduler(); }