diff --git a/src/c/main.c b/src/c/main.c index 4e8f59f..fd45b7d 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -41,6 +41,7 @@ static GBitmap *s_f_icon; // tracking statics static int s_batt_level; +static uint8_t s_temp_level; // bar statics static const uint8_t s_bar_height = 8; @@ -56,7 +57,7 @@ static void batt_callback(BatteryChargeState state) { // TODO use temperature (for now it updates with battery usage) static void temp_update_proc(Layer *layer, GContext *ctx) { graphics_context_set_fill_color(ctx, GColorWhite); - const uint8_t bar_count = s_batt_level / 5; + const uint8_t bar_count = s_temp_level; uint16_t bar_bottom_pos = 195; for (uint8_t i = 0; i < bar_count; ++i) { @@ -258,14 +259,27 @@ static void minute_handler(struct tm *tick_time, TimeUnits units_changed) { static void inbox_received_handler(DictionaryIterator *iter, void *context) { Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_PKJS_READY); if (ready_tuple) { - // TODO request temp bars from PKJS + // request temp bars from PKJS + DictionaryIterator *out; + AppMessageResult result = app_message_outbox_begin(&out); + if (result != APP_MSG_OK) { + s_temp_level = 0; // error; set to 0 bars to indicate TODO invert color and set to 20? + return; + } + dict_write_int8(out, MESSAGE_KEY_PKJS_TEMP_BAR_COUNT, 1); + result = app_message_outbox_send(); + if (result != APP_MSG_OK) { + s_temp_level = 0; // error; set to 0 bars to indicate TODO invert color and set to 20? + return; + } return; } // TEMPERATURE DATA Tuple *temp_bar_count = dict_find(iter, MESSAGE_KEY_PKJS_TEMP_BAR_COUNT); if (temp_bar_count) { - // TODO + s_temp_level = temp_bar_count->value->uint8; + layer_mark_dirty(s_temp_layer); return; } diff --git a/src/pkjs/index.js b/src/pkjs/index.js index a7b5438..4b35b85 100644 --- a/src/pkjs/index.js +++ b/src/pkjs/index.js @@ -1,6 +1,8 @@ var Clay = require('@rebble/clay'); var clayConfig = require('./config'); var customClay = require('./customClay'); +require('./weather'); + new Clay(clayConfig, customClay); Pebble.addEventListener("ready", function () { diff --git a/src/pkjs/weather.js b/src/pkjs/weather.js new file mode 100644 index 0000000..4c07037 --- /dev/null +++ b/src/pkjs/weather.js @@ -0,0 +1,42 @@ +var xhrRequest = function (url, type, callback) { + var xhr = new XMLHttpRequest(); + xhr.onload = function () { + callback(this.responseText); + }; + xhr.open(type, url); + xhr.send(); +}; + +function locationSuccess(pos) { + var url = 'https://api.open-meteo.com/v1/forecast?' + + 'latitude=' + pos.coords.latitude + + '&longitude=' + pos.coords.longitude + + '¤t=temperature_2m'; + + xhrRequest(url, 'GET', + function (responseText) { + var json = JSON.parse(responseText); + + // clamp into a range of 80 (technically 81)C (-32,48), -32C=-25.6F, 48C=118.4F + var temperature = Math.min(Math.max(json.current.temperature_2m, -32), 48); + // set set range minimum to 0 (add 32) and convert to bars (0-20) + temperature = Math.round((temperature + 32) / 4); + + Pebble.sendAppMessage({ PKJS_TEMP_BAR_COUNT: temperature }); + } + ); +} + +function getWeather() { + navigator.geolocation.getCurrentPosition( + locationSuccess, + null, + { timeout: 15000, maximumAge: 60000 } + ); +} + +Pebble.addEventListener("appmessage", function (dict) { + if (dict.payload["PKJS_TEMP_BAR_COUNT"]) { + getWeather(); + } +});