93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
import requests
|
|
import random
|
|
|
|
class AIOPrrintSystemAPI:
|
|
def __init__(self, api_url="http://127.0.0.1:5001/api/v1", api_key=""):
|
|
self.api_url = api_url
|
|
self.api_key = api_key
|
|
self.headers = {"X-Api-Key": self.api_key}
|
|
|
|
def _post_action(self, method, **kwargs):
|
|
url = f"{self.api_url}/octoprint_client"
|
|
payload = {"method": method, "kwargs": kwargs}
|
|
try:
|
|
r = requests.post(url, json=payload, headers=self.headers, timeout=5)
|
|
return r.json()
|
|
except:
|
|
return None
|
|
|
|
def get_status(self):
|
|
|
|
# test_data = {
|
|
# 'job': {
|
|
# 'job': {
|
|
# 'estimatedPrintTime': 1234,
|
|
# 'filament': {'length': 765, 'volume': 24356},
|
|
# 'file': {'display_name': 'Test File','date': None, 'name': '20260508141659_085359c9908947bebcaa0fe7490641e8.gcode', 'origin': 'local', 'path': None, 'size': 20934490},
|
|
# 'lastPrintTime': None,
|
|
# 'user': None
|
|
# },
|
|
# 'progress': {
|
|
# 'completion': random.uniform(0, 100.0),
|
|
# # 'filepos': random.randint(1234,20934490),
|
|
# 'filepos': 20934490,
|
|
# 'printTime': random.randint(1234,54321),
|
|
# 'printTimeLeft': random.randint(1234,54321),
|
|
# 'printTimeLeftOrigin': 5366
|
|
# },
|
|
# 'state': 'Printing'
|
|
# },
|
|
# 'status': {
|
|
# 'sd': {'ready': False},
|
|
# 'state': {
|
|
# 'error': '',
|
|
# 'flags': {
|
|
# 'cancelling': False,
|
|
# 'closedOrError': False,
|
|
# 'error': False,
|
|
# 'finishing': False,
|
|
# 'operational': False,
|
|
# 'paused': False,
|
|
# 'pausing': False,
|
|
# 'printing': True,
|
|
# 'ready': True,
|
|
# 'resuming': False,
|
|
# 'sdReady': False
|
|
# },
|
|
# 'text': 'Printing'
|
|
# },
|
|
# 'temperature': {
|
|
# 'bed': {'actual': random.randint(0,100), 'offset': 0, 'target': random.randint(0,100)},
|
|
# 'tool0': {'actual': random.randint(0,300), 'offset': 0, 'target': random.randint(0,300)}
|
|
# }
|
|
# }
|
|
# }
|
|
# return test_data
|
|
|
|
url = f"{self.api_url}/status"
|
|
try:
|
|
r = requests.get(url, headers=self.headers, timeout=5)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
except:
|
|
return {"status": {}, "job": {}}
|
|
|
|
def pause_print(self):
|
|
return self._post_action("pause_print", action="pause")
|
|
|
|
def stop_print(self):
|
|
return self._post_action("cancel_print")
|
|
|
|
def home_axes(self, axes=["x", "y", "z"]):
|
|
return self._post_action("home_axes", axes=axes)
|
|
|
|
def auto_leveling(self):
|
|
return self._post_action("auto_leveling")
|
|
|
|
def send_gcode(self, gcode):
|
|
return self._post_action("send_gcode", commands=gcode)
|
|
|
|
def off_motors(self):
|
|
return self.send_gcode("M84")
|
|
|
|
|