octoprint.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. trait OctoPrintHomepageItem
  3. {
  4. public function octoprintSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Octoprint',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/octoprint.png',
  10. 'category' => 'Monitor',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = array(
  17. 'settings' => array(
  18. 'Enable' => array(
  19. array(
  20. 'type' => 'switch',
  21. 'name' => 'homepageOctoprintEnabled',
  22. 'label' => 'Enable',
  23. 'value' => $this->config['homepageOctoprintEnabled']
  24. ),
  25. array(
  26. 'type' => 'select',
  27. 'name' => 'homepageOctoprintAuth',
  28. 'label' => 'Minimum Authentication',
  29. 'value' => $this->config['homepageOctoprintAuth'],
  30. 'options' => $this->groupOptions
  31. )
  32. ),
  33. 'Connection' => array(
  34. array(
  35. 'type' => 'input',
  36. 'name' => 'octoprintURL',
  37. 'label' => 'URL',
  38. 'value' => $this->config['octoprintURL'],
  39. 'help' => 'Enter the IP:PORT of your Octoprint instance e.g. http://octopi.local'
  40. ),
  41. array(
  42. 'type' => 'input',
  43. 'name' => 'octoprintToken',
  44. 'label' => 'API Key',
  45. 'value' => $this->config['octoprintToken'],
  46. 'help' => 'Enter your Octoprint API key, found in Octoprint settings page.'
  47. ),
  48. ),
  49. 'Options' => array(
  50. array(
  51. 'type' => 'input',
  52. 'name' => 'octoprintHeader',
  53. 'label' => 'Title',
  54. 'value' => $this->config['octoprintHeader'],
  55. 'help' => 'Sets the title of this homepage module',
  56. ),
  57. array(
  58. 'type' => 'switch',
  59. 'name' => 'octoprintToggle',
  60. 'label' => 'Toggle Title',
  61. 'value' => $this->config['octoprintHeaderToggle'],
  62. 'help' => 'Shows/hides the title of this homepage module'
  63. ),
  64. ),
  65. )
  66. );
  67. return array_merge($homepageInformation, $homepageSettings);
  68. }
  69. public function octoprintHomepagePermissions($key = null)
  70. {
  71. $permissions = [
  72. 'main' => [
  73. 'enabled' => [
  74. 'homepageOctoprintEnabled'
  75. ],
  76. 'auth' => [
  77. 'homepageOctoprintAuth'
  78. ],
  79. 'not_empty' => [
  80. 'octoprintURL',
  81. 'octoprintToken'
  82. ]
  83. ]
  84. ];
  85. if (array_key_exists($key, $permissions)) {
  86. return $permissions[$key];
  87. } elseif ($key == 'all') {
  88. return $permissions;
  89. } else {
  90. return [];
  91. }
  92. }
  93. public function homepageOrderOctoprint()
  94. {
  95. if ($this->homepageItemPermissions($this->octoprintHomepagePermissions('main'))) {
  96. return '
  97. <div id="' . __FUNCTION__ . '">
  98. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading OctoPrint...</h2></div>
  99. <script>
  100. // Octoprint
  101. homepageOctoprint("' . $this->config['homepageOctoprintRefresh'] . '");
  102. // End Octoprint
  103. </script>
  104. </div>
  105. ';
  106. }
  107. }
  108. public function getOctoprintHomepageData()
  109. {
  110. if (!$this->homepageItemPermissions($this->octoprintHomepagePermissions('main'), true)) {
  111. return false;
  112. }
  113. $api = [];
  114. $url = $this->qualifyURL($this->config['octoprintURL']);
  115. $endpoints = ['job', 'settings'];
  116. $api['data']['url'] = $this->config['octoprintURL'];
  117. foreach ($endpoints as $endpoint) {
  118. $dataUrl = $url . '/api/' . $endpoint;
  119. try {
  120. $headers = array('X-API-KEY' => $this->config['octoprintToken']);
  121. $response = Requests::get($dataUrl, $headers);
  122. if ($response->success) {
  123. $json = json_decode($response->body, true);
  124. $api['data'][$endpoint] = $json;
  125. $api['options'] = [
  126. 'title' => $this->config['octoprintHeader'],
  127. 'titleToggle' => $this->config['octoprintHeaderToggle'],
  128. ];
  129. } else {
  130. $this->setAPIResponse('error', 'OctoPrint connection error', 409);
  131. return false;
  132. }
  133. } catch (Requests_Exception $e) {
  134. $this->writeLog('error', 'Octoprint Function - Error: ' . $e->getMessage(), 'SYSTEM');
  135. $this->setAPIResponse('error', $e->getMessage(), 500);
  136. return false;
  137. };
  138. }
  139. $api = isset($api) ? $api : false;
  140. $this->setAPIResponse('success', null, 200, $api);
  141. return $api;
  142. }
  143. }