octoprint.php 4.0 KB

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