diff --git a/src/app/crypto_ticker/crypto_ticker_fetch.cpp b/src/app/crypto_ticker/crypto_ticker_fetch.cpp index aac7985..195bed1 100644 --- a/src/app/crypto_ticker/crypto_ticker_fetch.cpp +++ b/src/app/crypto_ticker/crypto_ticker_fetch.cpp @@ -23,6 +23,7 @@ #include "HTTPClient.h" #include "crypto_ticker.h" +#include "crypto_ticker_main.h" #include "crypto_ticker_widget.h" #include "crypto_ticker_fetch.h" @@ -30,7 +31,7 @@ #include "hardware/json_psram_allocator.h" -int crypto_ticker_fetch_today( crypto_ticker_config_t *crypto_ticker_config, crypto_ticker_widget_data_t *crypto_ticker_today ) { +int crypto_ticker_fetch_price( crypto_ticker_config_t *crypto_ticker_config, crypto_ticker_widget_data_t *crypto_ticker_widget_data ) { char url[512]=""; int httpcode = -1; @@ -64,10 +65,55 @@ int crypto_ticker_fetch_today( crypto_ticker_config_t *crypto_ticker_config, cry today_client.end(); - crypto_ticker_today->valide = true; - strcpy( crypto_ticker_today->price, doc["price"] ); + crypto_ticker_widget_data->valide = true; + strcpy( crypto_ticker_widget_data->price, doc["price"] ); doc.clear(); return( httpcode ); } + + + +int crypto_ticker_fetch_statistics( crypto_ticker_config_t *crypto_ticker_config, crypto_ticker_main_data_t *crypto_ticker_main_data ) { + char url[512]=""; + int httpcode = -1; + + + snprintf( url, sizeof( url ), "http://%s/api/CryptoTicker/24hrStatistics/%s", MY_TTGO_WATCH_HOST, crypto_ticker_config->symbol); + + HTTPClient today_client; + + today_client.useHTTP10( true ); + today_client.begin( url ); + today_client.addHeader("force-unsecure","true"); + httpcode = today_client.GET(); + + if ( httpcode != 200 ) { + log_e("HTTPClient error %d", httpcode, url ); + today_client.end(); + return( -1 ); + } + + Serial.println(ESP.getFreeHeap()); + + SpiRamJsonDocument doc( 1000 ); + + DeserializationError error = deserializeJson( doc, today_client.getStream() ); + if (error) { + log_e("crypto_ticker deserializeJson() failed: %s", error.c_str() ); + doc.clear(); + today_client.end(); + return( -1 ); + } + + today_client.end(); + + crypto_ticker_main_data->valide = true; + strcpy( crypto_ticker_main_data->lastPrice, doc["lastPrice"] ); + strcpy( crypto_ticker_main_data->priceChangePercent, doc["priceChangePercent"] ); + strcpy( crypto_ticker_main_data->volume, doc["volume"] ); + + doc.clear(); + return( httpcode ); +} \ No newline at end of file diff --git a/src/app/crypto_ticker/crypto_ticker_fetch.h b/src/app/crypto_ticker/crypto_ticker_fetch.h index 142b0c1..7f14776 100644 --- a/src/app/crypto_ticker/crypto_ticker_fetch.h +++ b/src/app/crypto_ticker/crypto_ticker_fetch.h @@ -20,13 +20,15 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "crypto_ticker_widget.h" +#include "crypto_ticker_widget.h" //TODO: why is this needed +#include "crypto_ticker_main.h" //TODO: why is this needed #ifndef _crypto_ticker_FETCH_H #define _crypto_ticker_FETCH_H #define MY_TTGO_WATCH_HOST "my-ttgo-watch.co.uk" - int crypto_ticker_fetch_today( crypto_ticker_config_t * crypto_ticker_config, crypto_ticker_widget_data_t * crypto_ticker_today ); + int crypto_ticker_fetch_price( crypto_ticker_config_t * crypto_ticker_config, crypto_ticker_widget_data_t * crypto_ticker_today ); + int crypto_ticker_fetch_statistics( crypto_ticker_config_t *crypto_ticker_config, crypto_ticker_main_data_t *crypto_ticker_main_data ); #endif // _crypto_ticker_FETCH_H \ No newline at end of file diff --git a/src/app/crypto_ticker/crypto_ticker_main.cpp b/src/app/crypto_ticker/crypto_ticker_main.cpp index dd56ea7..9cf1907 100644 --- a/src/app/crypto_ticker/crypto_ticker_main.cpp +++ b/src/app/crypto_ticker/crypto_ticker_main.cpp @@ -24,16 +24,29 @@ #include "crypto_ticker.h" #include "crypto_ticker_main.h" +#include "crypto_ticker_fetch.h" #include "gui/mainbar/app_tile/app_tile.h" #include "gui/mainbar/main_tile/main_tile.h" #include "gui/mainbar/mainbar.h" #include "gui/statusbar.h" +#include "hardware/wifictl.h" + +EventGroupHandle_t crypto_ticker_main_event_handle = NULL; +TaskHandle_t _crypto_ticker_main_sync_Task; + lv_obj_t *crypto_ticker_main_tile = NULL; lv_style_t crypto_ticker_main_style; -lv_task_t * _crypto_ticker_task; +lv_obj_t *crypto_ticker_main_update_label = NULL; + + +crypto_ticker_main_data_t crypto_ticker_main_data; + + +void crypto_ticker_main_sync_Task( void * pvParameters ); +void crypto_ticker_main_wifictl_event_cb( EventBits_t event, char* msg ); LV_IMG_DECLARE(exit_32px); LV_IMG_DECLARE(setup_32px); @@ -42,7 +55,7 @@ LV_FONT_DECLARE(Ubuntu_72px); static void exit_crypto_ticker_main_event_cb( lv_obj_t * obj, lv_event_t event ); static void enter_crypto_ticker_setup_event_cb( lv_obj_t * obj, lv_event_t event ); -void crypto_ticker_task( lv_task_t * task ); +static void refresh_crypto_ticker_main_event_cb( lv_obj_t * obj, lv_event_t event ); void crypto_ticker_main_setup( uint32_t tile_num ) { @@ -58,6 +71,15 @@ void crypto_ticker_main_setup( uint32_t tile_num ) { lv_obj_align(exit_btn, crypto_ticker_main_tile, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10 ); lv_obj_set_event_cb( exit_btn, exit_crypto_ticker_main_event_cb ); + lv_obj_t * reload_btn = lv_imgbtn_create( crypto_ticker_main_tile, NULL); + lv_imgbtn_set_src(reload_btn, LV_BTN_STATE_RELEASED, &refresh_32px); + lv_imgbtn_set_src(reload_btn, LV_BTN_STATE_PRESSED, &refresh_32px); + lv_imgbtn_set_src(reload_btn, LV_BTN_STATE_CHECKED_RELEASED, &refresh_32px); + lv_imgbtn_set_src(reload_btn, LV_BTN_STATE_CHECKED_PRESSED, &refresh_32px); + lv_obj_add_style(reload_btn, LV_IMGBTN_PART_MAIN, &crypto_ticker_main_style ); + lv_obj_align(reload_btn, crypto_ticker_main_tile, LV_ALIGN_IN_TOP_RIGHT, -10 , 10 ); + lv_obj_set_event_cb( reload_btn, refresh_crypto_ticker_main_event_cb ); + lv_obj_t * setup_btn = lv_imgbtn_create( crypto_ticker_main_tile, NULL); lv_imgbtn_set_src(setup_btn, LV_BTN_STATE_RELEASED, &setup_32px); lv_imgbtn_set_src(setup_btn, LV_BTN_STATE_PRESSED, &setup_32px); @@ -67,17 +89,27 @@ void crypto_ticker_main_setup( uint32_t tile_num ) { lv_obj_align(setup_btn, crypto_ticker_main_tile, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10 ); lv_obj_set_event_cb( setup_btn, enter_crypto_ticker_setup_event_cb ); - // uncomment the following block of code to remove the "myapp" label in background - lv_style_set_text_opa( &crypto_ticker_main_style, LV_OBJ_PART_MAIN, LV_OPA_70); - lv_style_set_text_font( &crypto_ticker_main_style, LV_STATE_DEFAULT, &Ubuntu_72px); - lv_obj_t *app_label = lv_label_create( crypto_ticker_main_tile, NULL); - lv_label_set_text( app_label, "myapp"); - lv_obj_reset_style_list( app_label, LV_OBJ_PART_MAIN ); - lv_obj_add_style( app_label, LV_OBJ_PART_MAIN, &crypto_ticker_main_style ); - lv_obj_align( app_label, crypto_ticker_main_tile, LV_ALIGN_CENTER, 0, 0); + + crypto_ticker_main_update_label = lv_label_create( crypto_ticker_main_tile , NULL); + lv_label_set_text( crypto_ticker_main_update_label, ""); + lv_obj_reset_style_list( crypto_ticker_main_update_label, LV_OBJ_PART_MAIN ); + lv_obj_align( crypto_ticker_main_update_label, crypto_ticker_main_tile, LV_ALIGN_IN_TOP_LEFT, 0, 0 ); - // create an task that runs every secound - _crypto_ticker_task = lv_task_create( crypto_ticker_task, 1000, LV_TASK_PRIO_MID, NULL ); + + + crypto_ticker_main_event_handle = xEventGroupCreate(); + + wifictl_register_cb( WIFICTL_OFF | WIFICTL_CONNECT, crypto_ticker_main_wifictl_event_cb ); +} + +void crypto_ticker_main_wifictl_event_cb( EventBits_t event, char* msg ) { + switch( event ) { + case WIFICTL_CONNECT: crypto_ticker_config_t *crypto_ticker_config = crypto_ticker_get_config(); + if ( crypto_ticker_config->autosync ) { + crypto_ticker_main_sync_request(); + } + break; + } } static void enter_crypto_ticker_setup_event_cb( lv_obj_t * obj, lv_event_t event ) { @@ -94,6 +126,56 @@ static void exit_crypto_ticker_main_event_cb( lv_obj_t * obj, lv_event_t event ) } } -void crypto_ticker_task( lv_task_t * task ) { - // put your code her + +static void refresh_crypto_ticker_main_event_cb( lv_obj_t * obj, lv_event_t event ) { + switch( event ) { + case( LV_EVENT_CLICKED ): crypto_ticker_main_sync_request(); + break; + } +} + + +void crypto_ticker_main_sync_request( void ) { + if ( xEventGroupGetBits( crypto_ticker_main_event_handle ) & crypto_ticker_main_SYNC_REQUEST ) { + return; + } + else { + xEventGroupSetBits( crypto_ticker_main_event_handle, crypto_ticker_main_SYNC_REQUEST ); + xTaskCreate( crypto_ticker_main_sync_Task, /* Function to implement the task */ + "crypto ticker main sync Task", /* Name of the task */ + 5000, /* Stack size in words */ + NULL, /* Task input parameter */ + 1, /* Priority of the task */ + &_crypto_ticker_main_sync_Task ); /* Task handle. */ + } +} + +void crypto_ticker_main_sync_Task( void * pvParameters ) { + crypto_ticker_config_t *crypto_ticker_config = crypto_ticker_get_config(); + int32_t retval = -1; + + log_i("start crypto ticker main task"); + + vTaskDelay( 250 ); + + if ( xEventGroupGetBits( crypto_ticker_main_event_handle ) & crypto_ticker_main_SYNC_REQUEST ) { + if ( crypto_ticker_config->autosync ) { + retval = crypto_ticker_fetch_statistics( crypto_ticker_config , &crypto_ticker_main_data ); + if ( retval == 200 ) { + time_t now; + struct tm info; + char buf[64]; + + + + time( &now ); + localtime_r( &now, &info ); + strftime( buf, sizeof(buf), "updated: %d.%b %H:%M", &info ); + lv_label_set_text( crypto_ticker_main_update_label, buf ); + lv_obj_invalidate( lv_scr_act() ); + } + } + } + xEventGroupClearBits( crypto_ticker_main_event_handle, crypto_ticker_main_SYNC_REQUEST ); + vTaskDelete( NULL ); } \ No newline at end of file diff --git a/src/app/crypto_ticker/crypto_ticker_main.h b/src/app/crypto_ticker/crypto_ticker_main.h index 4121936..40b6047 100644 --- a/src/app/crypto_ticker/crypto_ticker_main.h +++ b/src/app/crypto_ticker/crypto_ticker_main.h @@ -24,6 +24,17 @@ #include + #define crypto_ticker_main_SYNC_REQUEST _BV(0) + + typedef struct { + bool valide = false; + time_t timestamp = 0; + char lastPrice[50] = ""; + char priceChangePercent[50] = ""; + char volume[50] = ""; + } crypto_ticker_main_data_t; + void crypto_ticker_main_setup( uint32_t tile_num ); + void crypto_ticker_main_sync_request( void ); #endif // _crypto_ticker_MAIN_H \ No newline at end of file diff --git a/src/app/crypto_ticker/crypto_ticker_setup.cpp b/src/app/crypto_ticker/crypto_ticker_setup.cpp index 245f579..72501ff 100644 --- a/src/app/crypto_ticker/crypto_ticker_setup.cpp +++ b/src/app/crypto_ticker/crypto_ticker_setup.cpp @@ -87,7 +87,7 @@ void crypto_ticker_setup_setup( uint32_t tile_num ) { lv_textarea_set_pwd_mode( crypto_ticker_symbol_textfield, false); lv_textarea_set_one_line( crypto_ticker_symbol_textfield, true); lv_textarea_set_cursor_hidden( crypto_ticker_symbol_textfield, true); - lv_obj_set_width( crypto_ticker_symbol_textfield, LV_HOR_RES /4 * 3 ); + lv_obj_set_width( crypto_ticker_symbol_textfield, LV_HOR_RES /4 * 2 ); lv_obj_align( crypto_ticker_symbol_textfield, crypto_ticker_symbol_cont, LV_ALIGN_IN_RIGHT_MID, -5, 0 ); lv_obj_set_event_cb( crypto_ticker_symbol_textfield, crypto_ticker_textarea_event_cb ); diff --git a/src/app/crypto_ticker/crypto_ticker_widget.cpp b/src/app/crypto_ticker/crypto_ticker_widget.cpp index 0750877..9c202f0 100644 --- a/src/app/crypto_ticker/crypto_ticker_widget.cpp +++ b/src/app/crypto_ticker/crypto_ticker_widget.cpp @@ -165,7 +165,7 @@ void crypto_ticker_widget_sync_Task( void * pvParameters ) { vTaskDelay( 250 ); if ( xEventGroupGetBits( crypto_ticker_widget_event_handle ) & crypto_ticker_widget_SYNC_REQUEST ) { - uint32_t retval = crypto_ticker_fetch_today(crypto_ticker_get_config() , &crypto_ticker_widget_data ); + uint32_t retval = crypto_ticker_fetch_price(crypto_ticker_get_config() , &crypto_ticker_widget_data ); if ( retval == 200 ) { lv_img_set_src( crypto_ticker_widget_icon_info, &info_ok_16px );