octoprint.php 3.7 KB

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