Parse sync response from server
This commit is contained in:
		
							
								
								
									
										63
									
								
								esp32_device/main/sync_response.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								esp32_device/main/sync_response.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
#include "sync_response.h"
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <esp_log.h>
 | 
			
		||||
 | 
			
		||||
const static char *TAG = "sync_response";
 | 
			
		||||
 | 
			
		||||
sync_response *sync_response_parse(cJSON *res)
 | 
			
		||||
{
 | 
			
		||||
    cJSON *relays_json = cJSON_GetObjectItem(res, "relays");
 | 
			
		||||
    if (relays_json == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Missing relays status in sync response!");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int relays_size = cJSON_GetArraySize(relays_json);
 | 
			
		||||
    sync_response *sync_res = calloc(1, sizeof(sync_response) + relays_size * sizeof(bool));
 | 
			
		||||
 | 
			
		||||
    if (!sync_res)
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGE(TAG, "Failed to allocate memory to store synchronization response!");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sync_res->len = relays_size;
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < sync_res->len; i++)
 | 
			
		||||
    {
 | 
			
		||||
        sync_res->relays[i] = false;
 | 
			
		||||
 | 
			
		||||
        cJSON *item = cJSON_GetArrayItem(relays_json, i);
 | 
			
		||||
        assert(item != NULL);
 | 
			
		||||
        cJSON *enabled = cJSON_GetObjectItem(item, "enabled");
 | 
			
		||||
 | 
			
		||||
        if (enabled == NULL)
 | 
			
		||||
        {
 | 
			
		||||
            ESP_LOGE(TAG, "At least a relay is missing the enabled field. Assuming false.");
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (cJSON_IsTrue(enabled))
 | 
			
		||||
        {
 | 
			
		||||
            sync_res->relays[i] = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return sync_res;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sync_response_print(sync_response *res)
 | 
			
		||||
{
 | 
			
		||||
    ESP_LOGI(TAG, " === sync response begin === ");
 | 
			
		||||
    for (size_t i = 0; i < res->len; i++)
 | 
			
		||||
        ESP_LOGI(TAG, "Relay[%d]=%s", i, res->relays[i] ? "ON" : "off");
 | 
			
		||||
    ESP_LOGI(TAG, " === sync response end === ");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void sync_response_free(sync_response *res)
 | 
			
		||||
{
 | 
			
		||||
    if (res != NULL)
 | 
			
		||||
        free(res);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user