Skip to content

Commit

Permalink
Streamline logging sensor data to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
Workshop2 committed May 9, 2020
1 parent 86139b9 commit 3a8e4ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
2 changes: 0 additions & 2 deletions experiments/accelerometer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const run = async () => {
console.log("Stopped")
await psybot.motors.brakeAsync();

await psybot.movementSensors.writeLogsToDisk();

process.exit();
}

Expand Down
6 changes: 0 additions & 6 deletions psybot-actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 17 additions & 11 deletions psybot-lib/components/movementSensors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Accelerometer } from "johnny-five";
const fs = require('fs');
import { WriteStream, createWriteStream } from "fs";

export class MovementSensors {
private _accelerometer: Accelerometer;
Expand All @@ -9,6 +9,7 @@ export class MovementSensors {
private _isMovingCount: number = 0;
private _onStopped?: () => void;
private _log: Array<any> = null;
private _logStream: WriteStream = null;

constructor(accelerometerController: string) {
this._accelerometer = new Accelerometer({
Expand Down Expand Up @@ -46,6 +47,8 @@ export class MovementSensors {
else {
this._isMovingCount++;
}

console.log("logCount", this._log.length)
}, 100);

setInterval(() => {
Expand All @@ -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;
}
Expand Down

0 comments on commit 3a8e4ea

Please sign in to comment.