diff --git a/experiments/accelerometer-test.ts b/experiments/accelerometer-test.ts index bbf84db..130a94a 100644 --- a/experiments/accelerometer-test.ts +++ b/experiments/accelerometer-test.ts @@ -34,8 +34,6 @@ const run = async () => { console.log("Stopped") await psybot.motors.brakeAsync(); - await psybot.movementSensors.writeLogsToDisk(); - process.exit(); } diff --git a/psybot-actor.ts b/psybot-actor.ts index eef439b..b2a8fda 100644 --- a/psybot-actor.ts +++ b/psybot-actor.ts @@ -25,12 +25,6 @@ export class PsybotActor { } private wireUpExternalEvents() { - setInterval(() => { - console.log("Saving sensor data..."); - this._psybot.movementSensors.writeLogsToDisk(); - console.log("Saved"); - }, 5000); - this._psybot.sonar.setObstacleDetectedCallback(() => { if (this._stateMachine.can("obstacleDetected")) { this._stateMachine.obstacleDetected(); diff --git a/psybot-lib/components/movementSensors.ts b/psybot-lib/components/movementSensors.ts index a9d5836..34c7a2a 100644 --- a/psybot-lib/components/movementSensors.ts +++ b/psybot-lib/components/movementSensors.ts @@ -1,5 +1,5 @@ import { Accelerometer } from "johnny-five"; -const fs = require('fs'); +import { WriteStream, createWriteStream } from "fs"; export class MovementSensors { private _accelerometer: Accelerometer; @@ -9,6 +9,7 @@ export class MovementSensors { private _isMovingCount: number = 0; private _onStopped?: () => void; private _log: Array = null; + private _logStream: WriteStream = null; constructor(accelerometerController: string) { this._accelerometer = new Accelerometer({ @@ -46,6 +47,8 @@ export class MovementSensors { else { this._isMovingCount++; } + + console.log("logCount", this._log.length) }, 100); setInterval(() => { @@ -63,22 +66,25 @@ export class MovementSensors { this._isStoppedCount = 0; this._isMovingCount = 0; }, 1000); + + setInterval(() => { + if(!this._log) { + return; + } + + this._log.forEach(element => { + this._logStream.write(JSON.stringify(element) + ", "); + }); + + this._log = []; + }, 500); } public collectLogs() { + this._logStream = createWriteStream("movementData.json"); this._log = []; } - public writeLogsToDisk() { - if (!this._log) { - console.log("Logging wasn't turn on, nothing to persist"); - return; - } - - const data = JSON.stringify(this._log); - fs.writeFileSync("movementData.json", data); - } - public setStoppedCallback(onStopped: () => void): void { this._onStopped = onStopped; }