got parameter fetch working beta only

This commit is contained in:
Simon Schmidt 2021-09-06 00:41:11 +02:00
parent b309c0b207
commit 6bd0844927
4 changed files with 81 additions and 28 deletions

View File

@ -52,10 +52,42 @@ function reloadTerminal() {
.catch(error => console.log('Error:', error)); .catch(error => console.log('Error:', error));
}; };
function updateParameters() {
fetch('/parameters', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: ''
})
.then(response => response.json())
.then(json_str => {
const active_pwm = Math.round(json_str.active_pwm / 4095 * 100);
document.querySelector('#range_act_pwm').value = active_pwm;
document.querySelector('#out_act_pwm').value = active_pwm;
const idle_pwm_max = Math.round(json_str.idle_pwm_max / json_str.active_pwm * 100);
document.querySelector('#range_idl_pwm').value = idle_pwm_max;
document.querySelector('#out_idl_pwm').value = idle_pwm_max;
document.querySelector('#range_tim_sta').value = json_str.time_per_stair;
document.querySelector('#out_tim_sta').value = json_str.time_per_stair;
document.querySelector('#range_tim_ldr').value = json_str.time_ldr;
document.querySelector('#out_tim_ldr').value = json_str.time_ldr;
document.querySelector('#range_ldr_shw').value = json_str.ldr_schwelle;
document.querySelector('#out_ldr_shw').value = json_str.ldr_schwelle;
})
.catch(error => console.log('Error:', error));
};
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
setInterval(reloadTerminal, 1000); setInterval(reloadTerminal, 1000);
setInterval(reloadRangeValues, 1000); setInterval(reloadRangeValues, 1000);
updateParameters();
// use data- attributes for action // use data- attributes for action
document.querySelectorAll('.control').forEach((button) => { document.querySelectorAll('.control').forEach((button) => {

View File

@ -94,6 +94,14 @@ void HTTPServer::start_apps() {
send(202, "text/plain", ""); send(202, "text/plain", "");
} }
}); });
this->on("/parameters", HTTP_POST, [this]() {
logt("got /parameters\n");
char json_str[255];
treppe->param_to_json(json_str, 255);
logt("%s\n", json_str);
send(200, "application/json", json_str);
});
} }
template <class... Args> template <class... Args>

View File

@ -177,7 +177,8 @@ float Treppe::read_ldr() {
float ldr_ohm = 40.57 * (3.3 - voltage) / voltage; float ldr_ohm = 40.57 * (3.3 - voltage) / voltage;
float ldr_value = 6526.6 / (ldr_ohm * ldr_ohm); float ldr_value = 6526.6 / (ldr_ohm * ldr_ohm);
#ifdef LDRDEBUG #ifdef LDRDEBUG
Serial.printf("Ohm: %f lux: %f Comp: %d\n", ldr_ohm,ldr_value, param.ldr_schwelle); Serial.printf("Ohm: %f lux: %f Comp: %d\n", ldr_ohm, ldr_value,
param.ldr_schwelle);
#endif #endif
return ldr_value; return ldr_value;
} }
@ -271,7 +272,6 @@ void Treppe::task() {
if (!fsm_pend.anim_beendet) { if (!fsm_pend.anim_beendet) {
fsm_pend.anim_beendet = dimmer_tick(&dimmer_ldr, DIM_LDR); fsm_pend.anim_beendet = dimmer_tick(&dimmer_ldr, DIM_LDR);
} }
} }
if (fsm_outputs.status == ST_RUHEZUSTAND || if (fsm_outputs.status == ST_RUHEZUSTAND ||
@ -388,3 +388,14 @@ void Treppe::set_ldr_schwelle(const uint16_t value,
Serial.printf("Treppe: ldr_schwelle=%d\n", param_pend.ldr_schwelle); Serial.printf("Treppe: ldr_schwelle=%d\n", param_pend.ldr_schwelle);
} }
void Treppe::param_to_json(char *json_str, size_t sz) {
snprintf(json_str, sz, "{\n\
\"time_ldr\": %d,\n\
\"time_per_stair\": %d,\n\
\"idle_pwm_max\": %d,\n\
\"active_pwm\": %d,\n\
\"ldr_schwelle\": %d\n}\n",
param.time_ldr, param.time_per_stair, param.idle_pwm_max,
param.active_pwm, param.ldr_schwelle);
}

View File

@ -105,6 +105,8 @@ public:
// Parameter section // Parameter section
void save_param_to_eeprom(); void save_param_to_eeprom();
void param_to_json(char* json_str, size_t sz);
void set_idle_pwm_max(const uint16_t value, const vorgabe_typ_t vorgabe_typ); void set_idle_pwm_max(const uint16_t value, const vorgabe_typ_t vorgabe_typ);
void set_active_pwm(const uint16_t value, const vorgabe_typ_t vorgabe_typ); void set_active_pwm(const uint16_t value, const vorgabe_typ_t vorgabe_typ);