-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
107 lines (89 loc) · 2.18 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
'use strict';
var objectAssign = require('object-assign'),
events = require('events');
var MailService = require('./lib/MailService.js'),
GarminService = require('./lib/GarminService.js');
var defaults = {
username: '',
password: '',
host: 'imap.gmail.com',
port: 993,
tls: true,
secure: true,
label: 'INBOX',
// deleteAfterRead: true,
// reconnect: true,
// ignoreSeen: true
// autoUpdate: true
};
var Livetrack = function(options) {
/**
* Options object
* @private
* @type {Object}
*/
this._options = objectAssign(defaults, options);
/**
* Garmin service instance
* @private
* @type {GarminService}
*/
this._garminService = null;
/**
* Mail service instance
* @private
* @type {MailService}
*/
this._mailService = null;
// create mail service factory
this._mailServiceFactory();
};
/**
* Extend service prototype with EventEmitter
* @private
* @type {object}
*/
Livetrack.prototype.__proto__ = events.EventEmitter.prototype;
/**
* Factory for creating a mail service
* @private
* @type {Object}
*/
Livetrack.prototype._mailServiceFactory = function() {
// TODO: check if existing MailService then remove events
this._mailService = new MailService(this._options);
this._mailService.on('ready', this._onMailReady.bind(this));
this._mailService.on('session', this._onMailSession.bind(this));
this._mailService.on('error', this._onMailError.bind(this));
};
/**
* Mail service on ready event
* @private
*/
Livetrack.prototype._onMailReady = function() {
this.emit('ready');
};
/**
* Mail service on session event
* @private
* @param {String} sessionId Garmin service session id
* @param {String} sessionToken Garmin service session token
*/
Livetrack.prototype._onMailSession = function(sessionId, sessionToken) {
this._garminService = new GarminService(sessionId, sessionToken, (function(err) {
if(err) {
this.emit('error', err);
} else {
this.emit('session');
}
}).bind(this));
};
/**
* Mail service on error event
* @private
* @param {Error} err
*/
Livetrack.prototype._onMailError = function(err) {
this.emit('error', err);
};
module.exports = Livetrack;