templates/stop.html

changeset 134
4ac0f2e2ec4e
parent 133
091a48867bae
child 136
27324a81beca
--- a/templates/stop.html	Thu Dec 06 20:47:19 2018 +0200
+++ b/templates/stop.html	Thu Dec 06 23:39:20 2018 +0200
@@ -36,7 +36,8 @@
 	}
 	</style>
 	<script>
-	var getJSON = function(url, callback) {
+	var getJSON = function(url, callback)
+	{
 		var xhr = new XMLHttpRequest();
 		xhr.open('GET', url, true);
 		xhr.responseType = 'json';
@@ -49,21 +50,8 @@
 		xhr.send();
 	};
 	
-	var getUuid = function(blockref, originaimeddeparturetime, data, callback) {
-		var xhr = new XMLHttpRequest();
-		var url = "../find_halt/{{ref}}/" + blockref + "/" + originaimeddeparturetime.toString();
-		xhr.open('GET', url, true);
-		xhr.responseType = 'json';
-		xhr.onload = function() {
-			var status = xhr.status;
-			if (status === 200) {
-				callback(xhr.response['id'], data);
-			}
-		};
-		xhr.send();
-	};
-	
-	function timeConverter(UNIX_timestamp){
+	function timeConverter(UNIX_timestamp)
+	{
 		var a = new Date(UNIX_timestamp * 1000);
 		var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
 		var year = a.getFullYear();
@@ -76,18 +64,39 @@
 		return time;
 	}
 	
-	function updateTime(uuid, time) {
-		const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+	function epoch()
+	{
+		var d = new Date();
+		if (d.getHours() < 4)
+			d.setDate(d.getDate() - 1);
+		d.setHours(0);
+		d.setMinutes(0);
+		d.setSeconds(0);
+		return Math.floor(d.getTime() / 1000);
+	}
+	
+	function primarykey(data)
+	{
+		var key = data['blockref'] + '-';
+		key += (data['originaimeddeparturetime'] - epoch()).toString() + '-';
+		key += data['visitnumber'];
+		return key;
+	}
+	
+	function updateTime(key, time)
+	{
 		const d = new Date();
 		const now = (d.getTime() + d.getTimezoneOffset()) / 1000;
-		row = document.getElementById(uuid);
-		timeCell = document.getElementById(uuid + '-time');
-		t0 = parseInt(document.getElementById(uuid + '-stamp').innerHTML);
-		if (Math.abs(time - t0) > 90) {
+		row = document.getElementById(key);
+		timeCell = document.getElementById(key + '-time');
+		t0 = parseInt(document.getElementById(key + '-stamp').innerHTML);
+		if (Math.abs(time - t0) > 90)
+		{
 			oldTime = timeConverter(t0);
 			newTime = timeConverter(time);
 			timeCell.innerHTML = '<s style="color:gray">' + oldTime + '</s> ⟶ <b>' + newTime + '</b>';
 		}
+		
 		if (time + 15 > now)
 			row.classList.remove('gone');
 		else
@@ -153,9 +162,8 @@
 				var blockref = data['result'][i]['blockref'];
 				var originaimeddeparturetime = data['result'][i]['originaimeddeparturetime'];
 				var expecteddeparturetime = data['result'][i]['expecteddeparturetime'];
-				getUuid(blockref, originaimeddeparturetime, expecteddeparturetime, function(id, expecteddeparturetime) {
-					updateTime(id, expecteddeparturetime);
-				});
+				var key = primarykey(data['result'][i]);
+				updateTime(key, expecteddeparturetime);
 			}
 		});
 	</script>

mercurial