| | | 1 | | /* This file is a template for fault_handler.c. Content will be filled by yagiz on 2025-12-29. */ |
| | | 2 | | #include "fault_handler.h" |
| | | 3 | | #include "../../database/cJSON.h" |
| | | 4 | | #include <string.h> |
| | | 5 | | #include <stdlib.h> |
| | | 6 | | #include <stdio.h> |
| | | 7 | | |
| | | 8 | | // Fault/Arıza verileri için JSON serialize fonksiyonu |
| | 1 | 9 | | char *serialize_faults_to_json(void) { |
| | 1 | 10 | | cJSON *root = cJSON_CreateObject(); |
| | 1 | 11 | | cJSON *faults_array = cJSON_CreateArray(); |
| | | 12 | | // Mock fault/arıza verileri |
| | 1 | 13 | | cJSON *fault1 = cJSON_CreateObject(); |
| | 1 | 14 | | cJSON_AddNumberToObject(fault1, "id", 1); |
| | 1 | 15 | | cJSON_AddNumberToObject(fault1, "machine_id", 1); |
| | 1 | 16 | | cJSON_AddStringToObject(fault1, "machine_name", "CNC Machine"); |
| | 1 | 17 | | cJSON_AddStringToObject(fault1, "description", "Overheating detected"); |
| | 1 | 18 | | cJSON_AddStringToObject(fault1, "severity", "CRITICAL"); |
| | 1 | 19 | | cJSON_AddStringToObject(fault1, "status", "active"); |
| | 1 | 20 | | cJSON_AddStringToObject(fault1, "detected_at", "2026-02-11 10:30:00"); |
| | 1 | 21 | | cJSON_AddItemToArray(faults_array, fault1); |
| | 1 | 22 | | cJSON *fault2 = cJSON_CreateObject(); |
| | 1 | 23 | | cJSON_AddNumberToObject(fault2, "id", 2); |
| | 1 | 24 | | cJSON_AddNumberToObject(fault2, "machine_id", 2); |
| | 1 | 25 | | cJSON_AddStringToObject(fault2, "machine_name", "Conveyor Belt"); |
| | 1 | 26 | | cJSON_AddStringToObject(fault2, "description", "Belt misalignment"); |
| | 1 | 27 | | cJSON_AddStringToObject(fault2, "severity", "WARNING"); |
| | 1 | 28 | | cJSON_AddStringToObject(fault2, "status", "acknowledged"); |
| | 1 | 29 | | cJSON_AddStringToObject(fault2, "detected_at", "2026-02-11 09:15:00"); |
| | 1 | 30 | | cJSON_AddItemToArray(faults_array, fault2); |
| | 1 | 31 | | cJSON_AddItemToObject(root, "faults", faults_array); |
| | 1 | 32 | | char *json = cJSON_Print(root); |
| | 1 | 33 | | cJSON_Delete(root); |
| | 1 | 34 | | return json; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | // Tek bir fault detayı için JSON serialize |
| | 1 | 38 | | char *serialize_fault_detail_to_json(int fault_id) { |
| | 1 | 39 | | cJSON *root = cJSON_CreateObject(); |
| | 1 | 40 | | cJSON_AddNumberToObject(root, "id", fault_id); |
| | 1 | 41 | | cJSON_AddNumberToObject(root, "machine_id", 1); |
| | 1 | 42 | | cJSON_AddStringToObject(root, "machine_name", "CNC Machine"); |
| | 1 | 43 | | cJSON_AddStringToObject(root, "description", "Overheating detected - Temperature exceeded 90°C"); |
| | 1 | 44 | | cJSON_AddStringToObject(root, "severity", "CRITICAL"); |
| | 1 | 45 | | cJSON_AddStringToObject(root, "status", "active"); |
| | 1 | 46 | | cJSON_AddStringToObject(root, "detected_at", "2026-02-11 10:30:00"); |
| | 1 | 47 | | cJSON_AddStringToObject(root, "resolved_at", ""); |
| | 1 | 48 | | cJSON_AddStringToObject(root, "root_cause", "Coolant pump failure"); |
| | 1 | 49 | | cJSON_AddStringToObject(root, "recommended_action", "Check and replace coolant pump"); |
| | 1 | 50 | | char *json = cJSON_Print(root); |
| | 1 | 51 | | cJSON_Delete(root); |
| | 1 | 52 | | return json; |
| | | 53 | | } |
| | | 54 | | |
| | 6 | 55 | | void handle_fault_request(HttpRequest *req, HttpResponse *res) { |
| | | 56 | | // GET /api/faults - Tüm arızaları listele |
| | | 57 | | // GET /api/faults/1 - Tek bir arıza detayı |
| | | 58 | | // POST /api/faults - Yeni arıza bildir |
| | | 59 | | // PUT /api/faults/1/acknowledge - Arızayı onayla |
| | | 60 | | // PUT /api/faults/1/resolve - Arızayı çöz |
| | 6 | 61 | | if (strcmp(req->method, "GET") == 0) { |
| | | 62 | | // Check if it's a single fault request |
| | 2 | 63 | | char *fault_id_str = strstr(req->path, "/api/faults/"); |
| | | 64 | | |
| | 2 | 65 | | if (fault_id_str && strlen(fault_id_str) > 12) { |
| | | 66 | | // GET /api/faults/{id} |
| | 1 | 67 | | int fault_id = atoi(fault_id_str + 12); |
| | 1 | 68 | | char *json = serialize_fault_detail_to_json(fault_id); |
| | 1 | 69 | | res->status_code = 200; |
| | 1 | 70 | | strcpy(res->content_type, "application/json"); |
| | 1 | 71 | | strncpy(res->body, json, 8191); |
| | 1 | 72 | | res->body[8191] = '\0'; |
| | 1 | 73 | | free(json); |
| | | 74 | | } else { |
| | | 75 | | // GET /api/faults |
| | 1 | 76 | | char *json = serialize_faults_to_json(); |
| | 1 | 77 | | res->status_code = 200; |
| | 1 | 78 | | strcpy(res->content_type, "application/json"); |
| | 1 | 79 | | strncpy(res->body, json, 8191); |
| | 1 | 80 | | res->body[8191] = '\0'; |
| | 1 | 81 | | free(json); |
| | | 82 | | } |
| | 4 | 83 | | } else if (strcmp(req->method, "POST") == 0) { |
| | | 84 | | // POST /api/faults - Yeni arıza bildir |
| | 1 | 85 | | res->status_code = 201; |
| | 1 | 86 | | strcpy(res->content_type, "application/json"); |
| | 1 | 87 | | strcpy(res->body, "{\"success\": true, \"message\": \"Fault reported successfully\", \"fault_id\": 3}"); |
| | 3 | 88 | | } else if (strcmp(req->method, "PUT") == 0) { |
| | | 89 | | // PUT /api/faults/1/acknowledge veya /api/faults/1/resolve |
| | 2 | 90 | | if (strstr(req->path, "/acknowledge")) { |
| | 1 | 91 | | res->status_code = 200; |
| | 1 | 92 | | strcpy(res->content_type, "application/json"); |
| | 1 | 93 | | strcpy(res->body, "{\"success\": true, \"message\": \"Fault acknowledged\"}"); |
| | 1 | 94 | | } else if (strstr(req->path, "/resolve")) { |
| | 1 | 95 | | res->status_code = 200; |
| | 1 | 96 | | strcpy(res->content_type, "application/json"); |
| | 1 | 97 | | strcpy(res->body, "{\"success\": true, \"message\": \"Fault resolved\"}"); |
| | | 98 | | } else { |
| | 0 | 99 | | res->status_code = 400; |
| | 0 | 100 | | strcpy(res->content_type, "application/json"); |
| | 0 | 101 | | strcpy(res->body, "{\"error\": \"Invalid fault operation\"}"); |
| | | 102 | | } |
| | | 103 | | } else { |
| | 1 | 104 | | res->status_code = 405; |
| | 1 | 105 | | strcpy(res->content_type, "application/json"); |
| | 1 | 106 | | strcpy(res->body, "{\"error\": \"Method not supported for faults\"}"); |
| | | 107 | | } |
| | 6 | 108 | | } |