-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (95 loc) · 3.08 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
const fs = require('fs')
const os = require('os')
const path = require('path')
const { EventEmitter } = require('events')
const debug = require('debug')('tracksix')
const gpsd = require('node-gpsd')
const mqtt = require('mqtt')
const throttleEnd = require('throttle-end')
const relayErrorEvents = (sourceEmitter, drainEmitter) => {
sourceEmitter.on('error', (err) => {
drainEmitter.emit('error', err)
})
}
const configPath = (configFilePath) => {
return configFilePath
? path.resolve(process.cwd(), configFilePath)
: path.resolve(os.homedir(), './.tracksix.config.json')
}
const readConfig = (configFilePath) => {
configFilePath = configPath(configFilePath)
debug('config: reading ' + configFilePath)
if (fs.existsSync(configFilePath) === false) {
throw new Error(`Config file not found at '${configFilePath}'`)
}
return JSON.parse(fs.readFileSync(configFilePath, 'utf8'))
}
const validConfig = config => {
return config
}
class Tracksix extends EventEmitter {
constructor (config) {
super()
this.config = validConfig(config)
this.reportLocation = throttleEnd(this.reportLocation, config.ping * 1000, this)
// start services
this.gps = this.initGps(config)
this.mqtt = this.initMqtt(config)
// report information up
relayErrorEvents(this.gps, this)
relayErrorEvents(this.mqtt, this)
this.gps.on('TPV', this.handleTPV.bind(this))
this.mqtt.on('connect', () => { this.emit('info', 'mqtt: connected') })
}
mqttConnectionString () {
const { auth, username, password, tls, host, port } = this.config
const authS = auth ? `${username}:${password}@` : ''
const protoS = tls ? 'mqtts' : 'mqtt'
return `${protoS}://${authS}${host}:${port}`
}
initGps (config) {
const gps = new gpsd.Listener()
gps.connect(() => {
gps.watch()
})
return gps
}
initMqtt (config) {
return mqtt.connect(this.mqttConnectionString(), {
keepalive: config.keepalive,
rejectUnauthorized: config.allowinvalidcerts === true,
clean: config.cleanSession,
clientId: config.clientId || config.username + ' ' + config.deviceId
})
}
handleTPV (tpv) {
if (('lat' in tpv === false) || ('lon' in tpv === false)) {
return console.warn('Skipping, no lon/lat')
}
const report = {
_type: 'location',
alt: tpv.alt || 0,
lat: Math.round(tpv.lat * 100000) / 100000,
lon: Math.round(tpv.lon * 100000) / 100000,
tid: this.config.tid || this.config.deviceId.slice(-2),
tst: Math.round((new Date(tpv.time)).getTime() / 1000),
acc: Math.max(tpv.epx, tpv.epy)
}
this.reportLocation(report)
}
reportLocation (report) {
this.emit('location', report)
const config = this.config
const topic = config.pubTopicBase.replace('%u', config.username).replace('%d', config.deviceId)
debug(topic)
this.mqtt.publish(topic, JSON.stringify(report), {
retain: config.pubRetain,
qos: config.pubQos
})
}
}
const tracksix = (config) => {
return new Tracksix(config)
}
module.exports = tracksix
module.exports.readConfigSync = readConfig