add: faster broadcast on status change
This commit is contained in:
		
							parent
							
								
									eca6f7f704
								
							
						
					
					
						commit
						f49c760e97
					
				
					 2 changed files with 70 additions and 59 deletions
				
			
		| 
						 | 
					@ -1,7 +1,6 @@
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <time.h>
 | 
					 | 
				
			||||||
#include <syslog.h>
 | 
					#include <syslog.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <mongoose.h>
 | 
					#include <mongoose.h>
 | 
				
			||||||
| 
						 | 
					@ -142,16 +141,10 @@ main(int argc, const char** argv)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /******************** START MAIN LOOP ********************/
 | 
					    /******************** START MAIN LOOP ********************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    time_t timer = time(NULL);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (;;)
 | 
					    for (;;)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_mgr_poll(&mgr, 200);
 | 
					        mg_mgr_poll(&mgr, 200);
 | 
				
			||||||
        if(time(NULL) - timer >= 10)
 | 
					        status_broadcast(&mgr);
 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            status_broadcast(&mgr);
 | 
					 | 
				
			||||||
            timer = time(NULL);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    terminate(0);
 | 
					    terminate(0);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										120
									
								
								src/status.c
									
										
									
									
									
								
							
							
						
						
									
										120
									
								
								src/status.c
									
										
									
									
									
								
							| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <cache.h>
 | 
					#include <cache.h>
 | 
				
			||||||
| 
						 | 
					@ -8,56 +10,7 @@ relay_t **global_relay_status_list;
 | 
				
			||||||
char *relay_status_list_json_str;
 | 
					char *relay_status_list_json_str;
 | 
				
			||||||
size_t relay_status_list_json_str_len;
 | 
					size_t relay_status_list_json_str_len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					time_t timer;
 | 
				
			||||||
status_init()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    global_relay_status_list = relay_get_all();
 | 
					 | 
				
			||||||
    relay_status_list_json_str = NULL;
 | 
					 | 
				
			||||||
    relay_status_list_json_str_len = 0;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void
 | 
					 | 
				
			||||||
status_reload_entry(int relay_id)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    LOGGER_DEBUG("reloading relay status\n");
 | 
					 | 
				
			||||||
    relay_t **relays = global_relay_status_list;
 | 
					 | 
				
			||||||
    for(int i = 0; relays[i] != NULL; ++i)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        if(relays[i]->id != relay_id)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            continue;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        int is_on_backup = relays[i]->is_on;
 | 
					 | 
				
			||||||
        relay_t *updated_relay = relay_get_by_id(relay_id);
 | 
					 | 
				
			||||||
        relay_free(relays[i]);
 | 
					 | 
				
			||||||
        relays[i] = updated_relay;
 | 
					 | 
				
			||||||
        relays[i]->is_on = is_on_backup;
 | 
					 | 
				
			||||||
        relay_status_list_json_str = NULL;
 | 
					 | 
				
			||||||
        relay_status_list_json_str_len = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        cache_invalidate_relay(relay_id);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void
 | 
					 | 
				
			||||||
status_update_entry(int relay_id, int is_on)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    LOGGER_DEBUG("updating relay status ([%d] = %d)\n", relay_id, is_on);
 | 
					 | 
				
			||||||
    relay_t **relays = global_relay_status_list;
 | 
					 | 
				
			||||||
    for(int i = 0; relays[i] != NULL; ++i)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        if(relays[i]->id != relay_id)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            continue;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        relays[i]->is_on = is_on;
 | 
					 | 
				
			||||||
        relay_status_list_json_str = NULL;
 | 
					 | 
				
			||||||
        relay_status_list_json_str_len = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        cache_invalidate_relay(relay_id);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
is_websocket(const struct mg_connection *nc)
 | 
					is_websocket(const struct mg_connection *nc)
 | 
				
			||||||
| 
						 | 
					@ -65,6 +18,14 @@ is_websocket(const struct mg_connection *nc)
 | 
				
			||||||
    return nc->flags & MG_F_IS_WEBSOCKET;
 | 
					    return nc->flags & MG_F_IS_WEBSOCKET;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					invalidate_json_str_cache()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    relay_status_list_json_str = NULL;
 | 
				
			||||||
 | 
					    relay_status_list_json_str_len = 0;
 | 
				
			||||||
 | 
					    timer = 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
validate_json_str_cache()
 | 
					validate_json_str_cache()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -83,13 +44,70 @@ validate_json_str_cache()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					status_init()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    global_relay_status_list = relay_get_all();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    invalidate_json_str_cache();
 | 
				
			||||||
 | 
					    timer = time(NULL);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					status_reload_entry(int relay_id)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    LOGGER_DEBUG("reloading relay status\n");
 | 
				
			||||||
 | 
					    relay_t **relays = global_relay_status_list;
 | 
				
			||||||
 | 
					    for(int i = 0; relays[i] != NULL; ++i)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if(relays[i]->id != relay_id)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int is_on_backup = relays[i]->is_on;
 | 
				
			||||||
 | 
					        relay_t *updated_relay = relay_get_by_id(relay_id);
 | 
				
			||||||
 | 
					        relay_free(relays[i]);
 | 
				
			||||||
 | 
					        relays[i] = updated_relay;
 | 
				
			||||||
 | 
					        relays[i]->is_on = is_on_backup;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        invalidate_json_str_cache();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        cache_invalidate_relay(relay_id);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					status_update_entry(int relay_id, int is_on)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    LOGGER_DEBUG("updating relay status ([%d] = %d)\n", relay_id, is_on);
 | 
				
			||||||
 | 
					    relay_t **relays = global_relay_status_list;
 | 
				
			||||||
 | 
					    for(int i = 0; relays[i] != NULL; ++i)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if(relays[i]->id != relay_id)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        relays[i]->is_on = is_on;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        invalidate_json_str_cache();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        cache_invalidate_relay(relay_id);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
status_broadcast(struct mg_mgr *mgr)
 | 
					status_broadcast(struct mg_mgr *mgr)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    if(timer && (timer - time(NULL) < 10))
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    timer = time(NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    struct mg_connection *c;
 | 
					    struct mg_connection *c;
 | 
				
			||||||
    validate_json_str_cache();
 | 
					    validate_json_str_cache();
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue