#Generate position file
$charset = "ISO-8859-15";
$host = "localhost";
$user = "root";
$pwd = "JgJoCt3_sql";
$sql_id = mysql_connect($host,$user,$pwd);
if(!$sql_id) {
die('Could not connect to database server: ' . mysql_error());
}
$eventid = mysql_escape_string($_REQUEST["eventid"]);
$raceid = mysql_escape_string($_REQUEST["raceid"]);
$mq_event = mysql_query("SELECT * FROM tractrac.Events WHERE UUID = '$eventid'") or die(mysql_error());
$event = mysql_fetch_assoc($mq_event);
$database = $event["Database"];
mysql_select_db($database);
$filename = "";
$mq_race = mysql_query("SELECT * FROM " . $database . ".Races WHERE UUID = '$raceid'") or die(mysql_error());
while ($race_arr = mysql_fetch_assoc($mq_race)) {
$race = $race_arr["UUID"];
$filename = "/tmp/" . $database . "_" . preg_replace("[^a-zA-Z0-9]","",$race_arr['UUID']) . ".txt";
$fp = fopen($filename,"w");
$compGpsQuery = "(SELECT TrackingDeviceId FROM ".$database.".tracking_device_sessions INNER JOIN ".$database.".RaceObject ON tracking_device_sessions.RaceObjectId = RaceObject.UUID WHERE RaceId = '$race')";
$markGPSQuery = "(SELECT TrackingDeviceId FROM ".$database.".tracking_device_sessions INNER JOIN ".$database.".RaceObject ON tracking_device_sessions.RaceObjectId = RaceObject.UUID WHERE TrackedObjectId IN (SELECT ControlPoint FROM ".$database.".RouteControlPoints WHERE Route IN (SELECT DefaultRoute FROM ".$database.".Races WHERE UUID = '$race')))";
$query = "SELECT * FROM ".$database.".GPSData WHERE (TrackingDeviceId IN $compGpsQuery OR TrackingDeviceId IN $markGPSQuery) AND SampleTime >= (Select StartTime FROM ".$database.".Races WHERE UUID = '$race') AND SampleTime <= (Select EndTime FROM ".$database.".Races WHERE UUID = '$race') AND Reduced = 0 ORDER BY SampleTime";
$result = mysql_query($query);
if (mysql_check_query($result,$query)) {
while ($row = mysql_fetch_assoc($result)) {
$gpsid = $row['TrackingDeviceId'];
$gpscount[$gpsid] = $gpscount[$gpsid]+1;
$lat = $row['Latitude'];
$lon = $row['Longitude'];
if ($lat >= 0) {
$latstr = "N";
} else {
$latstr = "S";
}
$lat = abs($lat);
$deg = ((int)floor($lat));
$min = ($lat-$deg)*60;
#if ($deg < 10) { $deg = "0".$deg; }
if ($min < 10) { $min = "0".$min; }
$latstr .= $deg.".".$min;
if ($lon >= 0) {
$lonstr = "E";
} else {
$lonstr = "W";
}
$lon = abs($lon);
$deg = ((int)floor($lon));
$min = ($lon-$deg)*60;
#if ($deg < 10) { $deg = "0".$deg; }
if ($min < 10) { $min = "0".$min; }
$lonstr .= $deg.".".$min;
// 2007.03.12 08:00:18 : -18,UNITSTAT,20070312,070011,7000931435,N60.31.9159,E8.12.5665,13,251,9,3922,-61,2,24201,766,9,1893
//fwrite($fp,logdatestr($row['ReceivedTime'])." : ");
fwrite($fp, "-".$gpscount[$gpsid].",");
fwrite($fp, "UNITSTAT,");
fwrite($fp,sampledatestr($row['SampleTime']).",");
fwrite($fp,$gpsid.",");
fwrite($fp,$latstr.",");
fwrite($fp,$lonstr.",");
fwrite($fp,sprintf("%1.0f,", $row['Speed']));
fwrite($fp,sprintf("%1.0f,", $row['Direction']));
fwrite($fp,sprintf("%d,", $row['Satellites']));
fwrite($fp,sprintf("%1.0f,", $row['Voltage']));
fwrite($fp,sprintf("-00,")); // Signal strength
fwrite($fp,sprintf("0000,")); // Unit State
fwrite($fp,sprintf("00000,")); // Current network
fwrite($fp,sprintf("%1.0f,", $row['Elevation']));
fwrite($fp,sprintf("000,")); // HDOP
fwrite($fp,sprintf("0000")); // CellId
fwrite($fp,"\n");
}
mysql_free_result($result);
}
fclose($fp);
}
#Clear the existing markroundings
$sql = "DELETE FROM ControlPointResults WHERE RaceObjectId IN (SELECT UUID FROM RaceObject WHERE RaceId = '$raceid')";
mysql_query($sql) or die(mysql_error());
#reset devicehandler and broker
system("sudo -u tractrac /home/tractrac/services/system-main/broker-recalc/reset > /dev/null 2> /dev/null");
system("sudo -u tractrac /home/tractrac/services/system-main/devicehandler-recalc/reset > /dev/null 2> /dev/null");
#Run lommysim
system("java -jar /data/software/2.0/lommysimpipe/dist-current/LommySim.jar dest=localhost:13400 sourcetext=\"$filename\" speedup=100 replaytime=sample gui=0 reloaddataserver=1 gmsdestport=3411 eventid=$eventid raceid=$raceid");
?>
Please wait a few minutes for the recalculation to finish