'use strict';
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://test.mosquitto.org');
client.on('message', function(topic, data) {
var sender;
if (!!data && (data.length > 0)) {
data = JSON.parse(data);
sender = data.sender;
}
if (sender !== 'self') {
console.log('Topic name ==>', topic);
console.log('Payload ==>', data);
}
});
var payload = JSON.stringify({
sender: 'self'
});
client.subscribe('/api/robots');
client.publish('/api/robots', payload);
client.subscribe('/api/robots/cybot');
client.publish('/api/robots/cybot', payload);
client.subscribe('/api/robots/cybot/devices');
client.publish('/api/robots/cybot/devices', payload);
client.subscribe('/api/robots/cybot/events');
client.publish('/api/robots/cybot/events', payload);
client.subscribe('/api/robots/cybot/commands');
client.publish('/api/robots/cybot/commands', payload);
var params = {
sender: 'self',
args: ['param1', 'param2', 'param3']
};
payload = JSON.stringify(params);
client.subscribe('/api/robots/cybot/toggle');
setInterval(function() {
client.publish(
'/api/robots/cybot/toggle',
payload);
}, 2000);