001/*
002 *  $Rev: 1020 $:     Revision of last commit
003 *  $Author: tgutwin $:  Author of last commit
004 *  $Date: 2015-10-28 15:59:29 -0700 (Wed, 28 Oct 2015) $:    Date of last commit
005 *  $URL: svn://svn.webarts.bc.ca/open/trunk/projects/WebARTS/ca/bc/webarts/tools/F1Telemetry.java $
006 */
007/*
008 *
009 *  Written by Tom Gutwin - WebARTS Design.
010 *  Copyright (C) 2015 WebARTS Design, North Vancouver Canada
011 *  http://www.webarts.bc.ca
012 *
013 *  This program is free software; you can redistribute it and/or modify
014 *  it under the terms of the GNU General Public License as published by
015 *  the Free Software Foundation; either version 2 of the License, or
016 *  (at your option) any later version.
017 *
018 *  This program is distributed in the hope that it will be useful,
019 *  but WITHOUT ANY WARRANTY; without_ even the implied warranty of
020 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 *  GNU General Public License for more details.
022 *
023 *  You should have received a copy of the GNU General Public License
024 *  along with this program; if not, write to the Free Software
025 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
026 */
027package ca.bc.webarts.tools;
028
029import java.io.*;
030import java.net.*;
031
032// Jar it up with this:
033// cd WebARTS/build/classes
034//   jar cfe ../F1Telemetry.jar ca.bc.webarts.tools.F1Telemetry ca/bc/webarts/tools/F1*.class
035
036/**
037  * Listens to the Codemasters F1 Game telemetry.
038  * Before receiving the data we need to reconfigure F1 2012 to point to our end point.
039  * This is done by modifying the "hardware_settings_config.xml" file found in your My Documents
040  * folder subpath ./My Games/FormulaOne2012/hardwaresettings/. Open the file and change the lines
041  * <br>
042  *  &nbsp;&nbsp;&nbsp;&lt;motion enabled="true" extradata="0" delay="1" port="20777" ip="dbox"/&gt;<br>&nbsp;&nbsp;&nbsp;
043  *  &lt;led_display nativeSupport="true"/&gt;<br>to<br>
044  *  &nbsp;&nbsp;&nbsp;&lt;led_display nativeSupport="false" fanatecNativeSupport="false" /&gt;<br>
045  *  &nbsp;&nbsp;&nbsp;&lt;motion enabled="true" ip="127.0.0.1" port="20777" delay="1" extradata="3" /&gt;<br>
046  * See more at: http://www.robertgray.net.au/posts/2012/3/connecting-to-codemasters-f1-telemetry-feed#.VQ-HbMviveQ
047  *   or http://www.robertgray.net.au/posts/codemasters-f1-data-feed-updated
048  *<br>
049  * <p>See the @see F1TelemetryPacket
050**/
051class F1Telemetry
052{
053  public static final int TELEMETRY_PORT_DEFAULT = 20777;
054  public static final int TELEMETRY_PACKET_SIZE_DEFAULT = 10240;
055  public static int receivedBytes = 0;
056  public static String helpText = "F1Telementry - Listens to the Codemasters F1 Game telemetry.\n"+
057                                    "  Before receiving the data you need to reconfigure F1 2012 to point to our end point.\n"+
058                                    "  this is done by modifying the \"hardware_settings_config.xml\" file found in your\n"+
059                                    "  My Documents folder,  subpath ./My Games/FormulaOne2012/hardwaresettings/. \n"+
060                                    "  Open the file and change the lines:\n"+
061                                    "      <motion enabled=\"true\" extradata=\"0\" delay=\"1\" port=\"20777\" ip=\"dbox\">\n"+
062                                    "      <led_display nativeSupport=\"true\">\n   to\n"+
063                                    "      <motion enabled=\"true\" extradata=\"3\" delay=\"1\" ip=\"127.0.0.1\" port=\"20777\">\n"+
064                                    "      <led_display nativeSupport=\"false\" fanatecNativeSupport=\"false\" >\n"+
065                                    "  See more at: http://www.robertgray.net.au/posts/2012/3/connecting-to-codemasters-f1-telemetry-feed#.VQ-HbMviveQ";
066
067  public static void main(String args[]) throws Exception
068  {
069    DatagramSocket serverSocket = new DatagramSocket(TELEMETRY_PORT_DEFAULT);
070    System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
071    System.out.println(helpText);
072    System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
073    System.out.println("   F1Telementry listening on PORT: "+TELEMETRY_PORT_DEFAULT);
074    byte[] receiveData = new byte[TELEMETRY_PACKET_SIZE_DEFAULT];
075    F1TelemetryPacket f1Packet = null;
076    while (true)
077    {
078      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
079      serverSocket.receive(receivePacket);
080      receivedBytes = receivePacket.getLength();
081
082      byte[] myObjectDataBytes = new byte[receivedBytes];
083
084      for(int i = 0; i < receivedBytes; i++)
085      {
086           myObjectDataBytes[i] = receiveData[i];
087      }
088
089      f1Packet = F1TelemetryPacket.deserializePacket(myObjectDataBytes);
090      doStuffWithDataPacket(f1Packet);
091
092      /*
093      InetAddress IPAddress = receivePacket.getAddress();
094      int port = receivePacket.getPort();
095      String capitalizedSentence = sentence.toUpperCase();
096      sendData = capitalizedSentence.getBytes();
097      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
098      serverSocket.send(sendPacket);
099      */
100    }
101  }
102
103  public static void doStuffWithDataPacket(F1TelemetryPacket f1Packet)
104  {
105    // whatever you want to do!
106    if(f1Packet!=null) System.out.println("\n  ----------\n Packet: \n" + f1Packet.toString());
107
108  }
109
110}