api.mapon.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * MapOn cars GPS location service API wrapper
  4. */
  5. class MapOn {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains
  14. *
  15. * @var string
  16. */
  17. protected $apiKey = '';
  18. /**
  19. * MaponAPI SDK object placeholder
  20. *
  21. * @var object
  22. */
  23. protected $api = '';
  24. /**
  25. * System messages object placeholder
  26. *
  27. * @var object
  28. */
  29. protected $messages = '';
  30. /**
  31. * Default API URL
  32. */
  33. const API_URL = 'https://mapon.com/api/v1/';
  34. /**
  35. * Creates new API wrapper
  36. */
  37. public function __construct() {
  38. $this->loadConfig();
  39. $this->initMessages();
  40. $this->initMapOn();
  41. }
  42. /**
  43. * Loads all required configs and sets some options
  44. *
  45. * @global object $ubillingConfig
  46. *
  47. * @return void
  48. */
  49. protected function loadConfig() {
  50. global $ubillingConfig;
  51. $this->altCfg = $ubillingConfig->getAlter();
  52. $this->apiKey = $this->altCfg['MAPON_APIKEY'];
  53. }
  54. /**
  55. * Inits MaponAPI SDK object into protected proterty for further usage
  56. *
  57. * @return void
  58. */
  59. protected function initMapOn() {
  60. require_once 'api/libs/api.maponapi.php';
  61. $this->api = new MaponAPI($this->apiKey, self::API_URL);
  62. }
  63. /**
  64. * Inits system message helper object instance
  65. *
  66. * @return void
  67. */
  68. protected function initMessages() {
  69. $this->messages = new UbillingMessageHelper();
  70. }
  71. /**
  72. * Get all unit routes between some dates
  73. *
  74. * @param string $dateFrom
  75. * @param string $dateTo
  76. *
  77. * @return stdObj
  78. */
  79. public function getRoutes($dateFrom, $dateTo) {
  80. $result = $this->api->get('route/list', array(
  81. 'from' => '' . $dateFrom,
  82. 'till' => '' . $dateTo,
  83. 'include' => array('polyline', 'speed')
  84. ));
  85. return ($result);
  86. }
  87. /**
  88. * Returns array of all unit routes by current day
  89. *
  90. * @return array
  91. */
  92. public function getTodayRoutes() {
  93. $result = array();
  94. $curday = curdate();
  95. $routes = $this->getRoutes($curday . 'T00:00:00Z', $curday . 'T23:59:59Z');
  96. if ($routes) {
  97. if (isset($routes->data)) {
  98. foreach ($routes->data->units as $io => $each) {
  99. $unitId = $each->unit_id;
  100. foreach ($each->routes as $route) {
  101. if ($route->type == 'route') {
  102. if (@$route->speed) {
  103. $points = $this->api->decodePolyline($route->polyline, $route->speed, strtotime($route->start->time));
  104. $result[$unitId][] = $points;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. return ($result);
  112. }
  113. /**
  114. * Reuturns current units state
  115. *
  116. * @return array
  117. */
  118. public function getUnits() {
  119. $result = array();
  120. $raw = $this->api->get('unit/list', array('include' => array('drivers', 'supply_voltage')));
  121. if ($raw) {
  122. if ($raw->data) {
  123. foreach ($raw->data as $io => $eachUnit) {
  124. if (!empty($eachUnit)) {
  125. foreach ($eachUnit as $ia => $each) {
  126. $unitId = $each->unit_id;
  127. $result[$unitId]['unitid'] = $unitId;
  128. $result[$unitId]['label'] = $each->label;
  129. $result[$unitId]['number'] = $each->number;
  130. $result[$unitId]['mileage'] = $each->mileage;
  131. $result[$unitId]['speed'] = $each->speed;
  132. $result[$unitId]['lat'] = $each->lat;
  133. $result[$unitId]['lng'] = $each->lng;
  134. $result[$unitId]['supply_voltage'] = $each->supply_voltage->value;
  135. $result[$unitId]['last_update'] = $each->last_update;
  136. $result[$unitId]['state'] = $each->state->name;
  137. $result[$unitId]['driver'] = @$each->drivers->driver1->name;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. return ($result);
  144. }
  145. }
  146. ?>