174 lines
6.6 KiB
C
174 lines
6.6 KiB
C
#include "rest_server_internal.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "domain.h"
|
|
esp_err_t rest_server_job_get(httpd_req_t *req) {
|
|
if (!rest_server_auth_ok(req)) {
|
|
return rest_server_send_error(req, "401 Unauthorized", "unauthorized");
|
|
}
|
|
|
|
uint32_t job_id = 0;
|
|
if (!rest_server_parse_uri_u32_tail(req->uri, &job_id)) {
|
|
return rest_server_send_error(req, "400 Bad Request", "invalid job id");
|
|
}
|
|
|
|
print_job_info_t info;
|
|
if (!printer_protocol_get_job(job_id, &info)) {
|
|
return rest_server_send_error(req, "404 Not Found", "job not found");
|
|
}
|
|
|
|
cJSON *root = cJSON_CreateObject();
|
|
cJSON_AddBoolToObject(root, "ok", true);
|
|
cJSON_AddNumberToObject(root, "job_id", info.id);
|
|
cJSON_AddStringToObject(root, "state", printer_protocol_job_state_str(info.state));
|
|
cJSON_AddNumberToObject(root, "progress", info.progress);
|
|
cJSON_AddStringToObject(root, "density", info.density);
|
|
cJSON_AddNumberToObject(root, "width", info.width);
|
|
cJSON_AddNumberToObject(root, "height", info.height);
|
|
cJSON_AddNumberToObject(root, "data_len", (double)info.data_len);
|
|
cJSON_AddStringToObject(root, "error", info.error);
|
|
cJSON_AddNumberToObject(root, "created_ms", (double)info.created_ms);
|
|
cJSON_AddNumberToObject(root, "started_ms", (double)info.started_ms);
|
|
cJSON_AddNumberToObject(root, "finished_ms", (double)info.finished_ms);
|
|
|
|
esp_err_t err = rest_server_send_json(req, "200 OK", root);
|
|
cJSON_Delete(root);
|
|
return err;
|
|
}
|
|
|
|
esp_err_t rest_server_jobs_get(httpd_req_t *req) {
|
|
if (!rest_server_auth_ok(req)) {
|
|
return rest_server_send_error(req, "401 Unauthorized", "unauthorized");
|
|
}
|
|
|
|
print_job_info_t jobs[16];
|
|
size_t total_count = 0;
|
|
esp_err_t list_rc = printer_protocol_list_jobs(jobs, 16, &total_count);
|
|
if (list_rc != ESP_OK) {
|
|
return rest_server_send_error(req, "500 Internal Server Error", "list jobs failed");
|
|
}
|
|
|
|
size_t emit_count = total_count;
|
|
if (emit_count > 16) {
|
|
emit_count = 16;
|
|
}
|
|
|
|
cJSON *root = cJSON_CreateObject();
|
|
cJSON_AddBoolToObject(root, "ok", true);
|
|
cJSON_AddNumberToObject(root, "total", (double)total_count);
|
|
cJSON_AddNumberToObject(root, "returned", (double)emit_count);
|
|
|
|
cJSON *arr = cJSON_AddArrayToObject(root, "jobs");
|
|
for (size_t i = 0; i < emit_count; ++i) {
|
|
cJSON *item = cJSON_CreateObject();
|
|
cJSON_AddNumberToObject(item, "job_id", jobs[i].id);
|
|
cJSON_AddStringToObject(item, "state", printer_protocol_job_state_str(jobs[i].state));
|
|
cJSON_AddNumberToObject(item, "progress", jobs[i].progress);
|
|
cJSON_AddStringToObject(item, "density", jobs[i].density);
|
|
cJSON_AddNumberToObject(item, "width", jobs[i].width);
|
|
cJSON_AddNumberToObject(item, "height", jobs[i].height);
|
|
cJSON_AddNumberToObject(item, "data_len", (double)jobs[i].data_len);
|
|
cJSON_AddStringToObject(item, "error", jobs[i].error);
|
|
cJSON_AddNumberToObject(item, "created_ms", (double)jobs[i].created_ms);
|
|
cJSON_AddNumberToObject(item, "started_ms", (double)jobs[i].started_ms);
|
|
cJSON_AddNumberToObject(item, "finished_ms", (double)jobs[i].finished_ms);
|
|
cJSON_AddItemToArray(arr, item);
|
|
}
|
|
|
|
if (total_count > emit_count) {
|
|
cJSON_AddStringToObject(root, "warning", "job list truncated");
|
|
}
|
|
|
|
esp_err_t err = rest_server_send_json(req, "200 OK", root);
|
|
cJSON_Delete(root);
|
|
return err;
|
|
}
|
|
|
|
esp_err_t rest_server_job_delete(httpd_req_t *req) {
|
|
if (!rest_server_auth_ok(req)) {
|
|
return rest_server_send_error(req, "401 Unauthorized", "unauthorized");
|
|
}
|
|
|
|
uint32_t job_id = 0;
|
|
if (!rest_server_parse_uri_u32_tail(req->uri, &job_id)) {
|
|
return rest_server_send_error(req, "400 Bad Request", "invalid job id");
|
|
}
|
|
|
|
char cancel_err[128] = {0};
|
|
esp_err_t cancel_rc = printer_protocol_cancel_job(job_id, cancel_err, sizeof(cancel_err));
|
|
if (cancel_rc == ESP_ERR_NOT_FOUND) {
|
|
return rest_server_send_error(req, "404 Not Found", "job not found");
|
|
}
|
|
if (cancel_rc == ESP_ERR_INVALID_STATE) {
|
|
return rest_server_send_error(req,
|
|
"409 Conflict",
|
|
cancel_err[0] != '\0' ? cancel_err : "job already finished");
|
|
}
|
|
if (cancel_rc != ESP_OK) {
|
|
return rest_server_send_error(req,
|
|
"409 Conflict",
|
|
cancel_err[0] != '\0' ? cancel_err : "cancel failed");
|
|
}
|
|
|
|
print_job_info_t info;
|
|
bool ok = printer_protocol_get_job(job_id, &info);
|
|
|
|
cJSON *root = cJSON_CreateObject();
|
|
cJSON_AddBoolToObject(root, "ok", true);
|
|
cJSON_AddNumberToObject(root, "job_id", job_id);
|
|
if (ok) {
|
|
cJSON_AddStringToObject(root, "state", printer_protocol_job_state_str(info.state));
|
|
cJSON_AddStringToObject(root, "error", info.error);
|
|
} else {
|
|
cJSON_AddStringToObject(root, "state", "canceled");
|
|
}
|
|
|
|
esp_err_t err = rest_server_send_json(req, "200 OK", root);
|
|
cJSON_Delete(root);
|
|
return err;
|
|
}
|
|
|
|
esp_err_t rest_server_jobs_delete(httpd_req_t *req) {
|
|
if (!rest_server_auth_ok(req)) {
|
|
return rest_server_send_error(req, "401 Unauthorized", "unauthorized");
|
|
}
|
|
|
|
bool include_success = true;
|
|
bool include_failed = true;
|
|
bool include_canceled = true;
|
|
|
|
if (req->content_len > 0) {
|
|
char *body = NULL;
|
|
esp_err_t body_err = rest_server_read_body(req, &body);
|
|
if (body_err != ESP_OK) {
|
|
return rest_server_send_error(req, "400 Bad Request", "invalid request body");
|
|
}
|
|
|
|
cJSON *json = cJSON_Parse(body);
|
|
free(body);
|
|
if (json == NULL) {
|
|
return rest_server_send_error(req, "400 Bad Request", "invalid json");
|
|
}
|
|
|
|
include_success = rest_server_json_bool_with_default(cJSON_GetObjectItemCaseSensitive(json, "include_success"), true);
|
|
include_failed = rest_server_json_bool_with_default(cJSON_GetObjectItemCaseSensitive(json, "include_failed"), true);
|
|
include_canceled = rest_server_json_bool_with_default(cJSON_GetObjectItemCaseSensitive(json, "include_canceled"), true);
|
|
cJSON_Delete(json);
|
|
}
|
|
|
|
size_t removed = printer_protocol_cleanup_jobs(include_success, include_failed, include_canceled);
|
|
|
|
cJSON *root = cJSON_CreateObject();
|
|
cJSON_AddBoolToObject(root, "ok", true);
|
|
cJSON_AddNumberToObject(root, "removed", (double)removed);
|
|
cJSON_AddBoolToObject(root, "include_success", include_success);
|
|
cJSON_AddBoolToObject(root, "include_failed", include_failed);
|
|
cJSON_AddBoolToObject(root, "include_canceled", include_canceled);
|
|
|
|
esp_err_t err = rest_server_send_json(req, "200 OK", root);
|
|
cJSON_Delete(root);
|
|
return err;
|
|
}
|
|
|