-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautoMowerAPI.js
193 lines (169 loc) · 5.66 KB
/
autoMowerAPI.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const request = require('request');
//var locks = require('locks');
//var mutex = locks.createMutex();
var EventEmitter = require('events');
const autoMowerConst = require('./autoMowerConst');
var inherits = require('util').inherits;
module.exports = {
AutoMowerAPI: AutoMowerAPI,
};
function AutoMowerAPI(log, platform) {
EventEmitter.call(this);
this.log = log;
this.platform = platform;
this.login = platform.login;
this.password = platform.password;
this.discoverdMowers = [];
this.headers = {
Accept: 'application/json',
'Content-type': 'application/json',
};
this.imApiUrl = 'https://iam-api.dss.husqvarnagroup.net/api/v3/';
this.trackApiUrl = 'https://amc-api.dss.husqvarnagroup.net/app/v1/';
}
AutoMowerAPI.prototype = {
logResult: function (result) {
this.log.debug('INFO - mower status : ' + JSON.stringify(result.status));
this.log.debug('INFO - mower activity : ' + result.status.mowerStatus.activity);
},
authenticate: function (callback) {
var dte = new Date();
if (!this.token || (this.token && this.loginExpires && this.loginExpires < dte)) {
this.log.debug('INFO - authenticating');
var jsonBody = {
data: {
attributes: {
password: this.password,
username: this.login,
},
type: 'token',
},
};
var that = this;
//mutex.lock(function () {
request(
{
url: that.imApiUrl + 'token',
method: 'POST',
headers: that.headers,
body: jsonBody,
json: true,
},
function (error, response, body) {
//mutex.unlock();
if (error) {
that.log(error.message);
callback(error);
} else if (response && response.statusCode !== 201) {
that.log(
'ERROR - authenticate - No 201 return ' +
response.statusCode +
'/' +
JSON.stringify(response)
);
callback('No 201');
} else if (body && body.data) {
that.token = body.data.id;
that.tokenProvider = body.data.attributes.provider;
that.loginExpiry = body.data.attributes.expires_in;
that.loginExpires = new Date();
that.loginExpires.setMilliseconds(
that.loginExpires.getMilliseconds() + that.loginExpiry - 30000
);
that.headers['Authorization'] = 'Bearer ' + that.token;
that.headers['Authorization-Provider'] = that.tokenProvider;
callback();
} else {
that.log('ERROR - authenticate - No body');
callback('No body');
}
}
);
//});
} else {
this.log.debug('INFO - allready authenticate expiration : ' + this.loginExpires + '-' + dte);
callback();
}
},
getMowers: function () {
const that = this;
if (this.inProgress) return;
this.inProgress = true;
this.authenticate((error) => {
if (error) {
that.emit('mowersRefreshError');
that.inProgress = false;
} else {
request(
{
url: this.trackApiUrl + 'mowers',
method: 'GET',
headers: this.headers,
json: true,
},
function (error, response, body) {
if (error) {
that.log('ERROR - getMowers - retrieving mower - ' + error.message);
that.emit('mowersRefreshError');
} else if (response && response.statusCode !== 200) {
that.log('ERROR - getMowers - No 200 return ' + response.statusCode + '/' + response);
that.emit('mowersRefreshError');
} else if (body.length > 0) {
let mowers = [];
body.forEach((mower) => {
mowers.push(mower);
});
that.discoverdMowers = mowers;
that.emit('mowersUpdated');
} else {
that.log('ERROR - getMowers -No body returned from Automower API');
that.emit('mowersRefreshError');
}
that.inProgress = false;
}
);
}
});
},
sendCommand: function (command, callback) {
const that = this;
var bodyToSend = {};
var commandURL = this.trackApiUrl + 'mowers/' + command;
this.authenticate((error) => {
if (error) {
callback(error);
} else {
request(
{
url: commandURL,
method: 'POST',
body: bodyToSend,
headers: that.headers,
json: true,
},
function (error, response, body) {
that.log('INFO - Command sent : ' + commandURL);
that.log.debug('INFO - Body received : ' + JSON.stringify(body));
if (error) {
that.log(error.message);
callback(error);
} else if (response && response.statusCode !== 200) {
that.log('ERROR - sendCommand - No 200 return ' + response.statusCode);
callback(error);
} else {
callback();
}
}
);
}
});
},
};
inherits(AutoMowerAPI, EventEmitter);
//URLS
//me.trackApiUrl + 'mowers/' + me.mowerID + '/control/start/';
//me.trackApiUrl + 'mowers/' + me.mowerID + '/control/start/';
//me.trackApiUrl + 'mowers/' + me.mowerID + '/control/start/override/period'; + JSON String "duration" in body
//me.trackApiUrl + 'mowers/' + me.mowerID + '/control/pause';
//me.trackApiUrl + 'mowers/' + me.mowerID + '/control/park/duration/timer';
//me.trackApiUrl + 'mowers/' + me.mowerID + '/control/park/duration/period'; + JSON String "duration" in body