Skip to content

Commit

Permalink
simplify matching of charge point MQTT-topics in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
yankee42 committed Nov 29, 2022
1 parent 65fbb14 commit 8605d7e
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions web/themes/dark/processAllMqttMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,10 @@ function processLpMessages(mqttmsg, mqttpayload) {
// processes mqttmsg for topic openWB/lp
// called by handlevar
processPreloader(mqttmsg);
if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/w$/i ) ) {
var index = getIndex(mqttmsg); // extract number between two / /
let match = /^openWB\/lp\/(\d+)\/(.*)$/.exec(mqttmsg);
let index = match[1];
let lpSubTopic = match[2];
if (lpSubTopic === "W") {
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.actualPowerLp'); // now get parents respective child element
var actualPower = parseInt(mqttpayload, 10);
Expand All @@ -699,10 +701,9 @@ function processLpMessages(mqttmsg, mqttpayload) {
}
element.text(actualPower);
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/kWhchargedsinceplugged$/i ) ) {
else if (lpSubTopic === "kWhChargedSincePlugged") {
// energy charged since ev was plugged in
// also calculates and displays km charged
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.energyChargedLp'); // now get parents respective child element
var energyCharged = parseFloat(mqttpayload, 10);
Expand All @@ -721,9 +722,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
}
$(kmChargedLp).text(kmCharged);
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/kWhactualcharged$/i ) ) {
else if (lpSubTopic === "kWhActualCharged") {
// energy charged since reset of limitation
var index = getIndex(mqttmsg); // extract number between two / /
if ( isNaN(mqttpayload) ) {
mqttpayload = 0;
}
Expand All @@ -738,9 +738,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
var progress = (mqttpayload / limit * 100).toFixed(0);
element.width(progress+"%");
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/\%soc$/i ) ) {
else if (lpSubTopic === "%Soc") {
// soc of ev at respective charge point
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.socLp'); // now get parents respective child element
var soc = parseInt(mqttpayload, 10);
Expand All @@ -753,15 +752,13 @@ function processLpMessages(mqttmsg, mqttpayload) {
spinner.removeClass('fa-spin');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/timeremaining$/i ) ) {
else if (lpSubTopic === "TimeRemaining") {
// time remaining for charging to target value
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('.chargeLimitation[data-lp="' + index + '"]'); // get parent div element for charge limitation
var element = parent.find('.restzeitLp'); // get element
element.text('Restzeit ' + mqttpayload);
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/boolchargeatnight$/i ) ) {
var index = getIndex(mqttmsg); // extract number between two / /
else if (lpSubTopic === "boolChargeAtNight") {
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.nightChargingLp'); // now get parents respective child element
if ( mqttpayload == 1 ) {
Expand All @@ -770,9 +767,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
element.addClass('hide');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/boolplugstat$/i ) ) {
else if (lpSubTopic === "boolPlugStat") {
// status ev plugged in or not
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.plugstatLp'); // now get parents respective child element
if ( mqttpayload == 1 ) {
Expand All @@ -781,8 +777,7 @@ function processLpMessages(mqttmsg, mqttpayload) {
element.addClass('hide');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/boolchargestat$/i ) ) {
var index = getIndex(mqttmsg); // extract number between two / /
else if (lpSubTopic === "boolChargeStat") {
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.plugstatLp'); // now get parents respective child element
if ( mqttpayload == 1 ) {
Expand All @@ -791,17 +786,15 @@ function processLpMessages(mqttmsg, mqttpayload) {
element.removeClass('text-green').addClass('text-orange');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/strchargepointname$/i ) ) {
var index = getIndex(mqttmsg); // extract number between two / /
else if (lpSubTopic === "strChargePointName") {
$('.nameLp').each(function() { // fill in name for all element of class '.nameLp'
var lp = $(this).closest('[data-lp]').data('lp'); // get attribute lp from parent
if ( lp == index ) {
$(this).text(mqttpayload);
}
});
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/chargepointenabled$/i ) ) {
var index = getIndex(mqttmsg); // extract number between two / /
else if (lpSubTopic === "ChargePointEnabled") {
$('.nameLp').each(function() { // check all elements of class '.nameLp'
var lp = $(this).closest('[data-lp]').data('lp'); // get attribute lp from parent
if ( lp == index ) {
Expand All @@ -816,8 +809,7 @@ function processLpMessages(mqttmsg, mqttpayload) {
}
});
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/countphasesinuse/i ) ) {
var index = getIndex(mqttmsg); // extract number between two / /
else if (lpSubTopic === "countPhasesInUse") {
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.phasesInUseLp'); // now get parents respective child element
var phasesInUse = parseInt(mqttpayload, 10);
Expand All @@ -828,9 +820,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
element.text(' ' + phaseSymbols[phasesInUse]);
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/aconfigured$/i ) ) {
else if (lpSubTopic === "AConfigured") {
// target current value at charge point
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.targetCurrentLp'); // now get parents respective child element
var targetCurrent = parseInt(mqttpayload, 10);
Expand All @@ -840,9 +831,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
element.text(' ' + targetCurrent + ' A');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/boolsocconfigured$/i ) ) {
else if (lpSubTopic === "boolSocConfigured") {
// soc-module configured for respective charge point
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var elementIsConfigured = $(parent).find('.socConfiguredLp'); // now get parents respective child element
var elementIsNotConfigured = $(parent).find('.socNotConfiguredLp'); // now get parents respective child element
Expand All @@ -854,9 +844,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
$(elementIsConfigured).addClass('hide');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/boolsocmanual$/i ) ) {
else if (lpSubTopic === "boolSocManual") {
// manual soc-module configured for respective charge point
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var elementIsConfigured = $(parent).find('.socConfiguredLp'); // now get parents respective child element
if (mqttpayload == 1) {
Expand All @@ -869,9 +858,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
$(elementIsConfigured).find('.reloadLpSoc').removeClass('hide');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/boolchargepointconfigured$/i ) ) {
else if (lpSubTopic === "boolChargePointConfigured") {
// respective charge point configured
var index = getIndex(mqttmsg); // extract number between two / /
// now show/hide element containing data-lp attribute with value=index
switch (mqttpayload) {
case '0':
Expand All @@ -882,8 +870,7 @@ function processLpMessages(mqttmsg, mqttpayload) {
break;
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/autolockconfigured$/i ) ) {
var index = getIndex(mqttmsg); // extract first match = number from
else if (lpSubTopic === "AutolockConfigured") {
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.autolockConfiguredLp'); // now get parents respective child element
if ( mqttpayload == 0 ) {
Expand All @@ -892,13 +879,12 @@ function processLpMessages(mqttmsg, mqttpayload) {
element.removeClass('hide');
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/autolockstatus$/i ) ) {
else if (lpSubTopic === "AutolockStatus") {
// values used for AutolockStatus flag:
// 0 = standby
// 1 = waiting for autolock
// 2 = autolock performed
// 3 = auto-unlock performed
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.autolockConfiguredLp'); // now get parents respective child element
switch ( mqttpayload ) {
Expand All @@ -924,10 +910,9 @@ function processLpMessages(mqttmsg, mqttpayload) {
break;
}
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/energyconsumptionper100km$/i ) ) {
else if (lpSubTopic === "energyConsumptionPer100km") {
// store configured value in element attribute
// to calculate charged km upon receipt of charged energy
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.kmChargedLp'); // now get parents respective child element
var consumption = parseFloat(mqttpayload);
Expand All @@ -947,9 +932,8 @@ function processLpMessages(mqttmsg, mqttpayload) {
}
element.text(kmCharged);
}
else if ( mqttmsg.match( /^openwb\/lp\/[1-9][0-9]*\/boolfinishattimechargeactive$/i ) ) {
else if (lpSubTopic === "boolFinishAtTimeChargeActive") {
// respective charge point configured
var index = getIndex(mqttmsg); // extract number between two / /
var parent = $('[data-lp="' + index + '"]'); // get parent row element for charge point
var element = parent.find('.targetChargingLp'); // now get parents respective child element
if (mqttpayload == 1) {
Expand Down

0 comments on commit 8605d7e

Please sign in to comment.