From 3990a0a833870ec51ba676f23d74ae4f0a76dc62 Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Wed, 1 Feb 2023 22:12:56 +0200 Subject: [PATCH] gh-5 Register API route somewhat works now Internal data handling needs much improvement, as signed/unsigned integers and other data types are not yet handled. Javascript seems like a really poor choice for parsing the html table, currently it takes ~10 seconds to hande the data. API response time is good, so probably switching to Go templates for the dynamic tables is the best course of action. This will move the load from clientside to serverside, hopefully the planned RPi Zero W can handle the computations --- enervent-ctrl-go/main.go | 8 ++++ enervent-ctrl-go/static/html/js/tabledata.js | 47 +++++++++++++++----- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/enervent-ctrl-go/main.go b/enervent-ctrl-go/main.go index cf4289d..664e38b 100644 --- a/enervent-ctrl-go/main.go +++ b/enervent-ctrl-go/main.go @@ -18,9 +18,17 @@ func coils(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(pingvin.Coils) } +func registers(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + log.Println("Received request for /registers") + json.NewEncoder(w).Encode(pingvin.Registers) + log.Println("Handled request for /registers") +} + func listen() { log.Println("Starting pingvinAPI...") http.HandleFunc("/api/v1/coils/", coils) + http.HandleFunc("/api/v1/registers/", registers) static := http.FileServer(http.Dir("./static/html")) http.Handle("/", static) err := http.ListenAndServe(":8888", nil) diff --git a/enervent-ctrl-go/static/html/js/tabledata.js b/enervent-ctrl-go/static/html/js/tabledata.js index f458dcf..1a548b9 100644 --- a/enervent-ctrl-go/static/html/js/tabledata.js +++ b/enervent-ctrl-go/static/html/js/tabledata.js @@ -1,7 +1,8 @@ function zeroPad(number) { return ("0" + number).slice(-2) } -function getData() { + +function timeStamp() { now = new Date() Y = now.getFullYear() m = now.getMonth() @@ -9,7 +10,36 @@ function getData() { H = zeroPad(now.getHours()) M = zeroPad(now.getMinutes()) S = zeroPad(now.getSeconds()) - document.getElementById('time').innerHTML = `${Y}-${m}-${d} ${H}:${M}:${S}` + return `${Y}-${m}-${d} ${H}:${M}:${S}` +} + +function coils(data) { + document.getElementById('datatable').innerHTML = ""; + for (n=0; n${data[n].address}\ + ${Number(data[n].value)}\ + ${data[n].symbol}\ + ${data[n].description}` + document.getElementById('datatable').innerHTML += tablerow + } +} + +function registers(data) { + console.log(`${timeStamp()} Filling register data...`) + document.getElementById('datatable').innerHTML = ""; + for (n=0; n${data[n].address}\ + ${Number(data[n].value)}\ + ${data[n].symbol}\ + ${data[n].description}` + document.getElementById('datatable').innerHTML += tablerow + } + console.log(`${timeStamp()} Done.`) +} + +function getData() { + // document.getElementById('time').innerHTML = `${Y}-${m}-${d} ${H}:${M}:${S}` + document.getElementById('time').innerHTML = timeStamp() error = false // The same index.html is used for both coil and register data, @@ -37,18 +67,15 @@ function getData() { }) .then((data) => { // Populate table - document.getElementById('datatable').innerHTML = ""; - for (n=0; n${data[n].address}\ - ${Number(data[n].value)}\ - ${data[n].symbol}\ - ${data[n].description}` - document.getElementById('datatable').innerHTML += tablerow + if (url == '/api/v1/coils') { + coils(data) + } else if (url == '/api/v1/registers') { + registers(data) } }); } // Using setTimeout instead of setInterval to avoid possible connection issues // There's no need to update exactly every 5 seconds, the skew is fine - setTimeout(getData, 1*1000); + setTimeout(getData, 30*1000); } \ No newline at end of file