Air Quality Monitor
Portable C application for collecting, storing, and analyzing air quality sensor readings
Loading...
Searching...
No Matches
config_persistence.c
1#include "core/aqm_paths.h"
2#include "core/aqm_platform.h"
3#include "core/globals.h"
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8void save_config(void) {
9 char path[AQM_PATH_MAX];
10 if (aqm_get_config_path(path, sizeof(path)) != 0) {
11 fprintf(stderr, "Could not resolve config path.\n");
12 return;
13 }
14
15 FILE *file = fopen(path, "w");
16 if (file == NULL) {
17 fprintf(stderr, "Cannot open config file for writing: %s\n", path);
18 return;
19 }
20
21 fprintf(file, "limit_pm25=%.4f\n", limit_pm25);
22 fprintf(file, "limit_pm10=%.4f\n", limit_pm10);
23 fprintf(file, "limit_co=%.4f\n", limit_co);
24 fprintf(file, "limit_no2=%.6f\n", limit_no2);
25 fprintf(file, "limit_o3=%.6f\n", limit_o3);
26 fprintf(file, "limit_so2=%.6f\n", limit_so2);
27 fprintf(file, "collection_interval=%d\n", collection_interval);
28 fprintf(file, "retention_period=%d\n", retention_period);
29 fprintf(file, "sensor_plugins_enabled=%d\n", sensor_plugins_enabled);
30 fprintf(file, "sensor_mode=%s\n", sensor_mode);
31 fprintf(file, "sensor_plugin_path=%s\n", sensor_plugin_path);
32
33 // Save multi-sensor configurations
34 fprintf(file, "active_sensor_count=%d\n", active_sensor_count);
35 for (int i = 0; i < active_sensor_count; i++) {
36 fprintf(file, "sensor_%d_mode=%s\n", i, sensor_configs[i].mode);
37 fprintf(file, "sensor_%d_path=%s\n", i, sensor_configs[i].plugin_path);
38 fprintf(file, "sensor_%d_enabled=%d\n", i, sensor_configs[i].enabled);
39 }
40
41 fclose(file);
42 printf("Configuration saved to %s\n", path);
43}
44
45int load_config(void) {
46 char path[AQM_PATH_MAX];
47 if (aqm_get_config_path(path, sizeof(path)) != 0)
48 return 0;
49
50 FILE *file = fopen(path, "r");
51 if (file == NULL)
52 return 0;
53
54 init_sensor_configs();
55
56 char line[256];
57 while (fgets(line, sizeof(line), file)) {
58 aqm_trim_crlf(line);
59 if (line[0] == '\0')
60 continue;
61
62 char *eq = strchr(line, '=');
63 if (!eq)
64 continue;
65 *eq = '\0';
66 const char *key = line;
67 const char *value = eq + 1;
68
69 if (strcmp(key, "limit_pm25") == 0) {
70 float temp;
71 if (aqm_parse_float(value, &temp))
72 limit_pm25 = temp;
73 } else if (strcmp(key, "limit_pm10") == 0) {
74 float temp;
75 if (aqm_parse_float(value, &temp))
76 limit_pm10 = temp;
77 } else if (strcmp(key, "limit_co") == 0) {
78 float temp;
79 if (aqm_parse_float(value, &temp))
80 limit_co = temp;
81 } else if (strcmp(key, "limit_no2") == 0) {
82 float temp;
83 if (aqm_parse_float(value, &temp))
84 limit_no2 = temp;
85 } else if (strcmp(key, "limit_o3") == 0) {
86 float temp;
87 if (aqm_parse_float(value, &temp))
88 limit_o3 = temp;
89 } else if (strcmp(key, "limit_so2") == 0) {
90 float temp;
91 if (aqm_parse_float(value, &temp))
92 limit_so2 = temp;
93 } else if (strcmp(key, "collection_interval") == 0) {
94 int temp;
95 if (aqm_parse_int(value, &temp) && temp >= 1)
96 collection_interval = temp;
97 } else if (strcmp(key, "retention_period") == 0) {
98 int temp;
99 if (aqm_parse_int(value, &temp) && temp >= 1)
100 retention_period = temp;
101 } else if (strcmp(key, "sensor_plugins_enabled") == 0) {
102 int temp;
103 if (aqm_parse_int(value, &temp))
104 sensor_plugins_enabled = temp ? 1 : 0;
105 } else if (strcmp(key, "sensor_mode") == 0)
106 snprintf(sensor_mode, sizeof(sensor_mode), "%s", value);
107 else if (strcmp(key, "sensor_plugin_path") == 0)
108 snprintf(sensor_plugin_path, sizeof(sensor_plugin_path), "%s", value);
109 else if (strcmp(key, "active_sensor_count") == 0) {
110 int temp;
111 if (aqm_parse_int(value, &temp) && temp >= 0 && temp <= MAX_SENSORS)
112 active_sensor_count = temp;
113 } else if (strncmp(key, "sensor_", 7) == 0) {
114 // Parse sensor_N_mode, sensor_N_path, sensor_N_enabled
115 int idx = -1;
116 char suffix[32] = {0};
117 if (sscanf(key, "sensor_%d_%31s", &idx, suffix) == 2 && idx >= 0 && idx < MAX_SENSORS) {
118 if (strcmp(suffix, "mode") == 0)
119 snprintf(sensor_configs[idx].mode, sizeof(sensor_configs[idx].mode), "%s", value);
120 else if (strcmp(suffix, "path") == 0)
121 snprintf(sensor_configs[idx].plugin_path, sizeof(sensor_configs[idx].plugin_path), "%s", value);
122 else if (strcmp(suffix, "enabled") == 0)
123 sensor_configs[idx].enabled = atoi(value) ? 1 : 0;
124 }
125 }
126 }
127
128 fclose(file);
129 return 1;
130}
int enabled
Non-zero if this sensor slot is active for data collection.
Definition globals.h:55