149 lines
5.7 KiB
C
149 lines
5.7 KiB
C
#include <pebble.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
// declare pkjs statics
|
|
static bool s_js_ready;
|
|
|
|
// declare window statics
|
|
static Window *s_main_window;
|
|
static TextLayer *s_sleep_time_header_layer;
|
|
static TextLayer *s_sleep_time_layer;
|
|
static TextLayer *s_last_episode_header_layer;
|
|
static TextLayer *s_last_episode_layer;
|
|
static TextLayer *s_pin_notice_layer;
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
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, calling PKJS...");
|
|
send_to_pkjs();
|
|
}
|
|
|
|
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) {
|
|
s_sleep_timestamp = start_time;
|
|
return false;
|
|
}
|
|
|
|
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 main_window_load(Window *window) {
|
|
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));
|
|
text_layer_set_text(s_sleep_time_header_layer, "Last Sleep");
|
|
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));
|
|
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));
|
|
text_layer_set_text(s_last_episode_header_layer, "Last Episode");
|
|
s_last_episode_layer = text_layer_create(GRect(10, 96, PBL_DISPLAY_WIDTH, 30));
|
|
text_layer_set_background_color(s_last_episode_layer, GColorClear);
|
|
text_layer_set_font(s_last_episode_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24));
|
|
text_layer_set_text(s_last_episode_layer, "N/A"); // default - will be updated before display
|
|
s_pin_notice_layer = text_layer_create(GRect(0, PBL_DISPLAY_HEIGHT - 50, PBL_DISPLAY_WIDTH, 50));
|
|
text_layer_set_background_color(s_pin_notice_layer, GColorClear);
|
|
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);
|
|
|
|
// 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));
|
|
layer_add_child(window_layer, text_layer_get_layer(s_sleep_time_layer));
|
|
layer_add_child(window_layer, text_layer_get_layer(s_last_episode_header_layer));
|
|
layer_add_child(window_layer, text_layer_get_layer(s_last_episode_layer));
|
|
layer_add_child(window_layer, text_layer_get_layer(s_pin_notice_layer));
|
|
}
|
|
|
|
static void main_window_unload(Window *window) {
|
|
text_layer_destroy(s_sleep_time_header_layer);
|
|
text_layer_destroy(s_sleep_time_layer);
|
|
text_layer_destroy(s_last_episode_header_layer);
|
|
text_layer_destroy(s_last_episode_layer);
|
|
text_layer_destroy(s_pin_notice_layer);
|
|
}
|
|
|
|
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});
|
|
|
|
// 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);
|
|
}
|
|
|
|
static void deinit() { window_destroy(s_main_window); }
|
|
|
|
int main(void) {
|
|
init();
|
|
app_event_loop();
|
|
deinit();
|
|
}
|