#include #include #include #include #include #include #include #include #include "api_v1_controllers.h" using namespace api::v1; void controllers::get_relays_all(const HttpRequestPtr &req, std::function &&callback, const std::string& controller_id_str) { uuid_t controller_id; if(uuid_parse(controller_id_str.c_str(), controller_id)) { auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k400BadRequest); callback(resp); return; } relay_dbo **all_controller_relays = relay_dbo::get_by_simple("controller_id", (void *) controller_id, (intptr_t) sqlite3_bind_blob, sizeof(uuid_t)); Json::Value all_relays_json(Json::arrayValue); for(int i = 0; all_controller_relays[i] != nullptr; i++) { all_relays_json.append(all_controller_relays[i]->to_json()); } auto resp = HttpResponse::newHttpJsonResponse(all_relays_json); callback(resp); relay_dbo::free_list(all_controller_relays); } void controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req, std::function &&callback, const std::string& controller_id_str, int relay_num) { uuid_t controller_id; if(uuid_parse(controller_id_str.c_str(), controller_id)) { auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k400BadRequest); callback(resp); return; } relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num); if(relay) { auto resp = HttpResponse::newHttpJsonResponse(relay->to_json()); callback(resp); delete relay; } else { auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k404NotFound); callback(resp); } } void controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, std::function &&callback, const std::string& controller_id_str, int relay_num) { uuid_t controller_id; if(uuid_parse(controller_id_str.c_str(), controller_id)) { LOG_DEBUG << "bad uuid"; auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k400BadRequest); callback(resp); return; } if(!relay_dbo::valid_num_for_controller(controller_id, relay_num)) { LOG_DEBUG << "invalid num for controller"; auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k400BadRequest); callback(resp); return; } Json::Value body = *req->getJsonObject(); uuid_t active_schedule_id; if(schedule_dbo::parse_uid(body["active_schedule"]["id"].asCString(), active_schedule_id)) { LOG_DEBUG << "bad active_schedule uuid"; auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k400BadRequest); callback(resp); return; } uuid_t schedules_ids[7]; for(int i = 0; i < 7; ++i) { if(schedule_dbo::parse_uid(body["schedules"][i]["id"].asCString(), schedules_ids[i])) { LOG_DEBUG << "parse_uid failed for schedule " << i; auto res = drogon::HttpResponse::newHttpResponse(); res->setStatusCode(k400BadRequest); callback(res); return; } } relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num); schedule_dbo **schedule_list = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); schedule_dbo *active_schedule = schedule_list[0]; free(schedule_list); schedule_dbo *schedules[7]; for(int i = 0; i < 7; ++i) { schedule_list = schedule_dbo::get_by_simple("uid", schedules_ids[i], (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); schedules[i] = schedule_list[0]; free(schedule_list); } bool db_action_result; if(relay) { strncpy(relay->name, body["name"].asCString(), 127); uuid_copy(relay->controller_id, controller_id); for(int i = 0; i < 7; ++i) { relay->schedules[i] = schedules[i]; } relay->schedules[helpers::get_day_of_week()] = active_schedule; relay->active_schedule = active_schedule; db_action_result = relay->update(); } else { relay = new relay_dbo(); relay->number = relay_num; strncpy(relay->name, body["name"].asCString(), 127); for(int i = 0; i < 7; ++i) { relay->schedules[i] = schedules[i]; } relay->schedules[helpers::get_day_of_week()] = active_schedule; relay->active_schedule = active_schedule; uuid_copy(relay->controller_id, controller_id); db_action_result = relay->insert(); } if(!db_action_result) { auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k500InternalServerError); callback(resp); } else { auto schedules = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); auto controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); uint16_t *periods = schedules[0]->periods->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); 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_finish_map(&writer); // finish writing if (mpack_writer_destroy(&writer) != mpack_ok) { LOG_ERROR << "an error occurred encoding the data"; auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k500InternalServerError); callback(resp); return; } 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); } delete relay; }