parking.js
5.36 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/******************************************************************************
* Web based vehicle tracker based on Freematics Hub
* Developed by Stanley Huang https://www.facebook.com/stanleyhuangyc
* Distributed under BSD license
* Visit http://freematics.com/hub/api for Freematics Hub API reference
* To obtain your Freematics Hub server key, contact support@freematics.com.au
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
onResize();
$(function () {
$(document).ready(function () {
USER.load("DASH.load()");
self.setInterval(function () {
if (DASH.curLocation && OSMAP.map) OSMAP.setCenter(DASH.curLocation);
}, MAP_CENTERING_INTERVAL);
});
});
if (window.require) {
const shell = require('electron').shell;
function openLink(url) {
shell.openExternal(url);
}
}
function onResize()
{
var height = window.innerHeight;
var width = window.innerWidth;
//if (mapHeight < 300) mapHeight = 300;
if (width > height) {
document.getElementById("container").style.height = height + "px";
document.getElementById("map").style.height = height / 2 + "px";
document.getElementById("info").style.height = height / 2 + "px";
} else {
document.getElementById("container").style.height = height + "px";
document.getElementById("map").style.height = height * 3 / 4 + "px";
document.getElementById("info").style.height = height / 4 + "px";
}
}
var DASH = {
xhr: new XMLHttpRequest(),
deviceID: null,
curLocation: null,
data: null,
updateUserInfo: function (info, devid)
{
if (!USER.info) {
document.getElementById("userinfo").innerHTML = "<input type='button' onclick='location.href=\"/\";' value='Home'></input>";
return;
}
var s = "<select onchange='USER.goDash(this.value)'>";
var found = false;
for (var i = 0; i < info.devid.length; i++) {
s += "<option value=\"" + info.devid[i] + "\"";
if (info.devid[i] == devid) {
s += " selected";
found = true;
}
s += ">" + info.devid[i] + "</option>";
}
if (!found) {
s += "<option value=\"" + devid + "\" selected>" + devid + "</option>";
}
s += "</select><input type='button' onclick='USER.goNextDash(previousSibling.value)' value='Switch'></input><input type='button' onclick='location.href=\"/\";' value='Home'></input>";
document.getElementById("userinfo").innerHTML = s;
},
setText: function(name, text)
{
document.getElementById(name).innerText = text;
},
setClass: function (name, className)
{
document.getElementById(name).className = className;
},
setHTML: function(name, html)
{
document.getElementById(name).innerHTML = html;
},
getPIDValue: function (pid)
{
for (var i = 0; i < this.data.length; i++) {
if (this.data[i][0] == pid) {
return PID.normalize(pid, this.data[i][1]);
}
}
return null;
},
update: function (ch)
{
var parked = ch.parked || ch.age.data > TRIP_END_TIMEOUT;
if (parked) {
this.setText("elapsed", getHHMM(Math.floor(ch.age.data / 1000)));
this.setText("state", "PARKED");
this.setClass("state", "state_parked");
} else {
this.setText("elapsed", getHHMMSS(ch.elapsed));
this.setText("state", "RUNNING");
this.setClass("state", "state_running");
}
this.data = ch.data;
// update map
var lat = this.getPIDValue(PID.GPS.LATITUDE);
var lng = this.getPIDValue(PID.GPS.LONGITUDE);
if (lat != null && lng != null && lat != 0 && lng != 0) {
if (!OSMAP.map) OSMAP.init("map", lat, lng, 20);
//if (devid) OSMAP.setTooltip(0, devid);
if (!this.curLocation || this.curLocation[0] != lat || this.curLocation[1] != lng) {
this.curLocation = [lat, lng];
OSMAP.setMarker(0, this.curLocation);
OSMAP.setCenter(this.curLocation);
}
}
},
load: function()
{
this.updateUserInfo(USER.info, USER.devid);
this.reload();
},
reload: function()
{
// load channel data
this.xhr.onreadystatechange = function() {
if (this.readyState != 4) return;
if (this.status != 200) {
if (this.status) {
alert("Server under maintenance (status: " + this.status + ")");
}
return;
}
var chdata = JSON.parse(this.responseText);
if (chdata && chdata.id) {
DASH.deviceID = chdata.devid;
DASH.update(chdata);
self.setTimeout("DASH.reload()", chdata.parked ? DATA_FETCH_INTERVAL_PARKED : DATA_FETCH_INTERVAL);
} else {
alert("Not an active device. Please check if your device is working and the device ID is correct.");
}
};
var url = serverURL + "channels/" + USER.devid + "?data=1";
this.xhr.open('GET', url, true);
this.xhr.send(null);
}
};