Now to revisit this issue a few month later, since i only now had time to actually look into how plugins are supposed to work. Honestly the documentation on these is very lacking.
What i want to do is to be able to subscribe to arbitrary topics and push a value to arbitrary topics to be then later fix the incoming values with the correct number formatting. Apparently plugins do work with “devices” and as such i tried to define a device for each topic i am interested in.
My code is posted below. It doesn’t work yet, since apparently it doesn’t let me properly index all the available devices in the onPoll function. I tried to append the publish and subscribe Topics to the Id of the device, since i am not sure how such values could be otherwise stored in these objects. I hope to get some input on how to properly implement this plugin.
plugin.Name = "Test_MQTT";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 500;
plugin.DefaultSettings = { "Host": "", "Port": "", "Username": "", "Password": "" };
var mqtt = new MQTTClient();
var subscribed = false;
function onChangeRequest(device, attribute, value) {
// TODO
}
function onConnect() {
mqtt.connect("mqtt://" + plugin.Settings["Username"] + ":" + plugin.Settings["Password"] + "@" + plugin.Settings["Host"] + ":" + plugin.Settings["Port"]);
//This method is shared with both "onPoll" & "onSynchronizeDevices"
//We need to subscribe to different sets of topics for those functions.
//That's why we aren't subscribing here.
//Connecting with default options will clean our subscriptions, so we need to reset our "subscribed" variable.
subscribed = false;
console.log("connected");
}
function onDisconnect() {
mqtt.disconnect();
console.log("disconnected");
}
function onPoll() {
//Even though we have an infinite "while" loop below that reads messages, we need to consider the possiblity that a previous "onPoll" call was cancelled.
//And we don't want to send more subscribe requests than we need.
//So after we subscribe, set the "subscribed" variable so we know we are confidently subscribed.
if (!subscribed) {
var subscribeTopics = [];
for (var curDevice in plugin.Devices) {
var deviceIdParts = curDevice.Id.split(":");
var subscribeTopic = deviceIdParts[1];
if (subscribeTopic.length > 0) {
subscribeTopics.push(subscribeTopic);
}
}
mqtt.subscribe(subscribeTopics);
subscribed = true;
}
while (true) {
var message = mqtt.readMessage();
var device = null;
for (var curDevice in plugin.Devices) {
var deviceIdParts = curDevice.Id.split(":");
var subscribeTopic = deviceIdParts[1];
if (subscribeTopic.length > 0) {
if (subscribeTopic.toUpperCase() === message.topic.toUpperCase()) {
device = curDevice;
break;
}
}
}
if (device != null) {
var payload = message.payload.toString();
device.Variable = payload;
}
}
}
function newDevice(deviceId, subscribeTopic, publishTopic)
{
console.log(deviceId);
device = new Device();
device.DisplayName = deviceId;
device.Id = deviceId + ':' + subscribeTopic + ':' + publishTopic;
var attributes = [];
attributes.push("Variable");
device.Attributes = attributes;
plugin.Devices[deviceId] = device;
console.log(device.Id);
}
function onSynchronizeDevices() {
newDevice("HelloId", "Hello/test", "Hello/test/set");
newDevice("Hello2Id", "Hello/test2", "Hello/test2/set");
}