From ac400b23f7e5ebbbd7fec0ae8969cd704e7ec690 Mon Sep 17 00:00:00 2001 From: Alex Schokking Date: Tue, 28 Jan 2020 22:00:54 -0800 Subject: [PATCH] Write all double values from smart dashboard to influx (#12) * Proof of concept working * Little bit of cleanup * Rename the db to be more generic --- .../java/xbot/roboreader/RobotReaderMain.java | 58 ++++++++++++++----- visualizer/src/influx-api/Api.tsx | 11 ++-- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/RobotReader/src/main/java/xbot/roboreader/RobotReaderMain.java b/RobotReader/src/main/java/xbot/roboreader/RobotReaderMain.java index b92ac5c..a28a235 100644 --- a/RobotReader/src/main/java/xbot/roboreader/RobotReaderMain.java +++ b/RobotReader/src/main/java/xbot/roboreader/RobotReaderMain.java @@ -13,6 +13,7 @@ import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.networktables.NetworkTableInstance; +import edu.wpi.first.networktables.NetworkTableType; import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Option; @@ -64,21 +65,31 @@ public Void call() throws Exception InfluxDB idb = InfluxDBFactory.connect(fullInfluxAddress, "root", "root"); Pong p = idb.ping(); System.out.println("influxDb Ping results: " + p.getResponseTime() + "ms"); - - String poseDbName = "RobotPose"; - String poseDbRetentionPolicy = "RobotPoseRetentionPolicy"; + String dbName = "RobotData"; + String dbRetentionPolicy = "RobotDataRetentionPolicy"; String poseDbMeasurement = "Pose"; - System.out.println("Creating/connecting to influxDb database with name: " + poseDbName); + String doubleValuesDbMeasurement = "DoubleValues"; + System.out.println("Creating/connecting to influxDb database with name: " + dbName); System.out.println("Writing measurement with name: " + poseDbMeasurement); - idb.createDatabase(poseDbName); - idb.setDatabase(poseDbName); - idb.createRetentionPolicy(poseDbRetentionPolicy, poseDbName, "30d", "30m", 2, true); + idb.createDatabase(dbName); + idb.setDatabase(dbName); + idb.createRetentionPolicy(dbRetentionPolicy, dbName, "30d", "30m", 2, true); idb.enableBatch(); while (true) { String currentSession = session.getString("NoSessionSetYet"); + long currentTimestmap = System.currentTimeMillis(); + + // find all the entries in the tables that are of type number. + // if we don't do this repeatedly we might miss new properties that + // get added after this starts. consider doing it only every N loops if perf is + // an issue. + NetworkTableEntry[] entries = inst.getEntries( + "/SmartDashboard", + NetworkTableType.kDouble.getValue() + ); if (debugLogging) { System.out.println(inst.isConnected()); @@ -86,17 +97,34 @@ public Void call() throws Exception System.out.println(netX.getDouble(0)); System.out.println(netY.getDouble(0)); System.out.println(netHeading.getDouble(0)); + System.out.println("num entries:" + entries.length); } if (inst.isConnected() && currentSession != "NoSessionSetYet") { - - idb.write(poseDbName, poseDbRetentionPolicy, Point.measurement(poseDbMeasurement) - .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) - .tag("Session", currentSession) - .addField("X", netX.getDouble(0)) - .addField("Y", netY.getDouble(0)) - .addField("Heading", netHeading.getDouble(0)) - .build()); + // write every double + for(NetworkTableEntry entry : entries) { + idb.write( + dbName, + dbRetentionPolicy, + Point.measurement(doubleValuesDbMeasurement) + .time(currentTimestmap, TimeUnit.MILLISECONDS) + .tag("Session", currentSession) + .tag("DashboardKey", entry.getName()) + .addField("Value", entry.getDouble(0)) + .build()); + } + + // write pose + idb.write( + dbName, + dbRetentionPolicy, + Point.measurement(poseDbMeasurement) + .time(currentTimestmap, TimeUnit.MILLISECONDS) + .tag("Session", currentSession) + .addField("X", netX.getDouble(0)) + .addField("Y", netY.getDouble(0)) + .addField("Heading", netHeading.getDouble(0)) + .build()); idb.flush(); } diff --git a/visualizer/src/influx-api/Api.tsx b/visualizer/src/influx-api/Api.tsx index 29b88ab..b953757 100644 --- a/visualizer/src/influx-api/Api.tsx +++ b/visualizer/src/influx-api/Api.tsx @@ -2,7 +2,8 @@ import { InfluxDB } from 'influx'; import Session from '../model/Session'; import PosePoint from '../model/PosePoint'; -const dbName = 'RobotPose'; +const dbName = 'RobotData'; +const rentetionPolicy = 'RobotDataRetentionPolicy'; interface RawPosePoint { X: number, @@ -23,7 +24,7 @@ export default class Api { async fetchLatestPosition() { return this.influx.query(` select X, Y, Heading - from "RobotPoseRetentionPolicy"."Pose" + from "${rentetionPolicy}"."Pose" ORDER BY DESC limit 1 `).then( (results) => { @@ -37,7 +38,7 @@ export default class Api { const commonMeasurement = 'X'; // needs to be on every event we care about const startResults = await this.influx.query(` SELECT first(${commonMeasurement}), time - FROM "RobotPoseRetentionPolicy"."Pose" + FROM "${rentetionPolicy}"."Pose" GROUP BY "Session" `); const starts = new Map(startResults.map((result: any) => { @@ -46,7 +47,7 @@ export default class Api { const endResults = await this.influx.query(` SELECT last(${commonMeasurement}), time - FROM "RobotPoseRetentionPolicy"."Pose" + FROM "${rentetionPolicy}"."Pose" GROUP BY "Session" `); const ends = new Map(endResults.map((result: any) => { @@ -69,7 +70,7 @@ export default class Api { async getPointsForSession(sessionName: string): Promise { const results = await this.influx.query(` SELECT X, Y, Heading - FROM "RobotPoseRetentionPolicy"."Pose" + FROM "${rentetionPolicy}"."Pose" WHERE Session = '${sessionName}' ORDER BY ASC `);