|
|
@@ -382,6 +382,52 @@ static void dtu_thread_entry_recv(void *parameter)
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
+// 解析时间戳并返回一个tm结构体
|
|
|
+struct tm* parseTimestamp(const char *timestamp) {
|
|
|
+ if (strlen(timestamp) != 14) {
|
|
|
+ LOG_E("Invalid timestamp length: %s", timestamp);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ LOG_I("parseTimestamp:%s",timestamp);
|
|
|
+ int year, month, day, hour, minute, second;
|
|
|
+ sscanf(timestamp, "%4d%2d%2d%2d%2d%2d", &year, &month, &day, &hour, &minute, &second);
|
|
|
+ //校验时间日期
|
|
|
+ if(year<2024||month>12||day>31||hour>23||minute>59||second>59) return NULL;
|
|
|
+
|
|
|
+ struct tm* tm_time = (struct tm*)malloc(sizeof(struct tm));
|
|
|
+ tm_time->tm_year = year - 1900; // tm_year is years since 1900
|
|
|
+ tm_time->tm_mon = month - 1; // tm_mon is 0-11
|
|
|
+ tm_time->tm_mday = day;
|
|
|
+ tm_time->tm_hour = hour;
|
|
|
+ tm_time->tm_min = minute;
|
|
|
+ tm_time->tm_sec = second;
|
|
|
+
|
|
|
+ // Check if the time is valid
|
|
|
+ if (mktime(tm_time) == -1) {
|
|
|
+ free(tm_time);
|
|
|
+ LOG_E("Failed to parse time: %s", timestamp);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ return tm_time;
|
|
|
+}
|
|
|
+
|
|
|
+void setSystemTime(struct tm *timeinfo) {
|
|
|
+ time_t t = mktime(timeinfo);
|
|
|
+ if (t == -1) {
|
|
|
+ LOG_E("Failed to convert time to time_t");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // Convert the time_t value to a timeval structure
|
|
|
+ struct timeval tv;
|
|
|
+ tv.tv_sec = t;
|
|
|
+ tv.tv_usec = 0; // Assuming no microseconds in the timestamp
|
|
|
+ if (settimeofday(&tv, NULL) == -1) {
|
|
|
+ LOG_E("Failed to set system time!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LOG_I("System time set successfully!");
|
|
|
+}
|
|
|
|
|
|
// 函数定义:判断数组中是否有大于 100 的元素
|
|
|
int hasElementGreaterThan400(uint16_t arr[], int size) {
|
|
|
@@ -1724,6 +1770,31 @@ int parseDtu(char *json_string)
|
|
|
//if(device_status.sensor_485_sta!=0) return 0;
|
|
|
hum = cJSON_GetObjectItem(item_ext,"new_hum");
|
|
|
tmp = cJSON_GetObjectItem(item_ext,"new_tem");
|
|
|
+ cJSON* stamp_server = cJSON_GetObjectItem(item_ext,"stamp");
|
|
|
+ if(stamp_server!=NULL)
|
|
|
+ {
|
|
|
+ LOG_I("解析到温湿度stamp:%s",stamp_server->valuestring);
|
|
|
+ struct tm *tm_time = parseTimestamp(stamp_server->valuestring);
|
|
|
+ time_t timestamp;
|
|
|
+ timestamp = mktime(tm_time);
|
|
|
+ LOG_I("服务器时间戳:{}",timestamp);
|
|
|
+ //获取当前系统时间戳
|
|
|
+ struct timeval tv;
|
|
|
+ gettimeofday(&tv,NULL);
|
|
|
+ LOG_I("系统当前时间戳:{}",tv.tv_sec);
|
|
|
+ time_t timestamp_diff;
|
|
|
+ if(timestamp>tv.tv_sec)
|
|
|
+ {
|
|
|
+ timestamp_diff = timestamp - tv.tv_sec;
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ timestamp_diff = tv.tv_sec - timestamp;
|
|
|
+ }
|
|
|
+ if (tm_time != NULL&×tamp_diff>60) {
|
|
|
+ setSystemTime(tm_time);
|
|
|
+ free(tm_time); // Free the allocated memory
|
|
|
+ }
|
|
|
+ }
|
|
|
if(hum!=NULL&&tmp!=NULL)
|
|
|
{
|
|
|
//先判断是否是空字符再判断数值是否合理
|