|
| 1 | +import {LineChart} from "./modules/chart.js"; |
| 2 | + |
| 3 | +const widgetTypeLineChart = "lineChart" |
| 4 | + |
| 5 | +class Widget { |
| 6 | + constructor(config) { |
| 7 | + this.config = config |
| 8 | + const container = document.getElementById(this.config.id); |
| 9 | + |
| 10 | + this.chart; |
| 11 | + if (this.config.chartType == widgetTypeLineChart) { |
| 12 | + this.chart = new LineChart(container) |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + async getDeviceData() { |
| 17 | + let data; |
| 18 | + const urlDeviceModule = "/api/v1/device-module/" + this.config.deviceModuleId |
| 19 | + let endpointDeviceModule = new Request(urlDeviceModule); |
| 20 | + |
| 21 | + await fetch(endpointDeviceModule) |
| 22 | + .then((response) => { |
| 23 | + if (!response.ok) { |
| 24 | + throw new Error(`HTTP error! Status: ${response.status}`); |
| 25 | + } |
| 26 | + |
| 27 | + return response.json(); |
| 28 | + }) |
| 29 | + .then((response) => { |
| 30 | + data = response |
| 31 | + |
| 32 | + return response |
| 33 | + }); |
| 34 | + return data |
| 35 | + } |
| 36 | + |
| 37 | + async getDeviceReadings() { |
| 38 | + let data; |
| 39 | + |
| 40 | + // const url = section.getAttribute("data-json-feed") |
| 41 | + const url = window.location.origin + "/api/v1/device-module-readings/" |
| 42 | + const endpoint = new URL(url); |
| 43 | + endpoint.searchParams.append("device_module", this.config.deviceModuleId); |
| 44 | + endpoint.searchParams.append("format", "json"); |
| 45 | + endpoint.searchParams.append("start", this.config.startDate); |
| 46 | + endpoint.searchParams.append("end", this.config.endDate); |
| 47 | + |
| 48 | + let endpointDeviceModuleReading = new Request(endpoint); |
| 49 | + |
| 50 | + await fetch(endpointDeviceModuleReading) |
| 51 | + .then((response) => { |
| 52 | + if (!response.ok) { |
| 53 | + throw new Error(`HTTP error! Status: ${response.status}`); |
| 54 | + } |
| 55 | + |
| 56 | + return response.json(); |
| 57 | + }) |
| 58 | + .then((response) => { |
| 59 | + data = response |
| 60 | + |
| 61 | + return response |
| 62 | + }); |
| 63 | + return data |
| 64 | + } |
| 65 | + |
| 66 | + async render() { |
| 67 | + const deviceData = await this.getDeviceData() |
| 68 | + const deviceReadings = await this.getDeviceReadings() |
| 69 | + |
| 70 | + this.chart.draw(deviceReadings, deviceData.schema) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +// TODO: DB Entry that controls this config, the page will then spit them out onto the page via backend, |
| 76 | +// we then loop through each class with config embedded |
| 77 | +const widgetConfigs = [ |
| 78 | + { |
| 79 | + "id": "chartContainer1", |
| 80 | + "chartType": widgetTypeLineChart, |
| 81 | + "deviceModuleId": "c2f30e74-4708-4dff-9347-466563e353c8", |
| 82 | + "startDate": "2024-01-25", |
| 83 | + "endDate": "2024-02-24" |
| 84 | + }, |
| 85 | + { |
| 86 | + "id": "chartContainer2", |
| 87 | + "chartType": widgetTypeLineChart, |
| 88 | + "deviceModuleId": "a62408c2-bc89-48e2-8c23-9494bbf33cb7", |
| 89 | + "startDate": "2024-01-25", |
| 90 | + "endDate": "2024-02-24" |
| 91 | + } |
| 92 | +]; |
| 93 | + |
| 94 | +widgetConfigs.forEach((widgetConfig) => { |
| 95 | + const widget = new Widget(widgetConfig) |
| 96 | + widget.render() |
| 97 | +}); |
0 commit comments