| | | 1 | | #include "machine_handler.h" |
| | | 2 | | #include "../../database/api_handlers.h" |
| | | 3 | | #include "../../database/machine_service.h" |
| | | 4 | | #include "../../database/cJSON.h" |
| | | 5 | | #include <string.h> |
| | | 6 | | #include <stdlib.h> |
| | | 7 | | #include <stdio.h> |
| | | 8 | | |
| | | 9 | | |
| | | 10 | | |
| | 1 | 11 | | char *serialize_machines_to_json(void) { |
| | 1 | 12 | | cJSON *root = cJSON_CreateObject(); |
| | 1 | 13 | | cJSON *items = cJSON_CreateArray(); |
| | 1 | 14 | | cJSON *item1 = cJSON_CreateObject(); |
| | 1 | 15 | | cJSON_AddNumberToObject(item1, "id", 1); |
| | 1 | 16 | | cJSON_AddStringToObject(item1, "name", "CNC Machine"); |
| | 1 | 17 | | cJSON_AddStringToObject(item1, "status", "running"); |
| | 1 | 18 | | cJSON_AddItemToArray(items, item1); |
| | 1 | 19 | | cJSON_AddItemToObject(root, "machines", items); |
| | 1 | 20 | | char *json = cJSON_Print(root); |
| | 1 | 21 | | cJSON_Delete(root); |
| | 1 | 22 | | return json; |
| | | 23 | | } |
| | | 24 | | |
| | 3 | 25 | | void handle_machine_request(HttpRequest *req, HttpResponse *res) { |
| | 3 | 26 | | if (strcmp(req->method, "GET") == 0) { |
| | 1 | 27 | | char *json = serialize_machines_to_json(); |
| | 1 | 28 | | res->status_code = 200; |
| | 1 | 29 | | strcpy(res->content_type, "application/json"); |
| | 1 | 30 | | strncpy(res->body, json, 8191); |
| | 1 | 31 | | free(json); |
| | 2 | 32 | | } else if (strcmp(req->method, "POST") == 0) { |
| | | 33 | | // Simple mock for onboarding since we need more robust JSON parsing for real POST |
| | | 34 | | // In a real scenario, we'd parse req->body here |
| | 1 | 35 | | res->status_code = 201; |
| | 1 | 36 | | strcpy(res->body, "{\"success\": true, \"message\": \"Machine onboarded\"}"); |
| | | 37 | | } |
| | 3 | 38 | | } |