Skip to content

Commit

Permalink
Avoid changing active AC input serviceUid after Device is created
Browse files Browse the repository at this point in the history
Although Global.acInputs.activeInput is not added to a DeviceModel,
avoid modifying the serviceUid of the activeInput after creation, for
consistency with the behavior of other Device objects.

The main work is to create the new activeInput object imperatively
in onActiveInputInfoChanged instead of using Loader to create an
instance of ActiveAcInput that dynamically changes its serviceUid
depending on the active serviceName and serviceType.

Contributes to #1035
  • Loading branch information
blammit committed Jul 15, 2024
1 parent 0489826 commit 5ade11b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
33 changes: 23 additions & 10 deletions data/AcInputs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Victron.VenusOS
QtObject {
id: root

readonly property ActiveAcInput activeInput: _activeInputLoader.item
property ActiveAcInput activeInput
readonly property ActiveAcInput generatorInput: activeInput && activeInput.source === VenusOS.AcInputs_InputSource_Generator ? activeInput : null

readonly property AcInputSystemInfo activeInputInfo: input1Info.isActiveInput ? input1Info
Expand All @@ -30,23 +30,36 @@ QtObject {
// AC input metadata from com.victronenergy.system/Ac/In/<1|2>. There are always two inputs.
property AcInputSystemInfo input1Info: AcInputSystemInfo {
inputIndex: 0
isActiveInput: source === _activeInputSource.sourceAsInt
isActiveInput: valid && source === _activeInputSource.sourceAsInt
}
property AcInputSystemInfo input2Info: AcInputSystemInfo {
inputIndex: 1
isActiveInput: source === _activeInputSource.sourceAsInt
isActiveInput: valid && source === _activeInputSource.sourceAsInt
}

// Set activeInput to a valid object when /Ac/ActiveIn/Source is set to a valid source.
readonly property VeQuickItem _activeInputSource: VeQuickItem {
readonly property int sourceAsInt: value === undefined ? VenusOS.AcInputs_InputSource_NotAvailable : parseInt(value)

readonly property int sourceAsInt: !isValid ? VenusOS.AcInputs_InputSource_NotAvailable : parseInt(value)
uid: Global.system.serviceUid + "/Ac/ActiveIn/Source"
}
readonly property Loader _activeInputLoader: Loader {
active: root.sourceValid(root._activeInputSource.sourceAsInt)
sourceComponent: ActiveAcInput {
inputInfo: root.activeInputInfo

readonly property Component _activeAcInputComponent: Component {
ActiveAcInput {}
}

// Set activeInput to a valid object when /Ac/ActiveIn/Source is set to a valid source and
// triggers input1Info or input2Info to become the active input.
onActiveInputInfoChanged: {
if (activeInput) {
activeInput.destroy()
activeInput = null
}
if (activeInputInfo) {
const serviceUid = BackendConnection.type === BackendConnection.MqttSource
// this looks like: 'mqtt/vebus/289/'
? "mqtt/" + activeInputInfo.serviceType + "/" + activeInputInfo.deviceInstance
// this looks like: "dbus/com.victronenergy.vebus.ttyO1"
: BackendConnection.uidPrefix() + "/" + activeInputInfo.serviceName
activeInput = _activeAcInputComponent.createObject(root, { serviceUid: serviceUid, inputInfo: activeInputInfo })
}
}

Expand Down
2 changes: 2 additions & 0 deletions data/common/AcInputSystemInfo.qml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ QtObject {
required property int inputIndex
readonly property string bindPrefix: Global.system.serviceUid + "/Ac/In/" + inputIndex
property bool isActiveInput
readonly property bool valid: deviceInstance >= 0 && serviceType.length && serviceName.length

readonly property bool connected: _connected.value === 1
readonly property int deviceInstance: _deviceInstance.isValid ? _deviceInstance.value : -1
readonly property string serviceType: _serviceType.value || "" // e.g. "vebus"
Expand Down
9 changes: 0 additions & 9 deletions data/common/ActiveAcInput.qml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Device {
// Phase measurements from com.victronenergy.system/Ac/ActiveIn/L<1|2|3>
readonly property AcInputPhaseModel _phases: AcInputPhaseModel {
id: _phases

}

// Data from the input-specific service, e.g. com.victronenergy.vebus for a VE.Bus input,
Expand All @@ -38,12 +37,4 @@ Device {
serviceUid: root.serviceUid
serviceType: root.serviceType
}

serviceUid: BackendConnection.type === BackendConnection.MqttSource
// this looks like: 'mqtt/vebus/289/'
? inputInfo && serviceType.length && inputInfo.deviceInstance >= 0
? "mqtt/" + serviceType + "/" + inputInfo.deviceInstance
: ""
// this looks like: "dbus/com.victronenergy.vebus.ttyO1"
: serviceName.length ? BackendConnection.uidPrefix() + "/" + serviceName : ""
}

0 comments on commit 5ade11b

Please sign in to comment.