-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
121 lines (115 loc) · 3.9 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
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
const https = require("https");
const TractiveClient = "6536c228870a3c8857d452e8";
const tAccount = require('./src/account');
const tPet = require('./src/pet');
const tTracker = require('./src/tracker');
const tCommands = require('./src/commands');
accountDetails = {
email: "",
password: ""
}
gloOpts = {
method: "GET",
hostname: "graph.tractive.com",
path: ``,
headers: {
"X-Tractive-Client": TractiveClient,
"Authorization": `Bearer ${accountDetails.token}`,
"content-type": "application/json"
}
};
isAuthenticated = function() {
if(accountDetails?.token) return true;
return false;
}
async function authenticate() {
return new Promise(function(resolve, reject) {/4/
const options = {
"method": "POST",
"hostname": "graph.tractive.com",
"path": `/4/auth/token?grant_type=tractive&platform_email=${encodeURIComponent(accountDetails.email)}&platform_token=${encodeURIComponent(accountDetails.password)}`,
"headers": {
'X-Tractive-Client': TractiveClient,
'Content-Type': "application/json"
}
};
const req = https.request(options, function (res) {
res.on('data', function(d) {
// process.stdout.write(d + '\n\n');
accountDetails.token = JSON.parse(d).access_token;
accountDetails.uid = JSON.parse(d).user_id;
gloOpts = {
method: "GET",
hostname: "graph.tractive.com",
path: ``,
headers: {
"X-Tractive-Client": TractiveClient,
"Authorization": `Bearer ${accountDetails.token}`,
"content-type": "application/json"
}
};
resolve(true);
});
res.on('error', function(err) {
resolve(false);
});
});
req.end();
});
return promise;
}
async function connect(email, password) {
accountDetails.email = email;
accountDetails.password = password;
await authenticate();
return isAuthenticated() ? true : false;
}
async function getTrackerGeofences(trackerID) {
if(!isAuthenticated()) return console.log('Not authenticated.');
return new Promise(function(resolve, reject) {
let options = gloOpts;
options.path = `/4/tracker/${trackerID}/geofences`;
const req = https.request(options, function (res) {
res.on('data', function(d) {
let data = JSON.parse(d);
resolve(data)
});
});
req.end();
});
}
async function getGeofence(fenceID) {
if(!isAuthenticated()) return console.log('Not authenticated.');
return new Promise(function(resolve, reject) {
let options = gloOpts;
options.path = `/4/geofence/${fenceID}`;
const req = https.request(options, function (res) {
res.on('data', function(d) {
let data = JSON.parse(d);
resolve(data)
});
});
req.end();
});
}
module.exports = {
connect: connect,
isAuthenticated: isAuthenticated,
getAccountInfo: tAccount.getAccountInfo,
getAccountSubscriptions: tAccount.getAccountSubscriptions,
getAccountSubscription: tAccount.getAccountSubscription,
getAccountShares: tAccount.getAccountShares,
getPets: tPet.getPets,
getPet: tPet.getPet,
getAllTrackers: tTracker.getAllTrackers,
getTracker: tTracker.getTracker,
getTrackerHistory: tTracker.getTrackerHistory,
getTrackerLocation: tTracker.getTrackerLocation,
getTrackerHardware: tTracker.getTrackerHardware,
liveOn: tCommands.liveOn,
liveOff: tCommands.liveOff,
LEDOn: tCommands.LEDOn,
LEDOff: tCommands.LEDOff,
buzzerOn: tCommands.BuzzerOn,
buzzerOff: tCommands.BuzzerOff
}