+/*
+ * @brief Writing to LED for both temperature sliders
+ */
+
+static int temp_write_led()
+{
+ int rc = 0, red_flag, blue_flag, green_flag, red_value, green_value, blue_value;
+ int right_temp;
+ int left_temp;
+
+ // /sys/class/leds/blinkm-3-9-red/brightness
+ FILE* r = fopen(RED, "w");
+ if (r == NULL) {
+ AFB_ERROR("Unable to open RED path for writing");
+ red_flag = 1;
+ }
+
+ // /sys/class/leds/blinkm-3-9-green/brightness
+ FILE* g = fopen(GREEN, "w");
+ if (g == NULL) {
+ AFB_ERROR("Unable to open GREEN path for writing");
+ green_flag = 1;
+ }
+
+ // /sys/class/leds/blinkm-3-9-blue/brightness
+ FILE* b = fopen(BLUE, "w");
+ if (b == NULL) {
+ AFB_ERROR("Unable to open BLUE path for writing");
+ blue_flag = 1;
+ }
+
+ if (red_flag || green_flag || blue_flag)
+ {
+ rc = 1;
+ }
+
+ left_temp = read_temp_left_led() - 15;
+ right_temp = read_temp_right_led() - 15;
+
+ // Calculates average colour value taken from the temperature toggles
+ red_value = (degree_colours[left_temp].rgb[0] + degree_colours[right_temp].rgb[0]) / 2;
+ green_value = (degree_colours[left_temp].rgb[1] + degree_colours[right_temp].rgb[1]) / 2;
+ blue_value = (degree_colours[left_temp].rgb[2] + degree_colours[right_temp].rgb[2]) / 2;
+
+ // Writes to LED file the specific colour
+ fprintf(r, "%d", red_value);
+ fprintf(g, "%d", green_value);
+ fprintf(b, "%d", blue_value);
+ fclose(r);
+ fclose(g);
+ fclose(b);
+ return rc;
+
+}
+
+/*
+ * @brief Get temperature of left toggle in HVAC system
+ *
+ * @param struct afb_req : an afb request structure
+ *
+ */
+static void temp_left_zone_led(struct afb_req request)
+{
+ int i = 5, 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];
+
+ AFB_WARNING("In temp_left_zone_led.");
+
+ query = afb_req_json(request);
+
+ /* records initial values */
+ AFB_WARNING("Records initial values");
+ values[i] = saves[i] = hvac_values[i].value;
+
+
+ if (json_object_object_get_ex(query, hvac_values[i].name, &val))
+ {
+ AFB_WARNING("Value of values[i] = %d", values[i]);
+ AFB_WARNING("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);
+ AFB_WARNING("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);
+ AFB_WARNING("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;
+ AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
+ }
+ }
+ else {
+ AFB_WARNING("%s not found in query!",hvac_values[i].name);
+ }
+
+
+ if (changed) {
+ hvac_values[i].value = values[i]; // update structure at line 102
+ AFB_WARNING("WRITE_LED: value: %d", hvac_values[i].value);
+ rc = temp_write_led();
+ if (rc >= 0)
+ afb_req_success(request, NULL, NULL);
+ else if (retry(temp_write_led)) {
+ /* restore initial values */
+ hvac_values[i].value = saves[i];
+ afb_req_fail(request, "error", "I2C error");
+ }
+ }
+}
+
+/*
+ * @brief Get temperature of right toggle in HVAC system
+ *
+ * @param struct afb_req : an afb request structure
+ *
+ */
+static void temp_right_zone_led(struct afb_req request)
+{
+ int i = 6, 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];
+
+ AFB_WARNING("In temp_right_zone_led.");
+
+ query = afb_req_json(request);
+
+ /* records initial values */
+ AFB_WARNING("Records initial values");
+ values[i] = saves[i] = hvac_values[i].value;
+
+
+ if (json_object_object_get_ex(query, hvac_values[i].name, &val))
+ {
+ AFB_WARNING("Value of values[i] = %d", values[i]);
+ AFB_WARNING("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);
+ AFB_WARNING("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);
+ AFB_WARNING("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;
+ AFB_WARNING("%s changed to %d", hvac_values[i].name,x);
+ }
+ }
+ else {
+ AFB_WARNING("%s not found in query!", hvac_values[i].name);
+ }
+
+
+ if (changed) {
+ hvac_values[i].value = values[i]; // update structure at line 102
+ AFB_WARNING("WRITE_LED: value: %d", hvac_values[i].value);
+ rc = temp_write_led();
+ if (rc >= 0)
+ afb_req_success(request, NULL, NULL);
+ else if (retry(temp_write_led)) {
+ /* restore initial values */
+ hvac_values[i].value = saves[i];
+ afb_req_fail(request, "error", "I2C error");
+ }
+ }
+}
+