Skip to content

Commit 0c41b3d

Browse files
authored
Merge pull request #9 from devinaconley/flattened-json
Flattened json
2 parents bba1bab + e1a8766 commit 0c41b3d

File tree

4 files changed

+152
-90
lines changed

4 files changed

+152
-90
lines changed

listener/Graph.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void Update( double[] newData, int time )
110110
// Validate
111111
if ( newData.length != 2 )
112112
{
113-
this.parent.println( "Invalid data passed to X v. Y graph." );
113+
//this.parent.println( "Invalid data passed to X v. Y graph." );
114114
return;
115115
}
116116

@@ -122,7 +122,7 @@ public void Update( double[] newData, int time )
122122
// Validate
123123
if ( newData.length != this.numVars )
124124
{
125-
this.parent.println( "Invalid data passed to time graph." );
125+
//this.parent.println( "Invalid data passed to time graph." );
126126
return;
127127
}
128128

listener/listener.pde

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ final boolean DEBUG = false;
3939

4040
//CONSTANTS
4141
final char OUTER_KEY = '#';
42-
final String INNER_KEY = "@";
4342
final int MARGIN_SZ = 20; // between plots
4443
final int BG_COL = 75; // background
4544
final int PORT_INTERVAL = 5000; // time to sit on each port
@@ -147,30 +146,41 @@ void serialEvent( Serial ser )
147146
try
148147
{
149148
String message = ser.readStringUntil( OUTER_KEY );
150-
if ( message == null || message.isEmpty() || message.equals( "\n\n" + OUTER_KEY ) )
149+
if ( message == null || message.isEmpty() || message.equals( OUTER_KEY ) )
151150
{
152151
return;
153152
}
154-
String[] arrayMain = message.split( "\n" );
155-
153+
154+
JSONObject json = parseJSONObject( message );
155+
156+
if ( json == null )
157+
{
158+
return;
159+
}
160+
156161
// ********************************************************* //
157162
// ************* PLOT SETUP FROM CONFIG CODE *************** //
158163
// ********************************************************* //
159-
164+
165+
String tempCode = "";
166+
boolean config = false;
167+
if ( json.hasKey( "ng" ) && json.hasKey( "lu" ) )
168+
{
169+
tempCode = Integer.toString( json.getInt( "ng" ) ) + Integer.toString( json.getInt( "lu" ) );
170+
config = true;
171+
}
172+
160173
// If config code has changed, need to go through setup again
161-
if ( !configCode.equals( arrayMain[0] ) )
174+
if ( config && !configCode.equals( tempCode ) )
162175
{
163-
String[] arraySub = arrayMain[0].split( INNER_KEY );
176+
lastPortSwitch = millis(); // (likely on the right port, just need to reconfigure graph layout)
177+
164178
// Check for size of full transmission against expected to flag bad transmission
165-
try
166-
{
167-
numGraphs = Integer.parseInt( arraySub[0] );
168-
}
169-
catch ( Exception e )
170-
{
171-
return;
172-
}
173-
if ( arrayMain.length != numGraphs + 1 )
179+
numGraphs = json.getInt( "ng" );
180+
181+
JSONArray jsonGraphs = json.getJSONArray( "g" );
182+
183+
if ( jsonGraphs.size() != numGraphs )
174184
{
175185
return;
176186
}
@@ -186,23 +196,26 @@ void serialEvent( Serial ser )
186196
// Iterate through the individual graph data blocks to get graph specific info
187197
for ( int i = 0; i < numGraphs; i++ )
188198
{
189-
arraySub = arrayMain[i+1].split( INNER_KEY );
190-
String title = arraySub[0];
191-
boolean xvyTemp = Integer.parseInt( arraySub[1] ) == 1;
192-
int maxPoints = Integer.parseInt( arraySub[2] );
193-
int numVars = Integer.parseInt( arraySub[3] );
199+
JSONObject g = jsonGraphs.getJSONObject( i );
200+
201+
String title = g.getString( "t" );
202+
boolean xvyTemp = g.getInt( "xvy" ) == 1;
203+
int maxPoints = g.getInt( "pd" );
204+
int numVars = g.getInt( "sz" );
194205
String[] labelsTemp = new String[numVars];
195206
int[] colorsTemp = new int[numVars];
196207

197208
concatLabels += title;
198-
209+
210+
JSONArray l = g.getJSONArray( "l" );
211+
JSONArray c = g.getJSONArray( "c" );
199212
for ( int j = 0; j < numVars; j++ )
200213
{
201-
labelsTemp[j] = arraySub[4 + 3*j];
202-
colorsTemp[j] = COLORMAP.get( arraySub[5 + 3*j] );
214+
labelsTemp[j] = l.getString( j );
215+
colorsTemp[j] = COLORMAP.get( c.getString( j ) );
203216
if ( colorsTemp[j] == 0 )
204217
{
205-
logMessage( "Invalid color: " + arraySub[5 + 3*j] + ", defaulting to green.", true );
218+
logMessage( "Invalid color: " + c.getString( j ) + ", defaulting to green.", true );
206219
colorsTemp[j] = COLORMAP.get( "green" );
207220
}
208221
concatLabels += labelsTemp[j];
@@ -222,7 +235,7 @@ void serialEvent( Serial ser )
222235
// Set new config code
223236
if ( concatLabels.equals( lastLabels ) ) // Only when we're sure on labels
224237
{
225-
configCode = arrayMain[0];
238+
configCode = tempCode;
226239
lastConfig = millis();
227240
logMessage( "Configured " + graphs.size() + " graphs", false );
228241
}
@@ -238,20 +251,20 @@ void serialEvent( Serial ser )
238251
// *********************************************************** //
239252
// ************ NORMAL PLOTTING FUNCTIONALITY **************** //
240253
// *********************************************************** //
241-
int tempTime = millis();
254+
int tempTime = json.getInt( "t" );
242255

256+
JSONArray jsonGraphs = json.getJSONArray( "g" );
257+
243258
for ( int i = 0; i < numGraphs; i++ )
244259
{
245-
String[] arraySub = arrayMain[i+1].split( INNER_KEY );
260+
JSONArray data = jsonGraphs.getJSONObject( i ).getJSONArray( "d" );
246261

247-
double[] tempData = new double[ (arraySub.length - 5) / 3 ];
262+
double[] tempData = new double[ data.size() ];
248263

249264
// Update graph objects with new data
250-
int q = 0;
251-
for ( int j = 6; j < arraySub.length; j += 3 )
265+
for ( int j = 0; j < data.size(); j++ )
252266
{
253-
tempData[q] = Double.parseDouble( arraySub[j] );
254-
q++;
267+
tempData[j] = data.getDouble( j );
255268
}
256269
graphs.get( i ).Update( tempData, tempTime );
257270
}

plotter/Plotter.cpp

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929

3030
Plotter::Plotter()
3131
{
32-
Serial.begin(115200);
33-
head = NULL;
34-
tail = NULL;
35-
totalSize = 0;
36-
numGraphs = 0;
37-
maxPointsDisplayed = 0;
38-
lastUpdated = millis();
32+
Serial.begin( 115200 );
33+
head = NULL;
34+
tail = NULL;
35+
numGraphs = 0;
36+
lastUpdated = millis();
37+
counter = 0;
3938
}
4039

4140
Plotter::~Plotter()
@@ -65,13 +64,7 @@ void Plotter::AddGraphHelper( String title, VariableWrapper * wrappers, int sz,
6564
tail = temp;
6665
}
6766

68-
totalSize += sz;
69-
numGraphs++;
70-
if ( pointsDisplayed > maxPointsDisplayed )
71-
{
72-
maxPointsDisplayed = pointsDisplayed;
73-
}
74-
67+
numGraphs++;
7568
lastUpdated = millis();
7669
}
7770

@@ -99,7 +92,6 @@ bool Plotter::Remove( int index )
9992
}
10093
last->next = temp->next;
10194
numGraphs--;
102-
totalSize -= temp->size;
10395
delete temp;
10496
}
10597
lastUpdated = millis();
@@ -167,18 +159,35 @@ bool Plotter::SetColorHelper( int index, int sz, String * colors )
167159

168160
void Plotter::Plot()
169161
{
170-
String code = OUTER_KEY;
171-
code += ( numGraphs + INNER_KEY + totalSize + INNER_KEY
172-
+ maxPointsDisplayed + INNER_KEY + lastUpdated + INNER_KEY );
173-
Serial.print( code );
162+
bool config = counter == 0;
163+
164+
Serial.print( "{\"" + TIME_KEY + "\":" ); Serial.print( millis() );
165+
166+
if ( config )
167+
{
168+
Serial.print( ",\"" + NUM_GRAPH_KEY + "\":" ); Serial.print( numGraphs );
169+
Serial.print( ",\"" + LAST_UPDATED_KEY + "\":" ); Serial.print( lastUpdated );
170+
}
171+
172+
Serial.print( ",\"" + GRAPHS_KEY + "\":[" );
173+
174174
Graph * temp = head;
175-
while ( temp != NULL )
175+
while ( temp )
176176
{
177-
Serial.println();
178-
temp->Plot();
177+
temp->Plot( config );
179178
temp = temp->next;
179+
if ( temp )
180+
{
181+
Serial.print( "," );
182+
}
183+
}
184+
Serial.print( "]}" ); Serial.println( OUTER_KEY );
185+
186+
counter++;
187+
if ( counter >= CONFIG_INTERVAL )
188+
{
189+
counter = 0;
180190
}
181-
Serial.println( OUTER_KEY );
182191
}
183192

184193
// Graph
@@ -197,21 +206,50 @@ Plotter::Graph::~Graph()
197206
delete[] wrappers;
198207
}
199208

200-
void Plotter::Graph::Plot()
209+
void Plotter::Graph::Plot( bool config )
201210
{
202-
Serial.print( title ); Serial.print( INNER_KEY );
203-
Serial.print( xvy ); Serial.print( INNER_KEY );
204-
Serial.print( pointsDisplayed ); Serial.print( INNER_KEY );
205-
Serial.print( size ); Serial.print( INNER_KEY );
206-
211+
Serial.print( "{" );
212+
213+
if ( config )
214+
{
215+
Serial.print( "\"" + TITLE_KEY + "\":" ); Serial.print( "\"" + title + "\"" );
216+
Serial.print( ",\"" + XVY_KEY + "\":" ); Serial.print( xvy );
217+
Serial.print( ",\"" + POINTS_DISPLAYED_KEY + "\":" ); Serial.print( pointsDisplayed );
218+
Serial.print( ",\"" + SIZE_KEY + "\":" ); Serial.print( size );
219+
Serial.print( ",\"" + LABELS_KEY + "\":[" );
220+
for ( int i = 0; i < size; i++ )
221+
{
222+
Serial.print( "\"" + wrappers[i].GetLabel() + "\"" );
223+
if ( i + 1 < size )
224+
{
225+
Serial.print( "," );
226+
}
227+
}
228+
Serial.print( "],\"" + COLORS_KEY + "\":[" );
229+
for ( int i = 0; i < size; i++ )
230+
{
231+
Serial.print( "\"" + wrappers[i].GetColor() + "\"" );
232+
if ( i + 1 < size )
233+
{
234+
Serial.print( "," );
235+
}
236+
}
237+
Serial.print( "]," );
238+
}
239+
240+
Serial.print( "\"" + DATA_KEY + "\":[" );
207241
char val[15];
208242
for (int i = 0; i < size; i++)
209243
{
210-
Serial.print( wrappers[i].GetLabel() ); Serial.print( INNER_KEY );
211-
Serial.print( wrappers[i].GetColor() ); Serial.print( INNER_KEY );
212244
dtostrf( wrappers[i].GetValue(), 1, 7, val );
213-
Serial.print( val ); Serial.print( INNER_KEY );
245+
Serial.print( val );
246+
if ( i + 1 < size )
247+
{
248+
Serial.print( "," );
249+
}
214250
}
251+
252+
Serial.print( "]}" );
215253
}
216254

217255
bool Plotter::Graph::SetColor( int sz, String * colors )

0 commit comments

Comments
 (0)