Skip to content

Commit

Permalink
make mqtt topic patternable
Browse files Browse the repository at this point in the history
  • Loading branch information
yaxu committed Feb 7, 2025
1 parent 8bde61f commit 0c22467
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/mqtt/mqtt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,22 @@ Pattern.prototype.mqtt = function (
}
return this.withHap((hap) => {
const onTrigger = (t_deprecate, hap, currentTime, cps, targetTime) => {
let msg_topic = topic;
if (!connected) {
return;
}
let message = '';
if (typeof hap.value === 'object') {
let value = hap.value;

// Try to take topic from pattern if it's not set
if (typeof msg_topic === 'undefined' && 'topic' in value) {
msg_topic = value.topic;
if (Array.isArray(msg_topic)) {
msg_topic = msg_topic.join('/');
}
msg_topic = '/' + msg_topic;
}
if (add_meta) {
const duration = hap.duration.div(cps);
value = { ...value, duration: duration.valueOf(), cps: cps };
Expand All @@ -99,7 +109,7 @@ Pattern.prototype.mqtt = function (
message = hap.value;
}
message = new Paho.Message(message);
message.destinationName = topic;
message.destinationName = msg_topic;

const offset = (targetTime - currentTime + latency) * 1000;

Expand Down

0 comments on commit 0c22467

Please sign in to comment.