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
This commit is contained in:
Jarno Rankinen 2023-02-01 22:12:56 +02:00
parent 81a7e5cb22
commit 3990a0a833
2 changed files with 45 additions and 10 deletions

View File

@ -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)

View File

@ -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.length; n++) {
tablerow = `<tr><td class="addr" id="addr_${data[n].address}">${data[n].address}</td>\
<td class ="val" id="value_${data[n].address}">${Number(data[n].value)}</td>\
<td class="symbol" id="symbol_${data[n].address}">${data[n].symbol}</td>\
<td class="desc" id="description_${data[n].address}">${data[n].description}</td></tr>`
document.getElementById('datatable').innerHTML += tablerow
}
}
function registers(data) {
console.log(`${timeStamp()} Filling register data...`)
document.getElementById('datatable').innerHTML = "";
for (n=0; n<data.length; n++) {
tablerow = `<tr><td class="addr" id="addr_${data[n].address}">${data[n].address}</td>\
<td class ="val" id="value_${data[n].address}">${Number(data[n].value)}</td>\
<td class="symbol" id="symbol_${data[n].address}">${data[n].symbol}</td>\
<td class="desc" id="description_${data[n].address}">${data[n].description}</td></tr>`
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.length; n++) {
tablerow = `<tr><td class="addr" id="addr_${data[n].address}">${data[n].address}</td>\
<td class ="val" id="value_${data[n].address}">${Number(data[n].value)}</td>\
<td class="symbol" id="symbol_${data[n].address}">${data[n].symbol}</td>\
<td class="desc" id="description_${data[n].address}">${data[n].description}</td></tr>`
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);
}