Advertisement
yskang

threejs-viewer-35

Apr 20th, 2022
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (parent) {
  2.     const MQTT_MESSAGE_ARRIVED_EVENT = 'mqttMessageArrivedEvent';
  3.     class MqttWorker extends THREE.EventDispatcher {
  4.         constructor(parent, options) {
  5.             super();
  6.  
  7.             const guid = this.guid();
  8.             options = options || {};
  9.             this.parent = parent;
  10.             this.name = options.name || `threejs-mqtt-worker-${guid}`;
  11.             this.id = guid;
  12.             this.topics = options.topics || [];
  13.             this.options = options;
  14.             this.started = false;
  15.             this.setUp();
  16.         }
  17.  
  18.         guid(format = 'xxxxxxxxxx') {
  19.             let d = new Date().getTime();
  20.             return format.replace(
  21.                  /[xy]/g,
  22.                  function (c) {
  23.                      let r = (d + Math.random() * 16) % 16 | 0;
  24.                      d = Math.floor(d / 16);
  25.                      return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
  26.              });
  27.         }
  28.  
  29.         setUp() {
  30.             const features = this.parent.features;
  31.             const client = new Paho.Client(
  32.                 features.mqtt.broker.host, features.mqtt.broker.wsPort,
  33.                 this.id
  34.             );
  35.             client.onMessageArrived = (message) => {
  36.                 const payload = message.payloadString;
  37.                  if (!payload) {
  38.                      const error = new Error('EMPTY STATUS RESPONSE');
  39.                      error.raw = message; console.error(JSON.stringify(error));
  40.                  }
  41.                  let data = null;
  42.                  try {
  43.                      data = JSON.parse(payload);
  44.                      if (!data) throw 'Empty content of status response';
  45.                  } catch (ex) {
  46.                      const error = new Error('INVALID RESPONSE CONTENT');
  47.                      error.raw = ex; error.res = message;
  48.                      console.error(JSON.stringify(error));
  49.                  }
  50.                  this.dispatchEvent({ type: MQTT_MESSAGE_ARRIVED_EVENT, data });
  51.              };
  52.  
  53.             client.onConnectionLost = (responseObject) => {
  54.                 if (responseObject.errorCode !== 0 && responseObject.errorCode !== 8) {
  55.                     this.started = false;
  56.  
  57.                     const error = new Error('MQTT BROKER CONNECTION LOST');
  58.  
  59.                     error.raw = responseObject; console.error(JSON.stringify(error));
  60.                 }
  61.             };
  62.  
  63.             this.client = client;
  64.         }
  65.  
  66.         start() {
  67.             const client = this.client;
  68.             const opts = {
  69.                 //userName: 'guest',
  70.                 //password: 'guest',
  71.                 onSuccess: () => {
  72.                     this.started = true;
  73.  
  74.                     if (!this.topics || this.topics.length <= 0) return;
  75.  
  76.                     this.subscribeAll();
  77.                 },
  78.                 onFailure: (message) => {
  79.                     this.started = false;
  80.  
  81.                     if (this.client.isConnected())
  82.                         this.client.disconnect();
  83.  
  84.                     const error = new Error('MQTT CONNECTION FAILURE');
  85.                     error.raw = message; console.error(JSON.stringify(error));
  86.                 }
  87.             };
  88.             client.connect(opts);
  89.         }
  90.  
  91.         subscribe(topic) {
  92.             if (!topic || this.topics.includes(topic)) return;
  93.  
  94.             this.client.subscribe(topic, { qos: 2 });
  95.             this.topics.push(topic);
  96.         }
  97.  
  98.         unsubscribe(topic) {
  99.             if (!topic || !this.topics.includes(topic)) return;
  100.  
  101.             this.client.unsubscribe(topic);
  102.  
  103.             const index = this.topics.indexOf(topic);
  104.             if (index > -1)
  105.                 this.topics.splice(index, 1);
  106.         }
  107.  
  108.         subscribeAll() {
  109.             this.topics.forEach((topic) => {
  110.                 client.subscribe(topic, { qos: 2 });
  111.             });
  112.         }
  113.  
  114.         unsubscribeAll() {
  115.             this.topics.forEach((topic) => {
  116.                 client.unsubscribe(topic);
  117.             });
  118.  
  119.             while (this.topics.length > 0) {
  120.                 this.topics.pop();
  121.              }
  122.         }
  123.  
  124.         stop() {
  125.             try {
  126.                 this.started = false;
  127.  
  128.                 this.unsubscribeAll();
  129.  
  130.                 if (this.client.isConnected())
  131.                     this.client.disconnect();
  132.             } catch (ex) {
  133.                 console.error(ex);
  134.             }
  135.         }
  136.     }
  137.    
  138.     MqttWorker.MQTT_MESSAGE_ARRIVED_EVENT = MQTT_MESSAGE_ARRIVED_EVENT;
  139.     parent.MqttWorker = MqttWorker;
  140. })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement