Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (parent) {
- const MQTT_MESSAGE_ARRIVED_EVENT = 'mqttMessageArrivedEvent';
- class MqttWorker extends THREE.EventDispatcher {
- constructor(parent, options) {
- super();
- const guid = this.guid();
- options = options || {};
- this.parent = parent;
- this.name = options.name || `threejs-mqtt-worker-${guid}`;
- this.id = guid;
- this.topics = options.topics || [];
- this.options = options;
- this.started = false;
- this.setUp();
- }
- guid(format = 'xxxxxxxxxx') {
- let d = new Date().getTime();
- return format.replace(
- /[xy]/g,
- function (c) {
- let r = (d + Math.random() * 16) % 16 | 0;
- d = Math.floor(d / 16);
- return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
- });
- }
- setUp() {
- const features = this.parent.features;
- const client = new Paho.Client(
- features.mqtt.broker.host, features.mqtt.broker.wsPort,
- this.id
- );
- client.onMessageArrived = (message) => {
- const payload = message.payloadString;
- if (!payload) {
- const error = new Error('EMPTY STATUS RESPONSE');
- error.raw = message; console.error(JSON.stringify(error));
- }
- let data = null;
- try {
- data = JSON.parse(payload);
- if (!data) throw 'Empty content of status response';
- } catch (ex) {
- const error = new Error('INVALID RESPONSE CONTENT');
- error.raw = ex; error.res = message;
- console.error(JSON.stringify(error));
- }
- this.dispatchEvent({ type: MQTT_MESSAGE_ARRIVED_EVENT, data });
- };
- client.onConnectionLost = (responseObject) => {
- if (responseObject.errorCode !== 0 && responseObject.errorCode !== 8) {
- this.started = false;
- const error = new Error('MQTT BROKER CONNECTION LOST');
- error.raw = responseObject; console.error(JSON.stringify(error));
- }
- };
- this.client = client;
- }
- start() {
- const client = this.client;
- const opts = {
- //userName: 'guest',
- //password: 'guest',
- onSuccess: () => {
- this.started = true;
- if (!this.topics || this.topics.length <= 0) return;
- this.subscribeAll();
- },
- onFailure: (message) => {
- this.started = false;
- if (this.client.isConnected())
- this.client.disconnect();
- const error = new Error('MQTT CONNECTION FAILURE');
- error.raw = message; console.error(JSON.stringify(error));
- }
- };
- client.connect(opts);
- }
- subscribe(topic) {
- if (!topic || this.topics.includes(topic)) return;
- this.client.subscribe(topic, { qos: 2 });
- this.topics.push(topic);
- }
- unsubscribe(topic) {
- if (!topic || !this.topics.includes(topic)) return;
- this.client.unsubscribe(topic);
- const index = this.topics.indexOf(topic);
- if (index > -1)
- this.topics.splice(index, 1);
- }
- subscribeAll() {
- this.topics.forEach((topic) => {
- client.subscribe(topic, { qos: 2 });
- });
- }
- unsubscribeAll() {
- this.topics.forEach((topic) => {
- client.unsubscribe(topic);
- });
- while (this.topics.length > 0) {
- this.topics.pop();
- }
- }
- stop() {
- try {
- this.started = false;
- this.unsubscribeAll();
- if (this.client.isConnected())
- this.client.disconnect();
- } catch (ex) {
- console.error(ex);
- }
- }
- }
- MqttWorker.MQTT_MESSAGE_ARRIVED_EVENT = MQTT_MESSAGE_ARRIVED_EVENT;
- parent.MqttWorker = MqttWorker;
- })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement