Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SensorManager {
- // ... Other code snippets
- async fetch(url) {
- const resp = await fetch(url);
- if (!resp.ok) {
- throw new Error(await resp.text());
- }
- const json = await resp.json();
- return json;
- }
- async post(url, data) {
- const resp = await fetch(url, {
- method: 'post',
- body: data,
- headers: new Headers({ 'Content-Type': 'application/json' })
- });
- if (!resp.ok) {
- throw new Error(await resp.text());
- }
- const json = await resp.json();
- return json;
- }
- async delete(url) {
- const resp = await fetch(url, {
- method: 'delete'
- });
- if (!resp.ok) {
- throw new Error(await resp.text());
- }
- }
- //!>>>>>>>>>>> Find and replace original functions -- Start
- async addSensorAsync(objectId) {
- const code = this.guid();
- const data = {
- objectId, code,
- name: `sensor-${code}`,
- description: 'A temperature sensor'
- };
- //fetch POST sensors
- const json = await this.post('/iot/sensors', JSON.stringify(data));
- this.drawSensorMarkup(json);
- }
- async removeSensorAsync(objectId) {
- //fetch DELETE sensors
- const json = await this.fetch(`/iot/sensors?objectId=${objectId}`);
- if (json.length <= 0) throw `Sensor with objectId=${objectId} not found`;
- this.delete(`/iot/sensors/${json[0].id}`) ;
- this.removeSensorMarkup(objectId);
- }
- async showAllMarkups() {
- this.clearAllMarkups();
- //fetch GET sensors
- const sensors = await this.fetch('/iot/sensors');
- for (let sensor of sensors) {
- this.drawSensorMarkup(sensor);
- }
- }
- //!<<<<<<<<<<< Find and replace original functions -- End
- // ... Other code snippets
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement