diff --git a/src/c/main.c b/src/c/main.c index 15d181d..0c46ab8 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -4,6 +4,7 @@ // declare pkjs statics static bool s_js_ready; +static bool s_js_pending; // declare window statics static Window *s_main_window; @@ -13,54 +14,62 @@ static TextLayer *s_last_episode_header_layer; static TextLayer *s_last_episode_layer; static TextLayer *s_pin_notice_layer; -// declare tracking statics +// declare time tracking statics static time_t s_sleep_timestamp; +static void send_to_pkjs() { + DictionaryIterator *out; + AppMessageResult result = app_message_outbox_begin(&out); + if (result != APP_MSG_OK) { + text_layer_set_text(s_last_episode_layer, "outbox_begin failure"); + return; + } + dict_write_uint32(out, MESSAGE_KEY_SLEEP_TIMESTAMP, s_sleep_timestamp); + dict_write_uint8(out, MESSAGE_KEY_API_IS_PLEX, 0); + dict_write_cstring(out, MESSAGE_KEY_API_KEY, ""); + dict_write_cstring(out, MESSAGE_KEY_API_HOST, ""); + + result = app_message_outbox_send(); + if (result != APP_MSG_OK) { + text_layer_set_text(s_last_episode_layer, "outbox_send failure "); + return; + } + + s_js_pending = false; +} + static void inbox_received_handler(DictionaryIterator *iter, void *context) { Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_JS_READY); if (ready_tuple) { s_js_ready = true; } + APP_LOG(APP_LOG_LEVEL_INFO, "Received JS_READY"); +} + +static void inbox_dropped_handler(AppMessageResult reason, void *context) { + APP_LOG(APP_LOG_LEVEL_DEBUG, "Inbox dropped: %d", reason); +} + +static void outbox_failed_handler(DictionaryIterator *iter, AppMessageResult reason, void *context) { + APP_LOG(APP_LOG_LEVEL_DEBUG, "Outbox send failed: %d", reason); +} + +static void outbox_sent_handler(DictionaryIterator *iter, void *context) { + APP_LOG(APP_LOG_LEVEL_DEBUG, "Outbox send success"); } static bool cb_update_sleep_time(HealthActivity activity, time_t start_time, time_t end_time, void *context) { - static char buffer[8]; - struct tm *timeinfo = localtime(&start_time); - strftime(buffer, sizeof(buffer), "%I:%M", timeinfo); - text_layer_set_text(s_sleep_time_layer, buffer); s_sleep_timestamp = start_time; return false; } -static void update_sleep_time() { - time_t end_time = time(NULL); // now - time_t start_time = end_time - (SECONDS_PER_DAY * 1.5); // search up to the past 1.5 days - - health_service_activities_iterate(HealthActivitySleep, start_time, end_time, +static void update_sleep_time(time_t start, time_t end) { + health_service_activities_iterate(HealthActivitySleep, start, end, HealthIterationDirectionPast, cb_update_sleep_time, NULL); } -static void s_send_to_pkjs() { - DictionaryIterator *out; - AppMessageResult result = app_message_outbox_begin(&out); - if (result != APP_MSG_OK) { - text_layer_set_text(s_last_episode_layer, "Phone comm failure 1"); - return; - } - dict_write_cstring(out, MESSAGE_KEY_SLEEP_TIMESTAMP, text_layer_get_text(s_sleep_time_layer)); - dict_write_cstring(out, MESSAGE_KEY_API_IS_PLEX, ""); - dict_write_cstring(out, MESSAGE_KEY_API_KEY, ""); - dict_write_cstring(out, MESSAGE_KEY_API_HOST, ""); - result = app_message_outbox_send(); - if (result != APP_MSG_OK) { - text_layer_set_text(s_last_episode_layer, "Phone comm failure 2"); - return; - } -} - static void main_window_load(Window *window) { - // ensure sleep data is available s_sleep_time_header_layer = text_layer_create(GRect(6, 0, PBL_DISPLAY_WIDTH, 30)); text_layer_set_background_color(s_sleep_time_header_layer, GColorClear); text_layer_set_font(s_sleep_time_header_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD)); @@ -68,7 +77,14 @@ static void main_window_load(Window *window) { s_sleep_time_layer = text_layer_create(GRect(10, 24, PBL_DISPLAY_WIDTH, 30)); text_layer_set_background_color(s_sleep_time_layer, GColorClear); text_layer_set_font(s_sleep_time_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24)); - text_layer_set_text(s_sleep_time_layer, "N/A (rough night?)"); // default - will be updated before display + if (s_sleep_timestamp != 0) { + static char buffer[8]; + struct tm *timeinfo = localtime(&s_sleep_timestamp); + strftime(buffer, sizeof(buffer), clock_is_24h_style() ? "%H:%M" : "%I:%M", timeinfo); + text_layer_set_text(s_sleep_time_layer, buffer); + } else { + text_layer_set_text(s_sleep_time_layer, "N/A (rough night?)"); + } s_last_episode_header_layer = text_layer_create(GRect(6, 72, PBL_DISPLAY_WIDTH, 30)); text_layer_set_background_color(s_last_episode_header_layer, GColorClear); text_layer_set_font(s_last_episode_header_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD)); @@ -82,21 +98,6 @@ static void main_window_load(Window *window) { text_layer_set_font(s_pin_notice_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14)); text_layer_set_text_alignment(s_pin_notice_layer, GTextAlignmentCenter); - // populate data text layers - time_t now = time(NULL); - if (health_service_any_activity_accessible(HealthActivitySleep, now - (2 * SECONDS_PER_DAY), now) == HealthServiceAccessibilityMaskAvailable) { - update_sleep_time(); - } - if (strcmp(text_layer_get_text(s_sleep_time_layer), "N/A (rough night?)") != 0) { - // get last episode using PKJS - psleep(100); // TODO Implement proper wait for PKJS to respond ready - s_send_to_pkjs(); - - if (strcmp(text_layer_get_text(s_last_episode_layer), "N/A") != 0) { - text_layer_set_text(s_pin_notice_layer, "press select\nto pin to timeline and exit"); - } - } - // add layers as children to windows Layer *window_layer = window_get_root_layer(window); layer_add_child(window_layer, text_layer_get_layer(s_sleep_time_header_layer)); @@ -115,11 +116,28 @@ static void main_window_unload(Window *window) { } static void init() { + // initialize window s_main_window = window_create(); window_set_window_handlers( s_main_window, (WindowHandlers){.load = main_window_load, .unload = main_window_unload}); - app_message_open(8192, 8192); + + // initialize pkjs + app_message_register_inbox_received(inbox_received_handler); + app_message_register_inbox_dropped(inbox_dropped_handler); + app_message_register_outbox_failed(outbox_failed_handler); + app_message_register_outbox_sent(outbox_sent_handler); + app_message_open(1024, 1024); + + // get sleep time (UNIX timestamp) + time_t end = time(NULL); + time_t start = end - (SECONDS_PER_DAY * 1.5); + if (health_service_any_activity_accessible(HealthActivitySleep, start, end) == HealthServiceAccessibilityMaskAvailable) { + update_sleep_time(start, end); + } else { + APP_LOG(APP_LOG_LEVEL_DEBUG, "Sleep activity inaccessible", NULL); + } + window_stack_push(s_main_window, true); }