add: weekly schedules support

This commit is contained in:
Tobias Reisinger 2020-04-25 00:49:32 +02:00
parent 701f01c495
commit 5b508b3735
4 changed files with 45 additions and 26 deletions

View file

@ -142,10 +142,13 @@ controllers::put_one_by_id(const HttpRequestPtr &req, std::function<void(const H
// write the example on the msgpack homepage
mpack_start_map(&writer, 2);
mpack_write_uint(&writer, COMMAND_MAPPING_CODE);
mpack_write_u8(&writer, config::command_code_set_name);
mpack_write_uint(&writer, COMMAND_MAPPING_NAME);
mpack_write_cstr(&writer, controllers[0]->name);
mpack_finish_map(&writer);
// finish writing

View file

@ -85,7 +85,7 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
continue;
}
char *answer_payload = (char*)malloc((payload_length + 1));
char *answer_payload = (char*)malloc((payload_length));
ssize_t bytes_transferred;
if((bytes_transferred = recv(client_fd, answer_payload, payload_length, 0)) <= 0)
@ -104,21 +104,18 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
}
uuid_t discovered_id;
int id_size = sizeof(uuid_t);
mpack_tree_t tree;
mpack_tree_init_data(&tree, answer_payload, payload_length);
mpack_tree_parse(&tree);
mpack_node_t root = mpack_tree_root(&tree);
memcpy(discovered_id, mpack_node_data(mpack_node_map_uint(root, DISCOVERY_MAPPING_ID)), id_size);
memcpy(discovered_id, mpack_node_data(mpack_node_map_uint(root, DISCOVERY_MAPPING_ID)), sizeof(uuid_t));
uint32_t discovered_command_port = mpack_node_u16(mpack_node_map_uint(root, DISCOVERY_MAPPING_COMMAND_PORT));
uint16_t discovered_command_port = mpack_node_u16(mpack_node_map_uint(root, DISCOVERY_MAPPING_COMMAND_PORT));
uint8_t discovered_relay_count = mpack_node_u8(mpack_node_map_uint(root, DISCOVERY_MAPPING_RELAY_COUNT));
const char *discovered_name = mpack_node_str(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
mpack_tree_destroy(&tree);
bool found_discovered_in_list = false;
for(int i = 0; known_controllers[i] != nullptr; i++)
@ -156,6 +153,7 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
discovered_controller.insert();
}
mpack_tree_destroy(&tree);
free(answer_payload);
discover_answer_buf[0] = config::discover_code_accept;

View file

@ -175,30 +175,50 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
}
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);
// 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_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);
}

View file

@ -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 */