add: logging
fix: rename branch
This commit is contained in:
		
							parent
							
								
									9e5718db43
								
							
						
					
					
						commit
						2f9be90ec1
					
				
					 5 changed files with 63 additions and 49 deletions
				
			
		
							
								
								
									
										16
									
								
								.drone.yml
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								.drone.yml
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -8,21 +8,12 @@ steps:
 | 
			
		|||
- name: download-controller
 | 
			
		||||
  image: plugins/download
 | 
			
		||||
  settings:
 | 
			
		||||
    source: https://git.serguzim.me/emgauwa/controller/archive/master.tar.gz
 | 
			
		||||
    source: https://git.serguzim.me/emgauwa/controller/archive/main.tar.gz
 | 
			
		||||
    destination: emgauwa-controller.tar.gz
 | 
			
		||||
  when:
 | 
			
		||||
    ref:
 | 
			
		||||
    - refs/heads/master
 | 
			
		||||
    - refs/heads/main
 | 
			
		||||
    - refs/tags/v*
 | 
			
		||||
- name: download-controller-testing
 | 
			
		||||
  image: plugins/download
 | 
			
		||||
  settings:
 | 
			
		||||
    source: https://git.serguzim.me/emgauwa/controller/archive/testing.tar.gz
 | 
			
		||||
    destination: emgauwa-controller.tar.gz
 | 
			
		||||
  when:
 | 
			
		||||
    ref:
 | 
			
		||||
    - refs/heads/testing
 | 
			
		||||
    - refs/tags/testing-*
 | 
			
		||||
 | 
			
		||||
- name: build-controller
 | 
			
		||||
  image: serguzim/emgauwa-builder
 | 
			
		||||
| 
						 | 
				
			
			@ -61,6 +52,5 @@ steps:
 | 
			
		|||
trigger:
 | 
			
		||||
  ref:
 | 
			
		||||
    include:
 | 
			
		||||
    - refs/heads/master
 | 
			
		||||
    - refs/heads/testing
 | 
			
		||||
    - refs/heads/main
 | 
			
		||||
    - refs/tags/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -93,6 +93,8 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
 | 
			
		|||
            LOGGER_DEBUG("failed to normalize uri %.*s\n", hm->uri.len, hm->uri.p);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        LOGGER_DEBUG("requested file: %.*s\n", hm->uri.len, hm->uri.p);
 | 
			
		||||
 | 
			
		||||
        char *request_file_org = malloc(sizeof(char) * hm->uri.len);
 | 
			
		||||
        strncpy(request_file_org, hm->uri.p + 1, hm->uri.len);
 | 
			
		||||
        request_file_org[hm->uri.len - 1] = '\0';
 | 
			
		||||
| 
						 | 
				
			
			@ -103,7 +105,6 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
 | 
			
		|||
            ++request_file;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        LOGGER_DEBUG("%s\n", request_file);
 | 
			
		||||
        char *request_file_path = malloc(sizeof(char) * (strlen(request_file) + strlen(global_config.content_dir) + 2));
 | 
			
		||||
        sprintf(request_file_path, "%s/%s", global_config.content_dir, request_file);
 | 
			
		||||
        int access_result = access(request_file_path, R_OK);
 | 
			
		||||
| 
						 | 
				
			
			@ -123,31 +124,27 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(!endpoint->func)
 | 
			
		||||
    if(endpoint->method == HTTP_METHOD_OPTIONS)
 | 
			
		||||
    {
 | 
			
		||||
        if(endpoint->method == HTTP_METHOD_OPTIONS)
 | 
			
		||||
        {
 | 
			
		||||
            char options_header[256]; // TODO make more generic
 | 
			
		||||
            sprintf(options_header, "Allow: OPTIONS%s%s%s%s",
 | 
			
		||||
                    endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
 | 
			
		||||
                    endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
 | 
			
		||||
                    endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
 | 
			
		||||
                    endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : ""
 | 
			
		||||
                   );
 | 
			
		||||
            char *extra_headers = add_extra_headers(options_header);
 | 
			
		||||
            mg_send_head(nc, 204, 0, extra_headers);
 | 
			
		||||
            free(extra_headers);
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            mg_send_head(nc, 501, 0, "Content-Type: text/plain");
 | 
			
		||||
        }
 | 
			
		||||
        LOGGER_DEBUG("sending options for %s\n", endpoint->full_route);
 | 
			
		||||
        char options_header[128];
 | 
			
		||||
        sprintf(options_header, "Allow: OPTIONS%s%s%s%s",
 | 
			
		||||
                endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
 | 
			
		||||
                endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
 | 
			
		||||
                endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
 | 
			
		||||
                endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : ""
 | 
			
		||||
               );
 | 
			
		||||
        char *extra_headers = add_extra_headers(options_header);
 | 
			
		||||
        mg_send_head(nc, 204, 0, extra_headers);
 | 
			
		||||
        free(extra_headers);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        LOGGER_DEBUG("calling endpoint function %p\n", endpoint->func);
 | 
			
		||||
        LOGGER_DEBUG("calling endpoint function for route %s\n", endpoint->full_route);
 | 
			
		||||
        endpoint->func(nc, hm, endpoint->args, &response);
 | 
			
		||||
        LOGGER_DEBUG("sending response to %p\n", nc);
 | 
			
		||||
        char addr[32];
 | 
			
		||||
        mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
 | 
			
		||||
        LOGGER_DEBUG("sending response to %s\n", addr);
 | 
			
		||||
        send_response(nc, &response);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -175,7 +172,9 @@ handler_http(struct mg_connection *nc, int ev, void *p)
 | 
			
		|||
    }
 | 
			
		||||
    if(ev == MG_EV_HTTP_REQUEST)
 | 
			
		||||
    {
 | 
			
		||||
        LOGGER_DEBUG("new http request (%p)\n", nc);
 | 
			
		||||
        char addr[32];
 | 
			
		||||
        mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
 | 
			
		||||
        LOGGER_DEBUG("new http request from %s\n", addr);
 | 
			
		||||
        struct http_message *hm = (struct http_message*)p;
 | 
			
		||||
        handle_http_request(nc, hm);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -186,6 +186,11 @@ controller_free_list(controller_t **controllers)
 | 
			
		|||
cJSON*
 | 
			
		||||
controller_to_json(controller_t *controller)
 | 
			
		||||
{
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    uuid_unparse(controller->uid, uuid_str);
 | 
			
		||||
 | 
			
		||||
    LOGGER_DEBUG("JSONifying controller %s\n", uuid_str);
 | 
			
		||||
 | 
			
		||||
    cJSON *json = cJSON_CreateObject();
 | 
			
		||||
 | 
			
		||||
    cJSON *json_name = cJSON_CreateString(controller->name);
 | 
			
		||||
| 
						 | 
				
			
			@ -196,8 +201,6 @@ controller_to_json(controller_t *controller)
 | 
			
		|||
    }
 | 
			
		||||
    cJSON_AddItemToObject(json, "name", json_name);
 | 
			
		||||
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    uuid_unparse(controller->uid, uuid_str);
 | 
			
		||||
    cJSON *json_id = cJSON_CreateString(uuid_str);
 | 
			
		||||
    if(json_name == NULL)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -253,6 +256,7 @@ controller_to_json(controller_t *controller)
 | 
			
		|||
controller_t*
 | 
			
		||||
controller_get_by_id(int id)
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting controller [id=%d] from database\n", id);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM controllers WHERE id = ?1;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -269,6 +273,9 @@ controller_get_by_id(int id)
 | 
			
		|||
controller_t*
 | 
			
		||||
controller_get_by_uid(uuid_t uid)
 | 
			
		||||
{
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    uuid_unparse(uid, uuid_str);
 | 
			
		||||
    LOGGER_DEBUG("getting controller [uid=%s] from database\n", uuid_str);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM controllers WHERE uid = ?1;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -285,6 +292,7 @@ controller_get_by_uid(uuid_t uid)
 | 
			
		|||
controller_t**
 | 
			
		||||
controller_get_all()
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting all controllers from database\n");
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM controllers;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -196,6 +196,17 @@ relay_free_list(relay_t **relays)
 | 
			
		|||
cJSON*
 | 
			
		||||
relay_to_json(relay_t *relay)
 | 
			
		||||
{
 | 
			
		||||
    controller_t *controller = controller_get_by_id(relay->controller_id);
 | 
			
		||||
    if(!controller)
 | 
			
		||||
    {
 | 
			
		||||
        LOGGER_WARNING("failed to get controller\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    uuid_unparse(controller->uid, uuid_str);
 | 
			
		||||
    LOGGER_DEBUG("JSONifying relay %s:%d\n", uuid_str, relay->number);
 | 
			
		||||
 | 
			
		||||
    relay_reload_active_schedule(relay);
 | 
			
		||||
 | 
			
		||||
    cJSON *json = cJSON_CreateObject();
 | 
			
		||||
| 
						 | 
				
			
			@ -218,16 +229,6 @@ relay_to_json(relay_t *relay)
 | 
			
		|||
    }
 | 
			
		||||
    cJSON_AddItemToObject(json, "name", json_name);
 | 
			
		||||
 | 
			
		||||
    controller_t *controller = controller_get_by_id(relay->controller_id);
 | 
			
		||||
    if(!controller)
 | 
			
		||||
    {
 | 
			
		||||
        LOGGER_WARNING("failed to get controller\n");
 | 
			
		||||
        cJSON_Delete(json);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    uuid_unparse(controller->uid, uuid_str);
 | 
			
		||||
    cJSON *json_controller_id = cJSON_CreateString(uuid_str);
 | 
			
		||||
    if(json_controller_id == NULL)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -340,6 +341,7 @@ relay_get_by_uid(uuid_t uid)
 | 
			
		|||
relay_t**
 | 
			
		||||
relay_get_all()
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting all relays from database\n");
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM relays;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -350,6 +352,7 @@ relay_get_all()
 | 
			
		|||
relay_t**
 | 
			
		||||
relay_get_with_schedule(int schedule_id)
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting relays [schedule_id=%d] from database\n", schedule_id);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT DISTINCT relays.* FROM relays INNER JOIN junction_relay_schedule ON relays.id == junction_relay_schedule.relay_id WHERE junction_relay_schedule.schedule_id = ?1;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -361,6 +364,7 @@ relay_get_with_schedule(int schedule_id)
 | 
			
		|||
relay_t*
 | 
			
		||||
relay_get_for_controller(int controller_id, int relay_num)
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting relay [controller_id=%d, relay_num=%d] from database\n", controller_id, relay_num);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM relays WHERE controller_id = ?1 AND number = ?2;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -379,6 +383,7 @@ relay_get_for_controller(int controller_id, int relay_num)
 | 
			
		|||
relay_t**
 | 
			
		||||
relay_get_by_controller_id(int controller_id)
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting relays [controller_id=%d] from database\n", controller_id);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM relays WHERE controller_id = ?1;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -218,6 +218,11 @@ schedule_periods_to_blob(schedule_t *schedule)
 | 
			
		|||
cJSON*
 | 
			
		||||
schedule_to_json(schedule_t *schedule)
 | 
			
		||||
{
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    schedule_uid_unparse(schedule->uid, uuid_str);
 | 
			
		||||
 | 
			
		||||
    LOGGER_DEBUG("JSONifying schedule %s\n", uuid_str);
 | 
			
		||||
 | 
			
		||||
    cJSON *json = cJSON_CreateObject();
 | 
			
		||||
 | 
			
		||||
    cJSON *json_name = cJSON_CreateString(schedule->name);
 | 
			
		||||
| 
						 | 
				
			
			@ -228,8 +233,6 @@ schedule_to_json(schedule_t *schedule)
 | 
			
		|||
    }
 | 
			
		||||
    cJSON_AddItemToObject(json, "name", json_name);
 | 
			
		||||
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    schedule_uid_unparse(schedule->uid, uuid_str);
 | 
			
		||||
    cJSON *json_id = cJSON_CreateString(uuid_str);
 | 
			
		||||
    if(json_name == NULL)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -337,6 +340,7 @@ schedule_get_by_id_or_off(int id)
 | 
			
		|||
schedule_t*
 | 
			
		||||
schedule_get_by_id(int id)
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting schedule [id=%d] from database\n", id);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM schedules WHERE id = ?1;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -353,6 +357,9 @@ schedule_get_by_id(int id)
 | 
			
		|||
schedule_t*
 | 
			
		||||
schedule_get_by_uid_or_off(uuid_t uid)
 | 
			
		||||
{
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    schedule_uid_unparse(uid, uuid_str);
 | 
			
		||||
    LOGGER_DEBUG("getting schedule [uid=%s] or off from database\n", uuid_str);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM schedules WHERE uid = ?1;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -378,6 +385,9 @@ schedule_get_by_uid_or_off(uuid_t uid)
 | 
			
		|||
schedule_t*
 | 
			
		||||
schedule_get_by_uid(uuid_t uid)
 | 
			
		||||
{
 | 
			
		||||
    char uuid_str[UUID_STR_LEN];
 | 
			
		||||
    schedule_uid_unparse(uid, uuid_str);
 | 
			
		||||
    LOGGER_DEBUG("getting schedule [uid=%s] from database\n", uuid_str);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM schedules WHERE uid = ?1;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -394,6 +404,7 @@ schedule_get_by_uid(uuid_t uid)
 | 
			
		|||
schedule_t**
 | 
			
		||||
schedule_get_relay_weekdays(int relay_id)
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting schedules [relay_id=%d] from database\n", relay_id);
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT schedules.* FROM schedules INNER JOIN junction_relay_schedule ON schedules.id == junction_relay_schedule.schedule_id WHERE junction_relay_schedule.relay_id = ?1 ORDER BY junction_relay_schedule.weekday ASC", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			@ -405,6 +416,7 @@ schedule_get_relay_weekdays(int relay_id)
 | 
			
		|||
schedule_t**
 | 
			
		||||
schedule_get_all()
 | 
			
		||||
{
 | 
			
		||||
    LOGGER_DEBUG("getting all schedules from database\n");
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM schedules;", -1, &stmt, NULL);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue