Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
processVehicleTemplateMessages,
} from '@/components/chargePointList/processMessages'
import { processSmarthomeMessages } from '@/components/smartHome/processMessages'
import { processConsumerMessages } from '@/components/consumerList/processMessages'
import {
processConsumerMessages,
registerConsumerFromHierarchy,
} from '@/components/consumerList/processMessages'
import {
addCounter,
counters,
Expand All @@ -33,7 +36,7 @@ import {
import { mqttClientId } from './mqttClient'
import { add } from '@/components/mqttViewer/model'
import { globalConfig } from './themeConfig'
import { addConsumer, consumers } from '@/components/consumerList/model'
import { consumers } from '@/components/consumerList/model'

const topicsToSubscribe = [
'openWB/counter/#',
Expand Down Expand Up @@ -183,7 +186,7 @@ function processHierarchy(hierarchy: Hierarchy) {
addPvSystem(hierarchy.id)
break
case 'consumer':
addConsumer(hierarchy.id)
registerConsumerFromHierarchy(hierarchy.id)
break
default:
// console.warn('Ignored Hierarchy type: ' + hierarchy.type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ export function processConsumerMessages(topic: string, message: string) {
}
}

// device messages for a consumer id not yet created by the hierarchy are buffered here and
// replayed once registerConsumerFromHierarchy creates it, instead of being dropped or creating
// a consumer that may not actually be in the current hierarchy (eg. a stale retained topic)
const pendingMessages = new Map<number, { topic: string; message: string }[]>()

// called from hierarchy processing - the only place a consumer may be created, so its color
// (assigned from consumers.size at creation) stays consistent with the hierarchy order
export function registerConsumerFromHierarchy(index: number, showInList = true) {
addConsumer(index, showInList)
const pending = pendingMessages.get(index)
if (pending) {
pendingMessages.delete(index)
pending.forEach(({ topic, message }) =>
processConsumerDeviceMessages(topic, message),
)
}
}

// Consumers device messages
function processConsumerDeviceMessages(topic: string, message: string) {
const index = getIndex(topic)
Expand All @@ -19,7 +37,10 @@ function processConsumerDeviceMessages(topic: string, message: string) {
return
}
if (!consumers.has(index)) {
console.warn('Invalid consumer id received: ' + index)
const pending = pendingMessages.get(index) ?? []
pending.push({ topic, message })
pendingMessages.set(index, pending)
return
}
const dev = consumers.get(index)!
if (topic.match(/^openWB\/consumer\/[0-9]+\/get\/power$/i)) {
Expand Down
Loading