| | | 1 | | #include "alert_service.h" |
| | | 2 | | #include "db_connection.h" |
| | | 3 | | #include "logger.h" |
| | | 4 | | #include <stdio.h> |
| | | 5 | | #include <string.h> |
| | | 6 | | #include <stdlib.h> |
| | | 7 | | |
| | | 8 | | /* ------------------------------------------------------------------ |
| | | 9 | | * PRIVATE HELPERS |
| | | 10 | | * ------------------------------------------------------------------ */ |
| | | 11 | | |
| | 11 | 12 | | const char *severity_to_str(AlertSeverity severity) { |
| | 11 | 13 | | switch (severity) { |
| | 0 | 14 | | case SEVERITY_INFO: |
| | 0 | 15 | | return "INFO"; |
| | | 16 | | |
| | 7 | 17 | | case SEVERITY_WARNING: |
| | 7 | 18 | | return "WARNING"; |
| | | 19 | | |
| | 4 | 20 | | case SEVERITY_CRITICAL: |
| | 4 | 21 | | return "CRITICAL"; |
| | | 22 | | |
| | 0 | 23 | | default: |
| | 0 | 24 | | return "UNKNOWN"; |
| | | 25 | | } |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /* ------------------------------------------------------------------ |
| | | 29 | | * PUBLIC API |
| | | 30 | | * ------------------------------------------------------------------ */ |
| | | 31 | | |
| | 7 | 32 | | bool create_alert(int sensor_id, AlertSeverity severity, const char *message) { |
| | 7 | 33 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 34 | | |
| | 7 | 35 | | if (!conn_wrapper) { |
| | 1 | 36 | | LOG_ERROR("Could not acquire connection to create alert."); |
| | 1 | 37 | | return false; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | char query[1024]; |
| | 6 | 41 | | snprintf(query, sizeof(query), |
| | | 42 | | "INSERT INTO alerts (sensor_id, severity, message) VALUES (%d, '%s', '%s');", |
| | | 43 | | sensor_id, severity_to_str(severity), message); |
| | 6 | 44 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 45 | | |
| | 6 | 46 | | if (PQresultStatus(res) != PGRES_COMMAND_OK) { |
| | 1 | 47 | | LOG_ERROR("Failed to insert alert: %s", PQerrorMessage(conn_wrapper->pg_conn)); |
| | 1 | 48 | | PQclear(res); |
| | 1 | 49 | | db_pool_release(conn_wrapper); |
| | 1 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | |
| | 5 | 53 | | PQclear(res); |
| | 5 | 54 | | db_pool_release(conn_wrapper); |
| | 5 | 55 | | LOG_INFO("[ALERT CREATED] Sensor %d: %s (%s)", sensor_id, message, severity_to_str(severity)); |
| | 5 | 56 | | return true; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /* ------------------------------------------------------------------ */ |
| | | 60 | | |
| | 8 | 61 | | void check_and_trigger_alerts(int sensor_id, const char *sensor_type, double value) { |
| | 8 | 62 | | if (strcmp(sensor_type, "Temperature") == 0) { |
| | 3 | 63 | | if (value > 90.0) { |
| | | 64 | | char msg[128]; |
| | 1 | 65 | | snprintf(msg, sizeof(msg), "Critical Temperature detected: %.2f C", value); |
| | 1 | 66 | | create_alert(sensor_id, SEVERITY_CRITICAL, msg); |
| | 2 | 67 | | } else if (value > 75.0) { |
| | | 68 | | char msg[128]; |
| | 1 | 69 | | snprintf(msg, sizeof(msg), "High Temperature warning: %.2f C", value); |
| | 1 | 70 | | create_alert(sensor_id, SEVERITY_WARNING, msg); |
| | | 71 | | } |
| | 5 | 72 | | } else if (strcmp(sensor_type, "Vibration") == 0) { |
| | 2 | 73 | | if (value > 5.0) { |
| | | 74 | | char msg[128]; |
| | 1 | 75 | | snprintf(msg, sizeof(msg), "Excessive Vibration detected: %.2f Hz", value); |
| | 1 | 76 | | create_alert(sensor_id, SEVERITY_CRITICAL, msg); |
| | | 77 | | } |
| | 3 | 78 | | } else if (strcmp(sensor_type, "Pressure") == 0) { |
| | 2 | 79 | | if (value > 12.0) { |
| | | 80 | | char msg[128]; |
| | 1 | 81 | | snprintf(msg, sizeof(msg), "High Pressure detected: %.2f Bar", value); |
| | 1 | 82 | | create_alert(sensor_id, SEVERITY_WARNING, msg); |
| | | 83 | | } |
| | | 84 | | } |
| | 8 | 85 | | } |
| | | 86 | | |
| | | 87 | | /* ------------------------------------------------------------------ */ |
| | | 88 | | |
| | 3 | 89 | | int get_recent_alerts(AlertInfo *out_alerts, int max_alerts) { |
| | 3 | 90 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 91 | | |
| | 3 | 92 | | if (!conn_wrapper) return 0; |
| | | 93 | | |
| | | 94 | | char query[256]; |
| | 2 | 95 | | snprintf(query, sizeof(query), |
| | | 96 | | "SELECT id, sensor_id, severity, message, to_char(alert_time, 'YYYY-MM-DD HH24:MI:SS') " |
| | | 97 | | "FROM alerts ORDER BY alert_time DESC LIMIT %d;", |
| | | 98 | | max_alerts); |
| | 2 | 99 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 100 | | |
| | 2 | 101 | | if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| | 0 | 102 | | LOG_ERROR("Failed to fetch alerts: %s", PQerrorMessage(conn_wrapper->pg_conn)); |
| | 0 | 103 | | PQclear(res); |
| | 0 | 104 | | db_pool_release(conn_wrapper); |
| | 0 | 105 | | return 0; |
| | | 106 | | } |
| | | 107 | | |
| | 2 | 108 | | int rows = PQntuples(res); |
| | 2 | 109 | | int count = (rows < max_alerts) ? rows : max_alerts; |
| | | 110 | | |
| | 5 | 111 | | for (int i = 0; i < count; i++) { |
| | 3 | 112 | | out_alerts[i].id = atoi(PQgetvalue(res, i, 0)); |
| | 3 | 113 | | out_alerts[i].sensor_id = atoi(PQgetvalue(res, i, 1)); |
| | 3 | 114 | | strncpy(out_alerts[i].severity, PQgetvalue(res, i, 2), 15); |
| | 3 | 115 | | out_alerts[i].severity[15] = '\0'; |
| | 3 | 116 | | strncpy(out_alerts[i].message, PQgetvalue(res, i, 3), 255); |
| | 3 | 117 | | out_alerts[i].message[255] = '\0'; |
| | 3 | 118 | | strncpy(out_alerts[i].created_at, PQgetvalue(res, i, 4), 31); |
| | 3 | 119 | | out_alerts[i].created_at[31] = '\0'; |
| | | 120 | | } |
| | | 121 | | |
| | 2 | 122 | | PQclear(res); |
| | 2 | 123 | | db_pool_release(conn_wrapper); |
| | 2 | 124 | | return count; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /* ------------------------------------------------------------------ |
| | | 128 | | * JSON SERIALIZATION (API ENDPOINTS) |
| | | 129 | | * ------------------------------------------------------------------ */ |
| | | 130 | | |
| | | 131 | | #ifndef TEST_MODE |
| | | 132 | | |
| | 1 | 133 | | char *alert_service_serialize_alerts(void) { |
| | | 134 | | AlertInfo alerts[50]; |
| | 1 | 135 | | int count = get_recent_alerts(alerts, 50); |
| | 1 | 136 | | char *json = (char *)malloc(4096); |
| | | 137 | | |
| | 1 | 138 | | if (!json) return NULL; |
| | | 139 | | |
| | 1 | 140 | | char *ptr = json; |
| | 1 | 141 | | ptr += sprintf(ptr, "{\"alerts\":["); |
| | | 142 | | |
| | 2 | 143 | | for (int i = 0; i < count; i++) { |
| | 1 | 144 | | if (i > 0) { |
| | 0 | 145 | | ptr += sprintf(ptr, ","); |
| | | 146 | | } |
| | | 147 | | |
| | 1 | 148 | | ptr += sprintf(ptr, |
| | | 149 | | "{" |
| | | 150 | | "\"id\":%d," |
| | | 151 | | "\"sensor_id\":%d," |
| | | 152 | | "\"severity\":\"%s\"," |
| | | 153 | | "\"message\":\"%s\"," |
| | | 154 | | "\"created_at\":\"%s\"" |
| | | 155 | | "}", |
| | | 156 | | alerts[i].id, |
| | | 157 | | alerts[i].sensor_id, |
| | 1 | 158 | | alerts[i].severity, |
| | 1 | 159 | | alerts[i].message, |
| | 1 | 160 | | alerts[i].created_at |
| | | 161 | | ); |
| | | 162 | | } |
| | | 163 | | |
| | 1 | 164 | | ptr += sprintf(ptr, "]}"); |
| | 1 | 165 | | return json; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | #endif |
| | | 169 | | |
| | | 170 | | |
| | | 171 | | |
| | | 172 | | /* ------------------------------------------------------------------ |
| | | 173 | | * TEST/MOCK VERSION (compile with -DTEST_MODE) |
| | | 174 | | * ------------------------------------------------------------------ */ |
| | | 175 | | |
| | | 176 | | #ifdef TEST_MODE |
| | | 177 | | |
| | | 178 | | char *alert_service_serialize_alerts(void) { |
| | | 179 | | char *json = (char *)malloc(256); |
| | | 180 | | |
| | | 181 | | if (json) { |
| | | 182 | | strcpy(json, "{\"alerts\":[]}"); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | return json; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | #endif |