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:
parent
81a7e5cb22
commit
3990a0a833
|
@ -18,9 +18,17 @@ func coils(w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(pingvin.Coils)
|
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() {
|
func listen() {
|
||||||
log.Println("Starting pingvinAPI...")
|
log.Println("Starting pingvinAPI...")
|
||||||
http.HandleFunc("/api/v1/coils/", coils)
|
http.HandleFunc("/api/v1/coils/", coils)
|
||||||
|
http.HandleFunc("/api/v1/registers/", registers)
|
||||||
static := http.FileServer(http.Dir("./static/html"))
|
static := http.FileServer(http.Dir("./static/html"))
|
||||||
http.Handle("/", static)
|
http.Handle("/", static)
|
||||||
err := http.ListenAndServe(":8888", nil)
|
err := http.ListenAndServe(":8888", nil)
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
function zeroPad(number) {
|
function zeroPad(number) {
|
||||||
return ("0" + number).slice(-2)
|
return ("0" + number).slice(-2)
|
||||||
}
|
}
|
||||||
function getData() {
|
|
||||||
|
function timeStamp() {
|
||||||
now = new Date()
|
now = new Date()
|
||||||
Y = now.getFullYear()
|
Y = now.getFullYear()
|
||||||
m = now.getMonth()
|
m = now.getMonth()
|
||||||
|
@ -9,7 +10,36 @@ function getData() {
|
||||||
H = zeroPad(now.getHours())
|
H = zeroPad(now.getHours())
|
||||||
M = zeroPad(now.getMinutes())
|
M = zeroPad(now.getMinutes())
|
||||||
S = zeroPad(now.getSeconds())
|
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
|
error = false
|
||||||
// The same index.html is used for both coil and register data,
|
// The same index.html is used for both coil and register data,
|
||||||
|
@ -37,18 +67,15 @@ function getData() {
|
||||||
})
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
// Populate table
|
// Populate table
|
||||||
document.getElementById('datatable').innerHTML = "";
|
if (url == '/api/v1/coils') {
|
||||||
for (n=0; n<data.length; n++) {
|
coils(data)
|
||||||
tablerow = `<tr><td class="addr" id="addr_${data[n].address}">${data[n].address}</td>\
|
} else if (url == '/api/v1/registers') {
|
||||||
<td class ="val" id="value_${data[n].address}">${Number(data[n].value)}</td>\
|
registers(data)
|
||||||
<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
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using setTimeout instead of setInterval to avoid possible connection issues
|
// Using setTimeout instead of setInterval to avoid possible connection issues
|
||||||
// There's no need to update exactly every 5 seconds, the skew is fine
|
// There's no need to update exactly every 5 seconds, the skew is fine
|
||||||
setTimeout(getData, 1*1000);
|
setTimeout(getData, 30*1000);
|
||||||
}
|
}
|
Loading…
Reference in New Issue