1#include "core/aqm_paths.h"
2#include "core/aqm_platform.h"
3#include "core/globals.h"
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");
15 FILE *file = fopen(path,
"w");
17 fprintf(stderr,
"Cannot open config file for writing: %s\n", path);
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);
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);
42 printf(
"Configuration saved to %s\n", path);
45int load_config(
void) {
46 char path[AQM_PATH_MAX];
47 if (aqm_get_config_path(path,
sizeof(path)) != 0)
50 FILE *file = fopen(path,
"r");
54 init_sensor_configs();
57 while (fgets(line,
sizeof(line), file)) {
62 char *eq = strchr(line,
'=');
66 const char *key = line;
67 const char *value = eq + 1;
69 if (strcmp(key,
"limit_pm25") == 0) {
71 if (aqm_parse_float(value, &temp))
73 }
else if (strcmp(key,
"limit_pm10") == 0) {
75 if (aqm_parse_float(value, &temp))
77 }
else if (strcmp(key,
"limit_co") == 0) {
79 if (aqm_parse_float(value, &temp))
81 }
else if (strcmp(key,
"limit_no2") == 0) {
83 if (aqm_parse_float(value, &temp))
85 }
else if (strcmp(key,
"limit_o3") == 0) {
87 if (aqm_parse_float(value, &temp))
89 }
else if (strcmp(key,
"limit_so2") == 0) {
91 if (aqm_parse_float(value, &temp))
93 }
else if (strcmp(key,
"collection_interval") == 0) {
95 if (aqm_parse_int(value, &temp) && temp >= 1)
96 collection_interval = temp;
97 }
else if (strcmp(key,
"retention_period") == 0) {
99 if (aqm_parse_int(value, &temp) && temp >= 1)
100 retention_period = temp;
101 }
else if (strcmp(key,
"sensor_plugins_enabled") == 0) {
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) {
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) {
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;
int enabled
Non-zero if this sensor slot is active for data collection.