Ver Fonte

修改获取服务器字段没有的情况处理

zaki.wang há 5 anos atrás
pai
commit
b9717b6924
3 ficheiros alterados com 78 adições e 52 exclusões
  1. 2 7
      docs/samples.md
  2. 10 3
      samples/location_client.c
  3. 66 42
      src/wayz_pos.c

+ 2 - 7
docs/samples.md

@@ -59,17 +59,12 @@ post_data.cellulars.cell[0].lac = 36558;
 get_position_info(wlan_info, ACCESS_KEY, &post_data, &location); 
 ```
 
-## 4、在`main()`主函数调用例程
-在将下面代码嵌入main()中,即可实现调用:
-```c
-extern int location_client_start(void);
-location_client_start();
-```
+## 4、在`finsh`命令行中输入`location_test`命令即可调用示例代码
 
 
 ## 示例结果 ##
 ```c
-timestamp: 1606293694990 
+timestamp: 1606293694990 ms
 gcj02:
 	latitude:30.515105
 	longitude:114.401555

+ 10 - 3
samples/location_client.c

@@ -59,7 +59,7 @@ static void location_client_entry(void *parament)
     {
         location_print(location);
     }
-    
+
     while (1)
     {
         ret = get_position_info(wlan_info, ACCESS_KEY, RT_NULL, &location); 
@@ -74,13 +74,12 @@ static void location_client_entry(void *parament)
         rt_thread_mdelay(POINT_FRQ);
         rt_memset(&location, 0, sizeof (location));
     }
-    
+
 }
 
 int location_client_start(void)
 {
     rt_thread_t tid;
-				//cJsonTask();
     tid = rt_thread_create("location_client", location_client_entry, RT_NULL, 6 * 1024, RT_THREAD_PRIORITY_MAX / 3 - 1, 5);
     if (tid)
     {
@@ -91,3 +90,11 @@ int location_client_start(void)
 
     return -RT_ERROR;
 }
+
+#ifdef RT_USING_FINSH
+#include <finsh.h>
+FINSH_FUNCTION_EXPORT_ALIAS(location_client_start, location_test, location client test start);
+#ifdef FINSH_USING_MSH
+MSH_CMD_EXPORT_ALIAS(location_client_start, location_test, location client test start);
+#endif
+#endif

+ 66 - 42
src/wayz_pos.c

@@ -482,19 +482,22 @@ static void parse_point_cJson_handler(char *data, tlocation_info *location)
     }
 
     item = cJSON_GetObjectItem(point, "longitude");
-    buffer = cJSON_Print(item);
-    location->point.gcj02.longitude = atof(buffer);
-    rt_free(buffer);
-    buffer = RT_NULL;
-    // printf("longitude: %s, %3.6f\r\n", cJSON_Print(item), location.point.gcj02.longitude);
-    item = cJSON_GetObjectItem(point, "latitude");
-    buffer = cJSON_Print(item);
-    location->point.gcj02.latitude = atof(buffer);
-    rt_free(buffer);
-    buffer = RT_NULL;
-  
-    gcj02towgs84(location->point.gcj02.latitude, location->point.gcj02.longitude, &location->point.wgs84.latitude, &location->point.wgs84.longitude);
-
+    if (item)
+    {
+        buffer = cJSON_Print(item);
+        location->point.gcj02.longitude = atof(buffer);
+        rt_free(buffer);
+        buffer = RT_NULL;
+        // printf("longitude: %s, %3.6f\r\n", cJSON_Print(item), location.point.gcj02.longitude);
+        item = cJSON_GetObjectItem(point, "latitude");
+        buffer = cJSON_Print(item);
+        location->point.gcj02.latitude = atof(buffer);
+        rt_free(buffer);
+        buffer = RT_NULL;
+    
+        gcj02towgs84(location->point.gcj02.latitude, location->point.gcj02.longitude, &location->point.wgs84.latitude, &location->point.wgs84.longitude);
+    }
+    
     place = cJSON_GetObjectItem(object, "place");
     if (!place)
     {
@@ -503,41 +506,62 @@ static void parse_point_cJson_handler(char *data, tlocation_info *location)
     }
 
     item = cJSON_GetObjectItem(place, "id");
-    buffer = cJSON_Print(item);
-    rt_memset(location->place.id, 0, sizeof (location->place.id));
-    rt_sprintf(location->place.id , "%s", buffer);
-    rt_free(buffer);
-    buffer = RT_NULL;
+    if (item)
+    {
+        buffer = cJSON_Print(item);
+        rt_memset(location->place.id, 0, sizeof (location->place.id));
+        rt_sprintf(location->place.id , "%s", buffer);
+        rt_free(buffer);
+        buffer = RT_NULL;
+    }
 
     item = cJSON_GetObjectItem(place, "type");
-    buffer = cJSON_Print(item);
-    rt_memset(location->place.type, 0, sizeof (location->place.type));
-    rt_sprintf(location->place.type , "%s", buffer);
-    rt_free(buffer);
-    buffer = RT_NULL;
+    if (item)
+    {
+        buffer = cJSON_Print(item);
+        rt_memset(location->place.type, 0, sizeof (location->place.type));
+        rt_sprintf(location->place.type , "%s", buffer);
+        rt_free(buffer);
+        buffer = RT_NULL;
+    }
 
     item = cJSON_GetObjectItem(place, "name");
-    buffer = cJSON_Print(item);
-    rt_memset(location->place.name, 0, sizeof (location->place.name));
-    rt_sprintf(location->place.name , "%s", buffer);
-    rt_free(buffer);
-    buffer = RT_NULL;
+    if (item)
+    {
+        buffer = cJSON_Print(item);
+        rt_memset(location->place.name, 0, sizeof (location->place.name));
+        rt_sprintf(location->place.name , "%s", buffer);
+        rt_free(buffer);
+        buffer = RT_NULL;
+    }
 
     category = cJSON_GetObjectItem(place, "categories");
-    
-    temp = cJSON_GetArrayItem(category, 0);
-    item = cJSON_GetObjectItem(temp, "id");
-    buffer = cJSON_Print(item);
-    rt_memset(location->place.category.id, 0, sizeof (location->place.category.id));
-    rt_sprintf(location->place.category.id , "%s", buffer);
-    rt_free(buffer);
-    buffer = RT_NULL;
-    item = cJSON_GetObjectItem(temp, "name");
-    buffer = cJSON_Print(item);
-    rt_memset(location->place.category.name, 0, sizeof (location->place.category.name));
-    rt_sprintf(location->place.category.name , "%s", buffer);
-    rt_free(buffer);
-    buffer = RT_NULL;
+    if (category)
+    {
+        temp = cJSON_GetArrayItem(category, 0);
+        if (temp)
+        {
+            item = cJSON_GetObjectItem(temp, "id");
+            if (item)
+            {
+                buffer = cJSON_Print(item);
+                rt_memset(location->place.category.id, 0, sizeof (location->place.category.id));
+                rt_sprintf(location->place.category.id , "%s", buffer);
+                rt_free(buffer);
+                buffer = RT_NULL;
+            }
+
+            item = cJSON_GetObjectItem(temp, "name");
+            if (item)
+            {
+                buffer = cJSON_Print(item);
+                rt_memset(location->place.category.name, 0, sizeof (location->place.category.name));
+                rt_sprintf(location->place.category.name , "%s", buffer);
+                rt_free(buffer);
+                buffer = RT_NULL;
+            }
+        }
+    }
 
 _root_fail:
     cJSON_Delete(root);