diff --git a/controllers/api_v1_controllers.cc b/controllers/api_v1_controllers.cc index 0fabb7e..c586224 100644 --- a/controllers/api_v1_controllers.cc +++ b/controllers/api_v1_controllers.cc @@ -142,10 +142,13 @@ controllers::put_one_by_id(const HttpRequestPtr &req, std::functionname); + mpack_finish_map(&writer); // finish writing diff --git a/controllers/api_v1_controllers_discover.cc b/controllers/api_v1_controllers_discover.cc index c87b1b0..47d7f28 100644 --- a/controllers/api_v1_controllers_discover.cc +++ b/controllers/api_v1_controllers_discover.cc @@ -85,7 +85,7 @@ void controllers::post_discover(const HttpRequestPtr &req, std::functionperiods->to_blob(); - uint16_t periods_count = periods[0]; - char* data; size_t size; mpack_writer_t writer; mpack_writer_init_growable(&writer, &data, &size); - mpack_start_map(&writer, 5); + // 3 = code, relay num, relay name, schedules(array) + mpack_start_map(&writer, 3); + mpack_write_uint(&writer, COMMAND_MAPPING_CODE); mpack_write_u8(&writer, config::command_code_set_schedule); + mpack_write_uint(&writer, COMMAND_MAPPING_RELAY_NUM); - mpack_write_u8(&writer, relay_num); - mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_COUNT); - mpack_write_u16(&writer, periods_count); - mpack_write_uint(&writer, COMMAND_MAPPING_SCHEDULE_ID); - mpack_write_bin(&writer, (char*)schedules[0]->uid, sizeof(uuid_t)); - mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_BLOB); - // periods + 1 to skip length in periods[0] - // periods_count * 2 because each uint16_t is a timestamp. 2 are start and end - mpack_write_bin(&writer, (char*)(periods + 1), sizeof(uint16_t) * periods_count * 2); + mpack_write_u8(&writer, relay->number); + + mpack_write_uint(&writer, COMMAND_MAPPING_SCHEDULES_ARRAY); + // 7 = days of week + mpack_start_array(&writer, 7); + for(int i = 0; i < 7; ++i) + { + uint16_t *periods = relay->schedules[i]->periods->to_blob(); + uint16_t periods_count = periods[0]; + + // 3 = code, relaynum, schedules(array) + mpack_start_map(&writer, 3); + + mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_COUNT); + mpack_write_u16(&writer, periods_count); + + mpack_write_uint(&writer, COMMAND_MAPPING_SCHEDULE_ID); + mpack_write_bin(&writer, (char*)schedules[0]->uid, sizeof(uuid_t)); + + mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_BLOB); + // periods + 1 to skip length in periods[0] + // periods_count * 2 because each uint16_t is a timestamp. 2 are start and end + mpack_write_bin(&writer, (char*)(periods + 1), sizeof(uint16_t) * periods_count * 2); + + mpack_finish_map(&writer); + + free(periods); + } + mpack_finish_array(&writer); + mpack_finish_map(&writer); // finish writing @@ -212,12 +232,9 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, controllers[0]->command(config::command_code_set_name, data, size); - free(periods); - auto resp = HttpResponse::newHttpJsonResponse(relay->to_json()); callback(resp); - schedule_dbo::free_list(schedules); controller_dbo::free_list(controllers); } diff --git a/enums.h b/enums.h index b367307..d773c08 100644 --- a/enums.h +++ b/enums.h @@ -6,9 +6,10 @@ enum control_mapping COMMAND_MAPPING_CODE = 0, COMMAND_MAPPING_NAME = 1, COMMAND_MAPPING_RELAY_NUM = 2, - COMMAND_MAPPING_SCHEDULE_ID = 3, - COMMAND_MAPPING_PERIODS_COUNT = 4, - COMMAND_MAPPING_PERIODS_BLOB = 5, + COMMAND_MAPPING_SCHEDULES_ARRAY = 3, + COMMAND_MAPPING_SCHEDULE_ID = 4, + COMMAND_MAPPING_PERIODS_COUNT = 5, + COMMAND_MAPPING_PERIODS_BLOB = 6, }; #endif /* CORE_ENUM_H */