Files
analog_weather/src/c/messaging.c
2024-10-06 20:50:04 +00:00

223 lines
7.4 KiB
C

#include <pebble.h>
#include "messaging.h"
#include "constants.h"
#include "weather.h"
#include "date.h"
#include "digital_time.h"
#include "battery.h"
#include "analog_wd.h"
extern Options s_options;
extern Layer *s_canvas_layer;
void send(int key, int value, int key2, int value2, int key3, char value3[64]){
#if DEBUG
APP_LOG(APP_LOG_LEVEL_DEBUG, "send: s_options.pebble_js_ready: %d", s_options.pebble_js_ready);
#endif
if (s_options.pebble_js_ready) {
DictionaryIterator *iterator;
app_message_outbox_begin(&iterator);
dict_write_int(iterator, key, &value, sizeof(int), true);
dict_write_int(iterator, key2, &value2, sizeof(int), true);
dict_write_cstring(iterator, key3, value3);
app_message_outbox_send();
}
}
void inbox_received_callback(DictionaryIterator *iterator, void *context) {
// Read first item
Tuple *t = dict_read_first(iterator);
// For all items
while(t != NULL) {
// Which key was received?
switch(t->key) {
case KEY_TEMPERATURE:
s_options.tempF = (int)t->value->int32;
break;
case KEY_TEMPERATURE_LO:
s_options.tempFLo = (int)t->value->int32;
break;
case KEY_TEMPERATURE_HI:
s_options.tempFHi = (int)t->value->int32;
break;
case KEY_TEMPERATURE_IN_C:
s_options.tempC = (int)t->value->int32;
break;
case KEY_TEMPERATURE_IN_C_LO:
s_options.tempCLo = (int)t->value->int32;
break;
case KEY_TEMPERATURE_IN_C_HI:
s_options.tempCHi = (int)t->value->int32;
break;
case KEY_CONDITIONS:
snprintf(s_options.conditions, sizeof(s_options.conditions), "%s", t->value->cstring);
//check if successful weather retrieval
if (strlen(s_options.conditions) > 0) {
// Get a tm structure
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
//strftime(s_options.last_weather_update, sizeof("00:00"), "%H:%M", tick_time);
strftime(s_options.last_weather_update24hr, sizeof("00:00"), "%H:%M", tick_time);
strftime(s_options.last_weather_update12hr, sizeof("00:00 pm"), "%l:%M %P", tick_time);
s_options.last_update = temp;
}
break;
case KEY_CONDITION_CODE:
s_options.condition_code = (int)t->value->int32;
break;
case KEY_SHAKE_FOR_LOHI:
s_options.shake_for_lohi = (int)t->value->int32;
break;
case KEY_USE_CELSIUS:
s_options.use_celsius = (int)t->value->int32;
break;
case KEY_WEATHER_USE_GPS:
//force weather update after each config page Save
memset(s_options.conditions, 0,sizeof(s_options.conditions));
s_options.condition_code = DEFAULT_CONDITION_CODE;
s_options.weather_use_GPS = (int)t->value->int32;
break;
case KEY_WEATHER_LOCATION:
snprintf(s_options.weather_location, sizeof(s_options.weather_location), "%s", t->value->cstring);
break;
case KEY_WEATHER_FREQUENCY:
s_options.weather_frequency = (int)t->value->int32;
break;
case KEY_BACKGROUND_COLOR:
s_options.background_color = (int)t->value->int32;
window_set_background_color(s_main_window, GColorFromHEX(s_options.background_color));
break;
case KEY_TEXT_COLOR:
s_options.text_color = (int)t->value->int32;
text_layer_set_text_color(weather_layer1, GColorFromHEX(s_options.text_color));
text_layer_set_text_color(weather_layer_center, GColorFromHEX(s_options.text_color));
text_layer_set_text_color(weather_layer2, GColorFromHEX(s_options.text_color));
text_layer_set_text_color(digital_time_layer, GColorFromHEX(s_options.text_color));
text_layer_set_text_color(date_layer1, GColorFromHEX(s_options.text_color));
text_layer_set_text_color(date_layer2, GColorFromHEX(s_options.text_color));
text_layer_set_text_color(battery_layer, GColorFromHEX(s_options.text_color));
break;
case KEY_HOUR_HAND_COLOR:
s_options.hour_hand_color = (int)t->value->int32;
break;
case KEY_MINUTE_HAND_COLOR:
s_options.minute_hand_color = (int)t->value->int32;
break;
case KEY_SECONDS_HAND_COLOR:
s_options.seconds_hand_color = (int)t->value->int32;
break;
case KEY_HOUR_MARKERS_COLOR:
s_options.hour_markers_color = (int)t->value->int32;
break;
case KEY_MINOR_MARKERS_COLOR:
s_options.minor_markers_color = (int)t->value->int32;
break;
case KEY_READABILITY:
s_options.readability = (int)t->value->int32;
break;
case KEY_DISPLAY_DIGITAL_TIME:
s_options.display_digital_time = (int)t->value->int32;
break;
case KEY_DISPLAY_DATE:
s_options.display_date = (int)t->value->int32;
break;
case KEY_VIBBRATE_BT_STATUS:
s_options.vibrate_bt_status = (int)t->value->int32;
break;
case KEY_USE_THIN_HANDS:
s_options.use_thin_hands = (int)t->value->int32;
break;
case KEY_DISPLAY_BATTERY:
s_options.display_battery = (int)t->value->int32;
break;
case KEY_DISPLAY_HOUR_DIGITS:
s_options.display_hour_digits = (int)t->value->int32;
break;
case KEY_DISPLAY_SECONDS_HAND:
s_options.display_seconds_hand = (int)t->value->int32;
break;
case KEY_JS_READY:
s_options.pebble_js_ready = (int)t->value->int32;
#if DEBUG
APP_LOG(APP_LOG_LEVEL_DEBUG, "inbox_received_callback: s_options.pebble_js_ready: %d", s_options.pebble_js_ready);
#endif
break;
}
// Look for next item
t = dict_read_next(iterator);
}
//check update weather, date, digital time, and battery layers
update_date_layer();
update_digital_time_layer();
update_weather_layer();
update_battery_layer();
}
char *translate_error(AppMessageResult result) {
switch (result) {
case APP_MSG_OK: return "APP_MSG_OK";
case APP_MSG_SEND_TIMEOUT: return "APP_MSG_SEND_TIMEOUT";
case APP_MSG_SEND_REJECTED: return "APP_MSG_SEND_REJECTED";
case APP_MSG_NOT_CONNECTED: return "APP_MSG_NOT_CONNECTED";
case APP_MSG_APP_NOT_RUNNING: return "APP_MSG_APP_NOT_RUNNING";
case APP_MSG_INVALID_ARGS: return "APP_MSG_INVALID_ARGS";
case APP_MSG_BUSY: return "APP_MSG_BUSY";
case APP_MSG_BUFFER_OVERFLOW: return "APP_MSG_BUFFER_OVERFLOW";
case APP_MSG_ALREADY_RELEASED: return "APP_MSG_ALREADY_RELEASED";
case APP_MSG_CALLBACK_ALREADY_REGISTERED: return "APP_MSG_CALLBACK_ALREADY_REGISTERED";
case APP_MSG_CALLBACK_NOT_REGISTERED: return "APP_MSG_CALLBACK_NOT_REGISTERED";
case APP_MSG_OUT_OF_MEMORY: return "APP_MSG_OUT_OF_MEMORY";
case APP_MSG_CLOSED: return "APP_MSG_CLOSED";
case APP_MSG_INTERNAL_ERROR: return "APP_MSG_INTERNAL_ERROR";
default: return "UNKNOWN ERROR";
}
}
void message_dropped(AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Dropped message! Reason given: %s", translate_error(reason));
}
void message_out_success(DictionaryIterator *iter, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Message sent.");
}
void message_out_failed(DictionaryIterator *iter, AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Failed to send message. Reason = %s", translate_error(reason));
}