2023-02-01 21:52:55 +02:00
|
|
|
function zeroPad(number) {
|
|
|
|
return ("0" + number).slice(-2)
|
|
|
|
}
|
2023-02-01 22:12:56 +02:00
|
|
|
|
|
|
|
function timeStamp() {
|
2023-02-01 21:52:55 +02:00
|
|
|
now = new Date()
|
|
|
|
Y = now.getFullYear()
|
|
|
|
m = now.getMonth()
|
|
|
|
d = now.getDate()
|
|
|
|
H = zeroPad(now.getHours())
|
|
|
|
M = zeroPad(now.getMinutes())
|
|
|
|
S = zeroPad(now.getSeconds())
|
2023-02-01 22:12:56 +02:00
|
|
|
return `${Y}-${m}-${d} ${H}:${M}:${S}`
|
|
|
|
}
|
|
|
|
|
|
|
|
function coils(data) {
|
2023-02-02 13:25:40 +02:00
|
|
|
datable = document.getElementById('datatable')
|
|
|
|
|
|
|
|
for (n=0; n<data.length; n++) {
|
|
|
|
tablerow = document.createElement("tr")
|
|
|
|
fields = ["address", "value", "symbol", "description"]
|
|
|
|
|
|
|
|
for (i=0; i<fields.length; i++) {
|
|
|
|
td = document.createElement("td")
|
|
|
|
if (fields[i] == "value") {
|
|
|
|
value = document.createTextNode(Number(data[n][fields[i]]))
|
2023-02-02 13:31:24 +02:00
|
|
|
} else {
|
|
|
|
value = document.createTextNode(data[n][fields[i]])
|
2023-02-01 22:12:56 +02:00
|
|
|
}
|
2023-02-02 13:25:40 +02:00
|
|
|
td.appendChild(value)
|
|
|
|
tablerow.appendChild(td)
|
|
|
|
}
|
|
|
|
|
|
|
|
datatable.appendChild(tablerow)
|
|
|
|
}
|
2023-02-01 22:12:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function registers(data) {
|
|
|
|
console.log(`${timeStamp()} Filling register data...`)
|
2023-02-02 13:25:40 +02:00
|
|
|
datable = document.getElementById('datatable')
|
2023-02-02 13:10:30 +02:00
|
|
|
|
2023-02-01 22:12:56 +02:00
|
|
|
for (n=0; n<data.length; n++) {
|
2023-02-02 13:10:30 +02:00
|
|
|
tablerow = document.createElement("tr")
|
|
|
|
fields = ["address", "value", "symbol", "description"]
|
|
|
|
|
|
|
|
for (i=0; i<fields.length; i++) {
|
|
|
|
td = document.createElement("td")
|
2023-02-02 13:31:24 +02:00
|
|
|
if (fields[i] == "value" && data[n].type == "bitfield") {
|
|
|
|
value = document.createTextNode(data[n].bitfield)
|
|
|
|
} else {
|
|
|
|
value = document.createTextNode(data[n][fields[i]])
|
|
|
|
}
|
2023-02-02 13:10:30 +02:00
|
|
|
td.appendChild(value)
|
|
|
|
tablerow.appendChild(td)
|
2023-02-02 00:07:36 +02:00
|
|
|
}
|
2023-02-02 13:10:30 +02:00
|
|
|
|
|
|
|
datatable.appendChild(tablerow)
|
2023-02-01 22:12:56 +02:00
|
|
|
}
|
|
|
|
console.log(`${timeStamp()} Done.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getData() {
|
|
|
|
// document.getElementById('time').innerHTML = `${Y}-${m}-${d} ${H}:${M}:${S}`
|
|
|
|
document.getElementById('time').innerHTML = timeStamp()
|
2023-02-01 21:52:55 +02:00
|
|
|
|
|
|
|
error = false
|
|
|
|
// The same index.html is used for both coil and register data,
|
|
|
|
// change api url based on which we're looking at
|
|
|
|
if (document.location.pathname == "/coils/") {
|
|
|
|
url = "/api/v1/coils"
|
|
|
|
document.getElementById("title").innerHTML = "Coils | Enervent Pingvin Kotilämpö"
|
2023-02-02 13:25:40 +02:00
|
|
|
document.getElementById('caption').innerHTML = "Coil values at "
|
2023-02-01 21:52:55 +02:00
|
|
|
}
|
|
|
|
else if (document.location.pathname == "/registers/") {
|
|
|
|
url = "/api/v1/registers"
|
|
|
|
document.getElementById("title").innerHTML = "Registers | Enervent Pingvin Kotilämpö"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
document.getElementById("data").innerHTML = 'Page not found'
|
|
|
|
error = true
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
// Fetch data from API
|
|
|
|
fetch(url)
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Error fetching data: ${response.status}`)
|
|
|
|
}
|
|
|
|
return response.json()
|
|
|
|
})
|
|
|
|
.then((data) => {
|
|
|
|
// Populate table
|
2023-02-01 22:12:56 +02:00
|
|
|
if (url == '/api/v1/coils') {
|
|
|
|
coils(data)
|
|
|
|
} else if (url == '/api/v1/registers') {
|
|
|
|
registers(data)
|
2023-02-01 21:52:55 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using setTimeout instead of setInterval to avoid possible connection issues
|
|
|
|
// There's no need to update exactly every 5 seconds, the skew is fine
|
2023-02-02 13:25:40 +02:00
|
|
|
setTimeout(getData, 5*1000);
|
2023-02-01 21:52:55 +02:00
|
|
|
}
|