用sysinfo获取系统运行时间
authorkyz <kongyuzhong@gmail.com>
Mon, 26 Jun 2023 05:32:40 +0000 (13:32 +0800)
committerkyz <kongyuzhong@gmail.com>
Mon, 26 Jun 2023 05:42:46 +0000 (13:42 +0800)
app/inc/device.h
app/src/device_config.c
app/src/device_ctrl.c
app/src/device_netconfig.c

index cd2cf6470e400defbf1c479f2bebe792209608f2..5cdb3405583faf368a7db413a1c13ad69cf8bfdb 100644 (file)
@@ -339,5 +339,7 @@ int32_t tuya_bindcode_read( char * code, int32_t max_sz );
 int32_t fw_version_read( char * ver, int32_t max_sz );
 int32_t hw_version_read( char * ver, int32_t max_sz );
 int32_t tuya_productid_read( char * ver, int32_t max_sz );
+
+uint32_t device_uptime_sec( void );
 #endif //__MG_DEVICE_H__
 
index 82441f226fd7ae5c01d1728d3d418b678670645b..153b35f2f39d927e172b6d85ea42126e848ada5d 100644 (file)
@@ -221,7 +221,6 @@ int32_t read_device_uuid( mg_dev_t *dev )
 int32_t        mg_device_active( mg_dev_t *dev )
 {
        FILE *fp;
-       uint32_t now = time(NULL);
        char key_id[200];
        char uuid[100];
        char *s;
index 7467a655846fc8c8edaf7e773806ad54a9c540bf..d74f4282369c88d192ba093aa8de54fc55a9d9f6 100644 (file)
@@ -58,7 +58,7 @@ void * btn_light_ctrl_task( void* para )
 
                                MG_LOGI( "enter clean mode ..." );
                                dev->clean_device = 1;
-                               dev->clean_start_time = time(NULL);
+                               dev->clean_start_time = device_uptime_sec();
 
                        }else if( dev->clean_device && to_clean_mode_exit ){
 
@@ -67,9 +67,7 @@ void * btn_light_ctrl_task( void* para )
                                dev->clean_start_time = 0;
                        }else if( !dev->clean_device && !dev->netconfig &&  to_net_config ){
                                MG_LOGI( "enter net config mode ..." );
-                               //dev->netconfig = 1;
-                               //dev->netconfig_start_time = time(NULL);
-                               printf( "pressed key -- start net config, t:%ld\n", dev->netconfig_start_time );
+                               printf( "pressed key -- start net config\n" );
                                netconfig_start( dev );
                        }
 
@@ -93,7 +91,7 @@ void * btn_light_ctrl_task( void* para )
 
                if( dev->clean_device  ){
                        //检查清洁模式是否超时
-                       if( dev->clean_start_time && time(NULL) - dev->clean_start_time > CLEANMODE_TIMEOUT_SEC ){
+                       if( dev->clean_start_time && device_uptime_sec() - dev->clean_start_time > CLEANMODE_TIMEOUT_SEC ){
                                mg_dev_lock( dev );
                                dev->clean_device = 0;
                                dev->clean_start_time = 0;
@@ -102,12 +100,8 @@ void * btn_light_ctrl_task( void* para )
                        }
                }else if( dev->netconfig){
                        //检查配网超时
-                       if( dev->netconfig_start_time && time(NULL) - dev->netconfig_start_time > NETCONFIG_TIMEOUT_SEC ){
-                               printf( "net config, timeout, now:%ld, s:%ld\n", time(NULL), dev->netconfig_start_time );
-                               //mg_dev_lock( dev );
-                               //dev->netconfig= 0;
-                               //dev->netconfig_start_time = 0;
-                               //mg_dev_unlock( dev );
+                       if( dev->netconfig_start_time && device_uptime_sec() - dev->netconfig_start_time > NETCONFIG_TIMEOUT_SEC ){
+                               printf( "net config, timeout, now:%ld, s:%ld\n", device_uptime_sec(), dev->netconfig_start_time );
                                netconfig_stop( dev );
                        }
                }
@@ -176,9 +170,9 @@ void * btn_light_ctrl_task( void* para )
                                        light_mode_set( dev, LIGHT_MODE_TOF_ACTIVE );
                                }
 
-                               if( dev->tof && time(NULL ) - tof_check_time > cfg_mg_tof_interval_read( dev ) ){
+                               if( dev->tof && device_uptime_sec() - tof_check_time > cfg_mg_tof_interval_read( dev ) ){
 
-                                       tof_check_time = time(NULL);
+                                       tof_check_time = device_uptime_sec();
                                        memset( &info, 0, sizeof( info ) );
 
                                        //检查周期到达,读取tof状态
index 1e2c05e70bd4348d222b2e052b0d7a49bd20a127..d28b539b8bccff1dd90d7115d9dec20ac21ea284 100644 (file)
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <pthread.h>
+#include <sys/sysinfo.h>
 //#include "button.h"
 //#include "iot.h"
 //#include "mg_log.h"
 
 extern int32_t mg_device_bind_flag_write( void );
 
+//使用sysinfo来获取系统运行时间,因使用time(NULL)获取时间会受到ntp的干扰。
+uint32_t device_uptime_sec( void )
+{
+       struct sysinfo info;    
+
+       if( sysinfo( &info ) ){
+               return 0;
+       }
+
+       return info.uptime;
+}
+
 int32_t        netconfig_start( mg_dev_t *dev )
 {
-       printf( "netconfig_start,now:%ld, , cfg:%d, s:%ld\n", time(NULL),  dev->netconfig, dev->netconfig_start_time );
+       printf( "netconfig_start,now:%ld, , cfg:%d, s:%ld\n", device_uptime_sec(), dev->netconfig, dev->netconfig_start_time );
        mg_dev_lock( dev );
        dev->netconfig = 1;
-       dev->netconfig_start_time = time(NULL);
+       dev->netconfig_start_time = device_uptime_sec();
        mg_dev_unlock( dev );
        return 0;
 }
 
 int32_t        netconfig_stop( mg_dev_t *dev )
 {
-       printf( "netconfig_stop,now:%ld, , cfg:%d, s:%ld\n", time(NULL),  dev->netconfig, dev->netconfig_start_time );
+       printf( "netconfig_stop,now:%ld, , cfg:%d, s:%ld\n", device_uptime_sec(), dev->netconfig, dev->netconfig_start_time );
        mg_dev_lock( dev );
        dev->netconfig = 0;
        dev->netconfig_start_time = 0;
@@ -169,40 +182,3 @@ void device_netconfig_task_start( mg_dev_t *dev )
        }
 }
 
-#if 0
-int32_t        tuya_config_start( mg_dev_t *dev );
-int32_t        tuya_config_stop( mg_dev_t *dev );
-
-
-int32_t        wificonfig_start( mg_dev_t *dev )
-{
-       mg_dev_lock( dev );
-       dev->wifi_config = 1;
-       dev->wifi_config_timeout = time(NULL)+2*60;//2minutes timeout
-       mg_dev_unlock( dev );
-       //TODO: start ble adver ...
-       g_ble_adv = 1;
-       return 0;
-}
-
-int32_t        wificonfig_stop( mg_dev_t *dev )
-{
-       mg_dev_lock( dev );
-       dev->wifi_config = 0;
-       dev->wifi_config_timeout = 0;
-       mg_dev_unlock( dev );
-       //TODO: stop ble adver ...
-       g_ble_adv = 0;
-       return 0;
-}
-
-int32_t        wificonfig_success( mg_dev_t *dev )
-{
-       return 0;
-}
-
-int32_t        wificonfig_fail( mg_dev_t *dev )
-{
-       return 0;
-}
-#endif