Skip to content

Commit

Permalink
improve js submit function
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasfaust committed Jul 21, 2024
1 parent c52fb00 commit 259afed
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 59 deletions.
81 changes: 35 additions & 46 deletions data/web/Javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,65 +290,57 @@ function isVisible(_obj) {
return ret;
}

/*############################################################
# DataForm: id of FormElement which contains all input fields
# SubmitForm: id of FormElement which contains hidden input filed, named "json", for submit
# jsontype:
# 1 = standard, 1 level
# 2 = array, each item separately
############################################################*/
function onSubmit(DataForm, jsontype){
//set default
if (!jsontype) jsontype = 1;

// init json String
var JsonData;
if (jsontype == 1) { JsonData = {data: {}}; }
else if (jsontype == 2) { JsonData = {data: []}; }
/*******************************
separator:
regex of item ID to identify first element in row
- if set, returned json is an array, all elements per row, example: "^myonoffswitch.*"
- if emty, all elements at one level together, ONLY for small json´s (->memory issue)
*******************************/
function onSubmit(DataForm, separator='') {
// init json Objects
var JsonData, tempData;

var count = 0;
if (separator.length == 0) { JsonData = {data: {}}; }
else { JsonData = {data: []};}
tempData = {};

var elems = document.getElementById(DataForm).elements;
for(var i = 0; i < elems.length; i++){
if(elems[i].name && elems[i].value) {
if (!isVisible(elems[i])) continue;
if (!isVisible(elems[i])) { continue; }

// tempData -> JsonData if new row (first named element (-> match) in row)
if (separator.length > 0 && elems[i].id.match(separator) && Object.keys(tempData).length > 0) {
JsonData.data.push(tempData);
tempData = {};
} else if (separator.length == 0 && Object.keys(tempData).length > 0) {
JsonData.data[Object.keys(tempData)[0]] = tempData[Object.keys(tempData)[0]];
tempData = {};
}

if (elems[i].type == "checkbox") {
count++;
if (jsontype == 1) { JsonData.data[elems[i].name] = (elems[i].checked==true?1:0); }
else if (jsontype == 2) {
JsonData.data.push({ "name" : elems[i].name,
"value": (elems[i].checked==true?1:0) });
}
tempData[elems[i].name] = (elems[i].checked==true?1:0);
} else if (elems[i].id.match(/^Alle.*/) ||
elems[i].id.match(/^GpioPin.*/) ||
elems[i].id.match(/^AnalogPin.*/) ||
elems[i].type == "number") {
count++;
if (jsontype == 1) { JsonData.data[elems[i].name] = parseInt(elems[i].value); }
else if (jsontype == 2) {
JsonData.data.push({ "name" : elems[i].name,
"value": parseInt(elems[i].value) });
}
tempData[elems[i].name] = parseInt(elems[i].value);
} else if (elems[i].type == "radio") {
if (jsontype == 1) { if (elems[i].checked==true) {JsonData.data[elems[i].name] = elems[i].value;} }
else if (jsontype == 2) {
if (elems[i].checked==true) {
count++;
JsonData.data.push({ "name" : elems[i].name,
"value": elems[i].value });
}
}
if (elems[i].checked==true) {tempData[elems[i].name] = elems[i].value;}
} else {
if (jsontype == 1) { JsonData.data[elems[i].name] = elems[i].value; }
else if (jsontype == 2) {
count++;
JsonData.data.push({ "name" : elems[i].name,
"value": elems[i].value });
}
tempData[elems[i].name] = elems[i].value;
}
}
}

// ende elements
if (separator.length > 0 && Object.keys(tempData).length > 0) {
JsonData.data.push(tempData);
tempData = {};
} else if (separator.length == 0 && Object.keys(tempData).length > 0) {
JsonData.data[Object.keys(tempData)[0]] = tempData[Object.keys(tempData)[0]];
tempData = {};
}

setResponse(true, "save ...")

Expand All @@ -374,12 +366,9 @@ function onSubmit(DataForm, jsontype){
data['subaction'] = filename;
requestData(JSON.stringify(data), false);
});


}



/*******************************
blendet Zeilen der Tabelle aus
show: Array of shown IDs return true;
Expand Down
2 changes: 1 addition & 1 deletion data/web/modbusitemconfig.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</form>
<br />
<br/>
<input type='submit' onclick="onSubmit('DataForm', 2)" value='Speichern' />
<input type='submit' onclick="onSubmit('DataForm', '.*')" value='Speichern' />
<span id='response'></span>
</body>
</html>
29 changes: 17 additions & 12 deletions src/modbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ void modbus::QueryQueueToInverter() {
RS485Serial->flush();
digitalWrite(this->pin_RTS, RS485Receive);

// wait 100ms to get response
unsigned long timeout=millis()+100;
while (millis()<=timeout) { yield(); }

Expand Down Expand Up @@ -515,7 +516,7 @@ int modbus::JsonPosArrayToInt(JsonArray posArray, JsonArray posArray2) {
for(uint16_t v : posArray) {
if (v < this->DataFrame->size()) {
val_i = (val_i << 8) | this->DataFrame->at(v);
val_max = (val_max << 8) | 0xFF;
val_max = (val_max << 8) | 0xFF;
}
}
}
Expand Down Expand Up @@ -1220,28 +1221,32 @@ void modbus::LoadJsonItemConfig() {
}
}

String ItemName = elem["name"].as<String>();
//ItemName = ItemName.substring(7, ItemName.length()); //Name: active_<ItemName>
for (JsonPair kv : elem.as<JsonObject>()) {
const char* ItemName = kv.key().c_str();

for(uint16_t i=0; i<this->InverterLiveData->size(); i++) {
if (this->InverterLiveData->at(i).Name == ItemName ) {
this->InverterLiveData->at(i).active = elem["value"].as<bool>();
//Serial.println(kv.key().c_str());
//Serial.println(kv.value().as<const char*>());

if (Config->GetDebugLevel() >=3) {
Serial.printf("item %s -> %s\n", ItemName.c_str(), (this->InverterLiveData->at(i).active?"enabled":"disabled"));
}
for(uint16_t i=0; i<this->InverterLiveData->size(); i++) {
if (this->InverterLiveData->at(i).Name == ItemName ) {
this->InverterLiveData->at(i).active = kv.value().as<bool>();

if (Config->GetDebugLevel() >=3) {
Serial.printf("item %s -> %s\n", ItemName, (this->InverterLiveData->at(i).active?"enabled":"disabled"));
}

break;
break;
}
}
}

} while (stream.findUntil(",","]"));
configFile.close();
} else {
if (Config->GetDebugLevel() >=1) {Serial.println("failed to load modbus item json config, load default config");}
if (Config->GetDebugLevel() >=1) {Serial.println("failed to load modbusitemconfig.json, load default item config");}
}
} else {
if (Config->GetDebugLevel() >=3) {Serial.println("ModbusItemConfig.json config File not exists, all items are inactive as default");}
if (Config->GetDebugLevel() >=3) {Serial.println("modbusitemconfig.json config File not exists, all items are inactive as default");}
}
}

Expand Down

0 comments on commit 259afed

Please sign in to comment.