This repository was archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogparse.js
41 lines (37 loc) · 1.48 KB
/
logparse.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
const config = require('./config/config')
const deviceController = require('./hardware/deviceController')
const Tail = require('tail').Tail
module.exports = () => {
tail = new Tail(config.tailfile)
let devices = config.devices
let currentDevice = null
const deviceNumberRegex = /Received Sensor Feedback for device (\d*)/
const deviceDataRegex = /data: (\{.*\})$/
tail.on("line", function(data) {
let deviceNumber = data.match(deviceNumberRegex);
let deviceData = data.match(deviceDataRegex);
if (typeof currentDevice == 'number' && deviceData) {
try {
let dataString = deviceData[1].replace(/: u'/g, ': \'').replace(/: None,/g, ': null,')
let data = eval(`(${dataString})`)
console.log('received data: ', data)
if (deviceController.exists(currentDevice)) {
console.log(`device ${currentDevice} does exist!`)
deviceController.getDevice(currentDevice).setState(data);
}
} catch(e) {
console.log(e)
} finally {
console.log()
currentDevice = null;
}
} else if (deviceNumber) {
currentDevice = parseInt(deviceNumber[1]);
console.log('current device: ', currentDevice)
}
})
tail.on("error", function(error) {
//todo: re-tail file
console.log('error tailing file: ', error)
})
}