Исчезающие данные, что происходит? Галька новичок
Совершенно новый и наивный для Pebble SDK и C, но большой опыт работы на других языках. Это означает, что я, вероятно, буду смущен, когда кто-то решит это для меня очень легко, но... У меня есть два файла в проекте watchface, основанном на уроках... Я извлекаю разные данные, но все работает так же, как учебник по началу работы вплоть до того момента, когда код обновляет один из текстовых слоев... ничего. Меня это озадачило, потому что данные, которые он должен туда поместить, хорошо отображаются в журнале консоли, так что это определенно пропускается. В любом случае, вот C
include
#define KEY_MONTHMILES 0
#define KEY_MONTHELEVATION 1
#define KEY_TOTALMILES 2
#define KEY_TOTALELEVATION 3
static Window *s_main_window;
static TextLayer *s_time_layer;
static BitmapLayer *s_background_layer;
static GBitmap *s_background_bitmap;
static TextLayer *s_weather_layer;
static void update_time() {
// Get a tm structure
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
// Create a long-lived buffer
static char buffer[] = "00:00";
// Write the current hours and minutes into the buffer
if(clock_is_24h_style() == true) {
// Use 24 hour format
strftime(buffer, sizeof("00:00"), "%H:%M", tick_time);
} else {
// Use 12 hour format
strftime(buffer, sizeof("00:00"), "%I:%M", tick_time);
}
// Display this time on the TextLayer
text_layer_set_text(s_time_layer, buffer);
}
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_time();
}
static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
// Store incoming information
static char monthlyMiles_buffer[128];
static char monthlyElevation_buffer[128];
static char yearlyMiles_buffer[128];
static char yearlyElevation_buffer[128];
// Read first item
Tuple *t = dict_read_first(iterator);
// For all items
while(t != NULL) {
// Which key was received?
switch(t->key) {
case KEY_MONTHMILES:
snprintf(monthlyMiles_buffer, sizeof(monthlyMiles_buffer), "%d", (int)t->value->int32);
break;
case KEY_MONTHELEVATION:
snprintf(monthlyElevation_buffer, sizeof(monthlyElevation_buffer), "%d", (int)t->value->int32);
break;
case KEY_TOTALMILES:
snprintf(yearlyMiles_buffer, sizeof(yearlyMiles_buffer), "%d", (int)t->value->int32);
break;
case KEY_TOTALELEVATION:
snprintf(yearlyElevation_buffer, sizeof(yearlyElevation_buffer), "%d", (int)t->value->int32);
break;
default:
APP_LOG(APP_LOG_LEVEL_ERROR, "Key %d not recognized!", (int)t->key);
break;
}
// Look for next item
t = dict_read_next(iterator);
}
text_layer_set_text(s_weather_layer, monthlyMiles_buffer);
}
static void inbox_dropped_callback(AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped!");
}
static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox send failed!");
}
static void outbox_sent_callback(DictionaryIterator *iterator, void *context) {
APP_LOG(APP_LOG_LEVEL_INFO, "Outbox send success!");
}
static void main_window_load(Window *window) {
// Create GBitmap, then set to created BitmapLayer
s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_BG_IMAGE);
s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168));
bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer));
// Create time TextLayer
s_time_layer = text_layer_create(GRect(0, 60, 144, 50));
text_layer_set_background_color(s_time_layer, GColorClear);
text_layer_set_text_color(s_time_layer, GColorWhite);
layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));
// Improve the layout to be more like a watchface
text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
// Make sure the time is displayed from the start
update_time();
// Create temperature Layer
s_weather_layer = text_layer_create(GRect(0, 130, 144, 25));
text_layer_set_background_color(s_weather_layer, GColorClear);
text_layer_set_text_color(s_weather_layer, GColorWhite);
text_layer_set_text_alignment(s_weather_layer, GTextAlignmentCenter);
text_layer_set_text(s_weather_layer, "Loading...");
text_layer_set_font(s_weather_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14));
layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_weather_layer));
}
static void main_window_unload(Window *window) {
// Destroy TextLayer
text_layer_destroy(s_time_layer);
// Destroy GBitmap
gbitmap_destroy(s_background_bitmap);
// Destroy BitmapLayer
bitmap_layer_destroy(s_background_layer);
// Destroy weather elements
text_layer_destroy(s_weather_layer);
}
static void init() {
// Create main Window element and assign to pointer
s_main_window = window_create();
// Set handlers to manage the elements inside the Window
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
// Show the Window on the watch, with animated=true
window_stack_push(s_main_window, true);
// Register with TickTimerService
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
// Register callbacks
app_message_register_inbox_received(inbox_received_callback);
app_message_register_inbox_dropped(inbox_dropped_callback);
app_message_register_outbox_failed(outbox_failed_callback);
app_message_register_outbox_sent(outbox_sent_callback);
// Open AppMessage
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
}
static void deinit() {
// Destroy Window
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
и вот Javascript
var xhrRequest = function (url, type, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
callback(this.responseText);
};
xhr.open(type, url);
xhr.send();
};
function getStats() {
// Construct URL
var url = "http://crin.co.uk/stravaWatchface/getStats.php";
// Send request to crin.co.uk
xhrRequest(url, 'GET',
function(responseText) {
// responseText contains a JSON object with stat info
var json = JSON.parse(responseText);
// Monthly miles
var monthMiles = json.mM;
console.log("Monthly total miles is: " + monthMiles);
// Monthly Elevation
var monthElevation = json.mE;
console.log("Monthly total elevation gain is: " + monthElevation);
// Total Miles
var totalMiles = json.tM;
console.log("All-time total miles is: " + totalMiles);
// Total Elevation
var totalElevation = json.tE;
console.log("All-time total elevation gain is: " + totalElevation);
// Assemble dictionary using our keys
var dictionary = {
"KEY_MONTHMILES": monthMiles,
"KEY_MONTHELEVATION": monthElevation,
"KEY_TOTALMILES": totalMiles,
"KEY_TOTALELEVATION": totalElevation
};
// Send to Pebble
Pebble.sendAppMessage(dictionary,
function(e) {
console.log("Stats info sent to Pebble successfully!");
},
function(e) {
console.log("Error sending stats info to Pebble!");
}
);
}
);
}
// Listen for when the watchface is opened
Pebble.addEventListener('ready',
function(e) {
console.log("PebbleKit JS ready!");
// Get the initial weather
getStats();
}
);
// Listen for when an AppMessage is received
Pebble.addEventListener('appmessage',
function(e) {
console.log("AppMessage received!");
getStats();
}
);
Кто-нибудь заметил что-нибудь очевидное? Компилируется и устанавливается нормально, но эта строка...
text_layer_set_text(s_weather_layer, monthlyMiles_buffer);
Обновляет watchface с пустым пространством, а не значением, которое он должен.
Пожалуйста, помогите, я потерял день на это.