From 435110e61d6c16f8c8ac8582e19d3e79e2e18ed3 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Thu, 20 Aug 2020 11:37:21 +0200
Subject: [PATCH] fix: store IP on rediscover

---
 CMakeLists.txt                              |  2 +-
 src/endpoints/api_v1_controllers_discover.c |  5 +++--
 src/status.c                                | 12 ++++++------
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index de06cb4..0d8130b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
 cmake_minimum_required (VERSION 3.7)
 project(core
-        VERSION 0.2.16
+        VERSION 0.2.17
         LANGUAGES C)
 
 add_executable(core src/main.c)
diff --git a/src/endpoints/api_v1_controllers_discover.c b/src/endpoints/api_v1_controllers_discover.c
index 9b5e7f1..d68b11c 100644
--- a/src/endpoints/api_v1_controllers_discover.c
+++ b/src/endpoints/api_v1_controllers_discover.c
@@ -228,10 +228,11 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
                 {
                     if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0)
                     {
-                        LOGGER_DEBUG("rediscovered a known controller\n");
+                        LOGGER_DEBUG("rediscovered a known controller at %s\n", inet_ntoa(addr.sin_addr));
 
                         known_controllers[i]->active = 1;
                         strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
+                        strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr));
                         known_controllers[i]->name[discovered_name_len] = '\0';
                         known_controllers[i]->port = discovered_command_port;
                         known_controllers[i]->relay_count = discovered_relay_count;
@@ -251,7 +252,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
 
             if(!found_discovered_in_list)
             {
-                LOGGER_DEBUG("discovered a new controller\n");
+                LOGGER_DEBUG("discovered a new controller at %s\n", inet_ntoa(addr.sin_addr));
 
                 controller_t *discovered_controller = malloc(sizeof(controller_t));
                 discovered_controller->id = 0;
diff --git a/src/status.c b/src/status.c
index 56ff2e3..57c6c9c 100644
--- a/src/status.c
+++ b/src/status.c
@@ -59,7 +59,7 @@ status_init()
 void
 status_reload_entry(int relay_id)
 {
-    LOGGER_DEBUG("reloading relay status\n");
+    LOGGER_DEBUG("reloading relay %d\n", relay_id);
     relay_t **relays = global_relay_status_list;
     for(int i = 0; relays[i] != NULL; ++i)
     {
@@ -70,13 +70,13 @@ status_reload_entry(int relay_id)
 
         int is_on_backup = relays[i]->is_on;
         relay_t *updated_relay = relay_get_by_id(relay_id);
+        updated_relay->is_on = is_on_backup;
+
         relay_free(relays[i]);
         relays[i] = updated_relay;
-        relays[i]->is_on = is_on_backup;
-
-        invalidate_json_str_cache();
 
         cache_invalidate_relay(relay_id);
+        invalidate_json_str_cache();
     }
 }
 
@@ -91,11 +91,11 @@ status_update_entry(int relay_id, int is_on)
         {
             continue;
         }
+
         relays[i]->is_on = is_on;
 
-        invalidate_json_str_cache();
-
         cache_invalidate_relay(relay_id);
+        invalidate_json_str_cache();
     }
 }