Skip to content

Commit 2a04f95

Browse files
Report a status consistent with the controls. Add ability to report failures
1 parent cff0803 commit 2a04f95

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

houserelays.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ static const char *relays_status (const char *method, const char *uri,
8181
for (i = 0; i < count; ++i) {
8282
time_t pulsed = houserelays_gpio_deadline(i);
8383
const char *name = houserelays_gpio_name(i);
84+
const char *status = houserelays_gpio_failure(i);
85+
if (!status) status = houserelays_gpio_get(i)?"on":"off";
8486
const char *commanded = houserelays_gpio_commanded(i)?"on":"off";
8587

8688
int point = echttp_json_add_object (context, container, name);
87-
echttp_json_add_integer (context, point, "state", houserelays_gpio_get(i));
89+
echttp_json_add_string (context, point, "state", status);
8890
echttp_json_add_string (context, point, "command", commanded);
8991
if (pulsed)
9092
echttp_json_add_integer (context, point, "pulse", (int)pulsed);

houserelays_gpio.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
* Return the name of a relay point. The point name serves as an
4141
* identifier for application access.
4242
*
43+
* const char *houserelays_gpio_failure (int point);
44+
*
45+
* Return a strig describing the failure, or a null pointer if healthy.
46+
*
4347
* const char *houserelays_gpio_description (int point);
4448
*
4549
* Return the point's description. This is just text intended to help
@@ -169,6 +173,10 @@ const char *houserelays_gpio_description (int point) {
169173
return Relays[point].desc;
170174
}
171175

176+
const char *houserelays_gpio_failure (int point) {
177+
return 0; // A GPIO never fail, or never report it to us..
178+
}
179+
172180
int houserelays_gpio_commanded (int point) {
173181
if (point < 0 || point > RelaysCount) return 0;
174182
return Relays[point].commanded;
@@ -181,7 +189,8 @@ time_t houserelays_gpio_deadline (int point) {
181189

182190
int houserelays_gpio_get (int point) {
183191
if (point < 0 || point > RelaysCount) return 0;
184-
return gpiod_line_get_value (Relays[point].line);
192+
int state = gpiod_line_get_value (Relays[point].line);
193+
return (state == Relays[point].on);
185194
}
186195

187196
int houserelays_gpio_set (int point, int state, int pulse) {

houserelays_gpio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ int houserelays_gpio_count (void);
2828
const char *houserelays_gpio_name (int point);
2929
const char *houserelays_gpio_description (int point);
3030

31+
const char *houserelays_gpio_failure (int point);
32+
3133
int houserelays_gpio_commanded (int point);
3234
time_t houserelays_gpio_deadline (int point);
3335
int houserelays_gpio_get (int point);

public/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
for (const [key, value] of Object.entries(state)) {
1212
var state = document.getElementById ('state-'+key);
1313
var button = document.getElementById ('button-'+key);
14-
if (value.state == button.controlOn) {
14+
button.disabled = false;
15+
if (value.state == 'on') {
1516
state.innerHTML = 'ON';
1617
button.innerHTML = 'OFF';
1718
button.controlState = 'off';
18-
} else {
19+
} else if (value.state == 'off') {
1920
state.innerHTML = 'OFF';
2021
button.innerHTML = 'ON';
2122
button.controlState = 'on';
23+
} else {
24+
state.innerHTML = value.state;
25+
button.innerHTML = 'ON';
26+
button.disabled = true;
2227
}
23-
button.disabled = false;
2428
}
2529
}
2630

0 commit comments

Comments
 (0)