add dev mode support

This commit is contained in:
2024-12-04 21:38:21 +01:00
parent 4c19d757c6
commit 4a8e0188b3
9 changed files with 2739 additions and 32 deletions

View File

@@ -1,128 +0,0 @@
<html>
<body>
<input type="button" id="test" value="Test">
<h2>Current Firmware</h2>
<div>
<div id="firmware_buildtime">Buildtime loading</div>
<div id="firmware_githash">Build githash loading</div>
</div>
<h2>Time</h2>
<div>
<div id="esp_time">Esp time</div>
<div id="rtc_time">Rtc time</div>
<div id="browser_time">Rtc time</div>
<div>Store Browser time into esp and rtc</div>
<input type="button" id="time_upload" value="write">
</div>
<h2>firmeware OTA v3</h2>
<form id="upload_form" method="post">
<input type="file" name="file1" id="file1"><br>
<progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
<h3 id="status"></h3>
<h3 id="answer"></h3>
<p id="loaded_n_total"></p>
</form>
<div>
<h2>WIFI</h2>
<input type="button" id="scan" value="Scan">
<br>
<label for="ap_ssid">AP SSID:</label>
<input type="text" id="ap_ssid" list="ssidlist">
<label for="ssid">SSID:</label>
<input type="text" id="ssid" list="ssidlist">
<datalist id="ssidlist">
<option value="Not scanned yet">
</datalist>
<label for="ssid">Password:</label>
<input type="text" id="password">
</div>
<h2>config</h2>
<div id="configform">
<h3>Mqtt:</h3>
<div>
<input type="text" id="mqtt_url">
MQTT Url
</div>
<div>
<input type="text" id="base_topic">
Base Topic
</div>
<h3>Tank:</h3>
<div>
<input type="checkbox" id="tank_sensor_enabled">
Enable Tank Sensor
</div>
<div>
<input type="checkbox" id="tank_allow_pumping_if_sensor_error">
Allow Pumping if Sensor Error
</div>
<div>
<input type="number" min="2" max="500000" id="tank_useable_ml">
Tank Size mL
</div>
<div>
<input type="number" min="1" max="500000" id="tank_warn_percent">
Tank Warn Percent (mapped in relation to empty and full)
</div>
<div>
<input type="number" min="0" max="100" id="tank_empty_percent">
Tank Empty Percent (% max move)
</div>
<div>
<input type="number" min="0" max="100" id="tank_full_percent">
Tank Full Percent (% max move)
</div>
<h3>Light:</h3>
<div>
Start
<select type="time" id="night_lamp_time_start">
</select>
Stop
<select type="time" id="night_lamp_time_end">
</select>
</div>
<div>
<input type="checkbox" id="night_lamp_only_when_dark">
Light only when dark
</div>
<h2>Battery Firmeware (bq34z100 may be R2)</h2>
<form id="upload_form" method="post">
<input type="file" name="battery_flash_file" id="battery_flash_file"><br>
<progress id="battery_flash_progressBar" value="0" max="100" style="width:300px;"></progress>
<input type="button" name="battery_flash_button" id="battery_flash_button"><br>
<h3 id="battery_flash_status"></h3>
<p id="battery_flash_loaded_n_total"></p>
<div style="height: 100px; display: block; overflow-y: auto;" id = "battery_flash_message"></div>
</form>
<h3>Plants:</h3>
<div>
<input type="number" min="2" max="100" id="max_consecutive_pump_count">
Max consecutive pump count:
</div>
<div id="plants"></div>
</div>
<button id="submit">Submit</button>
<div id="submit_status"></div>
<br>
<textarea id="json" cols=50 rows=10></textarea>
<script src="bundle.js"></script>
</body>
</html>

View File

@@ -294,7 +294,9 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
let file_handle = BOARD_ACCESS.lock().unwrap().get_file_handle(&filename, false);
match file_handle {
Ok(mut file_handle) => {
let mut response = request.into_ok_response()?;
let headers = [("Access-Control-Allow-Origin","*")];
let mut response = request.into_response(200, None, &headers)?;
const BUFFER_SIZE: usize = 512;
let mut buffer: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
let mut total_read: usize = 0;
@@ -317,9 +319,7 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
//todo set headers here for filename to be error
let error_text = err.to_string();
println!("error handling get file {}", error_text);
request
.into_status_response(500)?
.write(error_text.as_bytes())?;
cors_response(request, 500, &error_text)?;
}
}
anyhow::Ok(())
@@ -345,15 +345,13 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
break;
}
}
request.into_ok_response().unwrap().write(format!("saved {total_read} bytes").as_bytes()).unwrap();
cors_response(request, 200, &format!("saved {total_read} bytes"))?;
},
Err(err) => {
//todo set headers here for filename to be error
let error_text = err.to_string();
println!("error handling get file {}", error_text);
request
.into_status_response(500)?
.write(error_text.as_bytes())?;
cors_response(request, 500, &error_text)?;
}
}
anyhow::Ok(())
@@ -368,11 +366,11 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
match board.delete_file(&filename) {
Ok(_) => {
let info = format!("Deleted file {copy}");
request.into_ok_response().unwrap().write(info.as_bytes()).unwrap();
cors_response(request, 200, &info)?;
},
Err(err) => {
let info = format!("Could not delete file {copy} {err:?}");
request.into_status_response(400).unwrap().write(info.as_bytes()).unwrap();
cors_response(request, 400, &info)?;
},
}
anyhow::Ok(())
@@ -450,7 +448,7 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
server
.fn_handler("/", Method::Get, move |request| {
let mut response = request.into_ok_response()?;
response.write(include_bytes!("config.html"))?;let mut buf = [0_u8; 3072];
response.write(include_bytes!("index.html"))?;let mut buf = [0_u8; 3072];
anyhow::Ok(())
})
.unwrap();
@@ -475,6 +473,13 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
server
}
fn cors_response(request: Request<&mut EspHttpConnection>, status: u16, body: &str) -> Result<(), anyhow::Error>{
let headers = [("Access-Control-Allow-Origin","*")];
let mut response = request.into_response(status, None, &headers)?;
response.write(body.as_bytes())?;
response.flush()?;
return anyhow::Ok(());
}
type AnyhowHandler =
fn(&mut Request<&mut EspHttpConnection>) -> Result<Option<std::string::String>, anyhow::Error>;
@@ -486,18 +491,16 @@ fn handle_error_to500(
match error {
Ok(answer) => match answer {
Some(json) => {
let mut response = request.into_ok_response()?;
response.write(json.as_bytes())?;
response.flush()?;
cors_response(request, 200, &json)?;
}
None => {
cors_response(request, 200, "")?;
}
None => {}
},
Err(err) => {
let error_text = err.to_string();
println!("error handling process {}", error_text);
request
.into_status_response(500)?
.write(error_text.as_bytes())?;
cors_response(request, 500, &error_text)?;
}
}
return anyhow::Ok(());