Guest User

Untitled

a guest
Feb 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. var mqtt = require('mqtt');
  2. var platform;
  3. var client;
  4. var config = {host: "", port: 0, credentials: false, channel: ""};
  5.  
  6. module.exports = {
  7. Mqtt: Mqtt
  8. };
  9.  
  10. function Mqtt(aPlatform, host, port, channel, credentials) {
  11. platform = aPlatform;
  12.  
  13. config = {host: host, port: port, credentials: credentials, channel: channel};
  14. if (typeof config.credentials === 'undefined' || typeof config.credentials.username === 'undefined' || config.credentials.username.length === 0) {
  15. config.credentials = false;
  16. }
  17. this.connect();
  18. }
  19.  
  20. Mqtt.prototype.connect = function() {
  21. var connectOptions = {
  22. host: config.host,
  23. port: config.port
  24. };
  25.  
  26. if (config.credentials)
  27. {
  28. connectOptions.username = config.credentials.username;
  29. connectOptions.password = config.credentials.password;
  30. }
  31.  
  32. client = mqtt.connect(connectOptions);
  33.  
  34. client.on('connect', function() {
  35. platform.forceLog("Successfully connected to MQTT broker.");
  36. client.subscribe(config.channel);
  37. });
  38.  
  39. client.on('close', function(error) {
  40. client.end(true, function() {
  41. this.error("Retrying in 5 seconds...");
  42. setTimeout(function() {
  43. platform.forceLog("Retrying connection to MQTT broker...");
  44. this.connect();
  45. }.bind(this), 5000);
  46. }.bind(this));
  47.  
  48. }.bind(this));
  49.  
  50. client.on('error', function(error) {
  51. client.end(true, function() {
  52. this.error(error);
  53. }.bind(this));
  54. }.bind(this));
  55.  
  56. client.on('message', function (topic, buffer) {
  57. var incoming = buffer.toString(),
  58. message, err = !1;
  59.  
  60. try {
  61. message = JSON.parse(buffer.toString());
  62. } catch (e) {
  63. err = !0;
  64. message = e;
  65. }
  66. if (typeof message.nvalue !== 'undefined' || typeof message.svalue1 !== 'undefined') {
  67. var accessory = platform.accessories.find(function(acc) {
  68. return acc.idx == message.idx;
  69. });
  70.  
  71. if (!accessory) {
  72. return;
  73. }
  74.  
  75. accessory.handleMQTTMessage(message, function(characteristic, value) {
  76. if (typeof value !== 'undefined' && typeof characteristic !== 'undefined') {
  77. characteristic.setValue(value, null, "eDomoticz-MQTT");
  78. }
  79. });
  80.  
  81. } else {
  82. var error = '[ERR] MQTT message received, but no nvalue or svalue1 was found:';
  83. platform.log(error);
  84. platform.log(message);
  85. }
  86. });
  87. };
  88.  
  89. Mqtt.prototype.send = function(message) {
  90. if (client)
  91. {
  92. var payload = message;
  93. if (typeof payload !== 'string') {
  94. payload = JSON.stringify(payload);
  95. }
  96. client.publish('domoticz/in', payload);
  97. }
  98. };
  99.  
  100. Mqtt.prototype.error = function(error) {
  101. var logMessage = "Could not connect to MQTT broker! (" + config.host + ":" + config.port + ")\n";
  102.  
  103. if (config.credentials !== false) {
  104. logMessage += "Note: You're using a username and password to connect. Please verify your username and password too.\n";
  105. }
  106.  
  107. if (error) {
  108. logMessage += error;
  109. }
  110.  
  111. platform.forceLog(logMessage);
  112. };
Add Comment
Please, Sign In to add comment