add: forward schedule updates to relays
This commit is contained in:
		
							parent
							
								
									574a384d2f
								
							
						
					
					
						commit
						606f2483a5
					
				
					 5 changed files with 75 additions and 18 deletions
				
			
		| 
						 | 
				
			
			@ -2,8 +2,11 @@
 | 
			
		|||
#include <constants.h>
 | 
			
		||||
#include <endpoints/api_v1_schedules.h>
 | 
			
		||||
#include <logger.h>
 | 
			
		||||
#include <command.h>
 | 
			
		||||
#include <models/junction_tag.h>
 | 
			
		||||
#include <models/junction_relay_schedule.h>
 | 
			
		||||
#include <models/schedule.h>
 | 
			
		||||
#include <models/relay.h>
 | 
			
		||||
#include <models/tag.h>
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
| 
						 | 
				
			
			@ -153,6 +156,16 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
 | 
			
		|||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int *relays = junction_relay_schedule_get_relays_ids(schedule->id);
 | 
			
		||||
    for(int i = 0; relays[i] != 0; ++i)
 | 
			
		||||
    {
 | 
			
		||||
        relay_t *relay = relay_get_by_id(relays[i]);
 | 
			
		||||
        if(relay)
 | 
			
		||||
        {
 | 
			
		||||
            command_set_relay_schedule(relay);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON *json_tag;
 | 
			
		||||
    cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
 | 
			
		||||
    if(cJSON_IsArray(json_tags))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,12 +8,14 @@ handler_connection(struct mg_connection *c, int ev, void *p)
 | 
			
		|||
{
 | 
			
		||||
    if (ev == MG_EV_HTTP_REQUEST)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_DEBUG("new http request\n");
 | 
			
		||||
        struct http_message *hm = (struct http_message *) p;
 | 
			
		||||
        LOG_DEBUG("new http request for %.*s\n", hm->uri.len, hm->uri.p);
 | 
			
		||||
 | 
			
		||||
        endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method);
 | 
			
		||||
 | 
			
		||||
        if(endpoint && endpoint->func)
 | 
			
		||||
        {
 | 
			
		||||
            if(endpoint->func)
 | 
			
		||||
            {
 | 
			
		||||
                endpoint->func(c, endpoint->args, p);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -29,6 +31,11 @@ handler_connection(struct mg_connection *c, int ev, void *p)
 | 
			
		|||
            {
 | 
			
		||||
                mg_send_head(c, 501, 0, "Content-Type: text/plain");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            mg_send_head(c, 500, 0, "Content-Type: text/plain");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p);
 | 
			
		||||
        //mg_printf(c, "%.*s", (int)hm->body.len, hm->body.p);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,9 @@
 | 
			
		|||
int
 | 
			
		||||
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id);
 | 
			
		||||
 | 
			
		||||
int*
 | 
			
		||||
junction_relay_schedule_get_relays_ids(int schedule_id);
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -30,15 +30,13 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
 | 
			
		|||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
 | 
			
		||||
static int*
 | 
			
		||||
get_ids(sqlite3_stmt *stmt)
 | 
			
		||||
{
 | 
			
		||||
    int result = 0;
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
    int *ids = malloc(sizeof(int));
 | 
			
		||||
    int new_id;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT schedule_id FROM junction_relay_schedule WHERE weekday=?1 AND relay_id=?2 LIMIT 1;", -1, &stmt, NULL);
 | 
			
		||||
    sqlite3_bind_int(stmt, 1, weekday);
 | 
			
		||||
    sqlite3_bind_int(stmt, 2, relay_id);
 | 
			
		||||
    int row = 0;
 | 
			
		||||
 | 
			
		||||
    while(true)
 | 
			
		||||
    {
 | 
			
		||||
| 
						 | 
				
			
			@ -47,7 +45,11 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
 | 
			
		|||
        s = sqlite3_step(stmt);
 | 
			
		||||
        if (s == SQLITE_ROW)
 | 
			
		||||
        {
 | 
			
		||||
            result = sqlite3_column_int(stmt, 0);
 | 
			
		||||
            new_id = sqlite3_column_int(stmt, 0);
 | 
			
		||||
            row++;
 | 
			
		||||
 | 
			
		||||
            ids = (int*)realloc(ids, sizeof(int) * (row + 1));
 | 
			
		||||
            ids[row - 1] = new_id;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
| 
						 | 
				
			
			@ -57,17 +59,46 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
 | 
			
		|||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                LOG_ERROR("error reading from database: %s", sqlite3_errstr(s));
 | 
			
		||||
                break;
 | 
			
		||||
                LOG_ERROR("error selecting junction ids from database: %s\n", sqlite3_errstr(s));
 | 
			
		||||
                sqlite3_finalize(stmt);
 | 
			
		||||
                return NULL;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sqlite3_finalize(stmt);
 | 
			
		||||
    ids[row] = 0;
 | 
			
		||||
 | 
			
		||||
    return ids;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT schedule_id FROM junction_relay_schedule WHERE weekday=?1 AND relay_id=?2 LIMIT 1;", -1, &stmt, NULL);
 | 
			
		||||
    sqlite3_bind_int(stmt, 1, weekday);
 | 
			
		||||
    sqlite3_bind_int(stmt, 2, relay_id);
 | 
			
		||||
 | 
			
		||||
    int *id_list = get_ids(stmt); 
 | 
			
		||||
    int result = id_list[0];
 | 
			
		||||
 | 
			
		||||
    free(id_list);
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int*
 | 
			
		||||
junction_relay_schedule_get_relays_ids(int schedule_id)
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT relay_id FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
 | 
			
		||||
    sqlite3_bind_int(stmt, 1, schedule_id);
 | 
			
		||||
 | 
			
		||||
    return get_ids(stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -133,9 +133,12 @@ schedule_save(schedule_t *schedule)
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        if(!schedule->id)
 | 
			
		||||
        {
 | 
			
		||||
            schedule->id = sqlite3_last_insert_rowid(global_database);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue