Skip to content

Commit

Permalink
Save and show weather and location onload
Browse files Browse the repository at this point in the history
  • Loading branch information
initialneil committed Oct 23, 2015
1 parent 61600c2 commit ad63b89
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 61 deletions.
15 changes: 9 additions & 6 deletions appinfo.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"appKeys": {
"SHOW_LOCATION": 4,
"SHOW_WEATHER": 3,
"WEATHER_CITY_KEY": 2,
"WEATHER_ICON_KEY": 0,
"WEATHER_TEMPERATURE_KEY": 1
"DEFAULT_LOCATION": 6,
"LOCATION_OPT": 7,
"PEBBLEKIT_JS_READY": 0,
"SHOW_LOCATION": 5,
"SHOW_WEATHER": 4,
"WEATHER_CITY_KEY": 3,
"WEATHER_ICON_KEY": 1,
"WEATHER_TEMPERATURE_KEY": 2
},
"capabilities": [
"location",
Expand Down Expand Up @@ -49,7 +52,7 @@
"chalk"
],
"uuid": "4464b4f0-d236-4d61-a0c6-ef0ff92aeb70",
"versionLabel": "1.3",
"versionLabel": "1.34",
"watchapp": {
"watchface": true
}
Expand Down
25 changes: 23 additions & 2 deletions src/app_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Store incoming information
static char temperature_buffer[8];
static char conditions_buffer[32];
static char city_buffer[32];
static char city_buffer[32], default_location_buffer[32], location_opt_buffer[32];

static bool s_show_weather = true, s_show_location = true;

Expand All @@ -19,20 +19,29 @@ void inbox_received_callback(DictionaryIterator *iterator, void *context) {
while(t != NULL) {
// Which key was received?
switch(t->key) {
// PebbleKit JS Ready
case PEBBLEKIT_JS_READY:
set_pebblekit_js_ready((bool)t->value->int8);
update_weather_with_app_msg();
break;

// Weather Info
case WEATHER_ICON_KEY:
snprintf(conditions_buffer, sizeof(conditions_buffer), "%s", t->value->cstring);
APP_LOG(APP_LOG_LEVEL_INFO, "weather condition = %s", conditions_buffer);
set_conditions_buffer(conditions_buffer);
break;

case WEATHER_TEMPERATURE_KEY:
snprintf(temperature_buffer, sizeof(temperature_buffer), "%d.C", (int)t->value->int32);
APP_LOG(APP_LOG_LEVEL_INFO, "temperature = %s", temperature_buffer);
set_temperature_buffer(temperature_buffer);
break;

case WEATHER_CITY_KEY:
snprintf(city_buffer, sizeof(city_buffer), "%s", t->value->cstring);
APP_LOG(APP_LOG_LEVEL_INFO, "city = %s", city_buffer);
set_city_buffer(city_buffer);
break;

// Display Weather & Location
Expand All @@ -45,6 +54,18 @@ void inbox_received_callback(DictionaryIterator *iterator, void *context) {
s_show_location = t->value->int8;
APP_LOG(APP_LOG_LEVEL_INFO, "show location = %d", s_show_location);
break;

case DEFAULT_LOCATION:
snprintf(default_location_buffer, sizeof(city_buffer), "%s", t->value->cstring);
APP_LOG(APP_LOG_LEVEL_INFO, "default location = %s", default_location_buffer);
set_default_location(default_location_buffer);
break;

case LOCATION_OPT:
snprintf(location_opt_buffer, sizeof(city_buffer), "%s", t->value->cstring);
APP_LOG(APP_LOG_LEVEL_INFO, "location opt = %s", location_opt_buffer);
set_location_opt(location_opt_buffer);
break;

default:
APP_LOG(APP_LOG_LEVEL_ERROR, "Key %d not recognized!", (int)t->key);
Expand All @@ -57,7 +78,7 @@ void inbox_received_callback(DictionaryIterator *iterator, void *context) {

// Update weather layer
config_weather_layer(s_show_weather, s_show_location);
update_weather_layer(conditions_buffer, temperature_buffer, city_buffer);
refresh_weather_display();
}


88 changes: 78 additions & 10 deletions src/app_msg.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
// Got from OpenWeatherMap's API example
var myAPIKey = 'bd82977b86bf27fb59a04b61b657fb6f';
var show_weather, show_location;
var default_location = "", location_opt = "";

// Listen for when the watchface is opened
Pebble.addEventListener('ready',
function(e) {
console.log('PebbleKit JS ready!');

// Get the initial weather
getWeather();
var dict = {
'PEBBLEKIT_JS_READY': true,
};

// Send to watchapp
Pebble.sendAppMessage(dict, function() {
console.log('Send successful: ' + JSON.stringify(dict));
}, function() {
console.log('Send failed!');
});
}
);

// Listen for when an AppMessage is received
Pebble.addEventListener('appmessage',
function(e) {
console.log('AppMessage received!');
console.log('Received message: ' + JSON.stringify(e.payload));

default_location = e.payload.DEFAULT_LOCATION;
location_opt = e.payload.LOCATION_OPT;
console.log("default_location = " + default_location);
console.log("location_opt = " + location_opt);

// Get the weather if required
getWeather();
refresh_weather();
}
);

// Configure
// Show Configure
Pebble.addEventListener('showConfiguration', function() {
var url = 'http://initialneil.github.io/PebbleFace-IvyTick';
console.log('Showing configuration page: ' + url);

Pebble.openURL(url);
});

// Configure Received
Pebble.addEventListener('webviewclosed', function(e) {
var configData = JSON.parse(decodeURIComponent(e.response));
console.log('Configuration page returned: ' + JSON.stringify(configData));

var show_weather = configData.show_weather;
var show_location = configData.show_location;
show_weather = configData.show_weather;
show_location = configData.show_location;
default_location = configData.default_location;
location_opt = configData.location_opt;
console.log("show_weather = " + show_weather);
console.log("show_location = " + show_location);
console.log("default_location = " + default_location);
console.log("location_opt = " + location_opt);

var dict = {
'SHOW_WEATHER': show_weather,
'SHOW_LOCATION': show_location,
'DEFAULT_LOCATION': default_location,
'LOCATION_OPT': location_opt,
};

// Send to watchapp
Expand All @@ -49,6 +70,10 @@ Pebble.addEventListener('webviewclosed', function(e) {
}, function() {
console.log('Send failed!');
});

// Refresh weather
if (show_weather || show_location)
refresh_weather();
});

// Check weather id
Expand All @@ -74,7 +99,7 @@ function iconFromWeatherId(weatherId) {
}
}

// Fetch weather from OpenWeatherMap
// Fetch weather from OpenWeatherMap with latitude and longitude
function fetchWeather(latitude, longitude) {
var req = new XMLHttpRequest();
req.open('GET', 'http://api.openweathermap.org/data/2.5/weather?' +
Expand Down Expand Up @@ -106,6 +131,38 @@ function fetchWeather(latitude, longitude) {
req.send(null);
}

// Fetch weather from OpenWeatherMap with city
function fetchWeather_city(city) {
var req = new XMLHttpRequest();
req.open('GET', 'http://api.openweathermap.org/data/2.5/weather?' +
'q=' + city + '&cnt=1&appid=' + myAPIKey, true);
console.log('http://api.openweathermap.org/data/2.5/weather?' +
'q=' + city + '&cnt=1&appid=' + myAPIKey);
req.onload = function () {
if (req.readyState === 4) {
if (req.status === 200) {
console.log(req.responseText);
var response = JSON.parse(req.responseText);
var temperature = Math.round(response.main.temp - 273.15);
var icon = iconFromWeatherId(response.weather[0].id);
//var city = response.name;
console.log(temperature);
console.log(icon);
console.log(city);
Pebble.sendAppMessage({
'WEATHER_ICON_KEY': icon,
//'WEATHER_TEMPERATURE_KEY': temperature + '\xB0C',
'WEATHER_TEMPERATURE_KEY': temperature,
'WEATHER_CITY_KEY': city
});
} else {
console.log('Error');
}
}
};
req.send(null);
}

function locationSuccess(pos) {
var coordinates = pos.coords;
console.log("latitude = " + coordinates.latitude, "longitude = " + coordinates.longitude);
Expand All @@ -114,12 +171,23 @@ function locationSuccess(pos) {

function locationError(err) {
console.log('Error requesting location!');

console.log('Getting default city: ' + default_location);
fetchWeather_city(default_location);
}

function getWeather() {
function getWeather_auto() {
navigator.geolocation.getCurrentPosition(
locationSuccess,
locationError,
{timeout: 15000, maximumAge: 60000}
);
}

function refresh_weather() {
if (location_opt == "Default Only") {
fetchWeather_city(default_location);
} else {
getWeather_auto();
}
}
10 changes: 9 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ typedef struct {
} Time;

enum APP_MSG_TYPE {
WEATHER_ICON_KEY = 0,
PEBBLEKIT_JS_READY = 0,
WEATHER_ICON_KEY,
WEATHER_TEMPERATURE_KEY,
WEATHER_CITY_KEY,
SHOW_WEATHER,
SHOW_LOCATION,
DEFAULT_LOCATION,
LOCATION_OPT,
};

static bool s_pebblekit_js_ready = false;
static void set_pebblekit_js_ready(bool ready) {
s_pebblekit_js_ready = ready;
}

#define COLORS true
//#define SHOW_SCREENSHOT
#define COLOR_PRESET 0
Expand Down
44 changes: 38 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ static Layer *s_panel_layer, *s_hand_layer, *s_weather_layer;
static TextLayer *s_month_layer, *s_date_layer, *s_weekday_layer;
static TextLayer *s_temperature_layer, *s_city_layer;

static char s_conditions_buffer[32], s_temperature_buffer[32], s_city_buffer[32];
static bool s_show_weather = true, s_show_location = true;
static char s_default_location[32], s_location_opt[32];

/************************************ UI **************************************/
static void hand_tick_handler(struct tm *tick_time, TimeUnits changed) {
Expand All @@ -32,8 +34,8 @@ static void hand_tick_handler(struct tm *tick_time, TimeUnits changed) {
set_date_layer_cur_time(tick_time);

// update weather
config_weather_layer(s_show_weather, s_show_location);
update_weather_with_app_msg(tick_time);
if (tick_time->tm_min % 30 == 0)
update_weather_with_app_msg();
}

static void window_load(Window *window) {
Expand Down Expand Up @@ -82,19 +84,45 @@ static void window_unload(Window *window) {
}

static void init_config() {
// display
if (persist_exists(SHOW_WEATHER))
s_show_weather = persist_read_bool(SHOW_WEATHER);

if (persist_exists(SHOW_LOCATION))
s_show_location = persist_read_bool(SHOW_LOCATION);

config_weather_layer(s_show_weather, s_show_location);

// weather & location diaplay
memset(s_conditions_buffer, 0, sizeof(s_conditions_buffer));
if (persist_exists(WEATHER_ICON_KEY))
persist_read_string(WEATHER_ICON_KEY, s_conditions_buffer, sizeof(s_conditions_buffer));

memset(s_temperature_buffer, 0, sizeof(s_temperature_buffer));
if (persist_exists(WEATHER_TEMPERATURE_KEY))
persist_read_string(WEATHER_TEMPERATURE_KEY, s_temperature_buffer, sizeof(s_temperature_buffer));

memset(s_city_buffer, 0, sizeof(s_city_buffer));
if (persist_exists(WEATHER_CITY_KEY))
persist_read_string(WEATHER_CITY_KEY, s_city_buffer, sizeof(s_city_buffer));

config_weather_buffer(s_conditions_buffer, s_temperature_buffer, s_city_buffer);

// location setting
memset(s_default_location, 0, sizeof(s_default_location));
if (persist_exists(DEFAULT_LOCATION))
persist_read_string(DEFAULT_LOCATION, s_default_location, sizeof(s_default_location));

memset(s_location_opt, 0, sizeof(s_location_opt));
if (persist_exists(LOCATION_OPT))
persist_read_string(LOCATION_OPT, s_location_opt, sizeof(s_location_opt));

config_location_setting(s_default_location, s_location_opt);
}

/*********************************** App **************************************/
static void init() {
srand(time(NULL));

// init configuration
init_config();

// init main window
s_main_window = window_create();
Expand All @@ -103,11 +131,15 @@ static void init() {
.unload = window_unload,
});
window_stack_push(s_main_window, true);

// init configuration
init_config();

// handle tick now
time_t t = time(NULL);
struct tm *time_now = localtime(&t);
hand_tick_handler(time_now, SHOW_SECOND);
hand_tick_handler(time_now, 0);
refresh_weather_display();

// subscribe on time tick
if (SHOW_SECOND)
Expand Down
6 changes: 3 additions & 3 deletions src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ void init_date_layer(Window *window) {
s_text_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_DIGITAL_16));

// Create month TextLayer
s_month_layer = text_layer_create(GRect(s_win_w - 39, 0, 40, 25));
s_month_layer = text_layer_create(GRect(s_win_w - 40, 0, 40, 25));
text_layer_set_text_color(s_month_layer, MONTH_COLOR);
text_layer_set_background_color(s_month_layer, MONTH_BACKGROUND_COLOR);
text_layer_set_text_alignment(s_month_layer, GTextAlignmentLeft);
text_layer_set_font(s_month_layer, s_text_font);

// Create date TextLayer
s_date_layer = text_layer_create(GRect(s_win_w - 17, 0, 15, 25));
s_date_layer = text_layer_create(GRect(s_win_w - 21, 0, 20, 25));
text_layer_set_text_color(s_date_layer, DATE_COLOR);
text_layer_set_background_color(s_date_layer, DATE_BACKGROUND_COLOR);
text_layer_set_text_alignment(s_date_layer, GTextAlignmentRight);
text_layer_set_font(s_date_layer, s_text_font);

// Create Weekday TextLayer
s_weekday_layer = text_layer_create(GRect(2, 0, 50, 20));
s_weekday_layer = text_layer_create(GRect(1, 0, 50, 20));
text_layer_set_text_color(s_weekday_layer, WEEKDAY_COLOR);
text_layer_set_background_color(s_weekday_layer, WEEKDAY_BACKGROUND_COLOR);
text_layer_set_text_alignment(s_weekday_layer, GTextAlignmentLeft);
Expand Down
Loading

0 comments on commit ad63b89

Please sign in to comment.