Exemple #1
0
// Update the bottom half of the screen: icon and temperature
void weather_layer_update(WeatherData *weather_data)
{
    // We have no weather data yet... don't update until we do
    if (weather_data->updated == 0) {
        return;
    }

    WeatherLayerData *wld = layer_get_data(weather_layer);

    if (weather_animation_timer && animation_timer_enabled) {
        app_timer_cancel(weather_animation_timer);
        // this is only needed to stop the error message when cancelling an already cancelled timer...
        animation_timer_enabled = false;
        layer_set_hidden(wld->loading_layer, true);
    }

    time_t current_time = time(NULL);

    bool stale = false;
    if (current_time - weather_data->updated > WEATHER_STALE_TIMEOUT) {
        stale = true;
    }

    //APP_LOG(APP_LOG_LEVEL_DEBUG, "ct:%i wup:%i, stale:%i",
    //  (int)current_time, (int)weather_data->updated, (int)WEATHER_STALE_TIMEOUT);

    // Update the weather icon and temperature
    if (weather_data->error) {
        // Only update the error icon if the weather data is stale
        if (stale) {
            weather_layer_clear_temperature();
            switch (weather_data->error) {
            case WEATHER_E_NETWORK:
                weather_layer_set_error();
                debug_update_message("Network error");
                break;
            case WEATHER_E_DISCONNECTED:
            case WEATHER_E_PHONE:
            default:
                weather_layer_set_error();
                debug_update_message("Phone disco / error");
                break;
            }
        }
    } else {

        layer_set_frame(bitmap_layer_get_layer(wld->primary_icon_layer), PRIMARY_ICON_NORMAL_FRAME);

        // Show the temperature as 'stale' if it has not been updated in WEATHER_STALE_TIMEOUT
        weather_layer_set_temperature(weather_data->temperature, stale);

        // Day/night check
        time_t utc = current_time;
//    time_t utc = current_time + weather_data->tzoffset;                                ///////// Change utc value to current_time for SDK3 by ktagjp
        bool night_time = is_night_time(weather_data->sunrise, weather_data->sunset, utc);

        /*
        APP_LOG(APP_LOG_LEVEL_DEBUG,
           "ct:%i, utc:%i sr:%i, ss:%i, nt:%i",
           (int)current_time, (int)utc, weather_data->sunrise, weather_data->sunset, night_time);
        */

        if (strcmp(weather_data->service, SERVICE_OPEN_WEATHER) == 0) {
            weather_layer_set_icon(open_weather_icon_for_condition(weather_data->condition, night_time), AREA_PRIMARY);
        } else if (strcmp(weather_data->service, SERVICE_YAHOO_WEATHER) == 0) {
            weather_layer_set_icon(yahoo_weather_icon_for_condition(weather_data->condition, night_time), AREA_PRIMARY);
        } else {
            weather_layer_set_icon(wunder_weather_icon_for_condition(weather_data->condition, night_time), AREA_PRIMARY);
        }

        if (weather_data->hourly_updated != 0 && weather_data->hourly_enabled) {
            time_t h1t = weather_data->h1_time;
            time_t h2t = weather_data->h2_time;
//      time_t h1t = weather_data->h1_time - weather_data->tzoffset;      ///////// Change time value for SDK3 by ktagjp
//      time_t h2t = weather_data->h2_time - weather_data->tzoffset;      ///////// Change time value for SDK3 by ktagjp
            strftime(time_h1, sizeof(time_h1), "%I%p", localtime(&h1t));
            strftime(time_h2, sizeof(time_h2), "%I%p", localtime(&h2t));

            if (time_h1[0] == '0') {
                memmove(time_h1, &time_h1[1], sizeof(time_h1) - 1);
            }
            if (time_h2[0] == '0') {
                memmove(time_h2, &time_h2[1], sizeof(time_h2) - 1);
            }

            text_layer_set_text(wld->h1_time_layer, time_h1);
            text_layer_set_text(wld->h2_time_layer, time_h2);

            localtime(&current_time);

            night_time = is_night_time(weather_data->sunrise, weather_data->sunset, weather_data->h1_time);
            weather_layer_set_icon(wunder_weather_icon_for_condition(weather_data->h1_cond, night_time), AREA_HOURLY1);

            night_time = is_night_time(weather_data->sunrise, weather_data->sunset, weather_data->h2_time);
            weather_layer_set_icon(wunder_weather_icon_for_condition(weather_data->h2_cond, night_time), AREA_HOURLY2);

            snprintf(wld->h1_temp_str, sizeof(wld->h1_temp_str),
                     "%i%s", weather_data->h1_temp, "°");
            snprintf(wld->h2_temp_str, sizeof(wld->h2_temp_str),
                     "%i%s", weather_data->h2_temp, "°");

            text_layer_set_text(wld->h1_temp_layer, wld->h1_temp_str);
            text_layer_set_text(wld->h2_temp_layer, wld->h2_temp_str);
        }
    }
}
static void appmsg_in_received(DictionaryIterator *received, void *context) {
  APP_LOG(APP_LOG_LEVEL_DEBUG, "In received.");

  WeatherData *weather = (WeatherData*) context;

  // Program Control
  Tuple *error_tuple       = dict_find(received, KEY_ERROR);
  Tuple *js_ready_tuple    = dict_find(received, KEY_JS_READY);

  // Current Weather (Via Yahoo, Open Weather Map)
  Tuple *temperature_tuple = dict_find(received, KEY_TEMPERATURE);
  Tuple *condition_tuple   = dict_find(received, KEY_CONDITION);
  Tuple *sunrise_tuple     = dict_find(received, KEY_SUNRISE);
  Tuple *sunset_tuple      = dict_find(received, KEY_SUNSET);
  Tuple *pub_date_tuple    = dict_find(received, KEY_PUB_DATE);
  Tuple *locale_tuple      = dict_find(received, KEY_LOCALE);
  Tuple *tzoffset_tuple    = dict_find(received, KEY_TZOFFSET);

  // Configuration Settings
  Tuple *service_tuple     = dict_find(received, KEY_SERVICE);
  Tuple *debug_tuple       = dict_find(received, KEY_DEBUG);
  Tuple *scale_tuple       = dict_find(received, KEY_SCALE);
  Tuple *battery_tuple     = dict_find(received, KEY_BATTERY);

  // Hourly Weather
  Tuple *h1_temp_tuple = dict_find(received, KEY_H1_TEMP);
  Tuple *h1_cond_tuple = dict_find(received, KEY_H1_COND);
  Tuple *h1_time_tuple = dict_find(received, KEY_H1_TIME);
  Tuple *h1_pop_tuple  = dict_find(received, KEY_H1_POP);
  Tuple *h2_temp_tuple = dict_find(received, KEY_H2_TEMP);
  Tuple *h2_cond_tuple = dict_find(received, KEY_H2_COND);
  Tuple *h2_time_tuple = dict_find(received, KEY_H2_TIME);
  Tuple *h2_pop_tuple  = dict_find(received, KEY_H2_POP);

  Tuple *hourly_enabled_tuple = dict_find(received, KEY_HOURLY_ENABLED);

  // Weather update
  if (temperature_tuple && condition_tuple) {
    weather->temperature = temperature_tuple->value->int32;
    weather->condition   = condition_tuple->value->int32;
    weather->sunrise     = sunrise_tuple->value->int32;
    weather->sunset      = sunset_tuple->value->int32;
    weather->error       = WEATHER_E_OK;
    weather->tzoffset    = tzoffset_tuple->value->int32;
    weather->updated     = time(NULL);

    strncpy(weather->pub_date, pub_date_tuple->value->cstring, 6);
    strncpy(weather->locale, locale_tuple->value->cstring, 255);

    if (weather->debug) {
      debug_enable_display();
      debug_update_weather(weather);
    }
    
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Weather temp:%i cond:%i pd:%s tzos:%i loc:%s", 
      weather->temperature, weather->condition, weather->pub_date, weather->tzoffset, weather->locale);
  }
  // Configuration Update
  else if (service_tuple) {
    char* service = strcmp(service_tuple->value->cstring, SERVICE_OPEN_WEATHER) == 0 ? SERVICE_OPEN_WEATHER : SERVICE_YAHOO_WEATHER;
    char* scale   = strcmp(scale_tuple->value->cstring, SCALE_CELSIUS) == 0 ? SCALE_CELSIUS : SCALE_FAHRENHEIT;
    strncpy(weather->service, service, 6);
    strncpy(weather->scale, scale, 2);

    weather->debug   = (bool)debug_tuple->value->int32;
    weather->battery = (bool)battery_tuple->value->int32;

    APP_LOG(APP_LOG_LEVEL_DEBUG, "Configuration serv:%s scale:%s debug:%i batt:%i", 
      weather->service, weather->scale, weather->debug, weather->battery);

    if (weather->battery) {
      battery_enable_display();
    } else {
      battery_disable_display();
    }

    if (weather->debug) {
      debug_enable_display();
      debug_update_weather(weather);
    } else {
      debug_disable_display();
    }

    store_persisted_values(weather);
  }
  // Hourly Weather Update
  else if (h1_temp_tuple) {
    weather->h1_temp = h1_temp_tuple->value->int32;
    weather->h1_cond = h1_cond_tuple->value->int32;
    weather->h1_time = h1_time_tuple->value->int32;
    weather->h1_pop  = h1_pop_tuple->value->int32;
    weather->h2_temp = h2_temp_tuple->value->int32;
    weather->h2_cond = h2_cond_tuple->value->int32;
    weather->h2_time = h2_time_tuple->value->int32;
    weather->h2_pop  = h2_pop_tuple->value->int32;

    weather->hourly_enabled = true;
    weather->hourly_updated = time(NULL);
  }
  // Initial Javascript Ready message
  else if (js_ready_tuple) {
    weather->js_ready = true;
    weather->error    = WEATHER_E_OK;
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Javascript is ready");
    debug_update_message("JS ready");
    initial_jsready_callback();
  }
  // Hourly enabled
  else if (hourly_enabled_tuple) {
    weather->hourly_enabled = (bool)hourly_enabled_tuple->value->int32;
    weather->hourly_updated = time(NULL);
  }
  else if (error_tuple) {
    weather->error   = WEATHER_E_NETWORK;
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Error: %s", error_tuple->value->cstring);
  }
  else {
    weather->error = WEATHER_E_PHONE;
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Message with unknown keys: t=%p c=%p e=%p",
      temperature_tuple, condition_tuple, error_tuple);
  }

  weather_layer_update(weather);

  // Success! reset the retry count...
  retry_count = 0;
}
// Update the bottom half of the screen: icon and temperature
void npr_layer_update(NprData *npr_data) 
{
  // We have no npr data yet... don't update until we do
  if (npr_data->updated == 0) {
    return;
  }

  NprLayerData *nld = layer_get_data(npr_layer);

  if (npr_animation_timer && animation_timer_enabled) {
    app_timer_cancel(npr_animation_timer);
    // this is only needed to stop the error message when cancelling an already cancelled timer... 
    animation_timer_enabled = false;
    layer_set_hidden(nld->loading_layer, true);
  }

  time_t current_time = time(NULL);
  bool stale = false;

  // APP_LOG(APP_LOG_LEVEL_DEBUG, "ct:%i wup:%i, stale:%i", 
  //   (int)current_time, (int)npr_data->updated, (int)NPR_STALE_TIMEOUT);

  // Update the npr icon and temperature
  if (npr_data->error) {
    // Only update the error icon if the npr data is stale
    if (stale) {
      switch (npr_data->error) {
        case ERROR_NETWORK:
          //npr_layer_set_icon(NPR_ICON_CLOUD_ERROR);
          debug_update_message("Network error");
          break;
        case ERROR_DISCONNECTED:
        case ERROR_PHONE:
        default:
          //npr_layer_set_icon(NPR_ICON_PHONE_ERROR);
          debug_update_message("Phone disco / error");
          break;
      }
    }
  } 
  else {

    layer_set_hidden(bitmap_layer_get_layer(nld->error_icon_layer), true);

    text_layer_set_text(nld->primary_frequency_layer, npr_data->primary_frequency);
    text_layer_set_text(nld->primary_call_layer, npr_data->primary_call);
    //text_layer_set_text(nld->primary_band_layer, npr_data->primary_band);

    nld->primary_strength = npr_data->primary_strength;
    layer_mark_dirty(nld->primary_strength_layer);

    if (npr_data->secondary_available) {

      text_layer_set_text(nld->secondary_frequency_layer, npr_data->secondary_frequency);
      text_layer_set_text(nld->secondary_call_layer, npr_data->secondary_call);
      //text_layer_set_text(nld->secondary_band_layer, npr_data->secondary_band);

      nld->secondary_strength = npr_data->secondary_strength;
      layer_mark_dirty(nld->secondary_strength_layer);
    }

    text_layer_set_text(nld->primary_program_layer, npr_data->primary_program);
    text_layer_set_text(nld->secondary_program_layer, npr_data->secondary_program);
  }
}