fix: use separate relay caches for status
This commit is contained in:
parent
0eef646fe0
commit
f98a01f3f0
13 changed files with 49 additions and 38 deletions
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(core
|
project(core
|
||||||
VERSION 0.3.1
|
VERSION 0.3.2
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(core src/main.c)
|
add_executable(core src/main.c)
|
||||||
|
|
|
@ -27,13 +27,13 @@ cache_invalidate_schedule(int schedule_id);
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
cache_put_json_relay(int relay_id, char *relay_json);
|
cache_put_json_relay(int relay_id, int status_relay, char *relay_json);
|
||||||
|
|
||||||
char*
|
char*
|
||||||
cache_get_json_relay(int relay_id);
|
cache_get_json_relay(int relay_id, int status_relay);
|
||||||
|
|
||||||
void
|
void
|
||||||
cache_invalidate_relay(int relay_id);
|
cache_invalidate_relay(int relay_id, int status_relay);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -33,10 +33,10 @@ void
|
||||||
relay_reload_schedules(relay_t *relay);
|
relay_reload_schedules(relay_t *relay);
|
||||||
|
|
||||||
cJSON*
|
cJSON*
|
||||||
relay_to_json(relay_t *relay);
|
relay_to_json(relay_t *relay, int status_relay);
|
||||||
|
|
||||||
cJSON*
|
cJSON*
|
||||||
relay_list_to_json(relay_t **relays);
|
relay_list_to_json(relay_t **relays, int status_relays);
|
||||||
|
|
||||||
void
|
void
|
||||||
relay_free(relay_t *relay);
|
relay_free(relay_t *relay);
|
||||||
|
|
22
src/cache.c
22
src/cache.c
|
@ -138,7 +138,7 @@ cache_invalidate_schedule(int schedule_id)
|
||||||
|
|
||||||
for(int i = 0; relays[i] != NULL; ++i)
|
for(int i = 0; relays[i] != NULL; ++i)
|
||||||
{
|
{
|
||||||
cache_invalidate_relay(relays[i]->id);
|
cache_invalidate_relay(relays[i]->id, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_free_list(relays);
|
relay_free_list(relays);
|
||||||
|
@ -147,26 +147,32 @@ cache_invalidate_schedule(int schedule_id)
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
cache_put_json_relay(int relay_id, char *relay_json)
|
cache_put_json_relay(int relay_id, int status_relay, char *relay_json)
|
||||||
{
|
{
|
||||||
char key[32];
|
char key[32];
|
||||||
sprintf(key, "relay_json:%d", relay_id);
|
sprintf(key, "relay_json:%d:%d", relay_id, status_relay);
|
||||||
cache_insert_value(key, relay_json);
|
cache_insert_value(key, relay_json);
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char*
|
||||||
cache_get_json_relay(int relay_id)
|
cache_get_json_relay(int relay_id, int status_relay)
|
||||||
{
|
{
|
||||||
char key[32];
|
char key[32];
|
||||||
sprintf(key, "relay_json:%d", relay_id);
|
sprintf(key, "relay_json:%d:%d", relay_id, status_relay);
|
||||||
return cache_get_value(key);
|
return cache_get_value(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
cache_invalidate_relay(int relay_id)
|
cache_invalidate_relay(int relay_id, int status_relay)
|
||||||
{
|
{
|
||||||
|
if(status_relay == -1)
|
||||||
|
{
|
||||||
|
cache_invalidate_relay(relay_id, 0);
|
||||||
|
cache_invalidate_relay(relay_id, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
char key[32];
|
char key[32];
|
||||||
sprintf(key, "relay_json:%d", relay_id);
|
sprintf(key, "relay_json:%d:%d", relay_id, status_relay);
|
||||||
cache_invalidate(key);
|
cache_invalidate(key);
|
||||||
|
|
||||||
int controller_id = relay_get_controller_id_for_relay(relay_id);
|
int controller_id = relay_get_controller_id_for_relay(relay_id);
|
||||||
|
@ -215,7 +221,7 @@ cache_invalidate_tagged(int tag_id)
|
||||||
int *relay_ids = junction_tag_get_relays_for_tag_id(tag_id);
|
int *relay_ids = junction_tag_get_relays_for_tag_id(tag_id);
|
||||||
for(int i = 0; relay_ids[i] != 0; ++i)
|
for(int i = 0; relay_ids[i] != 0; ++i)
|
||||||
{
|
{
|
||||||
cache_invalidate_relay(relay_ids[i]);
|
cache_invalidate_relay(relay_ids[i], -1);
|
||||||
}
|
}
|
||||||
free(relay_ids);
|
free(relay_ids);
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message
|
||||||
LOGGER_DEBUG("returning all relays for controller %s\n", args[0].value.v_str);
|
LOGGER_DEBUG("returning all relays for controller %s\n", args[0].value.v_str);
|
||||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||||
{
|
{
|
||||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
cJSON *json_relay = relay_to_json(all_relays[i], 0);
|
||||||
|
|
||||||
cJSON_AddItemToArray(json, json_relay);
|
cJSON_AddItemToArray(json, json_relay);
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER_DEBUG("returning relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
LOGGER_DEBUG("returning relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||||
cJSON *json = relay_to_json(relay);
|
cJSON *json = relay_to_json(relay, 0);
|
||||||
|
|
||||||
endpoint_response_json(response, 200, json);
|
endpoint_response_json(response, 200, json);
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
@ -268,7 +268,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
json = relay_to_json(relay);
|
json = relay_to_json(relay, 0);
|
||||||
|
|
||||||
command_relay_schedules_set(relay);
|
command_relay_schedules_set(relay);
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ api_v1_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_ar
|
||||||
|
|
||||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||||
{
|
{
|
||||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
cJSON *json_relay = relay_to_json(all_relays[i], 0);
|
||||||
|
|
||||||
cJSON_AddItemToArray(json, json_relay);
|
cJSON_AddItemToArray(json, json_relay);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, end
|
||||||
LOGGER_DEBUG("failed to get relay %d for tag %s\n", relays_ids[i], args[0].value.v_str);
|
LOGGER_DEBUG("failed to get relay %d for tag %s\n", relays_ids[i], args[0].value.v_str);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cJSON *json_relay = relay_to_json(relay);
|
cJSON *json_relay = relay_to_json(relay, 0);
|
||||||
|
|
||||||
cJSON_AddItemToArray(json, json_relay);
|
cJSON_AddItemToArray(json, json_relay);
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cJSON *json_relay = relay_to_json(relay);
|
cJSON *json_relay = relay_to_json(relay, 0);
|
||||||
|
|
||||||
cJSON_AddItemToArray(json_relays, json_relay);
|
cJSON_AddItemToArray(json_relays, json_relay);
|
||||||
|
|
||||||
|
|
|
@ -273,7 +273,7 @@ controller_to_json(controller_t *controller)
|
||||||
cJSON *json_relays = cJSON_CreateArray();
|
cJSON *json_relays = cJSON_CreateArray();
|
||||||
for(int i = 0; relays[i] != NULL; ++i)
|
for(int i = 0; relays[i] != NULL; ++i)
|
||||||
{
|
{
|
||||||
cJSON *json_relay = relay_to_json(relays[i]);
|
cJSON *json_relay = relay_to_json(relays[i], 0);
|
||||||
cJSON_AddItemToArray(json_relays, json_relay);
|
cJSON_AddItemToArray(json_relays, json_relay);
|
||||||
}
|
}
|
||||||
cJSON_AddItemToObject(json, "relays", json_relays);
|
cJSON_AddItemToObject(json, "relays", json_relays);
|
||||||
|
|
|
@ -47,7 +47,7 @@ junction_tag_insert(int tag_id, int relay_id, int schedule_id)
|
||||||
|
|
||||||
if(relay_id)
|
if(relay_id)
|
||||||
{
|
{
|
||||||
cache_invalidate_relay(relay_id);
|
cache_invalidate_relay(relay_id, -1);
|
||||||
}
|
}
|
||||||
if(schedule_id)
|
if(schedule_id)
|
||||||
{
|
{
|
||||||
|
@ -116,7 +116,7 @@ junction_tag_insert_list(int *tag_ids, int relay_id, int schedule_id, int count)
|
||||||
|
|
||||||
if(relay_id)
|
if(relay_id)
|
||||||
{
|
{
|
||||||
cache_invalidate_relay(relay_id);
|
cache_invalidate_relay(relay_id, -1);
|
||||||
}
|
}
|
||||||
if(schedule_id)
|
if(schedule_id)
|
||||||
{
|
{
|
||||||
|
@ -185,7 +185,7 @@ junction_tag_remove(int tag_id, int relay_id, int schedule_id)
|
||||||
|
|
||||||
if(relay_id)
|
if(relay_id)
|
||||||
{
|
{
|
||||||
cache_invalidate_relay(relay_id);
|
cache_invalidate_relay(relay_id, -1);
|
||||||
}
|
}
|
||||||
if(schedule_id)
|
if(schedule_id)
|
||||||
{
|
{
|
||||||
|
@ -206,7 +206,7 @@ junction_tag_remove_for_relay(int relay_id)
|
||||||
rc = sqlite3_step(stmt);
|
rc = sqlite3_step(stmt);
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
cache_invalidate_relay(relay_id);
|
cache_invalidate_relay(relay_id, -1);
|
||||||
|
|
||||||
return rc == SQLITE_DONE;
|
return rc == SQLITE_DONE;
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,7 +169,7 @@ relay_save(relay_t *relay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cache_invalidate_relay(relay->id);
|
cache_invalidate_relay(relay->id, -1);
|
||||||
status_reload_entry(relay->id);
|
status_reload_entry(relay->id);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -236,11 +236,11 @@ relay_free_list(relay_t **relays)
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON*
|
cJSON*
|
||||||
relay_to_json(relay_t *relay)
|
relay_to_json(relay_t *relay, int status_relay)
|
||||||
{
|
{
|
||||||
cJSON *json;
|
cJSON *json;
|
||||||
|
|
||||||
char *cached = cache_get_json_relay(relay->id);
|
char *cached = cache_get_json_relay(relay->id, status_relay);
|
||||||
if(cached)
|
if(cached)
|
||||||
{
|
{
|
||||||
json = cJSON_CreateRaw(cached);
|
json = cJSON_CreateRaw(cached);
|
||||||
|
@ -345,7 +345,7 @@ relay_to_json(relay_t *relay)
|
||||||
cJSON_AddItemToObject(json, "tags", json_tags);
|
cJSON_AddItemToObject(json, "tags", json_tags);
|
||||||
|
|
||||||
char *json_str = cJSON_Print(json);
|
char *json_str = cJSON_Print(json);
|
||||||
cache_put_json_relay(relay->id, json_str);
|
cache_put_json_relay(relay->id, status_relay, json_str);
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
|
||||||
json = cJSON_CreateRaw(json_str);
|
json = cJSON_CreateRaw(json_str);
|
||||||
|
@ -354,13 +354,13 @@ relay_to_json(relay_t *relay)
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON*
|
cJSON*
|
||||||
relay_list_to_json(relay_t **relays)
|
relay_list_to_json(relay_t **relays, int status_relays)
|
||||||
{
|
{
|
||||||
cJSON *json = cJSON_CreateArray();
|
cJSON *json = cJSON_CreateArray();
|
||||||
|
|
||||||
for(int i = 0; relays[i] != NULL; ++i)
|
for(int i = 0; relays[i] != NULL; ++i)
|
||||||
{
|
{
|
||||||
cJSON *json_relay = relay_to_json(relays[i]);
|
cJSON *json_relay = relay_to_json(relays[i], status_relays);
|
||||||
cJSON_AddItemToArray(json, json_relay);
|
cJSON_AddItemToArray(json, json_relay);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
src/status.c
15
src/status.c
|
@ -33,7 +33,7 @@ validate_json_str_cache()
|
||||||
{
|
{
|
||||||
relay_t **relays = global_relay_status_list;
|
relay_t **relays = global_relay_status_list;
|
||||||
|
|
||||||
cJSON *json = relay_list_to_json(relays);
|
cJSON *json = relay_list_to_json(relays, 1);
|
||||||
|
|
||||||
char *json_str = cJSON_Print(json);
|
char *json_str = cJSON_Print(json);
|
||||||
size_t json_str_len = strlen(json_str);
|
size_t json_str_len = strlen(json_str);
|
||||||
|
@ -68,15 +68,17 @@ status_reload_entry(int relay_id)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_on_backup = relays[i]->is_on;
|
LOGGER_DEBUG("is_on %d\n", relays[i]->is_on);
|
||||||
|
|
||||||
relay_t *updated_relay = relay_get_by_id(relay_id);
|
relay_t *updated_relay = relay_get_by_id(relay_id);
|
||||||
updated_relay->is_on = is_on_backup;
|
updated_relay->is_on = relays[i]->is_on;
|
||||||
|
|
||||||
relay_free(relays[i]);
|
relay_free(relays[i]);
|
||||||
relays[i] = updated_relay;
|
relays[i] = updated_relay;
|
||||||
|
|
||||||
cache_invalidate_relay(relay_id);
|
cache_invalidate_relay(relay_id, 1);
|
||||||
invalidate_json_str_cache();
|
invalidate_json_str_cache();
|
||||||
|
LOGGER_DEBUG("is_on %d\n", relays[i]->is_on);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,12 +94,15 @@ status_update_entry(int relay_id, int is_on)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(relays[i]->is_on != is_on)
|
||||||
|
{
|
||||||
relays[i]->is_on = is_on;
|
relays[i]->is_on = is_on;
|
||||||
|
|
||||||
cache_invalidate_relay(relay_id);
|
cache_invalidate_relay(relay_id, 1);
|
||||||
invalidate_json_str_cache();
|
invalidate_json_str_cache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
status_broadcast(struct mg_mgr *mgr)
|
status_broadcast(struct mg_mgr *mgr)
|
||||||
|
|
Loading…
Reference in a new issue