Friday, August 8, 2014

Baseball Scoreboard - The Parser

    While I'm presenting the parser near the end of this development, the initial work on this portion of the scoreboard occurred more than a year ago at the conclusion of last season when I was still toying with the idea of the project.  After initialization, the parser steps through the following loop:

  1. Wait for a file containing POST data,
  2. Unzip the gzipped POST data,
  3. Parse the resulting JSON to recover the game state (score/inning, etc),
  4. Send the appropriate serial command to the scoreboard,
  5. Parse the serial response until the ready prompt is received,
  6. Move the POST data file to the output directory

Before we step through how each part works, the quick start guide (starts after the jump):

Quick Start Guide

    In order to get up and running, you'll need to ssh into the Raspberry Pi and do the following:
    • sudo cpan -i Device::SerialPort Compress:Zlib
  • Download install tarballs into your home directory 
    • cd
    • wget
  • Unpack tarball in your root directory 
    • cd /
    • sudo tar -xvzf /home/pi/gc-parser.tar.gz
    At this point, if you installed config.tar.gz from the networking post, you should be good to go with a reboot.  If not, revisit the quick-start portion of that post to ensure the correct entry gets made into rc.local.

Design Goals

    For this portion of the project, my primary goals were to decode the messages copied by the proxy (see previous post), strip out the appropriate information about the game's state, and then feed that information to the scoreboard for display.  Secondarily, I also wanted to make sure that I had appropriate hooks in the parser to support additional functionality (for instance, displaying messages to the fans).

Decoding the POST data

    When the GameChanger app sends information to the server, it does so using gzip compressed POST data (in truth, there are some uncompressed messages, but they don't hold the info we care about).  Here's a small sample file to illustrate what the traffic looks like:

Content-Disposition: file; name="gzjson"; filename="json.gz"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary


In theory, this should be straight-forward.  Strip off the header and footer (I used the \x0d\x0a\0x0d\x0a\x78\x01 sequence to id the beginning of the gzip content and \x0d\x0a\x2d\x2d sequence to id the end), run it through the Compress:Zlib module, and voila! plain text content.  In reality, there was a little wrinkle that hung me up for the better part of a day trying to troubleshoot.  The problem turned out being that despite my best efforts, perl continued to treat my binary file as a text file, randomly stripping out what it considered to be line ends (hex \x0a).  By inserting a couple of simple checks to restore dropped line ends, the gzip file was made whole and reliable decompression was achieved.  Now that we have uncompressed content, it's time to pull the information we care about.

Parsing JSON

    Once decompressed, we are confronted with a JSON object.  Here's a small sample of that traffic, formatted for readability (not the same sample as given above, for those following along at home, game/player IDs altered as well):

   "stream" : {
      "game_id" : "0123456789abba9876543210",
      "shl" : {

      "state" : {
         "count" : {
            "outs" : 1,
            "strikes" : 0,
            "balls" : 1
         "situation" : {
            "inning" : 6,
            "half" : 0,
            "bases" : [
                  "base" : 1,
                  "player" : {
                     "$ref" : "player",
                     "$id" : "0123456789abba9876543210"
            "batter" : {
               "$ref" : "player",
               "$id" : "0123456789abba9876543210"
            "home_lineupindex" : 0,
            "pitcher" : {
               "$ref" : "player",
               "$id" : "0123456789abba9876543210"
            "away_lineupindex" : 9
         "score" : {
            "away" : 1,
            "home" : 9

Looking at the underlined content above, you can see that the game is 1-9 at the top(0) of the 6th.  Ball-Strike-Out count is 1-0-1 and there is a runner on 1st.  Using regular expressions, the parser pulls those pieces of information and places them in variables.  If nothing has changed since the last update, nothing is done.  If a change occurs, then the change is sent to the scoreboard.

Updating the Scoreboard

    Now that we have a new game state, we have to send the command to the scoreboard for display.  As we covered serial communications to the scoreboard in a previous post, I'll refer you there for more details.  For updating status, the primary command used is SGS (Set Game State).  Given the example above, the command sent to the scoreboard would be:

        SGS 1,9,0,6,1,0,0,1,0,1
Once the command is sent, the parser reads out response from the serial bus until hits the ready prompt (Enter command:), at which time, the scoreboard is considered set.

Other Parser Functions

     As the gatekeeper to the scoreboard, the parser also implements some additional functionality.  At startup, the parser initializes the USB serial connection and makes sure it's socket options are set correctly (believe it or not, this was a problem for me as most of my original scoreboard programming was done on a Mac).  After initialized, the scoreboard will also convert and send the team logo to the scoreboard (logo is located in the file /home/pi/gamechanger/team_settings/logo.txt).  If the scoreboard detects which side your team is on (using the team id within the /home/pi/gamechanger/team_settings/team_id.txt file), it will then set your team's side (resulting in your logo being shown on that side).  Lastly, the program periodically checks for the existence of a command file (located at /home/pi/gamechanger/command.txt) and, if detected, sends the command to the scoreboard.


   If you have slogged through all of the blog posts and gotten to this point, you should now actually be able to recreate my demo from the start (all of the parts exist for basic functionality).  Have fun!  The next post will cover a little of the gravy that I created to handle routine tasks I didn't want to have to SSH into the Pi for.

Up next …. the Webserver

No comments:

Post a Comment