simulator.js
5.35 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
/******************************************************************************
* Data feed simulator for 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.
******************************************************************************/
const interval = 1000; /* ms */
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;
var SIM = {
acc: null,
rotation: null,
orientation: null,
loc: null,
on: false,
manual: false,
init: function()
{
var options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 10000,
};
navigator.geolocation.getCurrentPosition(function (pos) { SIM.loc = pos.coords; }, null, options);
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', function (eventData) {
SIM.acc = eventData.acceleration;
SIM.rotation = eventData.rotationRate;
}, false);
}
if (window.DeviceOrientationEvent) {
window.addEventListener('deviceorientation', function(eventData) { SIM.orientation = eventData; });
}
self.setInterval("SIM.refresh()", interval);
},
refresh: function()
{
var tick = (new Date()).getTime() % 0xffffffff;
document.getElementById("ts").value = tick;
if (this.manual) return;
if (this.loc) {
document.getElementById("lat").value = this.loc.latitude;
document.getElementById("lng").value = this.loc.longitude;
}
if (this.acc && this.acc.x) {
document.getElementById("acc_x").value = Math.floor(this.acc.x * 10);
document.getElementById("acc_y").value = Math.floor(this.acc.y * 10);
document.getElementById("acc_z").value = Math.floor(this.acc.z * 10);
}
if (this.orientation) {
document.getElementById("ori_alpha").value = Math.floor(this.orientation.alpha * 10);
document.getElementById("ori_beta").value = Math.floor(this.orientation.beta * 10);
document.getElementById("ori_gamma").value = Math.floor(this.orientation.gamma * 10);
}
if (battery) {
document.getElementById("battery").value = battery.level;
}
if (FEED.connected() && this.on) {
this.feed();
}
},
login: function()
{
var devid = document.getElementById("devid").value;
var ts = document.getElementById("ts").value;
if (FEED.login(devid, ts)) {
this.print("Login successfully. Feed ID: " + FEED.id);
} else {
this.print("Login failed.");
}
},
logout: function()
{
if (FEED.id) {
if (FEED.logout()) {
this.print("Logout successful.");
} else {
this.print("Logout failed.");
}
}
},
start: function ()
{
if (!FEED.connected()) {
this.print("Please login first.");
} else {
this.on = true;
}
},
stop: function ()
{
this.print("");
this.on = false;
},
feed: function()
{
if (!FEED.connected()) {
this.print("Please login first.");
return false;
}
FEED.purge();
FEED.add(PID.TS, document.getElementById("ts").value);
FEED.add(PID.GPS.LATITUDE, document.getElementById("lat").value);
FEED.add(PID.GPS.LONGITUDE, document.getElementById("lng").value);
var acc_x = document.getElementById("acc_x").value;
var acc_y = document.getElementById("acc_y").value;
var acc_z = document.getElementById("acc_z").value;
if (acc_x != "" && acc_y != "" && acc_z != "") {
FEED.add(PID.ACC, acc_x + ";" + acc_y + ";" + acc_z);
}
var ori_alpha = document.getElementById("ori_alpha").value;
var ori_beta = document.getElementById("ori_beta").value;
var ori_gamma = document.getElementById("ori_gamma").value;
if (ori_alpha != "" && ori_beta != "" && ori_gamma != "") {
FEED.add(PID.ORIENTATION, ori_alpha + ";" + ori_beta + ";" + ori_gamma);
}
var volt = document.getElementById("battery").value;
if (volt != "") {
FEED.add(PID.BATTERY_VOLTAGE, parseFloat(volt) * 100);
}
FEED.add(PID.DEVICE_TEMP, document.getElementById("device_temp").value);
if (!FEED.post()) {
this.print("Error feeding data to server. Network down?");
return false;
} else {
this.print(FEED.payload);
return true;
}
},
print: function(msg)
{
document.getElementById("info").innerText = msg;
}
};