Skip to content

Commit

Permalink
Merge pull request #1463 from benderl/bugfix
Browse files Browse the repository at this point in the history
add chart labels for counter components
  • Loading branch information
benderl authored Mar 8, 2024
2 parents 1081458 + dfa25e1 commit 58f161b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/modules/web_themes/standard_legacy/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1153,13 +1153,13 @@ <h4 class="modal-title">
// some helper functions
'helperFunctions.js?ver=20221117',
// functions for processing messages
'processAllMqttMsg.js?ver=20240307',
'processAllMqttMsg.js?ver=20240308',
// respective Chart.js definition live
'livechart.js?ver=20240307',
'livechart.js?ver=20240308',
// respective Chart.js definition price based charging
'electricityPriceChart.js?ver=20240108',
// functions performing mqtt and start mqtt-service
'setupMqttServices.js?ver=20240108',
'setupMqttServices.js?ver=20240308',
];
scriptsToLoad.forEach(function (src) {
var script = document.createElement('script');
Expand Down
10 changes: 3 additions & 7 deletions packages/modules/web_themes/standard_legacy/web/livechart.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,6 @@ function addDataset(datasetId) {
newDataset = JSON.parse(JSON.stringify(datasetTemplates[datasetTemplate]));
newDataset.parsing.yAxisKey = datasetId;
newDataset.jsonKey = datasetId;
if (datasetIndex) {
if(chartLabels[datasetId] === undefined) {
console.warn('no label found for index: ' + datasetId);
chartLabels[datasetId] = newDataset.label + ' ' + datasetIndex;
}
newDataset.label = chartLabels[datasetId];
}
return chartDatasets.push(newDataset) - 1;
} else {
console.warn('no matching template found: ' + datasetId);
Expand All @@ -562,6 +555,9 @@ function initDataset(datasetId) {
index = addDataset(datasetId);
}
if (index != undefined) {
if (chartLabels[datasetId] !== undefined) {
chartDatasets[index].label = chartLabels[datasetId];
}
chartDatasets[index].data = allChartData;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var vehicleSoc = {};
var evuCounterIndex = undefined;
var chartLabels = {};

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

function processEvuMessages(mqttTopic, mqttPayload) {
// processes mqttTopic for topic openWB/counter/0
// called by handleMessage
if (mqttTopic == 'openWB/counter/' + evuCounterIndex + '/get/power') {
var unit = 'W';
var powerEvu = parseInt(mqttPayload, 10);
Expand Down Expand Up @@ -503,6 +503,27 @@ function processCounterMessages(mqttTopic, mqttPayload) {
}
}

function processComponentMessages(mqttTopic, mqttPayload) {
// let deviceIndex = getIndex(mqttTopic, 0); // first number in topic
// let componentIndex = getIndex(mqttTopic, 1); // second number in topic
if (mqttTopic.match(/^openWB\/system\/device\/[0-9]+\/component\/[0-9]+\/config$/i)) {
// JSON data
// name: str
// type: str
// id: int
// configuration: JSON
var configMessage = JSON.parse(mqttPayload);
// chart label
if (configMessage.type.includes("counter")) {
let key = `counter${configMessage.id}-power`;
if (configMessage.id == evuCounterIndex) {
key = "grid";
}
chartLabels[key] = configMessage.name;
}
}
}

function processBatteryMessages(mqttTopic, mqttPayload) {
// processes mqttTopic for topic openWB/bat
// called by handleMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var topicsToSubscribe = [
["openWB/bat/get/daily_exported", 1], // total daily imported energy; float, unit: kWh
["openWB/bat/get/daily_imported", 1], // total daily imported energy; float, unit: kWh

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

// devices and components
["openWB/system/device/+/component/+/config", 1], // configuration of components

// information for connected vehicle
["openWB/chargepoint/+/get/connected_vehicle/info", 1], // general info of the vehicle; JSON { "id": int, "name": str }
["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) }
Expand Down

0 comments on commit 58f161b

Please sign in to comment.