Skip to content

Commit

Permalink
Improve live logging (add hearbeats)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Jan 27, 2022
1 parent 2f792c8 commit f5b3cbe
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Added support for sending the current log file to Advantage Kit when starting robot code replay. Requires Advantage Kit v1.3.0 or later.
Improvements to live logging:
* Added timeouts on the input stream. After three seconds without new data, the connection is closed and restarted.
* Added support for heartbeats on the output stream. This is required for robot projects based on Advantage Kit v1.4.0 or later.
* Improved the handling of timestamp syncronization. This means that the stream of live data should appear smoother (especially on unreliable connections).
1 change: 0 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ app.on("open-file", (_, path) => {

// Record open file path to temp file for robot program
function recordOpenFile(filePath) {
console.log(path.join(app.getPath("temp"), lastOpenFileName))
fs.writeFile(path.join(app.getPath("temp"), lastOpenFileName), filePath, () => { })
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "advantage-scope",
"productName": "Advantage Scope",
"version": "1.14.0",
"version": "1.15.0",
"description": "Logging tool from FRC Team 6328.",
"main": "main.js",
"scripts": {
Expand Down
39 changes: 26 additions & 13 deletions preload/indexPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const os = require("os")
const net = require("net")

const connectTimeoutMs = 1000 // How long to wait when connecting
const dataTimeoutMs = 3000 // How long with no data until timeout
const heartbeatDelayMs = 1000 // How long to wait between heartbeats
const heartbeatData = new Uint8Array([6, 3, 2, 8])

// Window updates
ipcRenderer.on("set-fullscreen", (_, isFullscreen) => {
Expand Down Expand Up @@ -130,7 +133,8 @@ ipcRenderer.on("start-live", () => {
})


var client = null
var socket = null
var socketTimeout = null
var dataArray = new Uint8Array()

function appendArray(newArray) {
Expand All @@ -141,13 +145,21 @@ function appendArray(newArray) {
}

window.addEventListener("start-live-socket", event => {
client = net.createConnection({
socket = net.createConnection({
host: event.detail.address,
port: event.detail.port
})

client.on("data", data => {
socket.setTimeout(connectTimeoutMs, () => {
window.dispatchEvent(new Event("live-error"))
})

socket.on("data", data => {
appendArray(data)
clearTimeout(socketTimeout)
socketTimeout = setTimeout(() => {
socket.destroy()
}, dataTimeoutMs)

while (true) {
var expectedLength
Expand All @@ -163,34 +175,35 @@ window.addEventListener("start-live-socket", event => {
var singleArray = dataArray.slice(4, expectedLength)
dataArray = dataArray.slice(expectedLength)

if (client) {
if (socket) {
window.dispatchEvent(new CustomEvent("live-data", {
detail: new Uint8Array(singleArray)
}))
}
}
})

client.on("error", () => {
window.dispatchEvent(new Event("live-error"))
})

client.setTimeout(connectTimeoutMs, () => {
socket.on("error", () => {
window.dispatchEvent(new Event("live-error"))
})

client.on("close", () => {
socket.on("close", () => {
window.dispatchEvent(new Event("live-closed"))
})
})

window.addEventListener("stop-live-socket", () => {
if (client) {
client.destroy()
client = null
if (socket) {
socket.destroy()
}
})

window.setInterval(() => {
if (socket) {
socket.write(heartbeatData)
}
}, heartbeatDelayMs)

// Manage exporting as CSV
ipcRenderer.on("export-csv", () => {
window.dispatchEvent(new Event("export-csv"))
Expand Down
5 changes: 4 additions & 1 deletion www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ window.addEventListener("start-live", () => {
window.log.updateDisplayKeys()

var timeRange = window.log.getTimestamps()[window.log.getTimestamps().length - 1] - window.log.getTimestamps()[0]
window.liveStart = (new Date().getTime() / 1000) - timeRange
var newLiveStart = (new Date().getTime() / 1000) - timeRange
if (firstData || newLiveStart < window.liveStart) {
window.liveStart = newLiveStart
}

if (firstData) {
firstData = false
Expand Down

0 comments on commit f5b3cbe

Please sign in to comment.