Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 44 additions & 18 deletions Mainsail/Mainsail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,64 @@ public function livestats()
{
$status = "standby";
//https://moonraker.readthedocs.io/en/latest/web_api/#query-printer-object-status
$res = parent::execute($this->url('/printer/objects/query?display_status&toolhead&print_stats'));
$res = parent::execute($this->url('/printer/objects/query?display_status&print_stats&virtual_sdcard'));
if (!$res) {
return parent::getLiveStats($status, ["error" => "Connection"]);
return parent::getLiveStats($status, ["error" => "Connection"]);
}
$details = json_decode($res->getBody());
$printer = $details->result->status;
$data = [];

$status = $details->result->status->print_stats->state;
$status = $printer->print_stats->state ?? "standby";

if ($status == 'printing') {
$data["completed_pct"] = round($details->result->status->display_status->progress) . '%';

$total_seconds = $details->result->status->toolhead->estimated_print_time;
$completed_seconds = $details->result->status->print_stats->print_duration;

if ($completed_seconds === null) {
$data["estimated"] = "N/A";
} elseif ($completed_seconds > $total_seconds) {
$data["estimated"] = "Soon!";
} elseif ($completed_seconds > 0) {
$remaining_seconds = $total_seconds - $completed_seconds;
$data["estimated"] = Carbon::now()
->addSeconds($remaining_seconds)
->diffForHumans();
// Moonraker reports progress as a fraction between 0.0 and 1.0.
// display_status is set by M73, virtual_sdcard is the file position fallback.
$progress = $printer->display_status->progress ?? 0;
if (!$progress) {
$progress = $printer->virtual_sdcard->progress ?? 0;
}
$data["completed_pct"] = round($progress * 100) . '%';

// Time actually spent printing, excluding pauses
$elapsed = $printer->print_stats->print_duration ?? 0;
$remaining = null;

// Prefer the slicer estimate from the file metadata
//https://moonraker.readthedocs.io/en/latest/web_api/#get-gcode-metadata
$filename = $printer->print_stats->filename ?? '';
if ($filename !== '') {
$meta = parent::execute(
$this->url('/server/files/metadata?filename=' . rawurlencode($filename))
);
if ($meta) {
$estimated = json_decode($meta->getBody())->result->estimated_time ?? null;
if ($estimated) {
$remaining = $estimated - $elapsed;
}
}
}

// Otherwise extrapolate from progress and elapsed time
if ($remaining === null && $progress > 0 && $elapsed > 0) {
$remaining = ($elapsed / $progress) - $elapsed;
}

if ($remaining === null) {
$data["estimated"] = "N/A";
} elseif ($remaining <= 0) {
$data["estimated"] = "Soon!";
} else {
$data["estimated"] = "N/A";
$data["estimated"] = Carbon::now()
->addSeconds((int) round($remaining))
->diffForHumans();
}
} else {
$data["state"] = ucwords($status);
}
return parent::getLiveStats($status, $data);
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url, false) . ':' . $this->config->moonraker_port . $endpoint;
Expand Down
Loading