2017-12-12 17:40:07 -07:00
|
|
|
'use strict'
|
2017-06-27 11:36:46 -06:00
|
|
|
|
|
|
|
// Imports
|
2017-12-12 17:40:07 -07:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const debug = require('debug')('tracman-demo')
|
2017-06-27 11:36:46 -06:00
|
|
|
|
2017-12-21 12:35:34 -07:00
|
|
|
module.exports = (io, filename='demo.txt') => {
|
2017-12-18 23:37:50 -07:00
|
|
|
return new Promise( (resolve, reject) => {
|
2017-12-12 17:40:07 -07:00
|
|
|
|
2017-12-18 23:37:50 -07:00
|
|
|
// File is space-seperated: delay, lat, lon, dir, spd
|
2017-12-21 12:35:34 -07:00
|
|
|
fs.readFile(path.join(__dirname, filename), (err, data) => {
|
2017-12-18 23:37:50 -07:00
|
|
|
if (err) {
|
|
|
|
console.error(err.message)
|
|
|
|
reject()
|
2017-12-13 12:52:01 -07:00
|
|
|
} else {
|
2017-12-18 23:37:50 -07:00
|
|
|
|
2017-12-21 11:05:49 -07:00
|
|
|
const lines = String(data).split('\n');
|
2017-12-18 23:37:50 -07:00
|
|
|
|
|
|
|
(function sendLoc (ln) {
|
|
|
|
if (ln > 20754) {
|
|
|
|
sendLoc(0)
|
|
|
|
} else {
|
|
|
|
let loc = lines[ln].split(' ')
|
2018-08-13 13:34:58 -06:00
|
|
|
//debug(`Sending demo location: ${loc[1]}, ${loc[2]}`)
|
2017-12-18 23:37:50 -07:00
|
|
|
io.to('demo').emit('get', {
|
|
|
|
tim: new Date(),
|
|
|
|
lat: loc[1],
|
|
|
|
lon: loc[2],
|
|
|
|
dir: loc[3],
|
|
|
|
spd: loc[4]
|
|
|
|
})
|
|
|
|
|
|
|
|
// Repeat after delay in milliseconds
|
|
|
|
setTimeout(() => {
|
|
|
|
sendLoc(ln + 1) // next line of file
|
|
|
|
}, loc[0])
|
|
|
|
}
|
|
|
|
})(5667)
|
|
|
|
|
2017-12-21 12:35:34 -07:00
|
|
|
console.log(` Demo running from ./${filename}`)
|
2017-12-18 23:37:50 -07:00
|
|
|
resolve()
|
|
|
|
|
2017-12-12 17:40:07 -07:00
|
|
|
}
|
2017-12-18 23:37:50 -07:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2017-12-12 17:40:07 -07:00
|
|
|
})
|
|
|
|
}
|