-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
84 lines (67 loc) · 2.5 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
window.onload = async () => {
let data = await fetchData()
let imgcontainer = document.getElementById('img-container')
// Lazy code by Turtleb01: outdated-ness is not checked when updating
let responseTs = Date.parse(data.meta.responseTs)
data.results.map(item => imgcontainer.appendChild(buildCard(item, responseTs)))
// Set interval to update images every two minutes
setInterval(() => updateImages(), 121000)
}
const fetchData = async () => {
let url = "https://traffic-cameras.tampere.fi/api/v1/cameras"
let data = await fetch(url)
if(data.ok) {
return await data.json()
} else {
alert("Error fetching data" + data.status)
}
}
const buildCard = (item, ts) => {
// Initialize card
let card = document.createElement('div')
let templateCard = document.getElementById("imgCardTemplate").content.cloneNode(true)
card.append(templateCard)
// Card elements
let cardImg = card.getElementsByTagName("img")[0]
let cardText = card.getElementsByTagName("a")[0]
let location = item.location.geometry.coordinates
let cameraPresets = item.cameraPresets[0]
let cameraTs = Date.parse(cameraPresets.latestPictureTimestamp)
let tsDifference = (ts - cameraTs)
let outdated
// Fill out the blanks
card.id = item.cameraId
card.className = "img-card"
cardImg.alt = item.cameraPresets[0].directionDescription
cardImg.src = item.cameraPresets[0].imageUrl
cardText.innerText = item.cameraName
cardText.href = `https://www.google.com/maps/search/?api=1&query=${location.lat},${location.lon}`
cardText.target = "_blank"
// Check if the images are outdated
// if tsDifference > 10 minutes, mark as yellow
// if tsDifference > week, mark as red
if(tsDifference > 600000) {
outdated = true
card.className += (tsDifference > 604800000) ? " img-card-red" : " img-card-yellow"
if(outdated) {
card.className += " outdated" // currently completely hidden, but append required data anyway for later use
let imgTime = new Date(cameraPresets.latestPictureTimestamp).toLocaleDateString()
cardText.innerText += ` ${imgTime}`
}
} else {
card.className += " img-card-green"
}
// Hide image if it errors
cardImg.addEventListener("error", (event) => {
event.target.parentNode.style.display = "none"
})
return card
}
// Update only the image elements in the cards
const updateImages = async () => {
let data = await fetchData()
data.results.map((item) => {
let element = document.getElementById(item.cameraId)
element.children[0].src = item.cameraPresets[0].imageUrl
})
}