UNPKG

mqttjs-mongeriot

Version:

A javascript library that allows you to send mqtt commands to an mqtt server.

75 lines (60 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _pahoMqtt = _interopRequireDefault(require("paho-mqtt")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } // What we need to send this command is array of devices, array of commands. In array of commands we need the action and the value/parameter var mqttCommandEmitter = /*#__PURE__*/ function () { function mqttCommandEmitter() { _classCallCheck(this, mqttCommandEmitter); } _createClass(mqttCommandEmitter, [{ key: "sendMultiCommands", value: function sendMultiCommands(items, commands, connectionAddress, port) { var clientID = "clientID_" + parseInt(Math.random() * 100); var client = new _pahoMqtt["default"].Client(connectionAddress, Number(port), clientID); client.connect({ onSuccess: onMultiCommandsConnect }); function onMultiCommandsConnect() { for (var d = 0; d < items.length; d++) { var topic = "".concat(items[d]); client.subscribe(topic); for (var c = 0; c < commands.length; c++) { var value = !commands[c].value; var message = "".concat(commands[c], ":").concat(value); client.publish(topic, message); } } } } }, { key: "sendCommand", value: function sendCommand(item, commands, connectionAddress, port) { var clientID = "clientID_" + parseInt(Math.random() * 100); var client = new _pahoMqtt["default"].Client(connectionAddress, Number(port), clientID); client.connect({ onSuccess: onSingleCommandConnect }); function onSingleCommandConnect() { var topic = "".concat(item); client.subscribe(topic); for (var c = 0; c < commands.length; c++) { var value = !commands[c].value; var message = "".concat(commands[c], ":").concat(value); client.publish(topic, message); } } } }]); return mqttCommandEmitter; }(); var _default = mqttCommandEmitter; exports["default"] = _default;