In order to have a bit more of feedback and add some thrilling while running ("wait! when did he run all those miles?? no way I will let this be"), I want to have an updated table of the current's month competition state. That means:
- Including a new function into Competition Service - in Central Node (Raspberry Pi 3). This way, when requested the competition information, it will send back he appropiate data
- Implementing the "Podium" activity on the Competition Android App - in User's Node
Central Node - Send competition information
Existing file: insert_into_table.php
New functionality: Obtain last row of each column
So, apart from inserting information to the database, the competition service should be able to:
- Read last row of each of the users table
- Send it back to the phone
Read last values upon request
The main .php file is now able to read different types of messages. As a result, we differentiate:
- type = insert -> to update values in a table
- type = get_row -> to get last row of each table and extract its monthly distance
This request , the get_row, also contains the names of all the roommates, which will be use to select single tables. Then, the file will
1. Once we obtain the value from the right HTTP_POST ($json), the code extracts the roommates requested. Each roommate = table
2. Fetch last row of each of user's table
3. Extract monthly distance
//Decode JSON Into Array $data = json_decode($json); foreach($json as $key=>$val){ $row_last = $db->read_rows($val); $month = $row_last[NUM_MONTH_COLUMN]; }
(*) read_rows function is been developed to contains the corresponding SQL calls to obtain the last row, and fetch it as an array to return
4. At the end, Send it back to the requester, User's Node
User's Node - Podium activity
Initial setup: Nexus 5 / Android / SmartCompetitionHome App v 2
In this section, I explain how the PodiumTableActivity.java is implemented. It will request the current state of the competition from the server and display in a table. Again, results will be organized from top to bottom.
This Activity will only display a table (and later on, a REFRESH button).
PodiumTableActivity.java
When the Activity is created, it will request the monthly information for each user from the Central Node. Once the response arrives, the table is updated with the most recent data. The interesting part of this file is the new AsyncHttpResponseHandler which handles successful messages as follows:
//Handle succesful response public void onSuccess(String response) { System.out.println("Get comp Server response: "+response); try { //Convert to a JSON Array and get the arguments JSONArray arr = new JSONArray(response); //List<String> args = new ArrayList(); //Analyze each JSON object JSONObject jsonObj = (JSONObject)arr.get(0); Iterator<?> keys = jsonObj.keys(); while( keys.hasNext() ) { String key = (String) keys.next(); lastMonthValues.put(key, jsonObj.getString(key)); } //Update gui values: updateTableValues(); } catch (JSONException e) { e.printStackTrace(); } }
(*)lastMonthValues is a Map<String, String > structure holding each roommates monthly distance. updateTableValues() we use this information to organize the Podium table.
Competition Application running in the smartphone
NOTE: There should be a way of reducing that long delay when retrieving data
Conclusion
The competition android application is completed! With this version, each user can:
- Record their traveled distance
- Check what is the current status and the other total distance
Top Comments