octoprint.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageOctoprintEnabled'),
  21. $this->settingsOption('auth', 'homepageOctoprintAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'octoprintURL'),
  25. $this->settingsOption('token', 'octoprintToken'),
  26. $this->settingsOption('disable-cert-check', 'octoprintDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'octoprintUseCustomCertificate'),
  28. ],
  29. 'Options' => [
  30. $this->settingsOption('title', 'octoprintHeader'),
  31. $this->settingsOption('toggle-title', 'octoprintHeaderToggle'),
  32. ],
  33. ]
  34. ];
  35. return array_merge($homepageInformation, $homepageSettings);
  36. }
  37. public function octoprintHomepagePermissions($key = null)
  38. {
  39. $permissions = [
  40. 'main' => [
  41. 'enabled' => [
  42. 'homepageOctoprintEnabled'
  43. ],
  44. 'auth' => [
  45. 'homepageOctoprintAuth'
  46. ],
  47. 'not_empty' => [
  48. 'octoprintURL',
  49. 'octoprintToken'
  50. ]
  51. ]
  52. ];
  53. return $this->homepageCheckKeyPermissions($key, $permissions);
  54. }
  55. public function homepageOrderOctoprint()
  56. {
  57. if ($this->homepageItemPermissions($this->octoprintHomepagePermissions('main'))) {
  58. return '
  59. <div id="' . __FUNCTION__ . '">
  60. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading OctoPrint...</h2></div>
  61. <script>
  62. // Octoprint
  63. homepageOctoprint("' . $this->config['homepageOctoprintRefresh'] . '");
  64. // End Octoprint
  65. </script>
  66. </div>
  67. ';
  68. }
  69. }
  70. public function getOctoprintHomepageData()
  71. {
  72. if (!$this->homepageItemPermissions($this->octoprintHomepagePermissions('main'), true)) {
  73. return false;
  74. }
  75. $api = [];
  76. $url = $this->qualifyURL($this->config['octoprintURL']);
  77. $endpoints = ['job', 'settings'];
  78. $api['data']['url'] = $this->config['octoprintURL'];
  79. foreach ($endpoints as $endpoint) {
  80. $dataUrl = $url . '/api/' . $endpoint;
  81. try {
  82. $headers = array('X-API-KEY' => $this->config['octoprintToken']);
  83. $options = $this->requestOptions($url, $this->config['homepageOctoprintRefresh'], $this->config['octoprintDisableCertCheck'], $this->config['octoprintUseCustomCertificate']);
  84. $response = Requests::get($dataUrl, $headers, $options);
  85. if ($response->success) {
  86. $json = json_decode($response->body, true);
  87. $api['data'][$endpoint] = $json;
  88. $api['options'] = [
  89. 'title' => $this->config['octoprintHeader'],
  90. 'titleToggle' => $this->config['octoprintHeaderToggle'],
  91. ];
  92. } else {
  93. $this->setAPIResponse('error', 'OctoPrint connection error', 409);
  94. return false;
  95. }
  96. } catch (Requests_Exception $e) {
  97. $this->setLoggerChannel('Octoprint')->error($e);
  98. $this->setResponse(500, $e->getMessage());
  99. return false;
  100. };
  101. }
  102. $api = isset($api) ? $api : false;
  103. $this->setAPIResponse('success', null, 200, $api);
  104. return $api;
  105. }
  106. }