Bladeren bron

api: Implement PUT and DELETE verbs

Helmut Pozimski 1 jaar geleden
bovenliggende
commit
1d43d11b8c
1 gewijzigde bestanden met toevoegingen van 86 en 0 verwijderingen
  1. 86 0
      main/api.c

+ 86 - 0
main/api.c

@@ -16,6 +16,7 @@
  
 #define TAG "api"
 #define NUM_WEEK_DAYS 7
+#define BUF_LEN 1000
 
 static char WEEK_DAYS[NUM_WEEK_DAYS][4] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
 
@@ -71,6 +72,89 @@ static const httpd_uri_t wakeup_get = {
     .handler   = wakeup_get_handler
 };
 
+static uint8_t validate_time(int hour, int minute) {
+	return (hour >= 0 && hour < 24) && (minute >= 0 && minute < 60);
+}
+
+static esp_err_t wakeup_put_handler(httpd_req_t *req) {
+	char* wakeup_str = extract_wakeup(req->uri);
+	char buf[BUF_LEN];
+	if (!day_is_valid(wakeup_str)) {
+		httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid day requested");
+		return ESP_OK;	
+	}
+	
+	if (req->content_len >= BUF_LEN) {
+		httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Maximum content length exceeded");
+		return ESP_OK;	
+	}
+	
+	int bytes_received = httpd_req_recv(req, buf, BUF_LEN);
+	
+	if (bytes_received == 0) {
+		httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "No content received");
+		return ESP_OK;
+	} else if (bytes_received < 0) {
+		httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Request could not be processed");
+		return ESP_OK;
+	}
+	buf[req->content_len] = '\0';
+	
+	cJSON *root = cJSON_Parse(buf);
+	if (root == NULL) {
+		httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Could not parse object");
+		return ESP_OK;
+	}
+	
+	int hour = cJSON_GetObjectItem(root, "hour")->valueint;
+	int minute = cJSON_GetObjectItem(root, "minute")->valueint;
+	
+	if (validate_time(hour, minute)) {
+		esp_err_t ret = write_wakeup_time_str(wakeup_str, hour * 60 + minute);
+		if (ret == ESP_OK) {
+			httpd_resp_sendstr(req, "");
+		} else {
+			httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error while writing data");		
+		}
+	} else {
+		httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Time values outside allowed range");
+	}
+	cJSON_Delete(root);
+	return ESP_OK;
+}
+
+static const httpd_uri_t wakeup_put = {
+    .uri       = "/v1/wakeup/*",
+    .method    = HTTP_PUT,
+    .handler   = wakeup_put_handler
+};
+
+static esp_err_t wakeup_delete_handler(httpd_req_t *req) {
+	char* wakeup_str = extract_wakeup(req->uri);
+	if (!day_is_valid(wakeup_str)) {
+		httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid day requested");
+		return ESP_OK;	
+	}
+	
+	esp_err_t ret = delete_wakeup_time_str(wakeup_str);
+	if (ret == ESP_OK) {
+		httpd_resp_sendstr(req, "");
+		return ret;
+	} else if (ret == ESP_ERR_NVS_NOT_FOUND) {
+		httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Entry does not exist");
+	}
+	
+	httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error while deleting data");
+	return ESP_OK;
+}
+
+static const httpd_uri_t wakeup_delete = {
+    .uri       = "/v1/wakeup/*",
+    .method    = HTTP_DELETE,
+    .handler   = wakeup_delete_handler
+};
+
+
 httpd_handle_t start_webserver() {
     httpd_handle_t server;
     httpd_config_t config = HTTPD_DEFAULT_CONFIG();
@@ -81,6 +165,8 @@ httpd_handle_t start_webserver() {
     if (httpd_start(&server, &config) == ESP_OK) {
         ESP_LOGI(TAG, "Registering URI handlers");
         httpd_register_uri_handler(server, &wakeup_get);
+        httpd_register_uri_handler(server, &wakeup_put);
+		httpd_register_uri_handler(server, &wakeup_delete);
         return server;
     }