Skip to content

Commit 5c8da2d

Browse files
committed
Allow multiple parsers for one message.
1 parent dd7388c commit 5c8da2d

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

index.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ function handleMessage(msg) {
134134

135135
// Find a parser for this modelID and cid.
136136
const cid = msg.data.cid;
137-
const parser = parsers.find((p) => p.devices.includes(mappedModel.model) && p.cid === cid);
137+
const _parsers = parsers.filter((p) => p.devices.includes(mappedModel.model) && p.cid === cid);
138138

139-
if (!parser) {
139+
if (!_parsers.length) {
140140
console.log(`
141141
WARNING: No parser available for '${mappedModel.model}' with cid '${cid}'
142142
Please create an issue on https://github.com/Koenkk/xiaomi-zb2mqtt/issues
@@ -146,19 +146,20 @@ function handleMessage(msg) {
146146

147147
// Parse generic information from message.
148148
const friendlyName = settings.devices[device.ieeeAddr].friendly_name;
149-
const topic = `${settings.mqtt.base_topic}/${friendlyName}/${parser.topic}`;
150-
151-
// Define publish function.
152-
const publish = (payload) => mqttPublish(topic, payload.toString());
153149

154150
// Get payload for the message.
155151
// - If a payload is returned publish it to the MQTT broker
156152
// - If NO payload is returned do nothing. This is for non-standard behaviour
157153
// for e.g. click switches where we need to count number of clicks and detect long presses.
158-
const payload = parser.parse(msg, publish);
159-
if (payload) {
160-
publish(payload);
161-
}
154+
_parsers.forEach((parser) => {
155+
const topic = `${settings.mqtt.base_topic}/${friendlyName}/${parser.topic}`;
156+
const publish = (payload) => mqttPublish(topic, payload.toString());
157+
const payload = parser.parse(msg, publish);
158+
159+
if (payload) {
160+
publish(payload);
161+
}
162+
});
162163
}
163164

164165
function handleQuit() {

0 commit comments

Comments
 (0)