Improve energy charts when there is a hidden producer

This commit is contained in:
Michael Zanetti 2022-05-06 13:42:45 +02:00
parent 7959154f46
commit 9ed9cd2ac7
5 changed files with 87 additions and 45 deletions

View File

@ -175,7 +175,7 @@ private:
ThingClasses *m_thingClasses;
IOConnections *m_ioConnections;
bool m_fetchingData = false;
bool m_fetchingData = true;
int m_currentGetConfigIndex = 0;

View File

@ -7,7 +7,7 @@ Item {
property color accentColor: "#57baae"
property color iconColor: "#808080"
property color generationBaseColor: red
property color generationBaseColor: blue
property color tileBackgroundColor: Qt.tint(backgroundColor, Qt.rgba(foregroundColor.r, foregroundColor.g, foregroundColor.b, 0.05))
property color tileForegroundColor: foregroundColor
@ -112,7 +112,7 @@ Item {
property color white: "white"
property color gray: "gray"
property color darkGray: "darkGray"
property color blue: "deepskyblue"
property color blue: "#5c5ccd"
property color darkBlue: "royalblue"
property color orange: "#cd955c"
property color purple: "#6d5fd5"

View File

@ -168,6 +168,7 @@ MainViewBase {
Layout.preferredHeight: width
energyManager: energyManager
visible: rootMeter != null
producers: producers
}
ConsumerStats {

View File

@ -48,11 +48,12 @@ ChartView {
Connections {
target: energyManager
onPowerBalanceChanged: {
var consumption = energyManager.currentPowerConsumption
var consumersSummation = 0
for (var i = 0; i < consumers.count; i++) {
consumption -= consumers.get(i).stateByName("currentPower").value
consumersSummation += consumers.get(i).stateByName("currentPower").value
}
d.unknownSlice.value = consumption
d.consumersSummation = consumersSummation;
d.unknownSlice.value = Math.max(0, energyManager.currentPowerConsumption - consumersSummation)
}
}
@ -62,6 +63,8 @@ ChartView {
id: d
property var thingsColorMap: ({})
property PieSlice unknownSlice: null
property double consumersSummation: 0
}
function updateConsumers() {
@ -74,24 +77,26 @@ ChartView {
return;
}
var unknownConsumption = energyManager.currentPowerConsumption
var colorMap = {}
var consumersSummation = 0;
for (var i = 0; i < consumers.count; i++) {
var consumer = consumers.get(i)
let currentPowerState = consumer.stateByName("currentPower")
let slice = consumersBalanceSeries.append(consumer.name, currentPowerState.value)
print("***** slice border width", slice.borderWidth)
// slice.color = root.colors[i % root.colors.length]
slice.color = NymeaUtils.generateColor(Style.generationBaseColor, i)
colorMap[consumer] = slice.color
currentPowerState.valueChanged.connect(function() {
slice.value = currentPowerState.value
})
unknownConsumption -= currentPowerState.value
consumersSummation += currentPowerState.value
}
d.consumersSummation = consumersSummation
if (root.rootMeter) {
var unknownConsumption = Math.max(0, energyManager.currentPowerConsumption - consumersSummation)
print("Unknown consumption:", unknownConsumption, "consumption balance", energyManager.currentPowerConsumption, "consumers summation:", consumersSummation)
d.unknownSlice = consumersBalanceSeries.append(qsTr("Unknown"), unknownConsumption)
d.unknownSlice.color = Style.gray
}
@ -140,9 +145,14 @@ ChartView {
}
Label {
// We're using the maximum value of the energy managers consumption, the sum of all consumers because:
// * in a standard setup, the energy manager would know everything and the consumption will always be greater than the sum of all individual consumers
// * if there is a producer which is unknown to nymea though, it will decrease the consumption on the root meter so it may be smaller than the
// summation of all consumers. In this particular chart that would be nonsense so in the end we'll only lose the "unknown" power consumption in such a setup
property double finalTotal: Math.max(energyManager.currentPowerConsumption, d.consumersSummation)
text: "%1 %2"
.arg((energyManager.currentPowerConsumption / (energyManager.currentPowerConsumption > 1000 ? 1000 : 1)).toFixed(1))
.arg(energyManager.currentPowerConsumption > 1000 ? "kW" : "W")
.arg((finalTotal / (finalTotal > 1000 ? 1000 : 1)).toFixed(1))
.arg(finalTotal > 1000 ? "kW" : "W")
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
font: Style.bigFont

View File

@ -10,6 +10,13 @@ StatsBase {
property EnergyManager energyManager: null
property ThingsProxy producers: ThingsProxy {
engine: _engine
shownInterfaces: ["smartmeterproducer"]
}
readonly property bool hasProducers: producers.count > 0
QtObject {
id: d
property BarSet consumptionSet: null
@ -18,6 +25,33 @@ StatsBase {
property BarSet returnSet: null
}
function reload() {
if (selectionTabs.currentValue === undefined) {
return
}
if (engine.thingManager.fetchingData) {
return;
}
var config = root.configs[selectionTabs.currentValue.config]
print("Loading Power Balance Stats with config:", config.startTime(), config.sampleRate)
powerBalanceLogs.loadingInhibited = true
powerBalanceLogs.sampleRate = config.sampleRate
powerBalanceLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000)
powerBalanceLogs.loadingInhibited = false
chartView.reset();
}
Connections {
target: engine.thingManager
onFetchingDataChanged: {
print("Thingmanager loaded", engine.thingManager.fetchingData)
if (!engine.thingManager.fetchingData) root.reload()
}
}
ColumnLayout {
anchors.fill: parent
@ -47,19 +81,7 @@ StatsBase {
}
}
onCurrentValueChanged: {
if (currentValue === undefined) {
return
}
var config = root.configs[currentValue.config]
// print("config:", config.startTime(), config.sampleRate)
powerBalanceLogs.loadingInhibited = true
powerBalanceLogs.sampleRate = config.sampleRate
powerBalanceLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000)
powerBalanceLogs.loadingInhibited = false
chartView.reset();
root.reload()
}
}
@ -69,8 +91,10 @@ StatsBase {
var start = powerBalanceLogs.get(powerBalanceLogs.count - 1 )
// print("balance changed:", d.consumptionSet, powerBalanceLogs, powerBalanceLogs.count)
// print("updating", start ? start.timestamp : "", start ? start.totalConsumption : 0, root.energyManager.totalConsumption, root.energyManager.totalConsumption - (start ? start.totalConsumption : 0))
d.consumptionSet.replace(d.consumptionSet.count - 1, root.energyManager.totalConsumption - (start ? start.totalConsumption : 0))
d.productionSet.replace(d.productionSet.count - 1, root.energyManager.totalProduction - (start ? start.totalProduction : 0))
if (root.hasProducers) {
d.consumptionSet.replace(d.consumptionSet.count - 1, root.energyManager.totalConsumption - (start ? start.totalConsumption : 0))
d.productionSet.replace(d.productionSet.count - 1, root.energyManager.totalProduction - (start ? start.totalProduction : 0))
}
d.acquisitionSet.replace(d.acquisitionSet.count - 1, root.energyManager.totalAcquisition - (start ? start.totalAcquisition : 0))
d.returnSet.replace(d.returnSet.count - 1, root.energyManager.totalReturn - (start ? start.totalReturn : 0))
}
@ -87,7 +111,7 @@ StatsBase {
chartView.reset();
// print("Logs fetched")
print("Logs fetched")
var config = root.configs[selectionTabs.currentValue.config]
var labels = []
@ -172,13 +196,14 @@ StatsBase {
chartView.animationOptions = NymeaUtils.chartsAnimationOptions
for (var i = 0; i < entries.length; i++) {
// print("Appending to set", JSON.stringify(entries[i]))
d.consumptionSet.append(entries[i].consumption)
d.productionSet.append(entries[i].production)
print("Appending to set", JSON.stringify(entries[i]))
if (root.hasProducers) {
d.consumptionSet.append(entries[i].consumption)
d.productionSet.append(entries[i].production)
}
d.acquisitionSet.append(entries[i].acquisition)
d.returnSet.append(entries[i].returned)
}
}
}
@ -205,14 +230,15 @@ StatsBase {
timestamps.splice(0, 1)
categoryAxis.timestamps = timestamps
d.consumptionSet.remove(0, 1);
d.productionSet.remove(0, 1);
if (root.hasProducers) {
d.consumptionSet.remove(0, 1);
d.consumptionSet.append(consumptionValue)
d.productionSet.remove(0, 1);
d.productionSet.append(productionValue)
}
d.acquisitionSet.remove(0, 1);
d.returnSet.remove(0, 1);
d.consumptionSet.append(consumptionValue)
d.productionSet.append(productionValue)
d.acquisitionSet.append(acquisitionValue)
d.returnSet.remove(0, 1);
d.returnSet.append(returnValue)
chartView.animationOptions = NymeaUtils.chartsAnimationOptions
@ -239,14 +265,17 @@ StatsBase {
function reset() {
barSeries.clear();
valueAxis.max = 0
d.consumptionSet = barSeries.append(qsTr("Consumed"), [])
d.consumptionSet.color = Style.blue
d.consumptionSet.borderColor = d.consumptionSet.color
d.consumptionSet.borderWidth = 0
d.productionSet = barSeries.append(qsTr("Produced"), [])
d.productionSet.color = Style.green
d.productionSet.borderColor = d.productionSet.color
d.productionSet.borderWidth = 0
print("********** resetting chart")
if (root.hasProducers) {
d.consumptionSet = barSeries.append(qsTr("Consumed"), [])
d.consumptionSet.color = Style.blue
d.consumptionSet.borderColor = d.consumptionSet.color
d.consumptionSet.borderWidth = 0
d.productionSet = barSeries.append(qsTr("Produced"), [])
d.productionSet.color = Style.green
d.productionSet.borderColor = d.productionSet.color
d.productionSet.borderWidth = 0
}
d.acquisitionSet = barSeries.append(qsTr("From grid"), [])
d.acquisitionSet.color = Style.red
d.acquisitionSet.borderColor = d.acquisitionSet.color
@ -396,6 +425,7 @@ StatsBase {
}
RowLayout {
visible: root.hasProducers
Rectangle {
width: Style.extraSmallFont.pixelSize
height: width
@ -407,6 +437,7 @@ StatsBase {
}
}
RowLayout {
visible: root.hasProducers
Rectangle {
width: Style.extraSmallFont.pixelSize
height: width