-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
100 lines (87 loc) · 3.27 KB
/
main.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
const PlayerStateWriter = require('./src/playerStateWriter')
const ZwiftPacketMonitor = require('./src/ZwiftPacketMonitor')
const mysql = require('mysql')
const program = require('commander')
const Cap = require('cap').Cap, decoders=require('cap').decoders, PROTOCOL=decoders.PROTOCOL
const ZwiftAccount = require('zwift-mobile-api')
const Long = require('long')
const {wrappedStatus} = require('zwift-mobile-api/src/riderStatus')
let playerStateWriter = null
let worldTimeOffset = 0
program
.version('0.1.0')
.option('-u, --user <zwiftuser>', 'zwift user to log in with')
.option('-p, --password <password>', 'zwift password to log in with')
.option('-D, --mysql_database <db>', 'mysql database to connect to')
.option('-H, --mysql_host <host>', 'mysql host to connect to')
.option('-U, --mysql_user <user>', 'mysql user name')
.option('-P, --mysql_password <password>', 'mysql password')
.option('-I, --interface <interface>', 'interface to monitor')
.option('-l, --list_interfaces', 'list available interfaces and exit')
.option('-v, --verbose', 'turn on verbose mode')
.parse(process.argv)
const account = new ZwiftAccount(program.user, program.password)
const connection = mysql.createConnection({
host: program.mysql_host,
user: program.mysql_user,
password: program.mysql_password,
database: program.mysql_database,
charset: 'utf8mb4',
timezone: 'Z',
})
function processChalkLine (error, results, fields) {
if (error) throw error
for (var result of results) {
console.log(JSON.stringify(result))
}
}
function listDevices () {
for (var device of Cap.deviceList()) {
console.log(`${device.name} ${device.description} ${device.addresses[0].addr}`)
}
}
let lastPurgeTime = new Long(0)
let lastStatTime = new Date()
let startTime = lastStatTime
let totalNumPlayerStates = 0
let numPlayerStates = 0
let zpm = null
function processPlayerState (playerState, serverWorldTime) {
if (playerStateWriter) {
playerStateWriter.addPlayerState(wrappedStatus(playerState))
numPlayerStates++
}
if (serverWorldTime.greaterThanOrEqual(lastPurgeTime.add(1000))) {
playerStateWriter.purge(serverWorldTime.add(worldTimeOffset).add(-10000))
}
let now = new Date()
if (now - lastStatTime >= 10000) {
totalNumPlayerStates += numPlayerStates
console.log(`${(now - startTime) / 1000}s: ${totalNumPlayerStates} total updates ${totalNumPlayerStates * 1000 / (now - startTime)}/s`
+ ` ${(now - lastStatTime) / 1000}s elapsed since last ${numPlayerStates} updates ${numPlayerStates * 1000 / (now - lastStatTime)}/s`)
numPlayerStates = 0
lastStatTime = now
}
}
function handleMysqlError(err) {
if (err.fatal) {
console.log(`Mysql error (${err.code}) - reconnecting`)
connection.connect()
}
}
if (program.list_interfaces) {
listDevices()
return
}
connection.connect()
riders = account.getWorld().riders().then(riders => {
worldTimeOffset = (Number(riders.currentDateTime) * 1000) - Number(riders.currentWorldTime)
playerStateWriter = new PlayerStateWriter(connection, worldTimeOffset)
zpm = new ZwiftPacketMonitor(program.interface)
zpm.on('incomingPlayerState', processPlayerState)
zpm.on('endOfBatch', () => {playerStateWriter.flush()})
zpm.start()
console.log('Monitoring network traffic.')
}).catch(error => {
console.log(error)
})