yskang

threejs-viewer-36

Apr 20th, 2022 (edited)
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. constructor(viewer) {
  2.     this.viewer = viewer;
  3.     this.features = null;
  4.     this.onMqttMessageArrived = this.onMqttMessageArrived.bind(this);
  5.  
  6.     this.setUp();
  7. }
  8.  
  9. async setUp() {
  10.     this.features = await this.fetch('/Iot/features');
  11.  
  12.     const worker = new MqttWorker(this);
  13.     worker.start();
  14.  
  15.     worker.addEventListener(
  16.         MqttWorker.MQTT_MESSAGE_ARRIVED_EVENT,
  17.         this.onMqttMessageArrived
  18.     );
  19.  
  20.     this.mqttWorker = worker;
  21.     this.topicPrefix = this.features.mqtt.topic;
  22.    
  23.     console.log(`MQTT Topic prefix: ${this.topicPrefix}`);
  24. }
  25.  
  26.  
  27. //!>>>>>>>>>>>>>>> Find and replace the original function -- Start
  28. drawSensorMarkup(data) {
  29.     if (!data || !data.objectId) return;
  30.  
  31.     const markup = document.createElement('label');
  32.     let guid = data.code || this.guid();
  33.     data.code = guid;
  34.     data.mqttTopic = `${this.topicPrefix}/${data.code}`;
  35.     data.label = 'Nan';
  36.  
  37.     console.log('Markup created:', data);
  38.  
  39.     this.mqttWorker.subscribe(data.mqttTopic);
  40.  
  41.     markup.id = `sensor-markup-${guid}`;
  42.     markup.classList.add('markup', 'update');
  43.     markup.setAttribute('data-code', guid);
  44.     markup.setAttribute('data-dbId', data.id);
  45.     markup.setAttribute('data-objectId', data.objectId);
  46.     markup.style.display = 'block';
  47.  
  48.     this.viewer.container.appendChild(markup);
  49.  
  50.     const markupContent = document.createElement('span');
  51.     markupContent.classList.add('temperatureBorder', 'temperatureOk', 'fas', 'fa-thermometer-empty');
  52.     markupContent.innerHTML = data.label;//'450°C';
  53.  
  54.     markup.appendChild(markupContent);
  55. }
  56.  
  57. onMqttMessageArrived(event) {
  58.     const data = event.data;
  59.     if (!data) return;
  60.  
  61.     const code = data.code;
  62.     const val = (!isNaN(data.value)) ? data.value.toFixed(2) : 0;
  63.     const markupContent = document.querySelector(`label.markup[data-code="${code}"] span`);
  64.  
  65.     if (!markupContent) return;
  66.  
  67.     if (val > 25) {
  68.         markupContent.classList.remove('temperatureOk', 'fa-thermometer-empty');
  69.         markupContent.classList.add('temperatureHigh', 'fa-thermometer-full');
  70.     } else {
  71.         if (markupContent.classList.contains('temperatureHigh')) {
  72.             markupContent.classList.add('temperatureOk', 'fa-thermometer-empty');
  73.             markupContent.classList.remove('temperatureHigh', 'fa-thermometer-full');
  74.         }
  75.     }
  76.  
  77.     markupContent.innerHTML = `${val}°C`;
  78. }
  79. //!<<<<<<<<<<<<<<< Find and replace the original function -- End
Advertisement
Add Comment
Please, Sign In to add comment