Advertisement
tmax

Untitled

Sep 28th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import requests
  2.  
  3. __author__ = 'Andres Torti'
  4.  
  5.  
  6. class OctoprintAPI:
  7. def __init__(self, host):
  8. self.host = host
  9. self.s = requests.Session()
  10. self.s.headers.update({'X-Api-Key': 'B0BE78429FCA477DA8399CE75928E093',
  11. 'Content-Type': 'application/json'})
  12.  
  13. def is_printer_connected(self):
  14. if self.s.get('http://' + self.host + ':5000/api/printer').status_code != 200:
  15. return False
  16. else:
  17. return True
  18.  
  19. def set_bed_temp(self, temp):
  20. r = self.s.post('http://' + self.host + ':5000/api/printer/bed', json={'command': 'target', 'target': temp})
  21. print(r)
  22. print(r.content)
  23. print()
  24.  
  25. def pause_job(self):
  26. r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'pause'})
  27.  
  28. def resume_job(self):
  29. r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'pause'})
  30.  
  31. def start_job(self):
  32. r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'start'})
  33.  
  34. def cancel_job(self):
  35. r = self.s.post('http://' + self.host + ':5000/api/job', json={'command': 'cancel'})
  36.  
  37. def get_print_progress(self):
  38. data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
  39. for line in data:
  40. if 'completion' in line:
  41. # check if null
  42. if 'null' in line:
  43. return 0
  44. else:
  45. return int(float(line[line.find(':')+1:line.find(',')]))
  46. return 0
  47.  
  48. def get_total_print_time(self):
  49. data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
  50. for line in data:
  51. if 'estimatedPrintTime' in line:
  52. # check if null
  53. if 'null' in line:
  54. return 0
  55. else:
  56. return int(float(line[line.find(':')+1:line.find(',')]))
  57. return 0
  58.  
  59. def get_elapsed_print_time(self):
  60. data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
  61. for line in data:
  62. if 'printTime' in line:
  63. # check if null
  64. if 'null' in line:
  65. return 0
  66. else:
  67. return int(float(line[line.find(':')+1:line.find(',')]))
  68. return 0
  69.  
  70. def get_file_printing(self):
  71. data = self.s.get('http://' + self.host + ':5000/api/job').content.decode('utf-8').split('\n')
  72. for line in data:
  73. if 'name' in line:
  74. # check if null
  75. if 'null' in line:
  76. return "Detenido..."
  77. else:
  78. return line[line.find(':')+1:line.find(',')].replace('"', '').strip()
  79. return "Detenido..."
  80.  
  81. def get_extruder_target_temp(self):
  82. data = self.s.get('http://' + self.host + ':5000/api/printer/tool').content.decode('utf-8').split('\n')
  83. for line in data:
  84. if 'target' in line:
  85. if 'null' in line:
  86. return 0
  87. else:
  88. return int(float(line[line.find(':')+1:line.find(',')]))
  89. return 0
  90.  
  91. def get_extruder_current_temp(self):
  92. data = self.s.get('http://' + self.host + ':5000/api/printer/tool').content.decode('utf-8').split('\n')
  93. for line in data:
  94. if 'actual' in line:
  95. return int(float(line[line.find(':')+1:line.find(',')]))
  96. return 0
  97.  
  98. def get_bed_temp(self):
  99. data = self.s.get('http://' + self.host + ':5000/api/printer/bed').content.decode('utf-8').split('\n')
  100. for line in data:
  101. if 'target' in line:
  102. return int(float(line[line.find(':')+1:line.find('\'')]))
  103. return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement