Initial weather support
This commit is contained in:
+17
-3
@@ -41,6 +41,7 @@ static GBitmap *s_f_icon;
|
|||||||
|
|
||||||
// tracking statics
|
// tracking statics
|
||||||
static int s_batt_level;
|
static int s_batt_level;
|
||||||
|
static uint8_t s_temp_level;
|
||||||
|
|
||||||
// bar statics
|
// bar statics
|
||||||
static const uint8_t s_bar_height = 8;
|
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)
|
// TODO use temperature (for now it updates with battery usage)
|
||||||
static void temp_update_proc(Layer *layer, GContext *ctx) {
|
static void temp_update_proc(Layer *layer, GContext *ctx) {
|
||||||
graphics_context_set_fill_color(ctx, GColorWhite);
|
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;
|
uint16_t bar_bottom_pos = 195;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < bar_count; ++i) {
|
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) {
|
static void inbox_received_handler(DictionaryIterator *iter, void *context) {
|
||||||
Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_PKJS_READY);
|
Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_PKJS_READY);
|
||||||
if (ready_tuple) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEMPERATURE DATA
|
// TEMPERATURE DATA
|
||||||
Tuple *temp_bar_count = dict_find(iter, MESSAGE_KEY_PKJS_TEMP_BAR_COUNT);
|
Tuple *temp_bar_count = dict_find(iter, MESSAGE_KEY_PKJS_TEMP_BAR_COUNT);
|
||||||
if (temp_bar_count) {
|
if (temp_bar_count) {
|
||||||
// TODO
|
s_temp_level = temp_bar_count->value->uint8;
|
||||||
|
layer_mark_dirty(s_temp_layer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
var Clay = require('@rebble/clay');
|
var Clay = require('@rebble/clay');
|
||||||
var clayConfig = require('./config');
|
var clayConfig = require('./config');
|
||||||
var customClay = require('./customClay');
|
var customClay = require('./customClay');
|
||||||
|
require('./weather');
|
||||||
|
|
||||||
new Clay(clayConfig, customClay);
|
new Clay(clayConfig, customClay);
|
||||||
|
|
||||||
Pebble.addEventListener("ready", function () {
|
Pebble.addEventListener("ready", function () {
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user