module.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php namespace frieren\core;
  2. /* Code modified by Frieren Auto Refactor */
  3. class dump1090 extends Controller
  4. {
  5. protected $endpointRoutes = ['refreshInfo', 'refreshOutput', 'clearOutput', 'refreshStatus', 'toggledump1090', 'handleDependencies', 'handleDependenciesStatus', 'refreshHistory', 'deleteHistory', 'downloadHistory', 'viewHistory', 'getSettings', 'setSettings', 'refreshList'];
  6. protected function checkDep($dependencyName)
  7. {
  8. return ($this->systemHelper->checkDependency($dependencyName) && ($this->systemHelper->uciGet("dump1090.module.installed")));
  9. }
  10. protected function getDevice()
  11. {
  12. return trim(exec("cat /proc/cpuinfo | grep machine | awk -F: '{print $2}'"));
  13. }
  14. protected function refreshInfo()
  15. {
  16. $moduleInfo = @json_decode(file_get_contents("/pineapple/modules/dump1090/module.info"));
  17. $this->responseHandler->setData(array('title' => $moduleInfo->title, 'version' => $moduleInfo->version));
  18. }
  19. public function handleDependencies()
  20. {
  21. if (!$this->checkDep("dump1090")) {
  22. $this->systemHelper->execBackground("/pineapple/modules/dump1090/scripts/dependencies.sh install ".$this->request['destination']);
  23. $this->responseHandler->setData(array('success' => true));
  24. } else {
  25. $this->systemHelper->execBackground("/pineapple/modules/dump1090/scripts/dependencies.sh remove");
  26. $this->responseHandler->setData(array('success' => true));
  27. }
  28. }
  29. public function handleDependenciesStatus()
  30. {
  31. if (!file_exists('/tmp/dump1090.progress')) {
  32. $this->responseHandler->setData(array('success' => true));
  33. } else {
  34. $this->responseHandler->setData(array('success' => false));
  35. }
  36. }
  37. public function toggledump1090()
  38. {
  39. if (!$this->systemHelper->checkRunning("dump1090")) {
  40. $this->systemHelper->execBackground("/pineapple/modules/dump1090/scripts/dump1090.sh start");
  41. } else {
  42. $this->systemHelper->execBackground("/pineapple/modules/dump1090/scripts/dump1090.sh stop");
  43. }
  44. }
  45. public function refreshStatus()
  46. {
  47. if (!file_exists('/tmp/dump1090.progress')) {
  48. if (!$this->systemHelper->checkDependency("dump1090")) {
  49. $installed = false;
  50. $install = "Not installed";
  51. $installLabel = "danger";
  52. $processing = false;
  53. $status = "Start";
  54. $statusLabel = "success";
  55. $running = false;
  56. } else {
  57. $installed = true;
  58. $install = "Installed";
  59. $installLabel = "success";
  60. $processing = false;
  61. if ($this->systemHelper->checkRunning("dump1090")) {
  62. $status = "Stop";
  63. $statusLabel = "danger";
  64. $running = true;
  65. } else {
  66. $status = "Start";
  67. $statusLabel = "success";
  68. $running = false;
  69. }
  70. }
  71. } else {
  72. $installed = false;
  73. $install = "Installing...";
  74. $installLabel = "warning";
  75. $processing = true;
  76. $status = "Start";
  77. $statusLabel = "success";
  78. $running = false;
  79. }
  80. $device = $this->systemHelper->getDevice();
  81. $sdAvailable = $this->systemHelper->isSDAvailable();
  82. $this->responseHandler->setData(array("device" => $device, "sdAvailable" => $sdAvailable, "status" => $status, "statusLabel" => $statusLabel, "installed" => $installed, "install" => $install, "installLabel" => $installLabel, "processing" => $processing, "running" => $running));
  83. }
  84. public function refreshOutput()
  85. {
  86. if ($this->systemHelper->checkDependency("dump1090")) {
  87. if (file_exists("/tmp/dump1090_capture.log")) {
  88. $output = file_get_contents("/tmp/dump1090_capture.log");
  89. if (!empty($output)) {
  90. $this->responseHandler->setData($output);
  91. } else {
  92. $this->responseHandler->setData("dump1090 is running...");
  93. }
  94. } else {
  95. $this->responseHandler->setData("dump1090 is not running...");
  96. }
  97. } else {
  98. $this->responseHandler->setData("dump1090 is not installed...");
  99. }
  100. }
  101. public function clearOutput()
  102. {
  103. exec("rm -rf /tmp/dump1090_capture.log");
  104. }
  105. public function refreshHistory()
  106. {
  107. $this->streamFunction = function () {
  108. $log_list = array_reverse(glob("/pineapple/modules/dump1090/log/*.log"));
  109. echo '[';
  110. for ($i=0;$i<count($log_list);$i++) {
  111. $info = explode("_", basename($log_list[$i]));
  112. $entryDate = gmdate('Y-m-d H-i-s', $info[1]);
  113. $entryName = basename($log_list[$i], ".log");
  114. if (file_exists("/pineapple/modules/dump1090/log/".$entryName.".csv")) {
  115. echo json_encode(array($entryDate, $entryName.".log", $entryName.".csv"));
  116. } else {
  117. echo json_encode(array($entryDate, $entryName.".log", ''));
  118. }
  119. if ($i!=count($log_list)-1) {
  120. echo ',';
  121. }
  122. }
  123. echo ']';
  124. };
  125. }
  126. public function downloadHistory()
  127. {
  128. $this->responseHandler->setData(array("download" => $this->systemHelper->downloadFile("/pineapple/modules/dump1090/log/".$this->request['file'])));
  129. }
  130. public function viewHistory()
  131. {
  132. $log_date = gmdate("F d Y H:i:s", filemtime("/pineapple/modules/dump1090/log/".$this->request['file']));
  133. exec("strings /pineapple/modules/dump1090/log/".$this->request['file'], $output);
  134. if (!empty($output)) {
  135. $this->responseHandler->setData(array("output" => implode("\n", $output), "date" => $log_date));
  136. } else {
  137. $this->responseHandler->setData(array("output" => "Empty log...", "date" => $log_date));
  138. }
  139. }
  140. public function deleteHistory()
  141. {
  142. $file = basename($this->request['file'], ".log");
  143. exec("rm -rf /pineapple/modules/dump1090/log/".$file.".*");
  144. }
  145. public function getSettings()
  146. {
  147. $settings = array(
  148. 'csv' => $this->systemHelper->uciGet("dump1090.settings.csv"),
  149. 'gain' => $this->systemHelper->uciGet("dump1090.settings.gain"),
  150. 'frequency' => $this->systemHelper->uciGet("dump1090.settings.frequency"),
  151. 'metrics' => $this->systemHelper->uciGet("dump1090.settings.metrics"),
  152. 'agc' => $this->systemHelper->uciGet("dump1090.settings.agc"),
  153. 'aggressive' => $this->systemHelper->uciGet("dump1090.settings.aggressive")
  154. );
  155. $this->responseHandler->setData(array('settings' => $settings));
  156. }
  157. public function setSettings()
  158. {
  159. $settings = $this->request['settings'];
  160. $this->systemHelper->uciSet("dump1090.settings.gain", $settings->gain);
  161. $this->systemHelper->uciSet("dump1090.settings.frequency", $settings->frequency);
  162. if ($settings->csv) {
  163. $this->systemHelper->uciSet("dump1090.settings.csv", 1);
  164. } else {
  165. $this->systemHelper->uciSet("dump1090.settings.csv", 0);
  166. }
  167. if ($settings->metrics) {
  168. $this->systemHelper->uciSet("dump1090.settings.metrics", 1);
  169. } else {
  170. $this->systemHelper->uciSet("dump1090.settings.metrics", 0);
  171. }
  172. if ($settings->agc) {
  173. $this->systemHelper->uciSet("dump1090.settings.agc", 1);
  174. } else {
  175. $this->systemHelper->uciSet("dump1090.settings.agc", 0);
  176. }
  177. if ($settings->aggressive) {
  178. $this->systemHelper->uciSet("dump1090.settings.aggressive", 1);
  179. } else {
  180. $this->systemHelper->uciSet("dump1090.settings.aggressive", 0);
  181. }
  182. }
  183. public function refreshList()
  184. {
  185. $this->streamFunction = function () {
  186. echo file_get_contents("http://127.0.0.1:9090/data.json");
  187. };
  188. }
  189. }