#include "ChronoWebServer.h" #include #include #include #include #include #include #include "Display.h" const char *ssid = "routeguy"; const char *password = "wrangleR621!"; WebServer _server(80); void (*testModeCallback)(); // Constructor implementation ChronoWebServer::ChronoWebServer() {} void ChronoWebServer::_handleRoot() { String temp="\ \ FX Chronograph Display Settings\ \ \ \ \

ChronoDisplay Settings

\
\

Control Panel

\ \ \ \ \ \
\
\

OTA Firmware Update

\ \

Select a .bin firmware file to update over-the-air

\
\
\

Gun Profiles

\ \ "; ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); String currentGunName=chronoReadings.getProfile().getGunProfile().getName(); int currentGunPowerLevel=chronoReadings.getProfile().getGunProfile().getPowerLevel(); LinkedList& gprofiles= chronoReadings.getGunProfiles(); for(int i=0;i"; } else{ temp +=""; } temp +=""; } temp +="
NameType
"+name+""+type+"
\ \
\
\

Projectile Profiles

\ \ "; String currentProjectileProfileName=chronoReadings.getProfile().getProjectileProfile().getName(); float currentProjectileProfileWeight=chronoReadings.getProfile().getProjectileProfile().getWeight(); LinkedList& profiles= chronoReadings.getProjectileProfiles(); for(int i=0;i"; } else{ temp +=""; } temp +=""; } temp +="
NameTypeCaliberWeight
"+name+""+type+""+cal+""+weight+"
\ \
\ \ "; _server.send(200, "text/html", temp); } void ChronoWebServer::_handleGunProfile(){ String temp="\ \ Gun Profile\ \ \ \ \

Add Gun Profile

\
\

Name

\ \

Type

\ \
\
\ \ "; _server.send(200, "text/html", temp); } void ChronoWebServer::_handleAddGunProfile(){ if (_server.method() == HTTP_POST){ String name="Default"; GunProfile::GunProfileType type=GunProfile::PROFILE_TYPE_AIR_GUN_FAC; float caliber=0.0; float weight=0.0; if (_server.hasArg("name")){name=_server.arg("name");} if (_server.hasArg("type")){ String st=_server.arg("type"); if (strcmp(st.c_str(),"0")==0){type=GunProfile::PROFILE_TYPE_BOW_AIRSOFT;} else if (strcmp(st.c_str(),"1")==0){type=GunProfile::PROFILE_TYPE_CO2_PISTOL;} else if (strcmp(st.c_str(),"2")==0){type=GunProfile::PROFILE_TYPE_AIR_PISTOL;} else if (strcmp(st.c_str(),"3")==0){type=GunProfile::PROFILE_TYPE_AIR_GUN_UK;} else if (strcmp(st.c_str(),"4")==0){type=GunProfile::PROFILE_TYPE_AIR_GUN_FAC;} } ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); GunProfile p(name,type); chronoReadings.addGunProfile(p); } _server.send(200, "text/html", "ok"); } void ChronoWebServer::_handleSetGunProfile(){ if (_server.method() == HTTP_POST){ if (_server.hasArg("id")){ int id=_server.arg("id").toInt(); ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); chronoReadings.setGunProfile(id); //update profile with chronograph int type=chronoReadings.getProfile().getGunProfile().getPowerLevel(); ChronoBLE::changeGunType(type); //update display Display::updateProfile(chronoReadings.getProfile()); Display::refreshDisplay(); } } _server.send(200, "text/html", "ok"); } void ChronoWebServer::_handleRemoveGunProfile(){ if (_server.method() == HTTP_POST){ if (_server.hasArg("id")){ int id=_server.arg("id").toInt(); ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); chronoReadings.removeGunProfile(id); } } _server.send(200, "text/html", "ok"); } void ChronoWebServer::_handleProjectileProfile(){ String temp="\ \ Projectile Profile\ \ \ \ \

Add Projectile Profile

\
\

Name

\ \

Type

\ \

Caliber

\ \

Weight

\ \
\
\ \ "; _server.send(200, "text/html", temp); } void ChronoWebServer::_handleSetProjectileProfile(){ if (_server.method() == HTTP_POST){ if (_server.hasArg("id")){ int id=_server.arg("id").toInt(); ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); chronoReadings.setProjectileProfile(id); Display::updateProfile(chronoReadings.getProfile()); Display::refreshDisplay(); } } _server.send(200, "text/html", "ok"); } void ChronoWebServer::_handleAddProjectileProfile(){ String message = "Got request...\n\n"; message += "URI: "; message += _server.uri(); message += "\nMethod: "; message += (_server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += _server.args(); message += "\n"; for (uint8_t i = 0; i < _server.args(); i++) { message += " " + _server.argName(i) + ": " + _server.arg(i) + "\n"; } Serial.println(message); if (_server.method() == HTTP_POST){ String name=""; ProjectileProfile::ProjectileType type=ProjectileProfile::PELLET; float caliber=0.0; float weight=0.0; if (_server.hasArg("name")){name=_server.arg("name");} if (_server.hasArg("type")){ String st=_server.arg("type"); if (strcmp(st.c_str(),"slug")==0){ type=ProjectileProfile::SLUG; } } if (_server.hasArg("cal")){caliber=_server.arg("cal").toFloat();} if (_server.hasArg("weight")){weight=_server.arg("weight").toFloat();} ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); ProjectileProfile pp(name,type,caliber,weight); chronoReadings.addProjectileProfile(pp); } _server.send(200, "text/html", "ok"); } void ChronoWebServer::_handleRemoveProjectileProfile(){ if (_server.method() == HTTP_POST){ if (_server.hasArg("id")){ int id=_server.arg("id").toInt(); ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); chronoReadings.removeProjectileProfile(id); } } _server.send(200, "text/html", "ok"); } void ChronoWebServer::_handleExport(){ ChronoReadingManager& chronoReadings = ChronoReadingManager::getInstance(); LinkedList& readings=chronoReadings.getChronographReadings(); int count=readings.size(); Serial.print("exporting "); Serial.print(count); Serial.println(" readings to csv..."); //return csv data String csvData = "Shot #,FPS,FPE,Gun,Power,Projectile,Type,Caliber,Weight\n"; for (int i=0; iOTA Update Successful!
Restarting..."); delay(1000); ESP.restart(); } else { Serial.println("OTA: Error: Update end failed"); _server.send(500, "text/plain", "Update failed"); } } } } void ChronoWebServer::init(){ WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp32")) { Serial.println("MDNS responder started"); } _server.on("/gunprofile/add", _handleAddGunProfile); _server.on("/gunprofile/set", _handleSetGunProfile); _server.on("/gunprofile/remove", _handleRemoveGunProfile); _server.on("/gunprofile", _handleGunProfile); _server.on("/projectileprofile/set", _handleSetProjectileProfile); _server.on("/projectileprofile/add", _handleAddProjectileProfile); _server.on("/projectileprofile/remove", _handleRemoveProjectileProfile); _server.on("/projectileprofile", _handleProjectileProfile); _server.on("/export", _handleExport); _server.on("/reset", _handleReset); _server.on("/format", _handleFormat); _server.on("/testmode", _handleTestMode); _server.on("/ota", _handleOta); _server.on("/", _handleRoot); _server.begin(); } void ChronoWebServer::process(){ _server.handleClient(); }