Some work on the energy view

This commit is contained in:
Michael Zanetti 2021-08-19 12:54:17 +02:00
parent 47ea7087c4
commit 4a91dbd334
3 changed files with 49 additions and 19 deletions

View File

@ -36,7 +36,7 @@ import QtCharts 2.2
import Nymea 1.0
ChartView {
id: chart
id: root
backgroundColor: Style.tileBackgroundColor
backgroundRoundness: Style.cornerRadius
theme: ChartView.ChartThemeLight
@ -45,16 +45,19 @@ ChartView {
legend.alignment: Qt.AlignRight
titleColor: Style.foregroundColor
property Thing rootMeter: null
property ThingsProxy meters: null
property int multiplier: 1
readonly property State rootMeterTotalConsumedEnergyState: rootMeter ? rootMeter.stateByName("totalEnergyConsumed") : null
Connections {
target: meters
onCountChanged: chart.refresh()
onCountChanged: root.refresh()
}
Component.onCompleted: {
chart.refresh()
root.refresh()
}
QtObject {
@ -65,22 +68,28 @@ ChartView {
function refresh() {
pieSeries.clear();
d.sliceMap = {}
var unknownConsumerEnergy = 0;
if (rootMeter) {
unknownConsumerEnergy = rootMeter.stateByName("totalEnergyConsumed").value
}
for (var i = 0; i < meters.count; i++) {
var thing = meters.get(i);
var value = 0;
var totalConsumedStateType = thing.thingClass.stateTypes.findByName("totalEnergyConsumed")
if (totalConsumedStateType) {
var totalConsumedState = thing.states.getState(totalConsumedStateType.id)
value = value + (totalConsumedState.value * chart.multiplier)
value = value + (totalConsumedState.value * root.multiplier)
}
var totalProducedStateType = thing.thingClass.stateTypes.findByName("totalEnergyProduced")
if (totalProducedStateType) {
var totalProducedState = thing.states.getState(totalProducedStateType.id)
value = value - (totalProducedState.value * chart.multiplier)
value = value - (totalProducedState.value * root.multiplier)
}
var slice = pieSeries.append(thing.name, Math.max(0, value))
var color = Style.accentColor
for (var j = 0; j < i; j+=2) {
for (var j = 0; j <= i; j+=2) {
if (i % 2 == 0) {
color = Qt.lighter(color, 1.2);
} else {
@ -88,7 +97,13 @@ ChartView {
}
}
slice.color = color
d.sliceMap[slice] = i
d.sliceMap[slice] = thing
unknownConsumerEnergy -= value
}
if (rootMeter) {
var slice = pieSeries.append(qsTr("Unknown"), unknownConsumerEnergy)
slice.color = Style.accentColor
d.sliceMap[slice] = rootMeter
}
}
@ -98,24 +113,29 @@ ChartView {
size: 0.8
onClicked: {
print("clicked slice", slice, d.sliceMap[slice], meters.get(d.sliceMap[slice]))
pageStack.push("../devicepages/SmartMeterDevicePage.qml", {thing: meters.get(d.sliceMap[slice])})
print("clicked slice", slice, d.sliceMap[slice], d.sliceMap[slice].name)
pageStack.push("../devicepages/SmartMeterDevicePage.qml", {thing: d.sliceMap[slice]})
}
}
ColumnLayout {
x: chart.plotArea.x + (chart.plotArea.width * 0.5) - (width / 2)
y: chart.plotArea.y + (chart.plotArea.height * 0.5) - (height / 2)
x: root.plotArea.x + (root.plotArea.width * 0.5) - (width / 2)
y: root.plotArea.y + (root.plotArea.height * 0.5) - (height / 2)
width: root.width
Label {
font.pixelSize: app.largeFont
Layout.alignment: Qt.AlignHCenter
text: Math.round(pieSeries.sum * 1000) / 1000
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
text: root.rootMeter
? root.rootMeterTotalConsumedEnergyState.value.toFixed(2)
: Math.round(pieSeries.sum * 1000) / 1000
}
Label {
text: "KWh"
Layout.alignment: Qt.AlignHCenter
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
}
}
}

View File

@ -104,7 +104,7 @@ ThingsListPageBase {
Label {
Layout.fillWidth: true
text: sensorValueDelegate.stateValue
? "%1 %2".arg(1.0 * Math.round(Types.toUiValue(sensorValueDelegate.stateValue.value, sensorValueDelegate.stateType.unit) * 100000) / 100000).arg(Types.toUiUnit(sensorValueDelegate.stateType.unit))
? "%1 %2".arg((1.0 * Math.round(Types.toUiValue(sensorValueDelegate.stateValue.value, sensorValueDelegate.stateType.unit) * 100000) / 100000).toFixed(3)).arg(Types.toUiUnit(sensorValueDelegate.stateType.unit))
: ""
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter

View File

@ -40,11 +40,18 @@ import "../delegates"
MainViewBase {
id: root
ThingsProxy {
id: energyMeters
engine: _engine
shownInterfaces: ["energymeter"]
}
ThingsProxy {
id: consumers
engine: _engine
shownInterfaces: ["smartmeterconsumer"]
}
ThingsProxy {
id: producers
engine: _engine
@ -55,11 +62,11 @@ MainViewBase {
anchors.fill: parent
anchors.margins: app.margins / 2
contentHeight: energyGrid.childrenRect.height
visible: energyMeters.count > 0
GridLayout {
id: energyGrid
width: parent.width
visible: consumers.count > 0
columns: root.width > 600 ? 2 : 1
rowSpacing: 0
columnSpacing: 0
@ -68,6 +75,8 @@ MainViewBase {
Layout.fillWidth: true
// Layout.preferredWidth: energyGrid.width / energyGrid.columns
Layout.preferredHeight: width * .7
// FIXME: multiple root meters... Not exactly a use case, still possible tho
rootMeter: energyMeters.count > 0 ? energyMeters.get(0) : null
meters: consumers
title: qsTr("Total consumed energy")
visible: consumers.count > 0
@ -183,7 +192,7 @@ MainViewBase {
ValueAxis {
id: yAxis
readonly property XYSeriesAdapter adapter: consumersRepeater.count > 0 ? consumersRepeater.itemAt(consumersRepeater.count - 1).adapter : null
max: adapter ? Math.ceil(Math.max(adapter.maxValue * 0.95, adapter.maxValue * 1.05)) : 1
max: 7// adapter ? Math.ceil(Math.max(adapter.maxValue * 0.95, adapter.maxValue * 1.05)) : 1
min: adapter ? Math.floor(Math.min(adapter.minValue * 0.95, adapter.minValue * 1.05)) : 0
// This seems to crash occationally
// onMinChanged: applyNiceNumbers();
@ -360,6 +369,7 @@ MainViewBase {
Layout.preferredHeight: width * .7
backgroundColor: Style.tileBackgroundColor
backgroundRoundness: Style.cornerRadius
rootMeter: energyMeters.count > 0 ? energyMeters.get(0) : null
meters: producers
title: qsTr("Total produced energy")
visible: producers.count > 0
@ -371,9 +381,9 @@ MainViewBase {
EmptyViewPlaceholder {
anchors.centerIn: parent
width: parent.width - app.margins * 2
visible: !engine.thingManager.fetchingData && consumers.count == 0
visible: !engine.thingManager.fetchingData && energyMeters.count == 0
title: qsTr("There are no energy meters installed.")
text: qsTr("To get an overview of your current energy usage, install some energy meters.")
text: qsTr("To get an overview of your current energy usage, install an energy meter.")
imageSource: "../images/smartmeter.svg"
buttonText: qsTr("Add things")
onButtonClicked: pageStack.push(Qt.resolvedUrl("../thingconfiguration/NewThingPage.qml"))