PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/ACARS/xacars.php

http://github.com/nshahzad/phpVMS
PHP | 361 lines | 234 code | 60 blank | 67 comment | 21 complexity | b01833a6d48116007100f0d4e7b1b198 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * phpVMS ACARS integration
  4. *
  5. * Interface for use with XACARS
  6. * http://www.xacars.net/
  7. *
  8. *
  9. * This file goes as this:
  10. * The URL given is:
  11. * <site>/index.php/acars/xacars/<action>
  12. *
  13. * SDK Docs: http://www.xacars.net/index.php?Client-Server-Protocol
  14. */
  15. error_reporting(E_ALL ^ E_NOTICE);
  16. ini_set('display_errors', 'on');
  17. Debug::log($_SERVER['QUERY_STRING'], 'xacars');
  18. Debug::log($_SERVER['REQUEST_URI'], 'xacars');
  19. Debug::log(serialize($_REQUEST), 'xacars');
  20. class Coords {
  21. public $lat;
  22. public $lng;
  23. }
  24. switch($acars_action)
  25. {
  26. /* Request data about a flight */
  27. case 'data':
  28. $flight = $_REQUEST['DATA2'];
  29. Debug::log('FLIGHT PLAN REQUEST', 'xacars');
  30. # They requested latest bid
  31. if(strtolower($flight) == 'bid')
  32. {
  33. /*preg_match('/^([A-Za-z]*)(\d*)/', $_REQUEST['DATA4'], $matches);
  34. $code = $matches[1];
  35. $pilotid = intval($matches[2]) - Config::Get('PILOTID_OFFSET');*/
  36. $pilotid = PilotData::parsePilotID($_REQUEST['DATA4']);
  37. $route = SchedulesData::GetLatestBid($pilotid);
  38. if(!$route)
  39. {
  40. echo '0|No bids found!';
  41. return;
  42. }
  43. }
  44. else
  45. {
  46. if(is_numeric($flight))
  47. {
  48. echo '0|No airline code entered!';
  49. return;
  50. }
  51. $flightinfo = SchedulesData::getProperFlightNum($flight);
  52. $code = $flightinfo['code'];
  53. $flight_num = $flightinfo['flightnum'];
  54. $route = SchedulesData::GetScheduleByFlight($code, $flight_num);
  55. Debug::log(print_r($route, true), 'xacars');
  56. if(!$route)
  57. {
  58. echo '0|Flight not found, make sure you include the flight code!';
  59. return;
  60. }
  61. }
  62. /* Ok to proceed */
  63. if($route->flighttype=='H')
  64. {
  65. $maxpax = $route->maxpax;
  66. }
  67. else
  68. {
  69. if($route->flighttype=='C')
  70. {
  71. $maxcargo = FinanceData::getLoadCount($route->aircraftid, 'C');
  72. }
  73. else
  74. {
  75. $maxpax = FinanceData::getLoadCount($route->aircraftid, 'P');
  76. }
  77. }
  78. echo
  79. "1|flightplan
  80. $route->depicao
  81. $route->arricao
  82. $route->arricao
  83. $route->route
  84. $maxpax
  85. $maxcargo
  86. IFR
  87. $route->registration
  88. $route->flightlevel
  89. ";
  90. break;
  91. case 'acars':
  92. case 'xacars':
  93. # Pass success by default
  94. $outstring = 'Success';
  95. $fields = array();
  96. $_REQUEST['DATA2'] = strtoupper($_REQUEST['DATA2']);
  97. if($_REQUEST['DATA2'] == 'TEST')
  98. {
  99. echo '1|OK';
  100. return;
  101. }
  102. elseif($_REQUEST['DATA2'] == 'ENDFLIGHT')
  103. {
  104. echo '1|OK';
  105. return;
  106. }
  107. elseif($_REQUEST['DATA2'] == 'PAUSEFLIGHT')
  108. {
  109. echo '1|OK';
  110. return;
  111. }
  112. elseif($_REQUEST['DATA2'] == 'BEGINFLIGHT')
  113. {
  114. /*
  115. VMA001||VMW5421|N123K5||KORD~~KMIA|N51 28.3151 W0 26.8892|88||||59|328|00000|14|IFR|0||
  116. */
  117. Debug::log('BEGINFLIGHT', 'xacars');
  118. $data = explode('|', $_REQUEST['DATA3']);
  119. /* Get the pilot info */
  120. /*preg_match('/^([A-Za-z]*)(\d*)/', $data[0], $matches);
  121. $code = $matches[1];
  122. $pilotid = intval($matches[2]) - Config::Get('PILOTID_OFFSET');*/
  123. $pilotid = PilotData::parsePilotID($data[0]);
  124. /* Get Coordinates */
  125. $coords = Util::get_coordinates($data[6]);
  126. /* Get route */
  127. $route = explode('~', $data[5]);
  128. $depicao = $route[0];
  129. $arricao = $route[count($route)-1];
  130. /* Unset the start and end points of the route,
  131. and pass the rest in.
  132. @version 2.1
  133. */
  134. unset($route[0]);
  135. unset($route[count($route)-1]);
  136. $route = implode(' ', $route);
  137. $flightnum = $data[2];
  138. $aircraft = $data[3];
  139. $heading = $data[12];
  140. $alt = $data[7];
  141. $deptime = time();
  142. $fields = array(
  143. 'flightnum'=>$flightnum,
  144. 'aircraft'=>$aircraft,
  145. 'lat'=>$coords['lat'],
  146. 'lng'=>$coords['lng'],
  147. 'heading'=>$heading,
  148. 'route'=>$route,
  149. 'alt'=>$alt,
  150. 'gs'=>$gs,
  151. 'depicao'=>$depicao,
  152. 'arricao'=>$arricao,
  153. 'deptime'=>$deptime,
  154. 'phasedetail'=>'At the gate',
  155. 'online'=>$_GET['Online'],
  156. 'client'=>'xacars',
  157. );
  158. Debug::log(print_r($fields, true), 'xacars');
  159. $outstring = $pilotid;
  160. }
  161. elseif($_REQUEST['DATA2'] == 'MESSAGE')
  162. {
  163. $data = $_REQUEST['DATA4'];
  164. $pilotid = $_REQUEST['DATA3'];
  165. /* Get the flight information, from ACARS, need to
  166. pull the latest flight data via the flight number
  167. since acars messages don't transmit the pilot ID */
  168. preg_match("/Flight ID:.(.*)\n/", $data, $matches);
  169. $flight_data = ACARSData::get_flight_by_pilot($pilotid);
  170. Debug::log('Flight data:', 'xacars');
  171. Debug::log(print_r($_REQUEST, true), 'xacars');
  172. Debug::log('PilotID: '.$pilotid, 'xacars');
  173. // Get coordinates from ACARS message
  174. preg_match("/POS(.*)\n/", $data, $matches);
  175. $coords = Util::get_coordinates(trim($matches[1]));
  176. // Get our heading
  177. preg_match("/\/HDG.(.*)\n/", $data, $matches);
  178. $heading = $matches[1];
  179. // Get our altitude
  180. preg_match("/\/ALT.(.*)\n/", $data, $matches);
  181. $alt = $matches[1];
  182. // Get our speed
  183. preg_match("/\/IAS.(.*)\//", $data, $matches);
  184. $gs = $matches[1];
  185. // Get the OUT time
  186. preg_match("/OUT.(.*) \/ZFW/", $data, $matches);
  187. $deptime = $matches[1];
  188. /* We don't need to update every field, just a few of them
  189. */
  190. $fields = array(
  191. 'lat'=>$coords['lat'],
  192. 'lng'=>$coords['lng'],
  193. 'heading'=>$heading,
  194. 'alt'=>$alt,
  195. 'gs'=>$gs,
  196. 'phasedetail'=>'Enroute',
  197. );
  198. }
  199. else
  200. {
  201. return;
  202. }
  203. # Get the distance remaining
  204. $depapt = OperationsData::GetAirportInfo($depicao);
  205. $dist_remain = SchedulesData::distanceBetweenPoints($coords->lat, $coords->lng, $depapt->lat, $depapt->lng);
  206. # Estimate the time remaining
  207. if($gs > 0)
  208. {
  209. $time_remain = $dist_remain / $gs;
  210. }
  211. else
  212. {
  213. $time_remain = '00:00';
  214. }
  215. ob_start();
  216. $fields['distremain'] = $dist_remain;
  217. $fields['timeremaining'] = $time_remain;
  218. Debug::log(print_r($fields, true), 'xacars');
  219. ACARSData::UpdateFlightData($pilotid, $fields);
  220. echo '1|'.$outstring;
  221. break;
  222. case 'pirep':
  223. $data = explode('~', $_REQUEST['DATA2']);
  224. $flightinfo = SchedulesData::getProperFlightNum($data[2]);
  225. $code = $flightinfo['code'];
  226. $flightnum = $flightinfo['flightnum'];
  227. /*if(!is_numeric($data[0]))
  228. {
  229. # see if they are a valid pilot:
  230. preg_match('/^([A-Za-z]*)(\d*)/', $data[0], $matches);
  231. $pilot_code = $matches[1];
  232. $pilotid = intval($matches[2]) - Config::Get('PILOTID_OFFSET');
  233. }
  234. else
  235. {
  236. $pilotid = $data[0];
  237. }*/
  238. $pilotid = PilotData::parsePilotID($data[0]);
  239. # Make sure airports exist:
  240. # If not, add them.
  241. $depicao = $data[6];
  242. $arricao = $data[7];
  243. if(!OperationsData::GetAirportInfo($depicao))
  244. {
  245. OperationsData::RetrieveAirportInfo($depicao);
  246. }
  247. if(!OperationsData::GetAirportInfo($arricao))
  248. {
  249. OperationsData::RetrieveAirportInfo($arricao);
  250. }
  251. # Get aircraft information
  252. $reg = trim($data[3]);
  253. $ac = OperationsData::GetAircraftByReg($reg);
  254. # Load info
  255. /* If no passengers set, then set it to the cargo */
  256. $load = $data[14];
  257. if(empty($load))
  258. $load = $data[15];
  259. # Convert the time to xx.xx
  260. $flighttime = floatval(str_replace(':', '.', $data[11])) * 1.00;
  261. /* Fuel conversion - XAcars only reports in lbs */
  262. $fuelused = $data[12];
  263. if(Config::Get('LiquidUnit') == '0')
  264. {
  265. # Convert to KGs, divide by density since d = mass * volume
  266. $fuelused = ($fuelused * .45359237) / .8075;
  267. }
  268. # Convert lbs to gallons
  269. elseif(Config::Get('LiquidUnit') == '1')
  270. {
  271. $fuelused = $fuelused * 6.84;
  272. }
  273. # Convert lbs to kgs
  274. elseif(Config::Get('LiquidUnit') == '2')
  275. {
  276. $fuelused = $fuelused * .45359237;
  277. }
  278. $acars_data = ACARSData::get_flight_by_pilot($pilotid);
  279. $data = array(
  280. 'pilotid'=>$pilotid,
  281. 'code'=>$code,
  282. 'flightnum'=>$flightnum,
  283. 'depicao'=>$depicao,
  284. 'arricao'=>$arricao,
  285. 'aircraft'=>$ac->id,
  286. 'flighttime'=>$flighttime,
  287. 'submitdate'=>'NOW()',
  288. 'route' => $acars_data->route,
  289. 'route_details' => $acars_data->route_details,
  290. 'comment'=>$comment,
  291. 'fuelused'=>$fuelused,
  292. 'source'=>'xacars',
  293. 'load'=>$load,
  294. 'log'=> $_GET['log']
  295. );
  296. Debug::log(print_r($data, true), 'xacars');
  297. $ret = ACARSData::FilePIREP($pilotid, $data);
  298. echo '1|Success';
  299. break;
  300. }