octoprint.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. if (array_key_exists($key, $permissions)) {
  54. return $permissions[$key];
  55. } elseif ($key == 'all') {
  56. return $permissions;
  57. } else {
  58. return [];
  59. }
  60. }
  61. public function homepageOrderOctoprint()
  62. {
  63. if ($this->homepageItemPermissions($this->octoprintHomepagePermissions('main'))) {
  64. return '
  65. <div id="' . __FUNCTION__ . '">
  66. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading OctoPrint...</h2></div>
  67. <script>
  68. // Octoprint
  69. homepageOctoprint("' . $this->config['homepageOctoprintRefresh'] . '");
  70. // End Octoprint
  71. </script>
  72. </div>
  73. ';
  74. }
  75. }
  76. public function getOctoprintHomepageData()
  77. {
  78. if (!$this->homepageItemPermissions($this->octoprintHomepagePermissions('main'), true)) {
  79. return false;
  80. }
  81. $api = [];
  82. $url = $this->qualifyURL($this->config['octoprintURL']);
  83. $endpoints = ['job', 'settings'];
  84. $api['data']['url'] = $this->config['octoprintURL'];
  85. foreach ($endpoints as $endpoint) {
  86. $dataUrl = $url . '/api/' . $endpoint;
  87. try {
  88. $headers = array('X-API-KEY' => $this->config['octoprintToken']);
  89. $options = $this->requestOptions($url, $this->config['homepageOctoprintRefresh'], $this->config['octoprintDisableCertCheck'], $this->config['octoprintUseCustomCertificate']);
  90. $response = Requests::get($dataUrl, $headers, $options);
  91. if ($response->success) {
  92. $json = json_decode($response->body, true);
  93. $api['data'][$endpoint] = $json;
  94. $api['options'] = [
  95. 'title' => $this->config['octoprintHeader'],
  96. 'titleToggle' => $this->config['octoprintHeaderToggle'],
  97. ];
  98. } else {
  99. $this->setAPIResponse('error', 'OctoPrint connection error', 409);
  100. return false;
  101. }
  102. } catch (Requests_Exception $e) {
  103. $this->writeLog('error', 'Octoprint Function - Error: ' . $e->getMessage(), 'SYSTEM');
  104. $this->setAPIResponse('error', $e->getMessage(), 500);
  105. return false;
  106. };
  107. }
  108. $api = isset($api) ? $api : false;
  109. $this->setAPIResponse('success', null, 200, $api);
  110. return $api;
  111. }
  112. }