forked from coopcycle/coopcycle-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
86 lines (70 loc) · 2.01 KB
/
bot.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
/**
* A Bot to simulate courier activity.
* It uses a GPX file to determine its itinerary.
*/
var fs = require('fs');
var _ = require('lodash');
var parseXML = require('xml2js').parseString;
var Courier = require('./src/Courier');
var PM2Utils = require('./src/PM2Utils');
var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
storage: './data/db.sqlite'
});
var Db = require('./src/Db')(sequelize);
const CONFIG = require('./config.json');
var username = process.argv[2];
var password = process.argv[3];
var gpxFile = process.argv[4];
var httpBaseURL = CONFIG.COOPCYCLE_BASE_URL;
require('./src/fetch-polyfill')
const Client = require('./src/Client')
const WebSocketClient = require('./src/WebSocketClient')
var xml = fs.readFileSync(gpxFile);
var route = [];
var courier;
Db.Courier.findOne({
where: {username: username}
}).then((model) => {
console.log('Loading GPX file...');
parseXML(xml, function (err, result) {
_.each(result.gpx.wpt, function(point) {
route.push({
latitude: point['$'].lat,
longitude: point['$'].lon
})
});
const client = new Client(CONFIG.COOPCYCLE_BASE_URL, {
token: model.token,
refresh_token: model.refreshToken,
}, {
autoLogin: client => client.login(model.username, model.username)
})
const webSocketClient = new WebSocketClient(client, '/dispatch')
courier = new Courier(
model.username,
client,
webSocketClient,
{
lastPosition: model.get('lastPosition'),
route: route,
speedFactor: model.speedFactor
}
);
try {
courier.connect();
} catch (e) {
console.log('Stopping bot...', err);
PM2Utils.stopBot(courier, () => {});
}
});
process.on('SIGINT', function () {
var currentPosition = courier.currentPosition;
if (currentPosition) {
model.set('lastPosition', JSON.stringify(currentPosition));
model.save();
}
});
});