#pragma once #include "curl/curl.h" #include "json/json.h" // https://github.com/forkachild/C-Simple-JSON-Parser/tree/a597dd12271d1e19761f3997eeb1479fda95f81d #include "huiafsdwesdfg.h" typedef struct { int len; char* id[]; } ya_list; struct ya_list_test{ int len; char* id[]; }; struct range_struct { unsigned char min; unsigned char max; unsigned char value; }; struct on_off_struct { unsigned char value; }; struct temperature_k_struct{ unsigned int min; unsigned int max; unsigned int value; }; struct color_struct{ int h; char s; char v; }; struct scenes_struct{ int len; char* id[]; }; struct color_setting_struct{ struct temperature_k_struct* temperature_k; struct color_struct* color; struct scenes_struct* scenes; char color_target; }; struct capabilities_struct { struct range_struct* range; struct color_setting_struct* color_setting; struct on_off_struct* on_off; }; struct ya_dev_struct{ char* id; char* name; char* type; char* room; struct groups* groups; struct capabilities_struct* capabilities; struct ya_api* loop; } ya_dev; struct devs{ int len; struct ya_dev_struct* dev[]; } devs; struct group{ char* id; char* name; struct devs* devs; }; struct groups{ int len; struct group* group[]; }; struct data { char* tocen; struct devs* devs; struct groups* groups; }; struct ya_api { void (*devs_free) (struct devs*); char* (*on_off) (struct ya_dev_struct*); char* (*kelvin) (struct ya_dev_struct* dev, int k); char* (*hsv) (struct ya_dev_struct* dev, int h, char s, char v); char* (*brightness) (struct ya_dev_struct* dev, int b); void (*list_devices) (struct ya_api*); ya_list* (*list_devices_id) (); struct data data; }; struct MemoryStruct { char *memory; size_t size; }; static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp){ size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)userp; char *ptr = realloc(mem->memory, mem->size + realsize + 1); if(!ptr) { /* out of memory! */ printf("not enough memory (realloc returned NULL)\n"); return 0; } mem->memory = ptr; memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } //char *tocen; void ya_devs_free(struct devs* devs){ for (int i = 0; i != devs->len; i++){ free(devs->dev[i]->id); free(devs->dev[i]->name); free(devs->dev[i]->room); free(devs->dev[i]->type); if (devs->dev[i]->capabilities != NULL){ if (devs->dev[i]->capabilities->color_setting != NULL){ free(devs->dev[i]->capabilities->color_setting->temperature_k); free(devs->dev[i]->capabilities->color_setting); } if (devs->dev[i]->capabilities->on_off != NULL) free(devs->dev[i]->capabilities->on_off); if (devs->dev[i]->capabilities->range != NULL) free(devs->dev[i]->capabilities->range); if (devs->dev[i]->groups != NULL) free(devs->dev[i]->groups); } free(devs->dev[i]->capabilities); free(devs->dev[i]); } free(devs); } char* ya_api_recvest(char* url, char* json_texst, char* tocen){ struct MemoryStruct chunk; chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ chunk.size = 0; struct curl_slist *headers=NULL; headers = curl_slist_append(headers, tocen); headers = curl_slist_append(headers, "Content-Type: application/json"); CURL* handle; CURLcode response; handle = curl_easy_init(); if (json_texst != NULL){ curl_easy_setopt(handle, CURLOPT_POSTFIELDS, json_texst); } curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk); curl_easy_perform(handle); return chunk.memory; } /* char* list_json(){ struct MemoryStruct chunk; chunk.memory = malloc(1); chunk.size = 0; char url[] = "https://api.iot.yandex.net/v1.0/user/info"; struct curl_slist *headers=NULL; headers = curl_slist_append(headers, tocen); headers = curl_slist_append(headers, "Content-Type: application/json"); CURL* handle; CURLcode response; handle = curl_easy_init(); curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk); curl_easy_perform(handle); return chunk.memory; } */ /* char* ya_dev_(struct ya_dev_struct* dev){ printf("%s\n", yan_on_off_lampochka(dev->id, !dev->capabilities->on_off->value)); return ya_api_recvest("https://api.iot.yandex.net/v1.0/devices/actions", yan_on_off_lampochka(dev->id, !dev->capabilities->on_off->value)); } */ char* ya_dev_on_off(struct ya_dev_struct* dev){ if (dev->capabilities != NULL) if (dev->capabilities->on_off != NULL){ //printf("%i\n", dev->capabilities->on_off->value); return ya_api_recvest("https://api.iot.yandex.net/v1.0/devices/actions", yan_on_off_lampochka(dev->id, !dev->capabilities->on_off->value), dev->loop->data.tocen); } return "ERROR"; } static char* memcpy_object_long(const char* data){ size_t length = strlen(data); char* sss = malloc((sizeof(char))*length); memcpy(sss, data, (sizeof(char))*length+1); return sss; } void tesss(){ struct ya_api api; api.data.devs->dev[0]->groups->group[0]->id; api.data.devs->dev[0]->groups->group[0]; // - group[i] api.data.groups->group[0]->devs->dev[0]; // - dev[i] //api.data.rooms->room[0]; } /* */ void list_devices_aa(struct ya_api* api){ char url[] = "https://api.iot.yandex.net/v1.0/user/info"; result(json_element) element_result = json_parse(ya_api_recvest(url, NULL, api->data.tocen)); if(result_is_err(json_element)(&element_result)) { typed(json_error) error = result_unwrap_err(json_element)(&element_result); fprintf(stderr, "Error parsing JSON: %s\n", json_error_to_string(error)); //return; } typed(json_element) element = result_unwrap(json_element)(&element_result); result(json_element) groups_json = json_object_find(element.value.as_object, "groups"); struct groups* groups_s; //json_print(&element, 2); // PRINT { typed(json_element) ss = result_unwrap(json_element)(&groups_json); typed(json_array) *arr = ss.value.as_array; groups_s = (struct groups*)malloc(sizeof(struct groups*) + arr->count * sizeof(char*)); groups_s->len = arr->count; for(int i = 0; i < arr->count; i++) { typed(json_element) element = arr->elements[i]; typed(json_element_value) value = element.value; struct group *group = malloc(sizeof(struct group)); group->id = memcpy_object_long(json_object_find(value.as_object, "id").inner.value.value.as_string); group->name = memcpy_object_long(json_object_find(value.as_object, "name").inner.value.value.as_string); group->devs = malloc(sizeof(struct devs)); groups_s->group[i] = group; } } result(json_element) SS = json_object_find(element.value.as_object, "devices"); if(result_is_err(json_element)(&SS)) { typed(json_error) error = result_unwrap_err(json_element)(&SS); fprintf(stderr, "Error getting element \"devices\": %s\n", json_error_to_string(error)); //return; } typed(json_element) ss = result_unwrap(json_element)(&SS); typed(json_array) *arr = ss.value.as_array; struct devs* devs = (struct devs*)malloc(sizeof(struct devs*) + arr->count * sizeof(char*)); for(int i = 0; i < arr->count; i++) { ////////printf("%s", "SAS_1\n"); typed(json_element) element = arr->elements[i]; typed(json_element_value) value = element.value; struct ya_dev_struct *data; data = malloc(sizeof(struct ya_dev_struct)); data->id = memcpy_object_long(json_object_find(value.as_object, "id").inner.value.value.as_string); data->name = memcpy_object_long(json_object_find(value.as_object, "name").inner.value.value.as_string); ////////printf("%s%s", data->name, "\n"); data->type = memcpy_object_long(json_object_find(value.as_object, "type").inner.value.value.as_string); data->room = memcpy_object_long(json_object_find(value.as_object, "room").inner.value.value.as_string); result(json_element) nn = json_object_find(value.as_object, "groups"); //struct ya_list_test* fam2; struct groups* fam2; if(result_is_err(json_element)(&nn)) { fam2 = NULL; } else{ typed(json_array) *arr2 = result_unwrap(json_element)(&nn).value.as_array; //fam2 = malloc(sizeof(fam2) + arr2->count * sizeof(char*)); //fam2 = malloc(sizeof(struct groups)); fam2 = (struct groups*)malloc(sizeof(struct groups*) + arr2->count * sizeof(struct groups*)); for(int i = 0; i != arr2->count; i++) { typed(json_element) element = arr2->elements[i]; typed(json_element_value) value = element.value; for (int ix = 0; ix != groups_s->len; ix++){ if (strcmp(groups_s->group[ix]->id, value.as_string) == 0){ groups_s->group[ix]->devs->dev[groups_s->group[ix]->devs->len] = data; groups_s->group[ix]->devs->len = groups_s->group[ix]->devs->len + 1; fam2->group[i] = groups_s->group[ix]; } } //fam2->id[i] = (char*) value.as_string; } fam2->len = arr2->count; } result(json_element) nn_2 = json_object_find(value.as_object, "capabilities"); struct capabilities_struct* fam3; if(result_is_err(json_element)(&nn_2)) { fam3 = NULL; } else{ typed(json_array) *arr3 = result_unwrap(json_element)(&nn_2).value.as_array; fam3 = malloc(sizeof(struct capabilities_struct)); typed(json_array) *arr = ss.value.as_array; fam3->range = NULL; fam3->color_setting = NULL; fam3->on_off = NULL; ////////printf("%s", "QQQ\n"); for(int i = 0; i < arr3->count; i++) { result(json_element) SS = json_object_find(arr3->elements[i].value.as_object, "type"); //result(json_element) SS_state = json_object_find(arr3->elements[i].value.as_object, "state"); //result(json_element) SS_state_instance = json_object_find(arr3->elements[i].value.as_object, "instance"); if (strcmp(SS.inner.value.value.as_string, "devices.capabilities.range") == 0){ struct range_struct* sasss; sasss = malloc(sizeof(struct range_struct)); result(json_element) SS_2 = json_object_find(arr3->elements[i].value.as_object, "parameters"); result(json_element) SS_3 = json_object_find(SS_2.inner.value.value.as_object, "range"); sasss->min = json_object_find(SS_3.inner.value.value.as_object, "min").inner.value.value.as_number.value.as_long; sasss->max = json_object_find(SS_3.inner.value.value.as_object, "max").inner.value.value.as_number.value.as_long; sasss->value = json_object_find(SS_3.inner.value.value.as_object, "precision").inner.value.value.as_number.value.as_long; fam3->range = sasss; } else if (strcmp(SS.inner.value.value.as_string, "devices.capabilities.color_setting") == 0){ struct temperature_k_struct* sasss; sasss = malloc(sizeof(struct temperature_k_struct)); struct color_setting_struct* sasss_2; sasss_2 = malloc(sizeof(struct color_setting_struct)); result(json_element) json_parameters = json_object_find(arr3->elements[i].value.as_object, "parameters"); result(json_element) json_temperature_k = json_object_find(json_parameters.inner.value.value.as_object, "temperature_k"); sasss->min = json_object_find(json_temperature_k.inner.value.value.as_object, "min").inner.value.value.as_number.value.as_long; sasss->max = json_object_find(json_temperature_k.inner.value.value.as_object, "max").inner.value.value.as_number.value.as_long; /////////json_print(&arr3->elements[i], 2); result(json_element) color_model = json_object_find(json_parameters.inner.value.value.as_object, "color_model"); if(result_is_err(json_element)(&color_model)) { printf("ID: %s \n", "0"); sasss_2->color = NULL; } else{ if (strcmp(memcpy_object_long(color_model.inner.value.value.as_string), "hsv") == 0){ printf("ID: %s \n", data->id); struct color_struct* color_struct_dat; color_struct_dat = malloc(sizeof(struct color_struct)); color_struct_dat->h = 100; color_struct_dat->s = 100; color_struct_dat->v = 100; //color_struct_dat->status = true; sasss_2->color = color_struct_dat; //sasss_2->color_status = 2; } else{ sasss_2->color = NULL; } } /* else{ if (strcmp(memcpy_object_long(color_model.inner.value.value.as_string), "hsv") == 0){ printf("ID: %s", data->id); } } */ //result(json_element) state = json_object_find(SS_2.inner.value.value.as_object, "state"); //if (strcmp(memcpy_object_long(state.inner.value.value.as_string), "temperature_k") == 0) result(json_element) nn_2_n = json_object_find(json_parameters.inner.value.value.as_object, "value"); if(result_is_err(json_element)(&nn_2_n)) { sasss->value = -1; } else{ sasss->value = nn_2_n.inner.value.value.as_number.value.as_long; } sasss_2->temperature_k = sasss; //sasss_2->color->h = 2; fam3->color_setting = sasss_2; } //else if (strcmp(SS_state.inner.value.value.as_string, "hsv") == 0){ // printf("ID: %s", data->id); //} else if (strcmp(SS.inner.value.value.as_string, "devices.capabilities.on_off") == 0){ struct on_off_struct* sasss; sasss = malloc(sizeof(struct on_off_struct)); result(json_element) SS_2 = json_object_find(arr3->elements[i].value.as_object, "state"); sasss->value = json_object_find(SS_2.inner.value.value.as_object, "value").inner.value.value.as_boolean; fam3->on_off = sasss; } } } ////////printf("%s", "SAS_4\n"); data->loop = api; //data->groups = NULL; data->groups = fam2; data->capabilities = fam3; devs->dev[i] = data; } devs->len = arr->count; //devs->mem = &element; //return devs; ////////printf("SAS\n"); api->data.devs = devs; api->data.groups = groups_s; //main_data->data->ya_devs = devs; ////////printf("SAS\n"); json_free(&element); } void list_devices(struct ya_api* api){ int len_groups = 0; char url[] = "https://api.iot.yandex.net/v1.0/user/info"; result(json_element) element_result = json_parse(ya_api_recvest(url, NULL, api->data.tocen)); if(result_is_err(json_element)(&element_result)) { typed(json_error) error = result_unwrap_err(json_element)(&element_result); fprintf(stderr, "Error parsing JSON: %s\n", json_error_to_string(error)); //return; } typed(json_element) element = result_unwrap(json_element)(&element_result); result(json_element) groups_json = json_object_find(element.value.as_object, "groups"); struct groups* groups_s; //json_print(&element, 2); // PRINT { typed(json_element) ss = result_unwrap(json_element)(&groups_json); typed(json_array) *arr = ss.value.as_array; groups_s = (struct groups*)malloc(sizeof(struct groups*) + arr->count * sizeof(char*)); groups_s->len = arr->count; for(int i = 0; i < arr->count; i++) { typed(json_element) element = arr->elements[i]; typed(json_element_type) type = element.type; typed(json_element_value) value = element.value; struct group *group; group = malloc(sizeof(struct group)); group->id = memcpy_object_long(json_object_find(value.as_object, "id").inner.value.value.as_string); group->name = memcpy_object_long(json_object_find(value.as_object, "name").inner.value.value.as_string); group->devs = malloc(sizeof(struct devs)); groups_s->group[i] = group; } } result(json_element) SS = json_object_find(element.value.as_object, "devices"); if(result_is_err(json_element)(&SS)) { typed(json_error) error = result_unwrap_err(json_element)(&SS); fprintf(stderr, "Error getting element \"devices\": %s\n", json_error_to_string(error)); //return; } typed(json_element) ss = result_unwrap(json_element)(&SS); typed(json_array) *arr = ss.value.as_array; struct devs* devs = (struct devs*)malloc(sizeof(struct devs*) + arr->count * sizeof(char*)); for(int i = 0; i < arr->count; i++) { ////////printf("%s", "SAS_1\n"); typed(json_element) element = arr->elements[i]; typed(json_element_type) type = element.type; typed(json_element_value) value = element.value; struct ya_dev_struct *data; data = malloc(sizeof(struct ya_dev_struct)); data->id = memcpy_object_long(json_object_find(value.as_object, "id").inner.value.value.as_string); data->name = memcpy_object_long(json_object_find(value.as_object, "name").inner.value.value.as_string); ////////printf("%s%s", data->name, "\n"); data->type = memcpy_object_long(json_object_find(value.as_object, "type").inner.value.value.as_string); data->room = memcpy_object_long(json_object_find(value.as_object, "room").inner.value.value.as_string); result(json_element) nn = json_object_find(value.as_object, "groups"); //struct ya_list_test* fam2; struct groups* fam2; if(result_is_err(json_element)(&nn)) { fam2 = NULL; } else{ typed(json_array) *arr2 = result_unwrap(json_element)(&nn).value.as_array; //fam2 = malloc(sizeof(fam2) + arr2->count * sizeof(char*)); //fam2 = malloc(sizeof(struct groups)); fam2 = (struct groups*)malloc(sizeof(struct groups*) + arr2->count * sizeof(struct groups*)); for(int i = 0; i != arr2->count; i++) { typed(json_element) element = arr2->elements[i]; typed(json_element_type) type = element.type; typed(json_element_value) value = element.value; for (int ix = 0; ix != groups_s->len; ix++){ if (strcmp(groups_s->group[ix]->id, value.as_string) == 0){ groups_s->group[ix]->devs->dev[groups_s->group[ix]->devs->len] = data; groups_s->group[ix]->devs->len = groups_s->group[ix]->devs->len + 1; fam2->group[i] = groups_s->group[ix]; } } //fam2->id[i] = (char*) value.as_string; } fam2->len = arr2->count; } result(json_element) nn_2 = json_object_find(value.as_object, "capabilities"); struct capabilities_struct* fam3; if(result_is_err(json_element)(&nn_2)) { fam3 = NULL; } else{ typed(json_array) *arr3 = result_unwrap(json_element)(&nn_2).value.as_array; fam3 = malloc(sizeof(struct capabilities_struct)); typed(json_array) *arr = ss.value.as_array; fam3->range = NULL; fam3->color_setting = NULL; fam3->on_off = NULL; ////////printf("%s", "QQQ\n"); for(int i = 0; i < arr3->count; i++) { result(json_element) SS = json_object_find(arr3->elements[i].value.as_object, "type"); //result(json_element) SS_state = json_object_find(arr3->elements[i].value.as_object, "state"); //result(json_element) SS_state_instance = json_object_find(arr3->elements[i].value.as_object, "instance"); if (strcmp(SS.inner.value.value.as_string, "devices.capabilities.range") == 0){ struct range_struct* sasss; sasss = malloc(sizeof(struct range_struct)); result(json_element) SS_2 = json_object_find(arr3->elements[i].value.as_object, "parameters"); result(json_element) SS_3 = json_object_find(SS_2.inner.value.value.as_object, "range"); sasss->min = json_object_find(SS_3.inner.value.value.as_object, "min").inner.value.value.as_number.value.as_long; sasss->max = json_object_find(SS_3.inner.value.value.as_object, "max").inner.value.value.as_number.value.as_long; sasss->value = json_object_find(SS_3.inner.value.value.as_object, "precision").inner.value.value.as_number.value.as_long; fam3->range = sasss; } else if (strcmp(SS.inner.value.value.as_string, "devices.capabilities.color_setting") == 0){ struct temperature_k_struct* sasss; sasss = malloc(sizeof(struct temperature_k_struct)); struct color_setting_struct* sasss_2; sasss_2 = malloc(sizeof(struct color_setting_struct)); result(json_element) SS_2 = json_object_find(arr3->elements[i].value.as_object, "parameters"); result(json_element) state = json_object_find(arr3->elements[i].value.as_object, "state"); result(json_element) SS_3 = json_object_find(SS_2.inner.value.value.as_object, "temperature_k"); sasss->min = json_object_find(SS_3.inner.value.value.as_object, "min").inner.value.value.as_number.value.as_long; sasss->max = json_object_find(SS_3.inner.value.value.as_object, "max").inner.value.value.as_number.value.as_long; /////////json_print(&arr3->elements[i], 2); result(json_element) color_model = json_object_find(SS_2.inner.value.value.as_object, "color_model"); if(result_is_err(json_element)(&color_model)) { //printf("ID: %s \n", "0"); sasss_2->color = NULL; } else{ if (strcmp(memcpy_object_long(color_model.inner.value.value.as_string), "hsv") == 0){ //printf("ID: %s \n", data->id); struct color_struct* color_struct_dat; color_struct_dat = malloc(sizeof(struct color_struct)); color_struct_dat->h = -1; color_struct_dat->s = -1; color_struct_dat->v = -1; //color_struct_dat->status = true; sasss_2->color = color_struct_dat; } else{ sasss_2->color = NULL; } } /* else{ if (strcmp(memcpy_object_long(color_model.inner.value.value.as_string), "hsv") == 0){ printf("ID: %s", data->id); } } */ //result(json_element) state = json_object_find(SS_2.inner.value.value.as_object, "state"); //if (strcmp(memcpy_object_long(state.inner.value.value.as_string), "temperature_k") == 0) result(json_element) instance = json_object_find(state.inner.value.value.as_object, "instance"); if(result_is_err(json_element)(&instance)) { } else{ if (strcmp(instance.inner.value.value.as_string, "temperature_k") == 0){ //printf("K\n"); result(json_element) value_temperature_k= json_object_find(state.inner.value.value.as_object, "value"); if(result_is_err(json_element)(&value_temperature_k)) { sasss->value = -1; //result(json_element) nn_2_naa = json_object_find(SS_2.inner.value.value.as_object, "value"); } else{ sasss->value = value_temperature_k.inner.value.value.as_number.value.as_long; sasss_2->color_target = 1; } } else if (strcmp(instance.inner.value.value.as_string, "hsv") == 0){ //printf("hsv\n"); result(json_element) value_vsh= json_object_find(state.inner.value.value.as_object, "value"); sasss_2->color->h = json_object_find(value_vsh.inner.value.value.as_object, "h").inner.value.value.as_number.value.as_long; sasss_2->color->s = json_object_find(value_vsh.inner.value.value.as_object, "s").inner.value.value.as_number.value.as_long; sasss_2->color->v = json_object_find(value_vsh.inner.value.value.as_object, "v").inner.value.value.as_number.value.as_long; //printf("VSH = %i %i %i\n", sasss_2->color->v,sasss_2->color->s,sasss_2->color->h); sasss_2->color_target = 2; } } sasss_2->temperature_k = sasss; //sasss_2->color->h = 2; fam3->color_setting = sasss_2; } //else if (strcmp(SS_state.inner.value.value.as_string, "hsv") == 0){ // printf("ID: %s", data->id); //} else if (strcmp(SS.inner.value.value.as_string, "devices.capabilities.on_off") == 0){ struct on_off_struct* sasss; sasss = malloc(sizeof(struct on_off_struct)); result(json_element) SS_2 = json_object_find(arr3->elements[i].value.as_object, "state"); sasss->value = json_object_find(SS_2.inner.value.value.as_object, "value").inner.value.value.as_boolean; fam3->on_off = sasss; } } } ////////printf("%s", "SAS_4\n"); data->loop = api; //data->groups = NULL; data->groups = fam2; data->capabilities = fam3; devs->dev[i] = data; } devs->len = arr->count; //devs->mem = &element; //return devs; ////////printf("SAS\n"); api->data.devs = devs; api->data.groups = groups_s; //main_data->data->ya_devs = devs; ////////printf("SAS\n"); json_free(&element); } char* ya_dev_kelvin(struct ya_dev_struct* dev, int k){ if (dev->capabilities != NULL) if (dev->capabilities->color_setting != NULL){ char* s = yan_kelvin_lampochka(dev->id, k); return ya_api_recvest("https://api.iot.yandex.net/v1.0/devices/actions", s, dev->loop->data.tocen); } return "ERROR"; } char* ya_dev_hsv(struct ya_dev_struct* dev, int h, char s, char v){ if (dev->capabilities != NULL) if (dev->capabilities->color_setting != NULL){ char* aaa = yan_hsv_lampochka(dev->id, h,s,v); return ya_api_recvest("https://api.iot.yandex.net/v1.0/devices/actions", aaa, dev->loop->data.tocen); } return "ERROR"; } char* ya_dev_brightness(struct ya_dev_struct* dev, int b){ if (dev->capabilities != NULL) if (dev->capabilities->range != NULL){ char* s = yan_brightness(dev->id, b); return ya_api_recvest("https://api.iot.yandex.net/v1.0/devices/actions", s, dev->loop->data.tocen); } return "ERROR"; } ya_list* list_devices_id(struct ya_api api){ char url[] = "https://api.iot.yandex.net/v1.0/user/info"; result(json_element) element_result = json_parse(ya_api_recvest(url, NULL, api.data.tocen)); if(result_is_err(json_element)(&element_result)) { typed(json_error) error = result_unwrap_err(json_element)(&element_result); fprintf(stderr, "Error parsing JSON: %s\n", json_error_to_string(error)); //return; } typed(json_element) element = result_unwrap(json_element)(&element_result); result(json_element) SS = json_object_find(element.value.as_object, "devices"); if(result_is_err(json_element)(&SS)) { typed(json_error) error = result_unwrap_err(json_element)(&SS); fprintf(stderr, "Error getting element \"devices\": %s\n", json_error_to_string(error)); //return; } typed(json_element) ss = result_unwrap(json_element)(&SS); typed(json_array) *arr = ss.value.as_array; ya_list* fam1 = (ya_list*)malloc(sizeof(ya_list*) + arr->count * sizeof(char*)); for(int i = 0; i < arr->count; i++) { typed(json_element) element = arr->elements[i]; typed(json_element_type) type = element.type; typed(json_element_value) value = element.value; result(json_element) id = json_object_find(value.as_object, "id"); fam1->id[i] = (char*) id.inner.value.value.as_string; } fam1->len = arr->count; return fam1; } //// asdsdsd void list(){ struct MemoryStruct chunk; chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ chunk.size = 0; char url_2[100]; char url[] = "https://api.iot.yandex.net/v1.0/user/info"; struct curl_slist *headers=NULL; headers = curl_slist_append(headers, "TOKEN"); headers = curl_slist_append(headers, "Content-Type: application/json"); CURL* handle; CURLcode response; handle = curl_easy_init(); curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk); curl_easy_perform(handle); printf("%s\n", chunk.memory); result(json_element) element_result = json_parse(chunk.memory); if(result_is_err(json_element)(&element_result)) { typed(json_error) error = result_unwrap_err(json_element)(&element_result); fprintf(stderr, "Error parsing JSON: %s\n", json_error_to_string(error)); return; } typed(json_element) element = result_unwrap(json_element)(&element_result); result(json_element) SS = json_object_find(element.value.as_object, "devices"); if(result_is_err(json_element)(&SS)) { typed(json_error) error = result_unwrap_err(json_element)(&SS); fprintf(stderr, "Error getting element \"devices\": %s\n", json_error_to_string(error)); return; } typed(json_element) ss = result_unwrap(json_element)(&SS); typed(json_array) *arr = ss.value.as_array; int i; for(i = 0; i < arr->count; i++) { typed(json_element) element = arr->elements[i]; typed(json_element_type) type = element.type; typed(json_element_value) value = element.value; result(json_element) id = json_object_find(value.as_object, "id"); result(json_element) name = json_object_find(value.as_object, "name"); printf("name: %s \tid: %s\n", name.inner.value.value.as_string, id.inner.value.value.as_string); } //json_print(&ss, 2); //typed(json_entry) entry = SS.value.as_object->entries[0]; //entry.element.type; /* result(json_element) element_result = json_parse(chunk.memory); typed(json_element) element = result_unwrap(json_element)(&element_result); json_print(&element, 2); result(json_element) hello_element_result = json_object_find(element.value.as_object, "devices"); typed(json_element) hello_element = result_unwrap(json_element)(&hello_element_result); printf("SAS\n"); typed(json_entry) entry = hello_element.value.as_object->entries[0]; int i; typed(json_object) *obj = hello_element.value.as_object; for(i = 0; i < obj->count; i++) { typed(json_entry) entry = obj->entries[i]; typed(json_string) key = entry.key; typed(json_element_type) type = entry.element.type; typed(json_element_value) value = entry.element.value; // Do something with `key`, `type` and `value` } //printf("\n%s\n", obj->entries[0]); //result(json_element) json_object_find(typed(json_object) * object, typed(json_string) key); */ } struct ya_api ya_api_create(char* tocen){ struct ya_api api; api.data.tocen = tocen; api.list_devices = list_devices; api.on_off = ya_dev_on_off; api.list_devices_id = list_devices_id; api.kelvin = ya_dev_kelvin; api.hsv = ya_dev_hsv; api.devs_free = ya_devs_free; api.brightness = ya_dev_brightness; return api; }