cesanta / mongoose-os

Mongoose OS - an IoT Firmware Development Framework. Supported microcontrollers: ESP32, ESP8266, CC3220, CC3200, STM32F4, STM32L4, STM32F7. Amazon AWS IoT, Microsoft Azure, Google IoT Core integrated. Code in C or JavaScript.
https://mongoose-os.com
Other
2.49k stars 430 forks source link

Add event for configuration change. #490

Open zdila opened 5 years ago

zdila commented 5 years ago

It would be nice for some service to update itself immediately on configuration change without a reboot. Now it can only be implemented by polling a configuration value. A new event (eg. MGOS_CONFIG_EV_CHANGED would enable listening for such changes. There is no need to add any payload to this event as client service could just compare old and new setting in event handler.

Juan1ll0 commented 4 years ago

Hi @zdila.

I solved it creating a new event called 'MGOS_EVENT_CONFIG_CHANGED' inside file "include/mgos_event.h". Then you need to modify file "src/mgos_sys_config.c" loading mgos_events and tigger that event. Something similar to this:

.....
#include "mgos_ro_vars.h"
#include "mgos_utils.h"
#include "mgos_vfs.h"
.....
// Load Events
#include "mgos_event.h"
.....

// In this method
bool mgos_sys_config_save_level(const struct mgos_config *cfg,
.....
        if (mgos_conf_emit_f(cfg, defaults, mgos_config_schema(), true /* pretty */,
                               fname)) {
            LOG(LL_INFO, ("Saved to %s", fname));
            result = true;
            // Throw event.
            mgos_event_trigger(MGOS_EVENT_CONFIG_CHANGED, NULL);
        }
....

Then you can add handler to that event:

mgos_event_add_handler(MGOS_EVENT_CONFIG_CHANGED, on_change_config, NULL);

That's all. Is a primitive solution but is working for me. Best regards