-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (43 loc) · 1.4 KB
/
index.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
var tessel = require('tessel');
var gpsLib = require('gps-a2235h');
var gps = gpsLib.use(tessel.port['A']);
var sha1 = require('sha1');
var Client = require('node-rest-client').Client;
var fs;
fs = require('fs');
var config = JSON.parse(fs.readFileSync("config.json"));
var client = new Client();
// Wait until the module is connected
gps.on('ready', function () {
console.log('GPS module powered and ready. Waiting for satellites...');
// Emit coordinates when we get a coordinate fix
gps.on('coordinates', function (coords) {
console.log('Lat:', coords.lat, '\tLon:', coords.lon, '\tTimestamp:', coords.timestamp);
var now = Date.now();
var args = {
data: {
"objectId" : config.objectId,
"date" : now,
"longitude" : coords.lon,
"latitude" : coords.lat
},
headers: { "Content-Type": "application/json",
"Authorization" : sha1(config.token+now)}
};
client.registerMethod("postMethod", server, "POST");
client.methods.postMethod(args, function (data, response) {
console.log(response);
});
});
// Emitted when we have information about a fix on satellites
gps.on('fix', function (data) {
console.log(data.numSat, 'fixed.');
});
gps.on('dropped', function(){
// we dropped the gps signal
console.log("gps signal dropped");
});
});
gps.on('error', function(err){
console.log("got this error", err);
});