Skip to content

Commit 58f161b

Browse files
authored
Merge pull request #1463 from benderl/bugfix
add chart labels for counter components
2 parents 1081458 + dfa25e1 commit 58f161b

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

packages/modules/web_themes/standard_legacy/web/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,13 +1153,13 @@ <h4 class="modal-title">
11531153
// some helper functions
11541154
'helperFunctions.js?ver=20221117',
11551155
// functions for processing messages
1156-
'processAllMqttMsg.js?ver=20240307',
1156+
'processAllMqttMsg.js?ver=20240308',
11571157
// respective Chart.js definition live
1158-
'livechart.js?ver=20240307',
1158+
'livechart.js?ver=20240308',
11591159
// respective Chart.js definition price based charging
11601160
'electricityPriceChart.js?ver=20240108',
11611161
// functions performing mqtt and start mqtt-service
1162-
'setupMqttServices.js?ver=20240108',
1162+
'setupMqttServices.js?ver=20240308',
11631163
];
11641164
scriptsToLoad.forEach(function (src) {
11651165
var script = document.createElement('script');

packages/modules/web_themes/standard_legacy/web/livechart.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,6 @@ function addDataset(datasetId) {
542542
newDataset = JSON.parse(JSON.stringify(datasetTemplates[datasetTemplate]));
543543
newDataset.parsing.yAxisKey = datasetId;
544544
newDataset.jsonKey = datasetId;
545-
if (datasetIndex) {
546-
if(chartLabels[datasetId] === undefined) {
547-
console.warn('no label found for index: ' + datasetId);
548-
chartLabels[datasetId] = newDataset.label + ' ' + datasetIndex;
549-
}
550-
newDataset.label = chartLabels[datasetId];
551-
}
552545
return chartDatasets.push(newDataset) - 1;
553546
} else {
554547
console.warn('no matching template found: ' + datasetId);
@@ -562,6 +555,9 @@ function initDataset(datasetId) {
562555
index = addDataset(datasetId);
563556
}
564557
if (index != undefined) {
558+
if (chartLabels[datasetId] !== undefined) {
559+
chartDatasets[index].label = chartLabels[datasetId];
560+
}
565561
chartDatasets[index].data = allChartData;
566562
}
567563
}

packages/modules/web_themes/standard_legacy/web/processAllMqttMsg.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ var vehicleSoc = {};
1414
var evuCounterIndex = undefined;
1515
var chartLabels = {};
1616

17-
function getIndex(topic) {
17+
function getIndex(topic, position=0) {
1818
// get occurrence of numbers between / / in topic
1919
// since this is supposed to be the index like in openwb/lp/4/w
2020
// no lookbehind supported by safari, so workaround with replace needed
21-
var index = topic.match(/(?:\/)([0-9]+)(?=\/)/g)[0].replace(/[^0-9]+/g, '');
21+
// there may be multiple occurrences of numbers in the topic, so we need to specify the position
22+
var index = topic.match(/(?:\/)([0-9]+)(?=\/)/g)[position].replace(/[^0-9]+/g, '');
2223
if (typeof index === 'undefined') {
2324
index = '';
2425
}
@@ -362,6 +363,7 @@ function handleMessage(mqttTopic, mqttPayload) {
362363
processPreloader(mqttTopic);
363364
if (mqttTopic.match(/^openwb\/counter\/[0-9]+\//i)) { processCounterMessages(mqttTopic, mqttPayload) }
364365
else if (mqttTopic.match(/^openwb\/counter\//i)) { processGlobalCounterMessages(mqttTopic, mqttPayload); }
366+
else if (mqttTopic.match(/^openwb\/system\/device\/[0-9]+\/component\/[0-9]+\//i) ) { processComponentMessages(mqttTopic, mqttPayload); }
365367
else if (mqttTopic.match(/^openwb\/bat\//i)) { processBatteryMessages(mqttTopic, mqttPayload); }
366368
else if (mqttTopic.match(/^openwb\/pv\//i)) { processPvMessages(mqttTopic, mqttPayload); }
367369
else if (mqttTopic.match(/^openwb\/chargepoint\//i)) { processChargePointMessages(mqttTopic, mqttPayload); }
@@ -437,8 +439,6 @@ function processGlobalCounterMessages(mqttTopic, mqttPayload) {
437439
}
438440

439441
function processEvuMessages(mqttTopic, mqttPayload) {
440-
// processes mqttTopic for topic openWB/counter/0
441-
// called by handleMessage
442442
if (mqttTopic == 'openWB/counter/' + evuCounterIndex + '/get/power') {
443443
var unit = 'W';
444444
var powerEvu = parseInt(mqttPayload, 10);
@@ -503,6 +503,27 @@ function processCounterMessages(mqttTopic, mqttPayload) {
503503
}
504504
}
505505

506+
function processComponentMessages(mqttTopic, mqttPayload) {
507+
// let deviceIndex = getIndex(mqttTopic, 0); // first number in topic
508+
// let componentIndex = getIndex(mqttTopic, 1); // second number in topic
509+
if (mqttTopic.match(/^openWB\/system\/device\/[0-9]+\/component\/[0-9]+\/config$/i)) {
510+
// JSON data
511+
// name: str
512+
// type: str
513+
// id: int
514+
// configuration: JSON
515+
var configMessage = JSON.parse(mqttPayload);
516+
// chart label
517+
if (configMessage.type.includes("counter")) {
518+
let key = `counter${configMessage.id}-power`;
519+
if (configMessage.id == evuCounterIndex) {
520+
key = "grid";
521+
}
522+
chartLabels[key] = configMessage.name;
523+
}
524+
}
525+
}
526+
506527
function processBatteryMessages(mqttTopic, mqttPayload) {
507528
// processes mqttTopic for topic openWB/bat
508529
// called by handleMessage

packages/modules/web_themes/standard_legacy/web/setupMqttServices.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var topicsToSubscribe = [
3434
["openWB/bat/get/daily_exported", 1], // total daily imported energy; float, unit: kWh
3535
["openWB/bat/get/daily_imported", 1], // total daily imported energy; float, unit: kWh
3636

37-
// counter topics, counter with index 0 is always main grid counter
37+
// counter topics
3838
["openWB/counter/set/home_consumption", 1], // actual home power
3939
["openWB/counter/set/daily_yield_home_consumption", 1], // daily home energy
4040
["openWB/counter/+/get/power", 1], // actual power; int, unit: W
@@ -55,6 +55,9 @@ var topicsToSubscribe = [
5555
["openWB/chargepoint/+/get/enabled", 1], // is the chargepoint enabled? int, 0 = disabled, 1 = enabled
5656
["openWB/chargepoint/+/set/current", 1], // actual set current; float, unit: A
5757

58+
// devices and components
59+
["openWB/system/device/+/component/+/config", 1], // configuration of components
60+
5861
// information for connected vehicle
5962
["openWB/chargepoint/+/get/connected_vehicle/info", 1], // general info of the vehicle; JSON { "id": int, "name": str }
6063
["openWB/chargepoint/+/get/connected_vehicle/config", 1], // general configuration of the vehicle; JSON { "charge_template": int, "ev_template": int, "chargemode": str, "priority": bool, "average_consumption": int (Wh/100km) }

0 commit comments

Comments
 (0)