Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabalize HTTP requests #364

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ CONFIG_NVS=y
# Bluetooth
CONFIG_BT=y
CONFIG_BT_HCI=y
CONFIG_BT_HCI_ACL_FLOW_CONTROL=n
CONFIG_BT_HCI_ACL_FLOW_CONTROL=y
CONFIG_BT_CTLR=n
# Enable chaining of multiple CTEs in periodic advertising
CONFIG_BT_DEBUG_LOG=n
Expand Down
13 changes: 9 additions & 4 deletions app/src/applications/weather/weather_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,19 @@ static void http_rsp_cb(ble_http_status_code_t status, char *response)
ble_comm_request_gps_status(false);
} else {
LOG_ERR("HTTP request failed\n");
weather_ui_set_error(status == BLE_HTTP_STATUS_TIMEOUT ? "Timeout" : "Failed");
}
}

static void fetch_weather_data(double lat, double lon)
{
char weather_url[512];
snprintf(weather_url, sizeof(weather_url), HTTP_REQUEST_URL_FMT, lat, lon, WEATHER_UI_NUM_FORECASTS);
zsw_ble_http_get(weather_url, http_rsp_cb);
// TODO Handle if HTTP requests are not enabled or supported on phone.
int ret = zsw_ble_http_get(weather_url, http_rsp_cb);
if (ret != 0 && ret != -EBUSY) {
LOG_ERR("Failed to send HTTP request: %d", ret);
weather_ui_set_error("Failed fetching weather");
}
}

static void on_zbus_ble_data_callback(const struct zbus_channel *chan)
Expand All @@ -118,17 +122,18 @@ static void on_zbus_ble_data_callback(const struct zbus_channel *chan)

static void weather_app_start(lv_obj_t *root, lv_group_t *group)
{
weather_ui_show(root);
if (last_update_gps_time == 0 || k_uptime_delta(&last_update_gps_time) > MAX_GPS_AGED_TIME_MS) {
LOG_DBG("GPS data is too old, request GPS\n");
int res = ble_comm_request_gps_status(true);
if (res != 0) {
LOG_ERR("Failed to request GPS data\n");
LOG_ERR("Failed to request GPS data");
weather_ui_set_error("Failed to get GPS data");
}
// TODO Show GPS fetching in progress in app
} else {
fetch_weather_data(last_lat, last_lon);
}
weather_ui_show(root);

zsw_timeval_t time;
zsw_clock_get_time(&time);
Expand Down
29 changes: 20 additions & 9 deletions app/src/applications/weather/weather_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static lv_obj_t *root_page = NULL;

static lv_obj_t *ui_bg_img;
static lv_obj_t *ui_root_container;
static lv_obj_t *ui_location;
static lv_obj_t *ui_status_label;
static lv_obj_t *ui_forecast_widget;
static lv_obj_t *ui_time;
static lv_obj_t *ui_today_container;
Expand Down Expand Up @@ -75,14 +75,14 @@ void weather_ui_show(lv_obj_t *root)
lv_obj_clear_flag(ui_root_container, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_flag(ui_root_container, LV_OBJ_FLAG_HIDDEN);

ui_location = lv_label_create(ui_root_container);
lv_obj_set_width(ui_location, LV_SIZE_CONTENT);
lv_obj_set_height(ui_location, LV_SIZE_CONTENT);
lv_obj_set_x(ui_location, 0);
lv_obj_set_y(ui_location, 25);
lv_obj_set_align(ui_location, LV_ALIGN_TOP_MID);
lv_label_set_text(ui_location, "");
lv_obj_set_style_text_font(ui_location, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);
ui_status_label = lv_label_create(root_page);
lv_obj_set_width(ui_status_label, LV_SIZE_CONTENT);
lv_obj_set_height(ui_status_label, LV_SIZE_CONTENT);
lv_obj_set_x(ui_status_label, 0);
lv_obj_set_y(ui_status_label, 25);
lv_obj_set_align(ui_status_label, LV_ALIGN_TOP_MID);
lv_label_set_text(ui_status_label, "");
lv_obj_set_style_text_font(ui_status_label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);

ui_forecast_widget = lv_obj_create(ui_root_container);
lv_obj_remove_style_all(ui_forecast_widget);
Expand Down Expand Up @@ -233,6 +233,17 @@ void weather_ui_set_weather_data(weather_ui_current_weather_data_t current_weath
}
}

void weather_ui_set_error(char *error)
{
if (root_page == NULL) {
return;
}

lv_obj_add_flag(ui_loading_spinner, LV_OBJ_FLAG_HIDDEN);

lv_label_set_text(ui_status_label, error);
}

void weather_ui_set_time(int hour, int min, int second)
{
lv_label_set_text_fmt(ui_time, "%02d:%02d", hour, min);
Expand Down
2 changes: 2 additions & 0 deletions app/src/applications/weather/weather_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ void weather_ui_set_weather_data(weather_ui_current_weather_data_t current_weath
int num_forecasts);

void weather_ui_set_time(int hour, int min, int second);

void weather_ui_set_error(char *error);
1 change: 1 addition & 0 deletions app/src/ble/ble_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ K_WORK_DELAYABLE_DEFINE(ble_http_timeout_work, ble_http_timeout_handler);
static void ble_http_timeout_handler(struct k_work *work)
{
LOG_WRN("HTTP Timeout");
request_pending = false;
ble_http_cb(BLE_HTTP_STATUS_TIMEOUT, NULL);
}

Expand Down