octoprint.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 getOctoprintHomepageData()
  63. {
  64. if (!$this->config['homepageOctoprintEnabled']) {
  65. $this->setAPIResponse('error', 'OctoPrint homepage item is not enabled', 409);
  66. return false;
  67. }
  68. if (!$this->qualifyRequest($this->config['homepageOctoprintAuth'])) {
  69. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  70. return false;
  71. }
  72. if (empty($this->config['octoprintURL'])) {
  73. $this->setAPIResponse('error', 'OctoPrint URL is not defined', 422);
  74. return false;
  75. }
  76. if (empty($this->config['octoprintToken'])) {
  77. $this->setAPIResponse('error', 'OctoPrint Token is not defined', 422);
  78. return false;
  79. }
  80. $api = [];
  81. $url = $this->qualifyURL($this->config['octoprintURL']);
  82. $endpoints = ['job', 'settings'];
  83. $api['data']['url'] = $this->config['octoprintURL'];
  84. foreach ($endpoints as $endpoint) {
  85. $dataUrl = $url . '/api/' . $endpoint;
  86. try {
  87. $headers = array('X-API-KEY' => $this->config['octoprintToken']);
  88. $response = Requests::get($dataUrl, $headers);
  89. if ($response->success) {
  90. $json = json_decode($response->body, true);
  91. $api['data'][$endpoint] = $json;
  92. $api['options'] = [
  93. 'title' => $this->config['octoprintHeader'],
  94. 'titleToggle' => $this->config['octoprintHeaderToggle'],
  95. ];
  96. } else {
  97. $this->setAPIResponse('error', 'OctoPrint connection error', 409);
  98. return false;
  99. }
  100. } catch (Requests_Exception $e) {
  101. $this->writeLog('error', 'Octoprint Function - Error: ' . $e->getMessage(), 'SYSTEM');
  102. $this->setAPIResponse('error', $e->getMessage(), 500);
  103. return false;
  104. };
  105. }
  106. $api = isset($api) ? $api : false;
  107. $this->setAPIResponse('success', null, 200, $api);
  108. return $api;
  109. }
  110. }