mqtt_server/lib/mqtt/mqttClient.js

69 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-06-22 15:07:56 +00:00
var mqtt = require('mqtt');
var mqttClient = function() {
var orgId = '';
var userName = '';
var appKey = 'bob';
this.lighting = 'lights';
this.connected = false;
var options = {
keepalive: 3600,
clientId: 'a:' + orgId + ':' + Date.now(),
username: userName,
password: new Buffer(appKey)
};
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
this.client.on('connect', function() {
connected = true;
console.log('Connected to SIlvr Broker');
});
this.client.subscribe('lightsOut');
this.client.on('message', function(topic, message) {
console.log(message);
});
this.isConnected = function() {
return this.connected;
};
};
/*
Client = mqtt.connect();
client.subscribe('presence');
client.on('message', function(topic, message) {
console.log(message);
});
*/
mqttClient.prototype.lightsIn = function() {
var destinationName = 'lightsIn';
this.client.publish(destinationName, '0');
var destinationName = 'lightsIn';
this.client.publish(destinationName, '1');
};
mqttClient.prototype.lightsOut = function() {
var destinationName = 'lightsOut';
this.client.publish(destinationName, 'f');
destinationName = 'lightsOut';
this.client.publish(destinationName, 'g');
};
module.exports.mqttClient = mqttClient;