+static void set_acenabled(struct afb_req request)
+{
+ int i=4, rc, x, changed;
+ double d;
+ struct json_object *query, *val;
+ uint8_t values[sizeof hvac_values / sizeof *hvac_values];
+ uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
+
+ WARNING(interface, "in set_acenabled.");
+
+ query = afb_req_json(request);
+
+ /* records initial values */
+ WARNING(interface, "Records initial values");
+ values[i] = saves[i] = hvac_values[i].value;
+
+
+ if (json_object_object_get_ex(query, hvac_values[i].name, &val))
+ {
+ WARNING(interface, "We got it. Tests if it is an int or double.");
+ if (json_object_is_type(val, json_type_int)) {
+ x = json_object_get_int(val);
+ WARNING(interface, "We get an int: %d",x);
+ }
+ else if (json_object_is_type(val, json_type_double)) {
+ d = json_object_get_double(val);
+ x = (int)round(d);
+ WARNING(interface, "We get a double: %f => %d",d,x);
+ }
+ else {
+ afb_req_fail_f(request, "bad-request",
+ "argument '%s' isn't integer or double", hvac_values[i].name);
+ return;
+ }
+ if (x < 0 || x > 255)
+ {
+ afb_req_fail_f(request, "bad-request",
+ "argument '%s' is out of bounds", hvac_values[i].name);
+ return;
+ }
+ if (values[i] != x) {
+ values[i] = (uint8_t)x;
+ changed = 1;
+ WARNING(interface,"%s changed to %d",hvac_values[i].name,x);
+ }
+ }
+ else {
+ WARNING(interface, "%s not found in query!",hvac_values[i].name);
+ }
+
+ if (changed)
+ {
+ i = 4; //(int)(sizeof hvac_values / sizeof *hvac_values);
+ hvac_values[i].value = values[i];
+ WARNING(interface, "WRITE_LED: value: %d ",hvac_values[i].value);
+ rc = write_led();
+ if (rc >= 0)
+ afb_req_success(request, NULL, NULL);
+ else if (retry(write_led)) {
+ /* restore initial values */
+ i = 4; //(int)(sizeof hvac_values / sizeof *hvac_values);
+ hvac_values[i].value = saves[i];
+ afb_req_fail(request, "error", "I2C error");
+ }
+ }
+ else {
+ afb_req_success(request, NULL, "No changes");
+ }
+
+
+}
+