Android Open Source - frc-notebook T B A Datafeed






From Project

Back to project page frc-notebook.

License

The source code is released under:

GNU General Public License

If you think the Android project frc-notebook listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.plnyyanks.frcnotebook.datafeed;
//from  w  w  w  .  jav a  2  s  . c  o  m
import android.util.Log;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.plnyyanks.frcnotebook.Constants;
import com.plnyyanks.frcnotebook.activities.StartActivity;
import com.plnyyanks.frcnotebook.database.PreferenceHandler;
import com.plnyyanks.frcnotebook.datatypes.Event;
import com.plnyyanks.frcnotebook.datatypes.ListElement;
import com.plnyyanks.frcnotebook.datatypes.ListHeader;
import com.plnyyanks.frcnotebook.datatypes.ListItem;
import com.plnyyanks.frcnotebook.datatypes.Match;
import com.plnyyanks.frcnotebook.datatypes.Team;
import com.plnyyanks.frcnotebook.json.JSONManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * File created by phil on 4/2/14.
 * Copyright 2014, Phil Lopreiato
 * This file is part of FRC Notebook.
 * FRC Notebook is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * FRC Notebook is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with FRC Notebook. If not, see http://www.gnu.org/licenses/.
 */
public class TBADatafeed {

    public static String fetchEventDetails_TBAv1(String eventKey) {
        String data = GET_Request.getWebData("http://www.thebluealliance.com/api/v1/event/details?event=" + eventKey, true);

        JsonObject eventObject = JSONManager.getAsJsonObject(data);
        JsonArray teams = eventObject.getAsJsonArray("teams");
        JsonArray matches = eventObject.getAsJsonArray("matches");

        Event event = new Event();

        event.setEventKey(eventKey);

        String name = eventObject.get("name").getAsString();
        event.setEventName(name);

        String shortName = eventObject.get("short_name").getAsString();
        event.setShortName(shortName);

        String year = eventObject.get("year").getAsString();
        event.setEventYear(Integer.parseInt(year));

        String location = eventObject.get("location").getAsString();
        event.setEventLocation(location);

        String start = eventObject.get("start_date").getAsString();
        event.setEventStart(start);

        String end = eventObject.get("end_date").getAsString();
        event.setEventEnd(end);

        Log.d(Constants.LOG_TAG, "official: " + eventObject.get("official"));
        event.setOfficial(eventObject.get("official").getAsBoolean());

        long eventResult = StartActivity.db.addEvent(event);

        if (eventResult == -1) {
            return "Error writing event to database";
        }

        Iterator<JsonElement> iterator = teams.iterator();
        JsonElement teamElement;
        Team team;

        long teamResult = 0;
        while (iterator.hasNext() && teamResult != -1) {
            teamElement = iterator.next();
            team = new Team();
            team.setTeamKey(teamElement.getAsString());
            team.setTeamNumber(Integer.parseInt(team.getTeamKey().substring(3)));
            team.addEvent(eventKey);

            teamResult = StartActivity.db.addTeam(team);
        }

        if (teamResult == -1) {
            return "Error writing teams to database";
        } else {
            if(matches.size()>0){
                TBADatafeed.fetchMatches_TBAv1(matches.toString());
            }

            return "Info downloaded for " + eventKey;
        }

    }

    public static String fetchEventDetails_TBAv2(String eventKey){
        String eventData = GET_Request.getWebData("http://www.thebluealliance.com/api/v2/event/" + eventKey, true);
        String teamData = GET_Request.getWebData("http://www.thebluealliance.com/api/v2/event/" + eventKey+"/teams",true);

        JsonObject eventObject = JSONManager.getAsJsonObject(eventData);
        JsonArray teams = JSONManager.getasJsonArray(teamData);

        Event event = new Event();
        event.setEventKey(eventKey);
        event.setEventName(eventObject.get("name").getAsString());
        event.setShortName(eventObject.get("short_name").getAsString());
        event.setEventYear(eventObject.get("year").getAsInt());
        event.setEventLocation(eventObject.get("location").getAsString());
        event.setEventStart(eventObject.get("start_date").getAsString());
        event.setEventEnd(eventObject.get("end_date").getAsString());
        event.setOfficial(eventObject.get("official").getAsBoolean());

        long eventResult = StartActivity.db.addEvent(event);

        if (eventResult == -1) {
            return "Error writing event to database";
        }

        Iterator<JsonElement> iterator = teams.iterator();
        JsonObject teamObject;
        Team team;

        long teamResult = 0;
        while (iterator.hasNext() && teamResult != -1) {
            teamObject = iterator.next().getAsJsonObject();
            team = new Team();
            team.setTeamKey(teamObject.get("key").getAsString());
            team.setTeamNumber(teamObject.get("team_number").getAsInt());
            team.addEvent(eventKey);

            teamResult = StartActivity.db.addTeam(team);
        }

        if (teamResult == -1) {
            return "Error writing teams to database";
        } else {
            if(PreferenceHandler.getDataSource() == Constants.DATAFEED_SOURCES.TBAv2)
                fetchMatches_TBAv2(eventKey);
            else if(PreferenceHandler.getDataSource() == Constants.DATAFEED_SOURCES.USFIRST){
                String matchResult = USFIRSTParser.fetchMatches_USFIRST(eventKey);
                if(!matchResult.isEmpty())
                    return matchResult;
            }
        }
        return "Info downloaded for " + eventKey;
    }

    public static LinkedHashMap<String, ListItem> fetchEvents_TBAv1(String year) {
        String data = GET_Request.getWebData("http://www.thebluealliance.com/api/v1/events/list?year=" + year, true);
        JsonArray result = JSONManager.getasJsonArray(data);

        //now, add the events to the event picker activity
        Iterator<JsonElement> iterator = result.iterator();

        JsonObject element;
        String eventName, eventKey;
        ArrayList<Event> list = new ArrayList<Event>();
        for (int i = 0; i < result.size() && iterator.hasNext(); i++) {
            element = iterator.next().getAsJsonObject();
            eventName = element.get("name").getAsString();
            eventKey = element.get("key").getAsString();

            Event e = new Event();
            e.setEventKey(eventKey);
            e.setEventName(eventName);
            e.setEventStart(element.get("start_date").getAsString());
            list.add(e);
        }

        Collections.sort(list);
        int eventWeek = Integer.parseInt(Event.weekFormatter.format(new Date())),
                currentWeek;
        LinkedHashMap<String, ListItem> output = new LinkedHashMap<String, ListItem>();
        for (Event e : list) {
            currentWeek = e.getCompetitionWeek();
            if (eventWeek != currentWeek) {
                String header;
                if (currentWeek == 9) {
                    header = year + " Championship Event";
                } else {
                    header = year + " Week " + currentWeek;
                }
                output.put("week" + currentWeek, new ListHeader(header));
            }
            eventWeek = currentWeek;

            output.put(e.getEventKey(), new ListElement(e.getEventName(), e.getEventKey()));
        }

        return output;
    }

    /* Can't do this without lots of API requests because v2 of the API only gives an array of event keys.
          So we'll stick with v1 for this part now
     */
    public static HashMap<String, ListItem> fetchEvents_TBAv2(String year) {
        /*String data = GET_Request.getWebData("http://www.thebluealliance.com/api/v2/events/" + year, true);
        JsonArray result = JSONManager.getasJsonArray(data);

        //now, add the events to the event picker activity
        Iterator<JsonElement> iterator = result.iterator();

        JsonObject element;
        String eventName, eventKey;
        ArrayList<Event> list = new ArrayList<Event>();
        for (int i = 0; i < result.size() && iterator.hasNext(); i++) {
            Event e = new Event();
            e.setEventKey(eventKey);
            e.setEventName(eventName);
            e.setEventStart(element.get("start_date").getAsString());
            list.add(e);
        }

        Collections.sort(list);
        int eventWeek = Integer.parseInt(Event.weekFormatter.format(new Date())),
                currentWeek;*/
        HashMap<String, ListItem> output = new HashMap<String, ListItem>();
        /*for (Event e : list) {
            currentWeek = e.getCompetitionWeek();
            if (eventWeek != currentWeek) {
                String header;
                if (currentWeek == 9) {
                    header = year + " Championship Event";
                } else {
                    header = year + " Week " + currentWeek;
                }
                output.put("week" + currentWeek,new ListHeader(header));
            }
            eventWeek = currentWeek;

            output.put(e.getEventKey(),new ListElement(e.getEventName(), e.getEventKey()))
        }
        */
        return output;
    }

    public static void fetchMatches_TBAv1(String matchesToFetch) {
        String matchList = matchesToFetch.replaceAll("\"", "");
        matchList = matchList.substring(1, matchList.length() - 1);

        String requestString = "http://www.thebluealliance.com/api/v1/match/details?match=" + matchList;
        String result = GET_Request.getWebData(requestString, true);

        JsonArray matches = JSONManager.getasJsonArray(result);
        Iterator<JsonElement> iterator = matches.iterator();
        Match match;
        JsonObject element, alliances, red, blue;
        JsonArray redTeams, blueTeams;
        while (iterator.hasNext()) {
            //for each match we get data for...
            element = iterator.next().getAsJsonObject();
            match = new Match();
            match.setMatchKey(element.get("key").getAsString());
            match.setMatchType(Constants.MATCH_LEVELS.get(element.get("competition_level").getAsString()));
            match.setMatchNumber(Integer.parseInt(element.get("match_number").getAsString()));
            match.setSetNumber(Integer.parseInt(element.get("set_number").getAsString()));

            //now, for alliances. Hardest part of JSON wizardry...
            alliances = element.get("alliances").getAsJsonObject();
            red = alliances.get("red").getAsJsonObject();
            redTeams = red.get("teams").getAsJsonArray();
            blue = alliances.get("blue").getAsJsonObject();
            blueTeams = blue.get("teams").getAsJsonArray();

            match.setRedScore(Integer.parseInt(red.get("score").getAsString()));
            match.setBlueScore(Integer.parseInt(blue.get("score").getAsString()));

            match.setRedAlliance(redTeams.toString());
            match.setBlueAlliance(blueTeams.toString());
            StartActivity.db.addMatch(match);
        }
    }

    public static void fetchMatches_TBAv2(String eventKey) {
        String requestString = "http://www.thebluealliance.com/api/v2/event/" + eventKey + "/matches";
        String result = GET_Request.getWebData(requestString, true);

        JsonArray matches = JSONManager.getasJsonArray(result);
        Iterator<JsonElement> iterator = matches.iterator();
        Match match;
        JsonObject element, alliances, red, blue;
        JsonArray redTeams, blueTeams;
        while (iterator.hasNext()) {
            //for each match we get data for...
            element = iterator.next().getAsJsonObject();
            match = new Match();
            match.setMatchKey(element.get("key").getAsString());
            match.setMatchType(Constants.MATCH_LEVELS.get(element.get("comp_level").getAsString()));
            match.setMatchNumber(element.get("match_number").getAsInt());
            match.setSetNumber(element.get("set_number").getAsInt());

            //now, for alliances. Hardest part of JSON wizardry...
            alliances = element.get("alliances").getAsJsonObject();
            red = alliances.get("red").getAsJsonObject();
            redTeams = red.get("teams").getAsJsonArray();
            blue = alliances.get("blue").getAsJsonObject();
            blueTeams = blue.get("teams").getAsJsonArray();

            match.setRedScore(red.get("score").getAsInt());
            match.setBlueScore(blue.get("score").getAsInt());

            match.setRedAlliance(redTeams.toString());
            match.setBlueAlliance(blueTeams.toString());
            StartActivity.db.addMatch(match);
        }
    }
}




Java Source Code List

.BasicTest.java
.TestStartActivity.java
com.plnyyanks.frcnotebook.Constants.java
com.plnyyanks.frcnotebook.activities.AddEvent.java
com.plnyyanks.frcnotebook.activities.EventDownloadActivity.java
com.plnyyanks.frcnotebook.activities.FieldMonitorActivity.java
com.plnyyanks.frcnotebook.activities.PredefinedNoteManager.java
com.plnyyanks.frcnotebook.activities.SettingsActivity.java
com.plnyyanks.frcnotebook.activities.StartActivity.java
com.plnyyanks.frcnotebook.activities.ViewEvent.java
com.plnyyanks.frcnotebook.activities.ViewMatch.java
com.plnyyanks.frcnotebook.activities.ViewTeam.java
com.plnyyanks.frcnotebook.adapters.ActionBarCallback.java
com.plnyyanks.frcnotebook.adapters.AdapterInterface.java
com.plnyyanks.frcnotebook.adapters.AllianceExpandableListAdapter.java
com.plnyyanks.frcnotebook.adapters.CustomExapandableListAdapter.java
com.plnyyanks.frcnotebook.adapters.ListViewArrayAdapter.java
com.plnyyanks.frcnotebook.adapters.MatchListExpandableListAdapter.java
com.plnyyanks.frcnotebook.adapters.NotesExpandableListAdapter.java
com.plnyyanks.frcnotebook.background.AddMatchesFromURL.java
com.plnyyanks.frcnotebook.background.DeleteEvent.java
com.plnyyanks.frcnotebook.background.GetEventMatches.java
com.plnyyanks.frcnotebook.background.GetNotesForMatch.java
com.plnyyanks.frcnotebook.background.GetNotesForTeam.java
com.plnyyanks.frcnotebook.background.GetTeamsAttending.java
com.plnyyanks.frcnotebook.background.ShowLocalEvents.java
com.plnyyanks.frcnotebook.background.ValidateNewEventData.java
com.plnyyanks.frcnotebook.database.BackupDatabase.java
com.plnyyanks.frcnotebook.database.DatabaseHandler.java
com.plnyyanks.frcnotebook.database.ImportDatabase.java
com.plnyyanks.frcnotebook.database.PreferenceHandler.java
com.plnyyanks.frcnotebook.datafeed.EventDetailFetcher.java
com.plnyyanks.frcnotebook.datafeed.EventListFetcher.java
com.plnyyanks.frcnotebook.datafeed.GET_Request.java
com.plnyyanks.frcnotebook.datafeed.MATCH_PROPS.java
com.plnyyanks.frcnotebook.datafeed.MatchDetailFetcher.java
com.plnyyanks.frcnotebook.datafeed.TBADatafeed.java
com.plnyyanks.frcnotebook.datafeed.USFIRSTParser.java
com.plnyyanks.frcnotebook.datatypes.Event.java
com.plnyyanks.frcnotebook.datatypes.ListElement.java
com.plnyyanks.frcnotebook.datatypes.ListGroup.java
com.plnyyanks.frcnotebook.datatypes.ListHeader.java
com.plnyyanks.frcnotebook.datatypes.ListItem.java
com.plnyyanks.frcnotebook.datatypes.Match.java
com.plnyyanks.frcnotebook.datatypes.Note.java
com.plnyyanks.frcnotebook.datatypes.Team.java
com.plnyyanks.frcnotebook.dialogs.AddNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.AddPredefNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.DatePickerFragment.java
com.plnyyanks.frcnotebook.dialogs.DeleteDialog.java
com.plnyyanks.frcnotebook.dialogs.EditNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.EditPredefNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.InputURLForMatchesDialog.java
com.plnyyanks.frcnotebook.dialogs.ProgressDialog.java
com.plnyyanks.frcnotebook.json.JSONManager.java