move to typescript, testmode, shiftregister revival
This commit is contained in:
1540
rust/src_webpack/package-lock.json
generated
Normal file
1540
rust/src_webpack/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
rust/src_webpack/package.json
Normal file
11
rust/src_webpack/package.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"devDependencies": {
|
||||
"ts-loader": "^9.5.1",
|
||||
"typescript": "^5.3.3",
|
||||
"webpack": "^5.89.0",
|
||||
"webpack-cli": "^5.1.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"source-map-loader": "^4.0.1"
|
||||
}
|
||||
}
|
175
rust/src_webpack/src/form.ts
Normal file
175
rust/src_webpack/src/form.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
interface PlantConfig {
|
||||
tank_sensor_enabled:boolean,
|
||||
tank_full_ml:number,
|
||||
tank_warn_percent: number,
|
||||
night_lamp_time_start: string,
|
||||
night_lamp_time_end: string,
|
||||
night_lamp_only_when_dark: boolean,
|
||||
plants: {
|
||||
target_moisture: number,
|
||||
pump_time_s: number,
|
||||
pump_cooldown_min: number,
|
||||
pump_hour_start: string,
|
||||
pump_hour_end: string
|
||||
}[]
|
||||
}
|
||||
|
||||
let plants = document.getElementById("plants") as HTMLInputElement;
|
||||
|
||||
let fromWrapper = (() => {
|
||||
|
||||
let plantcount = 0;
|
||||
|
||||
|
||||
let tank_full_ml = document.getElementById("tank_full_ml") as HTMLInputElement;
|
||||
tank_full_ml.onchange = submitForm
|
||||
let tank_warn_percent = document.getElementById("tank_warn_percent") as HTMLInputElement;
|
||||
tank_warn_percent.onchange = submitForm
|
||||
let tank_sensor_enabled = document.getElementById("tank_sensor_enabled") as HTMLInputElement;
|
||||
tank_sensor_enabled.onchange = submitForm
|
||||
let night_lamp_only_when_dark = document.getElementById("night_lamp_only_when_dark") as HTMLInputElement;
|
||||
night_lamp_only_when_dark.onchange = submitForm
|
||||
let night_lamp_time_start = document.getElementById("night_lamp_time_start") as HTMLInputElement;
|
||||
night_lamp_time_start.onchange = submitForm
|
||||
let night_lamp_time_end = document.getElementById("night_lamp_time_end") as HTMLInputElement;
|
||||
night_lamp_time_end.onchange = submitForm
|
||||
|
||||
let json = document.getElementById('json') as HTMLInputElement
|
||||
|
||||
function createForm(){
|
||||
var current:PlantConfig = {
|
||||
tank_sensor_enabled:true,
|
||||
tank_full_ml:400,
|
||||
tank_warn_percent:50,
|
||||
night_lamp_time_start : "18:00",
|
||||
night_lamp_time_end : "02:00",
|
||||
night_lamp_only_when_dark: true,
|
||||
plants :[
|
||||
{
|
||||
target_moisture: 40,
|
||||
pump_time_s: 60,
|
||||
pump_cooldown_min: 60,
|
||||
pump_hour_start: "10:00",
|
||||
pump_hour_end: "18:00"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
for(let i=0;i<current.plants.length;i++){
|
||||
var plant = document.createElement("div");
|
||||
plants.appendChild(plant);
|
||||
var header = document.createElement("h4");
|
||||
header.textContent = "Plant " + (i+1);
|
||||
plant.appendChild(header);
|
||||
|
||||
{
|
||||
var holder = document.createElement("div");
|
||||
plant.appendChild(holder);
|
||||
var inputf = document.createElement("input");
|
||||
inputf.id = "plant_"+i+"_target_moisture";
|
||||
inputf.onchange = submitForm;
|
||||
inputf.type = "number";
|
||||
inputf.min = "0";
|
||||
inputf.max = "100";
|
||||
holder.appendChild(inputf)
|
||||
|
||||
var text = document.createElement("span");
|
||||
holder.appendChild(text)
|
||||
text.innerHTML += "Target Moisture"
|
||||
}
|
||||
{
|
||||
var holder = document.createElement("div");
|
||||
plant.appendChild(holder);
|
||||
var input = document.createElement("input");
|
||||
input.id = "plant_"+i+"_pump_time_s";
|
||||
input.onchange = submitForm;
|
||||
input.type = "number";
|
||||
input.min = "0";
|
||||
input.max = "600";
|
||||
holder.appendChild(input)
|
||||
|
||||
var text = document.createElement("span");
|
||||
holder.appendChild(text)
|
||||
text.innerHTML += "Pump Time (s)"
|
||||
}
|
||||
}
|
||||
sync(current);
|
||||
}
|
||||
|
||||
function sync(current:PlantConfig){
|
||||
plantcount = current.plants.length
|
||||
tank_full_ml.disabled = !current.tank_sensor_enabled;
|
||||
tank_warn_percent.disabled = !current.tank_sensor_enabled;
|
||||
|
||||
tank_sensor_enabled.checked = current.tank_sensor_enabled;
|
||||
tank_full_ml.value = current.tank_full_ml.toString();
|
||||
tank_warn_percent.value = current.tank_warn_percent.toString();
|
||||
night_lamp_time_start.value = current.night_lamp_time_start;
|
||||
night_lamp_time_end.value = current.night_lamp_time_end;
|
||||
|
||||
for(let i=0;i<current.plants.length;i++){
|
||||
let plant_target_moisture = document.getElementById("plant_"+i+"_target_moisture") as HTMLInputElement;
|
||||
plant_target_moisture.value = current.plants[i].target_moisture.toString();
|
||||
let plant_pump_time_s = document.getElementById("plant_"+i+"_pump_time_s") as HTMLInputElement;
|
||||
plant_pump_time_s.value = current.plants[i].pump_time_s.toString();
|
||||
let plant_pump_cooldown_min = document.getElementById("plant_"+i+"_pump_cooldown_min") as HTMLInputElement;
|
||||
plant_pump_cooldown_min.value = current.plants[i].pump_cooldown_min.toString();
|
||||
let plant_pump_hour_start = document.getElementById("plant_"+i+"_pump_hour_start") as HTMLInputElement;
|
||||
plant_pump_hour_start.value = current.plants[i].pump_hour_start;
|
||||
let plant_pump_hour_end = document.getElementById("plant_"+i+"_pump_hour_end") as HTMLInputElement;
|
||||
plant_pump_hour_end.value = current.plants[i].pump_hour_end;
|
||||
}
|
||||
}
|
||||
|
||||
function submitForm() {
|
||||
var current: PlantConfig = {
|
||||
tank_sensor_enabled: tank_sensor_enabled.checked,
|
||||
tank_full_ml: +tank_full_ml.value,
|
||||
tank_warn_percent: +tank_warn_percent.value,
|
||||
night_lamp_time_start: night_lamp_time_start.value,
|
||||
night_lamp_time_end: night_lamp_time_end.value,
|
||||
night_lamp_only_when_dark: night_lamp_only_when_dark.checked,
|
||||
plants: []
|
||||
}
|
||||
|
||||
for(let i=0;i<plantcount;i++){
|
||||
console.log("Adding plant " + i)
|
||||
let plant_target_moisture = document.getElementById("plant_"+i+"_target_moisture") as HTMLInputElement;
|
||||
let plant_pump_time_s = document.getElementById("plant_"+i+"_pump_time_s") as HTMLInputElement;
|
||||
let plant_pump_cooldown_min = document.getElementById("plant_"+i+"_pump_cooldown_min") as HTMLInputElement;
|
||||
let plant_pump_hour_start = document.getElementById("plant_"+i+"_pump_hour_start") as HTMLInputElement;
|
||||
let plant_pump_hour_end = document.getElementById("plant_"+i+"_pump_hour_end") as HTMLInputElement;
|
||||
|
||||
current.plants[i] = {
|
||||
target_moisture : +plant_target_moisture.value,
|
||||
pump_time_s: +plant_pump_time_s.value,
|
||||
pump_cooldown_min: +plant_pump_cooldown_min.value,
|
||||
pump_hour_start: plant_pump_hour_start.value,
|
||||
pump_hour_end: plant_pump_hour_end.value
|
||||
|
||||
}
|
||||
}
|
||||
sync(current);
|
||||
console.log(current);
|
||||
|
||||
var pretty = JSON.stringify(current, undefined, 4);
|
||||
json.value = pretty;
|
||||
}
|
||||
|
||||
let createDocumentBtn = document.getElementById("create") as HTMLButtonElement
|
||||
if(createDocumentBtn){
|
||||
createDocumentBtn.onclick = createForm;
|
||||
}
|
||||
let submitFormBtn = document.getElementById("submit") as HTMLButtonElement
|
||||
if(submitFormBtn){
|
||||
submitFormBtn.onclick = submitForm;
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
if(plants){
|
||||
fromWrapper()
|
||||
}
|
||||
|
37
rust/src_webpack/src/ota.ts
Normal file
37
rust/src_webpack/src/ota.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
export function uploadFile() {
|
||||
var file1 = document.getElementById("file1") as HTMLInputElement;
|
||||
var loaded_n_total = document.getElementById("loaded_n_total");
|
||||
var progressBar = document.getElementById("progressBar") as HTMLProgressElement;
|
||||
var status = document.getElementById("status");
|
||||
var answer = document.getElementById("answer");
|
||||
|
||||
var file = file1.files[0];
|
||||
var ajax = new XMLHttpRequest();
|
||||
|
||||
|
||||
ajax.upload.addEventListener("progress", event => {
|
||||
loaded_n_total.innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
|
||||
var percent = (event.loaded / event.total) * 100;
|
||||
progressBar.value = Math.round(percent);
|
||||
status.innerHTML = Math.round(percent) + "%";
|
||||
answer.innerHTML = "in progress";
|
||||
}, false);
|
||||
ajax.addEventListener("load", () => {
|
||||
status.innerHTML = ajax.responseText;
|
||||
answer.innerHTML = "finished";
|
||||
progressBar.value = 0;
|
||||
}, false);
|
||||
ajax.addEventListener("error", () => {
|
||||
status.innerHTML = ajax.responseText;
|
||||
answer.innerHTML = "failed";
|
||||
}, false);
|
||||
ajax.addEventListener("abort", () => {
|
||||
status.innerHTML = ajax.responseText;
|
||||
answer.innerHTML = "aborted";
|
||||
}, false);
|
||||
ajax.open("POST", "/ota");
|
||||
ajax.send(file);
|
||||
}
|
||||
|
||||
let file1Upload = document.getElementById("file1") as HTMLInputElement;
|
||||
file1Upload.onchange = uploadFile;
|
93
rust/src_webpack/src/wifi.ts
Normal file
93
rust/src_webpack/src/wifi.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
interface WifiConfig {
|
||||
ssid: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface SSIDList {
|
||||
ssids : [string]
|
||||
}
|
||||
|
||||
export function saveWifi(){
|
||||
var saveButton = (document.getElementById("save") as HTMLButtonElement);
|
||||
saveButton.disabled = true;
|
||||
var ssid = (document.getElementById("ssid") as HTMLInputElement).value
|
||||
var password = (document.getElementById("password") as HTMLInputElement).value
|
||||
|
||||
var wifistatus = document.getElementById("wifistatus")
|
||||
|
||||
var wificonfig:WifiConfig = {ssid, password}
|
||||
var pretty = JSON.stringify(wificonfig, undefined, 4);
|
||||
console.log("Sending config " + pretty)
|
||||
|
||||
var ajax = new XMLHttpRequest();
|
||||
ajax.onreadystatechange = () => {
|
||||
wifistatus.innerText = ajax.responseText
|
||||
};
|
||||
ajax.onerror = (evt) => {
|
||||
console.log(evt)
|
||||
wifistatus.innerText = ajax.responseText
|
||||
saveButton.disabled = false;
|
||||
alert("Failed to save config see console")
|
||||
}
|
||||
ajax.open("POST", "/wifisave");
|
||||
ajax.send();
|
||||
}
|
||||
|
||||
export function scanWifi(){
|
||||
var scanButton = (document.getElementById("scan") as HTMLButtonElement);
|
||||
scanButton.disabled = true;
|
||||
|
||||
var ajax = new XMLHttpRequest();
|
||||
ajax.responseType = 'json';
|
||||
ajax.onreadystatechange = () => {
|
||||
if (ajax.readyState === 4) {
|
||||
callback(ajax.response);
|
||||
}
|
||||
};
|
||||
ajax.onerror = (evt) => {
|
||||
console.log(evt)
|
||||
scanButton.disabled = false;
|
||||
alert("Failed to start see console")
|
||||
}
|
||||
ajax.open("POST", "/wifiscan");
|
||||
ajax.send();
|
||||
}
|
||||
|
||||
function test(){
|
||||
var testButton = (document.getElementById("test") as HTMLButtonElement);
|
||||
testButton.disabled = true;
|
||||
|
||||
var ajax = new XMLHttpRequest();
|
||||
ajax.responseType = 'json';
|
||||
ajax.onerror = (evt) => {
|
||||
console.log(evt)
|
||||
testButton.disabled = false;
|
||||
alert("Failed to start see console")
|
||||
}
|
||||
ajax.open("POST", "/boardtest");
|
||||
ajax.send();
|
||||
}
|
||||
|
||||
function callback(data:SSIDList){
|
||||
var ssidlist = document.getElementById("ssidlist")
|
||||
ssidlist.innerHTML = ''
|
||||
|
||||
for (var ssid of data.ssids) {
|
||||
var wi = document.createElement("option");
|
||||
wi.value = ssid;
|
||||
ssidlist.appendChild(wi);
|
||||
}
|
||||
}
|
||||
|
||||
let testBtn = document.getElementById("test") as HTMLButtonElement;
|
||||
if(testBtn){
|
||||
testBtn.onclick = test;
|
||||
}
|
||||
let scanWifiBtn = document.getElementById("scan") as HTMLButtonElement;
|
||||
if(scanWifiBtn){
|
||||
scanWifiBtn.onclick = scanWifi;
|
||||
}
|
||||
let saveWifiBtn = document.getElementById("save") as HTMLButtonElement;
|
||||
if(saveWifiBtn){
|
||||
saveWifiBtn.onclick = saveWifi;
|
||||
}
|
13
rust/src_webpack/tsconfig.json
Normal file
13
rust/src_webpack/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist/",
|
||||
"noImplicitAny": true,
|
||||
"module": "es6",
|
||||
"target": "es5",
|
||||
"jsx": "react",
|
||||
"allowJs": true,
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true
|
||||
}
|
||||
}
|
24
rust/src_webpack/webpack.config.js
Normal file
24
rust/src_webpack/webpack.config.js
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
mode: "development",
|
||||
entry: ['./src/form.ts','./src/ota.ts','./src/wifi.ts'],
|
||||
devtool: 'inline-source-map',
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
use: 'ts-loader',
|
||||
exclude: /node_modules/,
|
||||
},
|
||||
]
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.tsx', '.ts', '.js'],
|
||||
},
|
||||
output: {
|
||||
filename: 'bundle.js',
|
||||
path: path.resolve(__dirname, '../src/webserver'),
|
||||
},
|
||||
};
|
Reference in New Issue
Block a user