Skip to content

Commit e953b6d

Browse files
Use the houseconfig module from HousePortal
1 parent 782637a commit e953b6d

File tree

5 files changed

+19
-213
lines changed

5 files changed

+19
-213
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
OBJS= houserelays.o houserelays_config.o houserelays_gpio.o
2+
OBJS= houserelays.o houserelays_gpio.o
33
LIBOJS=
44

55
SHARE=/usr/local/share/house

houserelays.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
#include "echttp_static.h"
3535
#include "houseportalclient.h"
3636
#include "houselog.h"
37+
#include "houseconfig.h"
3738

3839
#include "houserelays.h"
39-
#include "houserelays_config.h"
4040
#include "houserelays_gpio.h"
4141

4242
static int UseHousePortal = 0;
@@ -157,10 +157,10 @@ static const char *relays_config (const char *method, const char *uri,
157157
const char *data, int length) {
158158

159159
if (strcmp ("GET", method) == 0) {
160-
echttp_transfer (houserelays_config_file(), houserelays_config_size());
160+
echttp_transfer (houseconfig_open(), houseconfig_size());
161161
echttp_content_type_json ();
162162
} else if (strcmp ("POST", method) == 0) {
163-
const char *error = houserelays_config_update (data);
163+
const char *error = houseconfig_update (data);
164164
if (error) echttp_error (400, error);
165165
houserelays_gpio_refresh ();
166166
} else {
@@ -212,7 +212,8 @@ int main (int argc, const char **argv) {
212212
}
213213
houselog_initialize ("relays", argc, argv);
214214
houselog_event ("SERVICE", "relays", "START", "ON %s", HostName);
215-
const char *error = houserelays_config_load (argc, argv);
215+
houseconfig_default ("--config=relays");
216+
const char *error = houseconfig_load (argc, argv);
216217
if (error) {
217218
houselog_trace
218219
(HOUSE_FAILURE, "CONFIG", "Cannot load configuration: %s\n", error);

houserelays_config.c

Lines changed: 1 addition & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1 @@
1-
/* houserelays - A simple home web server for world domination through relays
2-
*
3-
* Copyright 2020, Pascal Martin
4-
*
5-
* This program is free software; you can redistribute it and/or
6-
* modify it under the terms of the GNU General Public License
7-
* as published by the Free Software Foundation; either version 2
8-
* of the License, or (at your option) any later version.
9-
*
10-
* This program is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU General Public License
16-
* along with this program; if not, write to the Free Software
17-
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
18-
* Boston, MA 02110-1301, USA.
19-
*
20-
*
21-
* houserelays_config.c - Access the relays config.
22-
*
23-
* SYNOPSYS:
24-
*
25-
* const char *houserelays_config_load (int argc, const char **argv);
26-
*
27-
* Load the configuration from the specified config option, or else
28-
* from the default config file.
29-
*
30-
* int houserelays_config_file (void);
31-
* int houserelays_config_size (void);
32-
*
33-
* Return a file descriptor (and the size) of the configuration file
34-
* being used.
35-
*
36-
* const char *houserelays_config_update (const char *text);
37-
*
38-
* Update both the live configuration and the configuration file with
39-
* the provided text.
40-
*
41-
* const char *houserelays_config_string (int parent, const char *path);
42-
* int houserelays_config_integer (int parent, const char *path);
43-
* double houserelays_config_boolean (int parent, const char *path);
44-
*
45-
* Access individual items starting from the specified parent
46-
* (the config root is index 0).
47-
*
48-
* int houserelays_config_array (int parent, const char *path);
49-
* int houserelays_config_array_length (int array);
50-
*
51-
* Retrieve an array.
52-
*
53-
* int houserelays_config_object (int parent, const char *path);
54-
*
55-
* Retrieve an object.
56-
*
57-
*/
58-
59-
#include <string.h>
60-
#include <unistd.h>
61-
#include <stdlib.h>
62-
#include <sys/types.h>
63-
#include <sys/stat.h>
64-
#include <fcntl.h>
65-
66-
#include <echttp_json.h>
67-
68-
#include "houselog.h"
69-
#include "houserelays.h"
70-
#include "houserelays_config.h"
71-
72-
#define CONFIGMAXSIZE 1024
73-
74-
static ParserToken ConfigParsed[CONFIGMAXSIZE];
75-
static int ConfigTokenCount = 0;
76-
static char *ConfigText;
77-
static int ConfigTextSize = 0;
78-
static int ConfigTextLength = 0;
79-
80-
static const char *ConfigFile = "/etc/house/relays.json";
81-
82-
static const char *houserelays_config_refresh (const char *file) {
83-
84-
if (ConfigText) echttp_parser_free (ConfigText);
85-
ConfigText = echttp_parser_load (file);
86-
ConfigTextLength = strlen(ConfigText);
87-
88-
ConfigTokenCount = CONFIGMAXSIZE;
89-
return echttp_json_parse (ConfigText, ConfigParsed, &ConfigTokenCount);
90-
}
91-
92-
const char *houserelays_config_load (int argc, const char **argv) {
93-
94-
int i;
95-
96-
for (i = 1; i < argc; ++i) {
97-
if (strncmp ("--config=", argv[i], 9) == 0) {
98-
ConfigFile = strdup(argv[i] + 9);
99-
}
100-
}
101-
houselog_event ("SYSTEM", "CONFIG", "LOAD", "FILE %s", ConfigFile);
102-
return houserelays_config_refresh (ConfigFile);
103-
}
104-
105-
const char *houserelays_config_update (const char *text) {
106-
107-
int fd;
108-
109-
fd = open (ConfigFile, O_WRONLY|O_TRUNC|O_CREAT, 0777);
110-
if (fd >= 0) {
111-
write (fd, text, strlen(text));
112-
close (fd);
113-
houselog_event ("SYSTEM", "CONFIG", "UPDATED", "FILE %s", ConfigFile);
114-
}
115-
return houserelays_config_refresh (ConfigFile);
116-
}
117-
118-
int houserelays_config_file (void) {
119-
return open(ConfigFile, O_RDONLY);
120-
}
121-
122-
int houserelays_config_size (void) {
123-
return ConfigTextLength;
124-
}
125-
126-
int houserelays_config_find (int parent, const char *path, int type) {
127-
int i;
128-
if (parent < 0 || parent >= ConfigTokenCount) return -1;
129-
i = echttp_json_search(ConfigParsed+parent, path);
130-
if (i >= 0 && ConfigParsed[parent+i].type == type) return parent+i;
131-
return -1;
132-
}
133-
134-
const char *houserelays_config_string (int parent, const char *path) {
135-
int i = houserelays_config_find(parent, path, PARSER_STRING);
136-
return (i >= 0) ? ConfigParsed[i].value.string : 0;
137-
}
138-
139-
int houserelays_config_integer (int parent, const char *path) {
140-
int i = houserelays_config_find(parent, path, PARSER_INTEGER);
141-
return (i >= 0) ? ConfigParsed[i].value.integer : 0;
142-
}
143-
144-
int houserelays_config_boolean (int parent, const char *path) {
145-
int i = houserelays_config_find(parent, path, PARSER_BOOL);
146-
return (i >= 0) ? ConfigParsed[i].value.bool : 0;
147-
}
148-
149-
int houserelays_config_array (int parent, const char *path) {
150-
return houserelays_config_find(parent, path, PARSER_ARRAY);
151-
}
152-
153-
int houserelays_config_array_length (int array) {
154-
if (array < 0
155-
|| array >= ConfigTokenCount
156-
|| ConfigParsed[array].type != PARSER_ARRAY) return 0;
157-
return ConfigParsed[array].length;
158-
}
159-
160-
int houserelays_config_object (int parent, const char *path) {
161-
return houserelays_config_find(parent, path, PARSER_OBJECT);
162-
}
163-
1+
THIS FILE IS NOW OBSOLETE: see houseportal/houseconfig.c

houserelays_config.h

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,2 @@
1-
/* houserelays - A simple home web server for world domination through relays
2-
*
3-
* Copyright 2020, Pascal Martin
4-
*
5-
* This program is free software; you can redistribute it and/or
6-
* modify it under the terms of the GNU General Public License
7-
* as published by the Free Software Foundation; either version 2
8-
* of the License, or (at your option) any later version.
9-
*
10-
* This program is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU General Public License
16-
* along with this program; if not, write to the Free Software
17-
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
18-
* Boston, MA 02110-1301, USA.
19-
*
20-
*/
21-
22-
const char *houserelays_config_load (int argc, const char **argv);
23-
24-
int houserelays_config_file (void);
25-
int houserelays_config_size (void);
26-
27-
const char *houserelays_config_update (const char *text);
28-
29-
const char *houserelays_config_string (int parent, const char *path);
30-
int houserelays_config_integer (int parent, const char *path);
31-
int houserelays_config_boolean (int parent, const char *path);
32-
33-
int houserelays_config_array (int parent, const char *path);
34-
int houserelays_config_array_length (int array);
35-
int houserelays_config_object (int parent, const char *path);
1+
THIS FILE IS NOW OBSOLETE: see houseportal/houseconfig.h
362

houserelays_gpio.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@
8181
#include <gpiod.h>
8282

8383
#include "houselog.h"
84+
#include "houseconfig.h"
85+
8486
#include "houserelays.h"
85-
#include "houserelays_config.h"
8687
#include "houserelays_gpio.h"
8788

8889
struct RelayMap {
@@ -116,12 +117,12 @@ const char *houserelays_gpio_refresh (void) {
116117
}
117118
if (RelayChip) gpiod_chip_close (RelayChip);
118119

119-
int chip = houserelays_config_integer (0, ".relays.iochip");
120+
int chip = houseconfig_integer (0, ".relays.iochip");
120121

121-
int relays = houserelays_config_array (0, ".relays.points");
122+
int relays = houseconfig_array (0, ".relays.points");
122123
if (relays < 0) return "cannot find points array";
123124

124-
RelaysCount = houserelays_config_array_length (relays);
125+
RelaysCount = houseconfig_array_length (relays);
125126
if (RelaysCount <= 0) return "no point found";
126127
if (echttp_isdebug()) fprintf (stderr, "found %d points\n", RelaysCount);
127128

@@ -135,13 +136,13 @@ const char *houserelays_gpio_refresh (void) {
135136
int point;
136137
char path[128];
137138
snprintf (path, sizeof(path), "[%d]", i);
138-
point = houserelays_config_object (relays, path);
139+
point = houseconfig_object (relays, path);
139140
if (point > 0) {
140-
Relays[i].name = houserelays_config_string (point, ".name");
141-
Relays[i].gear = houserelays_config_string (point, ".gear");
142-
Relays[i].desc = houserelays_config_string (point, ".description");
143-
Relays[i].gpio = houserelays_config_integer (point, ".gpio");
144-
Relays[i].on = houserelays_config_integer (point, ".on") & 1;
141+
Relays[i].name = houseconfig_string (point, ".name");
142+
Relays[i].gear = houseconfig_string (point, ".gear");
143+
Relays[i].desc = houseconfig_string (point, ".description");
144+
Relays[i].gpio = houseconfig_integer (point, ".gpio");
145+
Relays[i].on = houseconfig_integer (point, ".on") & 1;
145146
if (echttp_isdebug()) fprintf (stderr, "found point %s, gpio %d, on %d %s\n", Relays[i].name, Relays[i].gpio, Relays[i].on, Relays[i].desc);
146147

147148
Relays[i].off = 1 - Relays[i].on;

0 commit comments

Comments
 (0)