Обновить меню и OTA BreathMonitor
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <FFat.h>
|
||||
#include <esp_sntp.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <Update.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <Arduino_GFX_Library.h>
|
||||
#include <TouchDrvCSTXXX.hpp>
|
||||
@@ -71,7 +72,7 @@ static constexpr int8_t IMU_TOP_EDGE_Y_SIGN = 1;
|
||||
static int8_t gScreenEdgeOrientation=-1,gScreenEdgeCandidate=-1;
|
||||
static uint32_t gScreenEdgeChanged=0;
|
||||
static bool gExhaleLongDone=false;
|
||||
enum class Screen : uint8_t { Home, Monitor, Settings, Wifi, WifiPassword, Time, ManualTime, Online, Account };
|
||||
enum class Screen : uint8_t { Home, Monitor, Settings, Wifi, WifiPassword, Time, ManualTime, Online, Account, Firmware, FirmwareConfirm };
|
||||
static Screen gScreen = Screen::Home;
|
||||
static bool gPowerReady = false, gAudioReady = false, gTouchDown = false;
|
||||
static uint32_t gLastPowerLong = 0;
|
||||
@@ -88,6 +89,7 @@ static lv_obj_t *gRoot=nullptr, *gState=nullptr, *gPhaseTime=nullptr, *gCycleVal
|
||||
static lv_obj_t *gAverageValue=nullptr, *gElapsedValue=nullptr, *gLimitValue=nullptr;
|
||||
static lv_obj_t *gHistory[5]={}, *gPausedBanner=nullptr;
|
||||
static lv_obj_t *gBatteryText=nullptr, *gBatteryFill=nullptr, *gBolt=nullptr, *gWifiText=nullptr,*gClockText=nullptr,*gRotationMark=nullptr;
|
||||
static lv_obj_t *gTopPhase=nullptr,*gTopPhaseTime=nullptr,*gPauseButton=nullptr;
|
||||
static lv_obj_t *gTimeCurrent=nullptr,*gTimeStatus=nullptr,*gAutoCheck=nullptr,*gTzDropdown=nullptr;
|
||||
static lv_obj_t *gManualPreview=nullptr, *gOnlineStatus=nullptr;
|
||||
static int gTzDraft=60, gManualField=0;
|
||||
@@ -98,7 +100,7 @@ static bool gKbSymbols=false,gKbShift=false,gKbPage=false,gKbShowPassword=false;
|
||||
static String gKbValue="";
|
||||
enum class InputTarget : uint8_t { WifiPass, AccountLogin, AccountPass };
|
||||
static InputTarget gInputTarget=InputTarget::WifiPass;
|
||||
enum class NetOperation : uint8_t { None, Test, Time, Upload, Pair };
|
||||
enum class NetOperation : uint8_t { None, Test, Time, Upload, Pair, ChangeAccount, Firmware };
|
||||
static NetOperation gNetOperation=NetOperation::None;
|
||||
static uint32_t gNetStarted=0;
|
||||
static Screen gOperationReturn=Screen::Wifi;
|
||||
@@ -106,8 +108,11 @@ static void networkStart(NetOperation);
|
||||
static void networkFinish(const String &);
|
||||
static void onlineScreen(lv_event_t * = nullptr);
|
||||
static void accountScreen(lv_event_t * = nullptr);
|
||||
static void accountConfirmScreen(lv_event_t * = nullptr);
|
||||
static void firmwareScreen(lv_event_t * = nullptr);
|
||||
static void networkTick();
|
||||
static String gAccountLogin="", gDeviceToken="", gAccountPassword="";
|
||||
static String gAccountLogin="", gDeviceToken="", gAccountPassword="", gPendingAccountLogin="", gPendingAccountPassword="", gFirmwareTarget="";
|
||||
static bool gChangingAccount=false;
|
||||
static String gDeviceId="";
|
||||
static Preferences gAccountPrefs;
|
||||
static volatile bool gWorkerActive=false, gWorkerDone=false, gWorkerSuccess=false;
|
||||
@@ -123,6 +128,7 @@ static String workerMessage(){char local[128];portENTER_CRITICAL(&gWorkerMux);st
|
||||
// Production HTTPS: this CA trusts Let's Encrypt certificates, including Caddy's default.
|
||||
// If Caddy uses a different issuer, replace this PEM and rebuild the firmware.
|
||||
static const char SERVER_URL[]="https://breathmonitor.shineup.me";
|
||||
static const char FIRMWARE_VERSION[]="1.1";
|
||||
static const char ROOT_CA[] PROGMEM = R"CERT(
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
||||
@@ -388,30 +394,37 @@ static void initAudio() { pinMode(PIN_PA,OUTPUT); digitalWrite(PIN_PA,HIGH); gI2
|
||||
static void beep(uint16_t hz,uint16_t count,int16_t amp) { if(!gAudioReady||amp<=0)return; static int16_t buf[1600*2]; count=min<uint16_t>(count,1600); for(uint16_t i=0;i<count;i++){int16_t v=(int16_t)(sinf(6.2831853f*hz*i/16000.0f)*amp);buf[i*2]=v;buf[i*2+1]=v;} gI2s.write((uint8_t *)buf,count*4); }
|
||||
static void quiet() { beep(880,480,TICK_VOLUME[gTick]); } static void loud(){beep(1200,1600,ALERT_VOLUME[gAlert]);}
|
||||
static void batteryUi() { if(!gBatteryText)return; int p=battery(); char t[8]; snprintf(t,sizeof(t),p<0?"--":"%d%%",p); lv_label_set_text(gBatteryText,t); if(gBatteryFill)lv_obj_set_width(gBatteryFill,p>0?max(2,min(26,p*26/100)):0); if(gBolt)lv_obj_set_style_opa(gBolt,(gPowerReady&&gPower.isCharging())?LV_OPA_COVER:LV_OPA_TRANSP,0); }
|
||||
static lv_obj_t *button(lv_obj_t *,const char *,int,int,int,int,uint32_t,lv_event_cb_t,void * = nullptr);
|
||||
static void togglePause(){
|
||||
if(gPaused){gPaused=false;gBreath=BreathState::Waiting;gRotationArmed=false;gOrientation=-1;gScreenEdgeOrientation=-1;gScreenEdgeCandidate=-1;appendEvent(5);}
|
||||
else {gPaused=true;gBreath=BreathState::Waiting;gCycleStarted=0;gPhaseStarted=0;gRotationArmed=false;gOrientation=-1;gScreenEdgeOrientation=-1;gScreenEdgeCandidate=-1;appendEvent(4);}
|
||||
}
|
||||
static void pauseClicked(lv_event_t *){togglePause();}
|
||||
static void topBar(const char *title) {
|
||||
// On the monitor: Wi-Fi on the left, battery on the right. Other pages: page title.
|
||||
// The main screen shows the clock; Wi-Fi is intentionally not shown because it is off by default.
|
||||
if(gScreen==Screen::Monitor){
|
||||
gRotationMark=label(gRoot,"",&lv_font_montserrat_16,0x38B26D);lv_obj_set_pos(gRotationMark,14,51);
|
||||
gWifiText=label(gRoot,"Wi-Fi: --",&lv_font_montserrat_16,0x9EB4C2);
|
||||
lv_obj_set_pos(gWifiText,39,51);
|
||||
lv_obj_set_width(gWifiText,188);
|
||||
gClockText=label(gRoot,"--:--",&lv_font_montserrat_16,0xFFFFFF);lv_obj_set_pos(gClockText,234,51);
|
||||
lv_label_set_long_mode(gWifiText,LV_LABEL_LONG_DOT);
|
||||
}else{
|
||||
}else if(gScreen==Screen::Settings){
|
||||
gTopPhase=label(gRoot,"WAITING",&lv_font_montserrat_20,0x4FB3FF);lv_obj_set_pos(gTopPhase,24,48);
|
||||
gTopPhaseTime=label(gRoot,"",&lv_font_montserrat_16,0xFFFFFF);lv_obj_set_pos(gTopPhaseTime,142,52);
|
||||
gPauseButton=button(gRoot,gPaused?"RESUME":"PAUSE",255,44,92,31,0x214D69,pauseClicked);
|
||||
}
|
||||
else{
|
||||
lv_obj_t *heading=label(gRoot,title,&lv_font_montserrat_20,0xFFFFFF);
|
||||
lv_obj_set_pos(heading,24,48);
|
||||
}
|
||||
gBatteryText=label(gRoot,"--",&lv_font_montserrat_16,0xC9D3DE);lv_obj_set_pos(gBatteryText,297,51);
|
||||
gBatteryText=label(gRoot,"--",&lv_font_montserrat_16,0xC9D3DE);lv_obj_set_pos(gBatteryText,390,51);
|
||||
if(gPowerReady){static const lv_point_t bolt[]={{6,0},{1,9},{5,9},{2,18},{10,7},{6,7}};gBolt=lv_line_create(gRoot);lv_line_set_points(gBolt,bolt,6);lv_obj_set_pos(gBolt,337,46);lv_obj_set_style_line_width(gBolt,2,0);lv_obj_set_style_line_color(gBolt,color(0xFFD34D),0);lv_obj_set_style_line_rounded(gBolt,true,0);}
|
||||
lv_obj_t *b=lv_obj_create(gRoot);lv_obj_set_pos(b,349,53);lv_obj_set_size(b,32,16);lv_obj_set_style_bg_opa(b,LV_OPA_TRANSP,0);lv_obj_set_style_border_color(b,color(0xC9D3DE),0);lv_obj_set_style_border_width(b,2,0);lv_obj_set_style_radius(b,4,0);lv_obj_clear_flag(b,LV_OBJ_FLAG_SCROLLABLE);
|
||||
lv_obj_t *cap=lv_obj_create(gRoot);lv_obj_set_pos(cap,381,57);lv_obj_set_size(cap,4,8);lv_obj_set_style_bg_color(cap,color(0xC9D3DE),0);lv_obj_set_style_border_width(cap,0,0);lv_obj_set_style_radius(cap,2,0);
|
||||
gBatteryFill=lv_obj_create(gRoot);lv_obj_set_pos(gBatteryFill,352,56);lv_obj_set_size(gBatteryFill,2,10);lv_obj_set_style_bg_color(gBatteryFill,color(0x38B26D),0);lv_obj_set_style_border_width(gBatteryFill,0,0);lv_obj_set_style_radius(gBatteryFill,2,0);batteryUi();
|
||||
}
|
||||
static void clearUi() { lv_obj_clean(lv_scr_act()); gRoot=lv_scr_act(); lv_obj_set_style_bg_color(gRoot,color(0x08111B),0);lv_obj_set_style_bg_opa(gRoot,LV_OPA_COVER,0);lv_obj_set_style_pad_all(gRoot,0,0); gBatteryText=gBatteryFill=gBolt=gWifiText=gWifiList=gPasswordField=gPasswordKeyboard=gOnlineStatus=nullptr;gClockText=gRotationMark=gTimeCurrent=gTimeStatus=gAutoCheck=gTzDropdown=gManualPreview=nullptr; }
|
||||
static void clearUi() { lv_obj_clean(lv_scr_act()); gRoot=lv_scr_act(); lv_obj_set_style_bg_color(gRoot,color(0x08111B),0);lv_obj_set_style_bg_opa(gRoot,LV_OPA_COVER,0);lv_obj_set_style_pad_all(gRoot,0,0); gBatteryText=gBatteryFill=gBolt=gWifiText=gWifiList=gPasswordField=gPasswordKeyboard=gOnlineStatus=nullptr;gClockText=gRotationMark=gTopPhase=gTopPhaseTime=gPauseButton=gTimeCurrent=gTimeStatus=gAutoCheck=gTzDropdown=gManualPreview=nullptr; }
|
||||
static void legend() {
|
||||
lv_obj_t *l=label(gRoot,"INHALE",&lv_font_montserrat_14,0x38B26D);lv_obj_set_pos(l,90,12);
|
||||
lv_obj_t *l=label(gRoot,"EXHALE",&lv_font_montserrat_14,0x4FB3FF);lv_obj_set_pos(l,90,12);
|
||||
l=label(gRoot,"SETTINGS",&lv_font_montserrat_14,0xC9D3DE);lv_obj_align(l,LV_ALIGN_TOP_MID,0,12);
|
||||
l=label(gRoot,"EXHALE",&lv_font_montserrat_14,0x4FB3FF);lv_obj_set_pos(l,338,12);
|
||||
l=label(gRoot,"INHALE",&lv_font_montserrat_14,0x38B26D);lv_obj_set_pos(l,338,12);
|
||||
static const lv_point_t rule[]={{0,0},{432,0}};
|
||||
lv_obj_t *line=lv_line_create(gRoot);lv_line_set_points(line,rule,2);
|
||||
lv_obj_set_pos(line,24,37);lv_obj_set_style_line_width(line,1,0);
|
||||
@@ -438,7 +451,7 @@ static void enterMonitor(lv_event_t *) {
|
||||
lv_obj_set_pos(gHistory[i],78+i*68,3);}
|
||||
}
|
||||
static void home(lv_event_t *); static void settings(lv_event_t *);
|
||||
static lv_obj_t *button(lv_obj_t *p,const char *text,int x,int y,int w,int h,uint32_t bg,lv_event_cb_t cb,void *userData=nullptr) { lv_obj_t *b=lv_btn_create(p);lv_obj_set_pos(b,x,y);lv_obj_set_size(b,w,h);card(b,bg);lv_obj_add_event_cb(b,cb,LV_EVENT_CLICKED,userData);lv_obj_t *l=label(b,text,&lv_font_montserrat_20,0xFFFFFF);lv_obj_center(l);return b; }
|
||||
static lv_obj_t *button(lv_obj_t *p,const char *text,int x,int y,int w,int h,uint32_t bg,lv_event_cb_t cb,void *userData) { lv_obj_t *b=lv_btn_create(p);lv_obj_set_pos(b,x,y);lv_obj_set_size(b,w,h);card(b,bg);lv_obj_add_event_cb(b,cb,LV_EVENT_CLICKED,userData);lv_obj_t *l=label(b,text,&lv_font_montserrat_20,0xFFFFFF);lv_obj_center(l);return b; }
|
||||
static void home(lv_event_t *) {enterMonitor(nullptr);}
|
||||
static void settingChanged(lv_event_t *e){
|
||||
uint8_t *v=(uint8_t *)lv_event_get_user_data(e);
|
||||
@@ -470,10 +483,10 @@ static void settings(lv_event_t *) {
|
||||
lv_obj_t *rot=lv_checkbox_create(gRoot);lv_checkbox_set_text(rot,"ROTATION CONTROL");lv_obj_set_pos(rot,32,437);
|
||||
if(gRotation)lv_obj_add_state(rot,LV_STATE_CHECKED);
|
||||
lv_obj_add_event_cb(rot,[](lv_event_t *e){gRotation=lv_obj_has_state(lv_event_get_target(e),LV_STATE_CHECKED);gRotationArmed=false;gOrientation=-1;gScreenEdgeOrientation=-1;gScreenEdgeCandidate=-1;saveSettings();appendEvent(10,UINT64_MAX,0,0,0,0,gRotation);},LV_EVENT_VALUE_CHANGED,nullptr);
|
||||
button(gRoot,"ONLINE",240,383,110,42,0x214D69,[](lv_event_t *){onlineScreen(nullptr);});
|
||||
button(gRoot,"BACK",358,383,98,42,0x214D69,[](lv_event_t *){
|
||||
if(gPaused){gPaused=false;gBreath=BreathState::Waiting;gRotationArmed=false;gOrientation=-1;gScreenEdgeOrientation=-1;gScreenEdgeCandidate=-1;appendEvent(5);}enterMonitor(nullptr);
|
||||
});
|
||||
button(gRoot,"ONLINE",240,383,100,42,0x214D69,[](lv_event_t *){onlineScreen(nullptr);});
|
||||
button(gRoot,"ACCOUNT",348,383,108,42,0x214D69,[](lv_event_t *){accountScreen(nullptr);});
|
||||
button(gRoot,"FIRMWARE",24,431,120,40,0x214D69,[](lv_event_t *){firmwareScreen(nullptr);});
|
||||
button(gRoot,"BACK",320,431,136,40,0x214D69,[](lv_event_t *){if(gPaused)togglePause();enterMonitor(nullptr);});
|
||||
}
|
||||
// PMU power key can blank the AMOLED independently of our timeout flag.
|
||||
// Explicitly wake the panel for any accepted KEY1 action.
|
||||
@@ -485,18 +498,18 @@ static void wakeDisplayFromKey1() {
|
||||
}
|
||||
static void keyShort(){
|
||||
wakeDisplayFromKey1();
|
||||
if(gScreen==Screen::WifiPassword||gScreen==Screen::Wifi||gScreen==Screen::Time||gScreen==Screen::ManualTime||gScreen==Screen::Online||gScreen==Screen::Account){
|
||||
if(gScreen==Screen::WifiPassword||gScreen==Screen::Wifi||gScreen==Screen::Time||gScreen==Screen::ManualTime||gScreen==Screen::Online||gScreen==Screen::Account||gScreen==Screen::Firmware||gScreen==Screen::FirmwareConfirm){
|
||||
if(!gWorkerActive && (gScreen==Screen::Wifi ||
|
||||
(gScreen==Screen::WifiPassword && gInputTarget==InputTarget::WifiPass)))networkFinish("Wi-Fi off");
|
||||
settings(nullptr);
|
||||
}
|
||||
else if(gScreen==Screen::Settings){if(gPaused){gPaused=false;gBreath=BreathState::Waiting;gRotationArmed=false;gOrientation=-1;gScreenEdgeOrientation=-1;gScreenEdgeCandidate=-1;appendEvent(5);}enterMonitor(nullptr);}
|
||||
else if(gScreen==Screen::Settings){if(gPaused)togglePause();enterMonitor(nullptr);}
|
||||
else settings(nullptr);
|
||||
}
|
||||
static void keyLong(){
|
||||
wakeDisplayFromKey1();
|
||||
if(gWifiEnabled&&!gWorkerActive)networkFinish("Wi-Fi off");
|
||||
if(!gPaused){gPaused=true;gBreath=BreathState::Waiting;gCycleStarted=0;gPhaseStarted=0;gRotationArmed=false;gOrientation=-1;gScreenEdgeOrientation=-1;gScreenEdgeCandidate=-1;appendEvent(4);}
|
||||
if(!gPaused)togglePause();
|
||||
settings(nullptr);
|
||||
}
|
||||
// Key1 is PMU PWRON (AXP2101), not a GPIO. Handle short/long PMU IRQs.
|
||||
@@ -550,16 +563,14 @@ static void poll(Button &b,bool inhale){
|
||||
}
|
||||
}
|
||||
static void refresh(){
|
||||
if(gScreen!=Screen::Monitor)return;
|
||||
uint32_t now=millis();char t[24];
|
||||
if(gRotationMark)lv_label_set_text(gRotationMark,gRotation?"✓":"");
|
||||
if(gClockText){time_t e=time(nullptr);char clockbuf[12]="--:--";if(gTimeValid&&e>1700000000){e+=(int32_t)gTimezoneMinutes*60;tm tmv={};gmtime_r(&e,&tmv);snprintf(clockbuf,sizeof(clockbuf),"%02d:%02d",tmv.tm_hour,tmv.tm_min);}lv_label_set_text(gClockText,clockbuf);}
|
||||
if(gWifiText){
|
||||
String wi = !gWifiEnabled?String("Wi-Fi: off"):(WiFi.status()==WL_CONNECTED ? String("Wi-Fi: ")+WiFi.SSID() : (gWifiConnecting?"Wi-Fi: connecting":"Wi-Fi: scanning"));
|
||||
lv_label_set_text(gWifiText,wi.c_str());
|
||||
lv_obj_set_style_text_color(gWifiText,color(WiFi.status()==WL_CONNECTED?0x38B26D:0x9EB4C2),0);
|
||||
}
|
||||
const char *state=gBreath==BreathState::Waiting?"WAITING":gBreath==BreathState::Inhale?"INHALE":"EXHALE";
|
||||
if(gTopPhase){lv_label_set_text(gTopPhase,state);lv_obj_set_style_text_color(gTopPhase,color(gBreath==BreathState::Inhale?0x38B26D:0x4FB3FF),0);}
|
||||
if(gTopPhaseTime){if(gBreath==BreathState::Waiting)lv_label_set_text(gTopPhaseTime,"");else{fmt(t,sizeof(t),now-gPhaseStarted);lv_label_set_text(gTopPhaseTime,t);}}
|
||||
if(gPauseButton){lv_obj_t *txt=lv_obj_get_child(gPauseButton,0);if(txt)lv_label_set_text(txt,gPaused?"RESUME":"PAUSE");}
|
||||
if(gScreen!=Screen::Monitor)return;
|
||||
lv_label_set_text(gState,state);
|
||||
lv_obj_set_style_text_color(gState,color(gBreath==BreathState::Inhale?0x38B26D:0x4FB3FF),0);
|
||||
if(gBreath==BreathState::Waiting)lv_label_set_text(gPhaseTime,"");
|
||||
@@ -597,7 +608,7 @@ static void refresh(){
|
||||
// LVGL owns all drawing; do not overwrite its tiles through direct GFX writes.
|
||||
}
|
||||
static void reminders(){
|
||||
if(gPaused||gBreath==BreathState::Waiting||gScreen!=Screen::Monitor)return;
|
||||
if(gPaused||gBreath==BreathState::Waiting)return;
|
||||
uint32_t now=millis(),age=now-gPhaseStarted;
|
||||
if(age>=LOUD_START_MS){
|
||||
if(gBreath==BreathState::Inhale&&!gInhaleExceeded){gInhaleExceeded=true;appendEvent(7);}
|
||||
@@ -632,8 +643,9 @@ static void networkFinish(const String &message){
|
||||
gNetOperation=NetOperation::None;gNtpPending=false;
|
||||
wifiEnable(false);gWifiNotice=message;
|
||||
if(op==NetOperation::Time){timeScreen(nullptr);}
|
||||
else if(op==NetOperation::Upload||op==NetOperation::Pair){onlineScreen(nullptr);}
|
||||
else if(op==NetOperation::Upload||op==NetOperation::Pair||op==NetOperation::ChangeAccount){if(op==NetOperation::ChangeAccount)accountScreen(nullptr);else onlineScreen(nullptr);}
|
||||
else if(gScreen==Screen::Wifi){wifiScreen(nullptr);}
|
||||
else if(op==NetOperation::Firmware){firmwareScreen(nullptr);}
|
||||
}
|
||||
static void networkStart(NetOperation op){
|
||||
if(gWorkerActive||gNetOperation!=NetOperation::None)return;
|
||||
@@ -715,7 +727,8 @@ static void wifiTick(){
|
||||
if(WiFi.status()==WL_CONNECTED){
|
||||
if(gNetOperation==NetOperation::Test){networkFinish("Wi-Fi test OK");return;}
|
||||
if(gNetOperation==NetOperation::Time){if(!gNtpPending)requestNtp();return;}
|
||||
if(gNetOperation==NetOperation::Upload||gNetOperation==NetOperation::Pair){
|
||||
if(gNetOperation==NetOperation::Firmware){networkTick();return;}
|
||||
if(gNetOperation==NetOperation::Upload||gNetOperation==NetOperation::Pair||gNetOperation==NetOperation::ChangeAccount){
|
||||
if(!gTimeValid){if(!gNtpPending)requestNtp();return;}
|
||||
networkTick();return;
|
||||
}
|
||||
@@ -804,17 +817,9 @@ static void kbClicked(lv_event_t *e){
|
||||
bool valid=v.length()>=3 && v.length()<=24;
|
||||
for(size_t i=0;i<v.length();i++)if(!((v[i]>='a'&&v[i]<='z')||(v[i]>='0'&&v[i]<='9')||v[i]=='_'||v[i]=='-'))valid=false;
|
||||
if(!valid){gWifiNotice="Login: 3-24 letters/digits/_/-";accountScreen(nullptr);return;}
|
||||
// Journals are bound to one owner. Do not silently upload past records to another account.
|
||||
if(gDeviceToken.length() && v!=gAccountLogin){
|
||||
gWifiNotice="Device linked: account change requires archival/reset";
|
||||
onlineScreen(nullptr);return;
|
||||
}
|
||||
if(v!=gAccountLogin)gAccountPassword="";
|
||||
gAccountLogin=v;gAccountPrefs.putString("login",v);
|
||||
if(gDeviceToken.isEmpty())gAccountPrefs.remove("token");
|
||||
accountScreen(nullptr);return;
|
||||
gPendingAccountLogin=v;gInputTarget=InputTarget::AccountPass;gKbValue="";gKbShowPassword=false;wifiPasswordScreen();return;
|
||||
}
|
||||
if(gInputTarget==InputTarget::AccountPass){gAccountPassword=gKbValue;gDeviceToken="";gAccountPrefs.remove("token");accountScreen(nullptr);return;}
|
||||
if(gInputTarget==InputTarget::AccountPass){gPendingAccountPassword=gKbValue;networkStart(NetOperation::ChangeAccount);accountScreen(nullptr);return;}
|
||||
String ssid=gSelectedSsid,pass=gKbValue;gPendingPassword=pass;
|
||||
wifiConnect(ssid,pass,true);gWifiNotice="Connecting...";wifiScreen(nullptr);return;
|
||||
}
|
||||
@@ -963,13 +968,7 @@ static void onlineScreen(lv_event_t *){
|
||||
if(gDeviceToken.isEmpty()&&(gAccountLogin.isEmpty()||gAccountPassword.isEmpty())){gWifiNotice="Set login + password in ACCOUNT";return;}
|
||||
networkStart(NetOperation::Upload);
|
||||
});
|
||||
button(gRoot,"ACCOUNT",248,300,208,55,0x214D69,[](lv_event_t *){accountScreen(nullptr);});
|
||||
button(gRoot,"REUPLOAD",24,377,208,50,0x553442,[](lv_event_t *){
|
||||
if(gWorkerActive||gNetOperation!=NetOperation::None){gWifiNotice="Upload in progress";return;}
|
||||
if(!gFileReady){gWifiNotice="FFat unavailable";return;}
|
||||
if(FFat.exists("/sync.state")&&!FFat.remove("/sync.state")){gWifiNotice="Cannot reset sync index";return;}
|
||||
gWifiNotice="Sync index reset; tap UPLOAD";
|
||||
});
|
||||
button(gRoot,"SETTINGS",248,300,208,55,0x214D69,[](lv_event_t *){settings(nullptr);});
|
||||
button(gRoot,"BACK",248,377,208,50,0x214D69,[](lv_event_t *){settings(nullptr);});
|
||||
}
|
||||
static void accountScreen(lv_event_t *){
|
||||
@@ -979,14 +978,34 @@ static void accountScreen(lv_event_t *){
|
||||
lv_obj_t *paired=label(gRoot,gDeviceToken.length()?"Paired with server":"Not paired",&lv_font_montserrat_20,gDeviceToken.length()?0x38B26D:0xFFD34D);lv_obj_set_pos(paired,26,156);
|
||||
lv_obj_t *explain=label(gRoot,"Password is kept in RAM only until pairing.\nToken is saved for subsequent uploads.",&lv_font_montserrat_14,0x9EB4C2);
|
||||
lv_obj_set_pos(explain,26,205);lv_obj_set_width(explain,420);
|
||||
button(gRoot,"LOGIN",24,280,208,51,0x214D69,[](lv_event_t *){
|
||||
gInputTarget=InputTarget::AccountLogin;gKbValue=gAccountLogin;gKbShowPassword=false;gKbSymbols=gKbPage=gKbShift=false;wifiPasswordScreen();});
|
||||
button(gRoot,"PASSWORD",248,280,208,51,0x214D69,[](lv_event_t *){
|
||||
gInputTarget=InputTarget::AccountPass;gKbValue="";gKbShowPassword=false;gKbSymbols=gKbPage=gKbShift=false;wifiPasswordScreen();});
|
||||
button(gRoot,"PAIR",24,348,208,51,0x2A9D8F,[](lv_event_t *){
|
||||
if(gAccountLogin.isEmpty()||gAccountPassword.isEmpty()){gWifiNotice="Enter login and password";onlineScreen(nullptr);return;}
|
||||
networkStart(NetOperation::Pair);onlineScreen(nullptr);});
|
||||
button(gRoot,"BACK",248,348,208,51,0x214D69,[](lv_event_t *){onlineScreen(nullptr);});
|
||||
button(gRoot,"CHANGE ACCOUNT",24,292,432,58,0xA56A36,[](lv_event_t *){accountConfirmScreen(nullptr);});
|
||||
button(gRoot,"BACK",140,375,200,52,0x214D69,[](lv_event_t *){settings(nullptr);});
|
||||
}
|
||||
static void accountConfirmScreen(lv_event_t *){
|
||||
gScreen=Screen::Account;clearUi();legend();topBar("CHANGE ACCOUNT");
|
||||
lv_obj_t *warning=label(gRoot,"All local results and saved account data\nwill be deleted after successful login\nwith the new account.",&lv_font_montserrat_18,0xFFD34D);lv_obj_set_pos(warning,24,112);
|
||||
button(gRoot,"CANCEL",24,320,200,55,0x553442,[](lv_event_t *){accountScreen(nullptr);});
|
||||
button(gRoot,"CONTINUE",256,320,200,55,0x2A9D8F,[](lv_event_t *){gChangingAccount=true;gPendingAccountLogin="";gPendingAccountPassword="";gInputTarget=InputTarget::AccountLogin;gKbValue="";gKbShowPassword=false;gKbSymbols=gKbPage=gKbShift=false;wifiPasswordScreen();});
|
||||
}
|
||||
static bool doFirmwareUpdate(WiFiClientSecure &secure){
|
||||
String url=String(SERVER_URL)+"/firmware/breathmonitor-v"+gFirmwareTarget+".bin";HTTPClient http;
|
||||
workerSet(String("Downloading firmware ")+gFirmwareTarget+"...");if(!http.begin(secure,url)){workerSet("OTA HTTPS initialization failed");return false;}
|
||||
http.setTimeout(30000);int status=http.GET();int total=http.getSize();if(status!=200||total<=0){http.end();workerSet(String("OTA download failed: HTTP ")+status);return false;}
|
||||
if(!Update.begin((size_t)total)){http.end();workerSet("OTA storage unavailable");return false;}
|
||||
WiFiClient *stream=http.getStreamPtr();uint8_t buffer[1024];size_t written=0;while(http.connected()&&(total<0||written<(size_t)total)){size_t available=stream->available();if(!available){delay(1);continue;}size_t n=stream->readBytes(buffer,min(available,sizeof(buffer)));if(!Update.write(buffer,n)){Update.abort();http.end();workerSet("OTA write failed");return false;}written+=n;}
|
||||
http.end();if(!Update.end()||!Update.isFinished()){workerSet("OTA finalize failed");return false;}workerSet("Firmware updated; restarting");delay(500);ESP.restart();return true;
|
||||
}
|
||||
static void firmwareConfirmScreen(lv_event_t *){
|
||||
gScreen=Screen::FirmwareConfirm;clearUi();legend();topBar("FIRMWARE UPDATE");String text="Update device to firmware "+gFirmwareTarget+"?";lv_obj_t *t=label(gRoot,text.c_str(),&lv_font_montserrat_20,0xFFFFFF);lv_obj_set_pos(t,24,130);
|
||||
button(gRoot,"CANCEL",24,300,200,55,0x553442,[](lv_event_t *){firmwareScreen(nullptr);});
|
||||
button(gRoot,"UPDATE",256,300,200,55,0x2A9D8F,[](lv_event_t *){networkStart(NetOperation::Firmware);firmwareScreen(nullptr);});
|
||||
}
|
||||
static void firmwareScreen(lv_event_t *){
|
||||
gScreen=Screen::Firmware;clearUi();legend();topBar("FIRMWARE");char line[96];snprintf(line,sizeof(line),"Installed: %s\nStable: 1.1\nTest: 1.2",FIRMWARE_VERSION);lv_obj_t *t=label(gRoot,line,&lv_font_montserrat_20,0xFFFFFF);lv_obj_set_pos(t,28,100);
|
||||
if(gNetOperation==NetOperation::Firmware||gWorkerActive){lv_obj_t *s=label(gRoot,workerMessage().c_str(),&lv_font_montserrat_16,0xFFD34D);lv_obj_set_pos(s,28,220);}
|
||||
button(gRoot,"UPDATE STABLE",24,285,208,55,0x2A9D8F,[](lv_event_t *){gFirmwareTarget="1.1";firmwareConfirmScreen(nullptr);});
|
||||
button(gRoot,"UPDATE TEST",248,285,208,55,0xA56A36,[](lv_event_t *){gFirmwareTarget="1.2";firmwareConfirmScreen(nullptr);});
|
||||
button(gRoot,"BACK",140,375,200,52,0x214D69,[](lv_event_t *){settings(nullptr);});
|
||||
}
|
||||
static void networkUiTick(){
|
||||
if(gScreen!=Screen::Online||!gOnlineStatus)return;
|
||||
@@ -1006,29 +1025,44 @@ static bool markSynced(const String &day,size_t sz){
|
||||
File f=FFat.open("/sync.state","a");if(!f)return false;
|
||||
size_t n=f.printf("%s %u\n",day.c_str(),(unsigned)sz);f.flush();f.close();return n>0;
|
||||
}
|
||||
static bool doPair(WiFiClientSecure &secure){
|
||||
if(gAccountLogin.isEmpty()||gAccountPassword.isEmpty()){workerSet("Enter login and password");return false;}
|
||||
static bool requestToken(WiFiClientSecure &secure,const String &login,const String &password,String &token){
|
||||
if(login.isEmpty()||password.isEmpty()){workerSet("Enter login and password");return false;}
|
||||
workerSet("Authorizing device...");
|
||||
HTTPClient http;String url=String(SERVER_URL)+"/api/device/login";
|
||||
if(!http.begin(secure,url)){workerSet("HTTPS initialization failed");return false;}
|
||||
http.setTimeout(15000);http.addHeader("Content-Type","text/plain; charset=utf-8");
|
||||
String body=gAccountLogin+"\n"+gAccountPassword+"\n"+gDeviceId;
|
||||
String body=login+"\n"+password+"\n"+gDeviceId;
|
||||
int status=http.POST(body);String response=http.getString();http.end();
|
||||
if(status!=200||response.length()!=64){workerSet(String("Login failed: HTTP ")+status);return false;}
|
||||
for(size_t i=0;i<response.length();i++)if(!isxdigit((unsigned char)response[i])){workerSet("Invalid token from server");return false;}
|
||||
gDeviceToken=response;gAccountPrefs.putString("token",gDeviceToken);
|
||||
gAccountPassword="";workerSet("Device paired");return true;
|
||||
token=response;return true;
|
||||
}
|
||||
static bool doPair(WiFiClientSecure &secure){
|
||||
String token;String login=gPendingAccountLogin.length()?gPendingAccountLogin:gAccountLogin;String password=gPendingAccountPassword.length()?gPendingAccountPassword:gAccountPassword;
|
||||
if(!requestToken(secure,login,password,token))return false;
|
||||
gAccountLogin=login;gDeviceToken=token;gAccountPassword="";gAccountPrefs.putString("login",gAccountLogin);gAccountPrefs.putString("token",gDeviceToken);gPendingAccountLogin="";gPendingAccountPassword="";gChangingAccount=false;workerSet("Device paired");return true;
|
||||
}
|
||||
static bool clearLocalAccountData(){
|
||||
if(!gFileReady)return false;
|
||||
FFat.remove("/events.bin");FFat.remove("/migration.done");FFat.remove("/sync.state");
|
||||
File dir=FFat.open("/logs");if(dir){File f=dir.openNextFile();while(f){String path=f.name();f.close();if(!FFat.remove(path))return false;f=dir.openNextFile();}dir.close();}
|
||||
if(!FFat.exists("/logs")&&!FFat.mkdir("/logs"))return false;gEventSeq=0;gCycleCount=0;gCompletedMs=0;return true;
|
||||
}
|
||||
static bool changeAccount(WiFiClientSecure &secure){
|
||||
String token;if(!requestToken(secure,gPendingAccountLogin,gPendingAccountPassword,token))return false;
|
||||
if(!clearLocalAccountData()){workerSet("New account authorized, local reset failed");return false;}
|
||||
gAccountLogin=gPendingAccountLogin;gDeviceToken=token;gAccountPassword="";gAccountPrefs.putString("login",gAccountLogin);gAccountPrefs.putString("token",gDeviceToken);gPendingAccountLogin="";gPendingAccountPassword="";gChangingAccount=false;workerSet("Account changed; local data cleared");return true;
|
||||
}
|
||||
static bool uploadFiles(WiFiClientSecure &secure){
|
||||
if(gDeviceToken.isEmpty()&&!doPair(secure))return false;
|
||||
if(!gFileReady){workerSet("FFat unavailable");return false;}
|
||||
File dir=FFat.open("/logs");if(!dir){workerSet("No /logs directory");return false;}
|
||||
File f=dir.openNextFile();unsigned sent=0,skipped=0;bool ok=true;
|
||||
String today=dayFor(unixMs(),gTimeValid?1:0);File f=dir.openNextFile();unsigned sent=0,skipped=0;bool ok=true;
|
||||
while(f){
|
||||
String fileName=String(f.name());int slash=fileName.lastIndexOf('/');if(slash>=0)fileName=fileName.substring(slash+1);
|
||||
if(!f.isDirectory()&&fileName.endsWith(".bin")){
|
||||
String day=fileName.substring(0,fileName.length()-4);
|
||||
if(day.length()==8||day=="undated"){
|
||||
if((day.length()==8||day=="undated")&&day==today){
|
||||
size_t snapshot=f.size();
|
||||
if(snapshot%sizeof(EventRecord)!=0){workerSet(String("Incomplete journal: ")+day);ok=false;}
|
||||
else if(snapshot==0 || lastSyncedBytes(day)==(int64_t)snapshot){skipped++;}
|
||||
@@ -1062,13 +1096,13 @@ static void networkWorker(void *){
|
||||
// ESP-IDF/WiFiClientSecure uses one TLS stream at a time.
|
||||
WiFiClientSecure secure;secure.setCACert(ROOT_CA);secure.setTimeout(30);
|
||||
NetOperation op=gNetOperation;
|
||||
bool ok=(op==NetOperation::Pair)?doPair(secure):uploadFiles(secure);
|
||||
bool ok=op==NetOperation::Pair?doPair(secure):(op==NetOperation::ChangeAccount?changeAccount(secure):(op==NetOperation::Firmware?doFirmwareUpdate(secure):uploadFiles(secure)));
|
||||
workerSet(workerMessage(),true,ok);gWorkerActive=false;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
static void networkTick(){
|
||||
if(gWorkerActive||gWorkerDone)return;
|
||||
if(gNetOperation!=NetOperation::Upload&&gNetOperation!=NetOperation::Pair)return;
|
||||
if(gNetOperation!=NetOperation::Upload&&gNetOperation!=NetOperation::Pair&&gNetOperation!=NetOperation::ChangeAccount&&gNetOperation!=NetOperation::Firmware)return;
|
||||
gWorkerActive=true;workerSet("Starting secure connection...");
|
||||
BaseType_t started=xTaskCreatePinnedToCore(networkWorker,"breath-sync",16384,nullptr,1,nullptr,0);
|
||||
if(started!=pdPASS){gWorkerActive=false;networkFinish("Cannot start upload task");}
|
||||
|
||||
Reference in New Issue
Block a user