diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..31aa732 --- /dev/null +++ b/.clang-format @@ -0,0 +1,7 @@ +--- +BasedOnStyle: LLVM +TabWidth: 4 +IndentWidth: 4 +UseTab: ForIndentation +ColumnLimit: 0 +... diff --git a/package.json b/package.json index f7a8fe6..d13c5c7 100644 --- a/package.json +++ b/package.json @@ -9,14 +9,16 @@ "capabilities": [ "health" ], - "name": "one-more-episode", + "name": "1-more-episode", "pebble": { - "displayName": "One More Episode", - "enableMultiJS": true, + "displayName": "1 More Episode", + "enableMultiJS": false, "messageKeys": [ - "APP_READY", - "SLEEP_TIME", - "API_FULL_URL" + "JS_READY", + "SLEEP_TIMESTAMP", + "API_IS_PLEX", + "API_KEY", + "API_HOST" ], "projectType": "native", "resources": { @@ -24,12 +26,8 @@ }, "sdkVersion": "3", "targetPlatforms": [ - "basalt", "chalk", - "diorite", - "flint", - "emery", - "gabbro" + "emery" ], "uuid": "3097b48e-552c-4a78-92b2-566b9128640e", "watchapp": { diff --git a/src/c/main.c b/src/c/main.c index 7129bc1..15d181d 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -2,7 +2,10 @@ #include #include -// declare general statics +// 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; @@ -10,25 +13,28 @@ static TextLayer *s_last_episode_header_layer; static TextLayer *s_last_episode_layer; static TextLayer *s_pin_notice_layer; -static void set_sleep_start_time_text(time_t *start_time) { - 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); +// declare tracking statics +static time_t s_sleep_timestamp; + +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; + } } -static bool cb_update_sleep_time(HealthActivity activity, time_t start_time, - time_t end_time, void *context) { - if (activity == HealthActivitySleep) { - set_sleep_start_time_text(&start_time); - return false; - } - return true; +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 - (2 * (SECONDS_PER_DAY / 2)); // search up to the past 1.5 days + 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, HealthIterationDirectionPast, @@ -42,8 +48,10 @@ static void s_send_to_pkjs() { text_layer_set_text(s_last_episode_layer, "Phone comm failure 1"); return; } - dict_write_cstring(out, MESSAGE_KEY_SLEEP_TIME, text_layer_get_text(s_sleep_time_layer)); - dict_write_cstring(out, MESSAGE_KEY_API_FULL_URL, "https://test.com/api/endpoint?apikey=42069"); + 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"); @@ -79,7 +87,7 @@ static void main_window_load(Window *window) { 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) { // TODO change back to != 0 after debug + 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(); @@ -111,7 +119,7 @@ static void init() { window_set_window_handlers( s_main_window, (WindowHandlers){.load = main_window_load, .unload = main_window_unload}); - app_message_open(4096, 4096); + app_message_open(8192, 8192); window_stack_push(s_main_window, true); } diff --git a/src/pkjs/index.js b/src/pkjs/index.js index c5f180a..739b132 100644 --- a/src/pkjs/index.js +++ b/src/pkjs/index.js @@ -1,9 +1,37 @@ -Pebble.addEventListener("ready", function (e) { - Pebble.sendAppMessage({ APP_READY: true }); +Pebble.addEventListener("ready", function () { + Pebble.sendAppMessage({ JS_READY: 1 }); }); Pebble.addEventListener("appmessage", function (dict) { - if (dict.payload["API_FULL_URL"]) { - console.log("Time to harass " + dict.payload["API_FULL_URL"] + " to find what you watched at " + dict.payload["SLEEP_TIME"]); + if (dict.payload["API_HOST"]) { + console.log("WOAH BRO"); + if (dict.payload["API_IS_PLEX"] == true) { + callAPI(dict.payload["API_HOST"] + "/status/sessions/history/all", true, dict.payload["API_KEY"]); + } else { + callAPI(dict.payload["API_HOST"] + "/System/ActivityLog/Entries", false, dict.payload["API_KEY"]); + } } }); + +function callAPI(fullURL, isPlex, apiKey) { + let data = null; + + let xhr = new XMLHttpRequest(); + xhr.withCredentials = true; + + xhr.addEventListener("readystatechange", function () { + if (this.readyState === this.DONE) { + console.log(this.responseText); + } + }); + + xhr.open("GET", fullURL); + xhr.setRequestHeader("accept", "application/json"); + if (isPlex == true) { + xhr.setRequestHeader("X-Plex-Token", apiKey); + } else { + xhr.setRequestHeader("Authorization", 'MediaBrowser Token="' + apiKey + '"'); + } + + xhr.send(data); +}