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

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后才可以查到自定义的函数名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值