Advertisement
yskang

threejs-viewer-33

Apr 20th, 2022 (edited)
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SensorManager {
  2.  
  3.     // ... Other code snippets
  4.  
  5.     async fetch(url) {
  6.         const resp = await fetch(url);
  7.         if (!resp.ok) {
  8.             throw new Error(await resp.text());
  9.         }
  10.         const json = await resp.json();
  11.         return json;
  12.     }
  13.  
  14.     async post(url, data) {
  15.         const resp = await fetch(url, {
  16.             method: 'post',
  17.             body: data,
  18.             headers: new Headers({ 'Content-Type': 'application/json' })
  19.         });
  20.         if (!resp.ok) {
  21.             throw new Error(await resp.text());
  22.         }
  23.         const json = await resp.json();
  24.         return json;
  25.     }
  26.  
  27.     async delete(url) {
  28.         const resp = await fetch(url, {
  29.             method: 'delete'
  30.         });
  31.  
  32.         if (!resp.ok) {
  33.             throw new Error(await resp.text());
  34.         }
  35.     }
  36.  
  37.     //!>>>>>>>>>>> Find and replace original functions -- Start
  38.     async addSensorAsync(objectId) {
  39.         const code = this.guid();
  40.         const data = {
  41.             objectId, code,
  42.             name: `sensor-${code}`,
  43.             description: 'A temperature sensor'
  44.         };
  45.  
  46.         //fetch POST sensors
  47.         const json = await this.post('/iot/sensors', JSON.stringify(data));    
  48.         this.drawSensorMarkup(json);
  49.     }
  50.  
  51.     async removeSensorAsync(objectId) {
  52.         //fetch DELETE sensors
  53.         const json = await this.fetch(`/iot/sensors?objectId=${objectId}`);
  54.         if (json.length <= 0) throw `Sensor with objectId=${objectId} not found`;
  55.      
  56.         this.delete(`/iot/sensors/${json[0].id}`) ;
  57.  
  58.         this.removeSensorMarkup(objectId);
  59.     }
  60.  
  61.     async showAllMarkups() {
  62.         this.clearAllMarkups();
  63.         //fetch GET sensors
  64.         const sensors = await this.fetch('/iot/sensors');
  65.         for (let sensor of sensors) {
  66.             this.drawSensorMarkup(sensor);
  67.         }
  68.     }
  69.     //!<<<<<<<<<<< Find and replace original functions -- End
  70.    
  71.     // ... Other code snippets
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement