当前位置: 首页 > 工具软件 > Mongoose OS > 使用案例 >

突变:mongoose-os的系统配置API改变

龙逸清
2023-12-01
BREAKING: Sys config API change
Now apps should use getters and setters instead of accessing struct
fields directly, e.g. instead of `get_cfg()->update.timeout` it should
be `mgos_sys_config_get_update_timeout()` to get the current value, and
`mgos_sys_config_set_update_timeout(123)` to update the value.

For now, the config structs are public, but they will be made private

soon, so use accessors to keep your code working.

现在,应用程序应当使用getters和setters访问结构体字段,而不是像以前那样,使用get_cfg()->update.timeout方式来直接取得字段值。

应当使用函数mgos_sys_config_get_update_timeout()来读取当前值,而使用mgos_sys_config_set_update_timeout(123)来更新此值。

目前为止,config结构是public的,但是将很快被改到private,因此应该使用访问器方法,以让你的代码继续工作。


如:

fw/examples/c_hello/src/main.c

enum mgos_app_init_result mgos_app_init(void) {
   { /* Print a message using a value from config. */
-    printf("Hello, %s!\n", get_cfg()->hello.who);
+    printf("Hello, %s!\n", mgos_sys_config_get_hello_who());
   }
 
   { /* Set up the blinky timer. */

fw/examples/c_mqtt/src/main.c

   va_start(ap, fmt);
   n = json_vprintf(&jmo, fmt, ap);
   va_end(ap);
-  mg_mqtt_publish(c, get_cfg()->mqtt.pub, mgos_mqtt_get_packet_id(),
+  mg_mqtt_publish(c, mgos_sys_config_get_mqtt_pub(), mgos_mqtt_get_packet_id(),
                   MG_MQTT_QOS(1), msg, n);
-  LOG(LL_INFO, ("%s -> %s", get_cfg()->mqtt.pub, msg));
+  LOG(LL_INFO, ("%s -> %s", mgos_sys_config_get_mqtt_pub(), msg));
 }
 
 static uint8_t from_hex(const char *s) {
 @@ -68,10 +68,11 @@ static void ev_handler(struct mg_connection *c, int ev, void *p,
 
   if (ev == MG_EV_MQTT_CONNACK) {
     LOG(LL_INFO, ("CONNACK: %d", msg->connack_ret_code));
-    if (get_cfg()->mqtt.sub == NULL || get_cfg()->mqtt.pub == NULL) {
+    if (mgos_sys_config_get_mqtt_sub() == NULL ||
+        mgos_sys_config_get_mqtt_pub() == NULL) {
       LOG(LL_ERROR, ("Run 'mgos config-set mqtt.sub=... mqtt.pub=...'"));
     } else {
-      sub(c, "%s", get_cfg()->mqtt.sub);
+      sub(c, "%s", mgos_sys_config_get_mqtt_sub());
     }
   } else if (ev == MG_EV_MQTT_SUBACK) {
     LOG(LL_INFO, ("Subscription %u acknowledged", msg->message_id));


批示:

就是名字好长,而且函数是在gen/gmgos_config.h中,要build后才可以查到自定义的函数名。

 类似资料: