Skip to content

Commit

Permalink
Write all double values from smart dashboard to influx (#12)
Browse files Browse the repository at this point in the history
* Proof of concept working

* Little bit of cleanup

* Rename the db to be more generic
  • Loading branch information
aschokking authored Jan 29, 2020
1 parent c1be3a6 commit ac400b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
58 changes: 43 additions & 15 deletions RobotReader/src/main/java/xbot/roboreader/RobotReaderMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,39 +65,66 @@ 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());
System.out.println(currentSession);
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();
}
Expand Down
11 changes: 6 additions & 5 deletions visualizer/src/influx-api/Api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -69,7 +70,7 @@ export default class Api {
async getPointsForSession(sessionName: string): Promise<PosePoint[]> {
const results = await this.influx.query(`
SELECT X, Y, Heading
FROM "RobotPoseRetentionPolicy"."Pose"
FROM "${rentetionPolicy}"."Pose"
WHERE Session = '${sessionName}'
ORDER BY ASC
`);
Expand Down

0 comments on commit ac400b2

Please sign in to comment.