Просмотр исходного кода

1.消除警告
2.优化debug打印
3.网关增加多个时间段的判断

cuci 1 год назад
Родитель
Сommit
1d8c14514f
6 измененных файлов с 97 добавлено и 86 удалено
  1. 85 79
      core/core.c
  2. 2 2
      core/core.h
  3. 1 1
      core/handle.request.c
  4. 3 1
      include/debug.h
  5. 6 1
      include/decode.h
  6. 0 2
      lib/decode.c

+ 85 - 79
core/core.c

@@ -194,7 +194,7 @@ int core_stats(void)
  *	Editor:			cc
  *	Description:	扫描网关设备数据
  ******************************************************************/
-int CoreAlarmGateway()
+int CoreAlarmGateway(void)
 {
   static MYSQL *conn = NULL;
   MYSQL_RES* res = NULL;
@@ -233,7 +233,7 @@ int CoreAlarmGateway()
   if(row_count<1)
   {
       mysql_free_result(res);
-      debug("row_count<1 \n");
+      debug("row_count<1 查询记录为0\n");
       return 1;
   }
   debug("CoreAlarmGateway row_count:%d \r\n",row_count);
@@ -295,7 +295,7 @@ int CoreAlarmGateway()
     sprintf(update_sql,"update dev_status set electric_update_flag=2 where device_mac='%s' and gateway_mac='%s' ",device_mac,gateway_mac);
     excuteSql(conn,update_sql);
 
-     //判断继电器数据
+     //判断继电器数据,有报警跳过后续判断
      result = RelayAlarm(gateway_mac,"0F0000000001",original_relay,conn);
      if(result==0) continue;
 
@@ -307,7 +307,7 @@ int CoreAlarmGateway()
         int result = compareModifiedTimes(tm_time2,tm_time);
         if(result>=0)//指令更新时间在2分钟内跳过后续判断
         {
-          debug("网关:%s,compareModifiedTimes tm_time2>=tm_time\n",device_name);
+          debug("网关:%s,compareModifiedTimes tm_time2>=tm_time ------------>skip\n",device_name);
           continue;
         }
      }
@@ -322,63 +322,70 @@ int CoreAlarmGateway()
      //判断自动模式
      if(control_mode==0)
      {
-        if(compareTimes(switch_time1,switch_time2)<=0)//switch_time1<switch_time2
+      int isInTime1 = 0;
+      int isInTime2 = 0;
+      if(row[8]&&row[9]&&(strlen(switch_time1)>4)&&(strlen(switch_time2)>4))//第1个时间段不为空
+      {
+        debug("网关:%s,strlen(switch_time1)=%zu,strlen(switch_time2)=%zu\n",device_name,strlen(switch_time1),strlen(switch_time2));
+        result1 = compareTimes(switch_time1,timeString);
+        result2 = compareTimes(timeString,switch_time2);
+        //时间正常顺序
+        if(compareTimes(switch_time1,switch_time2)<=0)
         {
-          debug("网关:%s,compareTimes switch_time1<switch_time2\n",device_name);
-          result1 = compareTimes(switch_time1,timeString);
-          result2 = compareTimes(timeString,switch_time2);
-          debug("result1:%d,result2:%d \n",result1,result1);
-          //合闸时间段继电器拉闸
-          if (result1<=0 && result2<=0)
-          {
-            if(current_on_off==0)
-            {
-              debug("网关:%s,合闸时间段继电器拉闸 \n",device_name);
-              int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1101,"合闸时间段继电器拉闸",conn);
-              if(ret==0) continue;
-            }
-          }
-          //拉闸时间段继电器合闸
-          else
-          {
-            if(current_on_off==1)
-            {
-              debug("网关:%s,拉闸时间段继电器合闸 \n",device_name);
-              int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1102,"拉闸时间段继电器合闸",conn);
-              if(ret==0) continue;
-            }
-          }
+          debug("网关:%s,时间正常顺序,compareTimes switch_time1<=switch_time2\n",device_name);
+          //合闸时间段
+          if (result1<=0 && result2<=0) isInTime1=1;
         }
+        //时间非正常顺序
         else
         {
-          debug("网关:%s,compareTimes switch_time1>switch_time2\n",device_name);
-          result1 = compareTimes(switch_time1,timeString);
-          result2 = compareTimes(timeString,switch_time2);
-          debug("result1:%d,result2:%d \n",result1,result1);
+          debug("网关:%s,时间非正常顺序,compareTimes switch_time1>=switch_time2\n",device_name);
           //合闸时间段
-          if (result1<=0 || result2<=0)
-          {
-            debug("网关:%s,在合闸时间段 \n",device_name);
-            if(current_on_off==0)
-            {
-              debug("网关:%s,合闸时间段继电器拉闸 \n",device_name);
-              int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1101,"合闸时间段继电器拉闸",conn);
-              if(ret==0) continue;
-            }
-          }
-          //拉闸时间段
-          else
-          {
-            debug("网关:%s,在拉闸时间段\n",device_name);
-            if(current_on_off==1)
-            {
-              debug("网关:%s,拉闸时间段继电器合闸 \n",device_name);
-              int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1102,"拉闸时间段继电器合闸",conn);
-              if(ret==0) continue;
-            }
-          }
+          if (result1<=0 || result2<=0) isInTime1=1;
         }
-        
+      }
+      else //第1个时间段为空
+      {
+        isInTime1=2;
+      }
+
+      if(row[10]&&row[11]&&(strlen(switch_time3)>4)&&(strlen(switch_time4)>4))//第2个时间段不为空
+      {
+        debug("网关:%s,strlen(switch_time3)=%zu,strlen(switch_time4)=%zu\n",device_name,strlen(switch_time3),strlen(switch_time4));
+        result3 = compareTimes(switch_time3,timeString);
+        result4 = compareTimes(timeString,switch_time4);
+        //时间正常顺序
+        if(compareTimes(switch_time3,switch_time4)<=0)
+        {
+          debug("网关:%s,时间正常顺序,compareTimes switch_time3<=switch_time4\n",device_name);
+          //合闸时间段
+          if (result3<=0 && result4<=0) isInTime2=1;
+        }
+        //时间非正常顺序
+        else
+        {
+          debug("网关:%s,时间非正常顺序,compareTimes switch_time3>=switch_time4\n",device_name);
+          //合闸时间段
+          if (result3<=0 || result4<=0) isInTime2=1;
+        }
+      }
+      else //第1个时间段为空
+      {
+        isInTime2=2;
+      }
+
+      if((isInTime1==1||isInTime2==1) && current_on_off==0)
+      {
+        debug("网关:%s,合闸时间段继电器拉闸 \n",device_name);
+        int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1101,"合闸时间段继电器拉闸",conn);
+        if(ret==0) continue;
+      }
+      else if((isInTime1==0||isInTime2==0) && current_on_off==1)
+      {
+        debug("网关:%s,拉闸时间段继电器合闸 \n",device_name);
+        int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1102,"拉闸时间段继电器合闸",conn);
+        if(ret==0) continue;
+      }       
      }
 
      //判断继电器拉闸状态有电流
@@ -423,7 +430,7 @@ int CoreAlarmGateway()
  *	Editor:			cc
  *	Description:	扫描节点设备数据
  ******************************************************************/
-int CoreAlarmNode()
+int CoreAlarmNode(void)
 {
   static MYSQL *conn2 = NULL;
   MYSQL_RES* res = NULL;
@@ -464,7 +471,7 @@ int CoreAlarmNode()
   if(row_count<1)
   {
       mysql_free_result(res);
-      debug("row_count<1 \n");
+      debug("row_count<1 查询记录为0 \n");
       return 1;
   }
   debug("CoreAlarmNode row_count:%d \r\n",row_count);
@@ -538,7 +545,7 @@ int CoreAlarmNode()
         int result = compareModifiedTimes(tm_time2,tm_time);
         if(result>=0)//指令更新时间在2分钟内跳过后续判断
         {
-          debug("网关:%s,节点:%s,compareModifiedTimes tm_time2>=tm_time\n",gateway_name,node_name);
+          debug("网关:%s,节点:%s,compareModifiedTimes tm_time2>=tm_time------------>skip\n",gateway_name,node_name);
           continue;
         }
      }
@@ -558,7 +565,7 @@ int CoreAlarmNode()
 
         if(row[9]&&row[10]&&(strlen(switch_time1)>4)&&(strlen(switch_time2)>4))//第1个时间段不为空
         {
-          debug("网关:%s,节点:%s,strlen(switch_time1)=%d,strlen(switch_time2)=%d\n",gateway_name,node_name,strlen(switch_time1),strlen(switch_time2));
+          debug("网关:%s,节点:%s,strlen(switch_time1)=%zu,strlen(switch_time2)=%zu\n",gateway_name,node_name,strlen(switch_time1),strlen(switch_time2));
           result1 = compareTimes(switch_time1,timeString);
           result2 = compareTimes(timeString,switch_time2);
           //时间正常顺序
@@ -583,7 +590,7 @@ int CoreAlarmNode()
 
         if(row[11]&&row[12]&&(strlen(switch_time3)>4)&&(strlen(switch_time4)>4))//第2个时间段不为空
         {
-          debug("网关:%s,节点:%s,strlen(switch_time3)=%d,strlen(switch_time4)=%d\n",gateway_name,node_name,strlen(switch_time3),strlen(switch_time4));
+          debug("网关:%s,节点:%s,strlen(switch_time3)=%zu,strlen(switch_time4)=%zu\n",gateway_name,node_name,strlen(switch_time3),strlen(switch_time4));
           result3 = compareTimes(switch_time3,timeString);
           result4 = compareTimes(timeString,switch_time4);
           //时间正常顺序
@@ -673,7 +680,6 @@ int ElectricAlarm(char* mac,char* subMac,char* gatewayName,char* nodeName,int al
   char strAlarm[512] = {0};
   char updateSql[1024] = {0};
   char insertSql[512] = {0};
-  int affectedRows = 0;
   int queryRow = 0;
   int fault_state=0;
   int fault_count=0;
@@ -1422,14 +1428,14 @@ int compareTimes(const char *time1, const char *time2)
     localtime_r(&current_time, &tm_base);
 
     // 解析时间字符串1
-    if (strptime(time1, "%H:%M", &tm_time1) == NULL) 
+    if (strptime(time1, "%H:%M", &tm_time1) == 0) 
     {
         fprintf(stderr, "Failed to parse time1\n");
         return 0; // or handle the error in an appropriate way
     }
 
     // 解析时间字符串2
-    if (strptime(time2, "%H:%M", &tm_time2) == NULL)
+    if (strptime(time2, "%H:%M", &tm_time2) == 0)
      {
         fprintf(stderr, "Failed to parse time2\n");
         return 0; // or handle the error in an appropriate way
@@ -1515,7 +1521,7 @@ int compareWithUpdateTime(const char *timeString,const char *time1,const char *t
   localtime_r(&current_time, &tm_base);
 
   // 解析timeString
-  if (strptime(timeString, "%H:%M", &tm_timeString) == NULL) 
+  if (strptime(timeString, "%H:%M", &tm_timeString) == 0) 
   {
     fprintf(stderr, "Failed to parse tm_timeString\n");
     return 0; // or handle the error in an appropriate way
@@ -1805,7 +1811,7 @@ void UpgradeFileAddFromDB(list_node* f_list,char* filename,MYSQL *_db,int ota_mo
 {
   char logstr[512] = {0};
   unsigned short ota_file_total_packets = 0;
-  unsigned int file_length_bytes = 0;
+  int file_length_bytes = 0;
   unsigned int ota_length_bytes = 0;
   list_node* list = f_list;
   ota_info* info = (ota_info*)malloc(sizeof(ota_info));
@@ -1824,7 +1830,7 @@ void UpgradeFileAddFromDB(list_node* f_list,char* filename,MYSQL *_db,int ota_mo
   unsigned char *fp = readfile_from_db(filename,_db,&file_length_bytes);
   if (file_length_bytes <= 0)
   {
-    log("%s file_length_bytes <= 0 ! \n",file_length_bytes);
+    log("%d file_length_bytes <= 0 ! \n",file_length_bytes);
     free(info);
     info = NULL;
     return;
@@ -1833,7 +1839,7 @@ void UpgradeFileAddFromDB(list_node* f_list,char* filename,MYSQL *_db,int ota_mo
   //计算数据库升级文件crc32校验
   init_crc32_tab();
   uint32_t crc_value = 0;
-  if(calc_crc32_from_buff(fp,file_length_bytes,&crc_value) == 0)
+  if(calc_crc32_from_buff((const char*)fp,file_length_bytes,&crc_value) == 0)
   {
     log("crc_value = %x\n",crc_value);
   }
@@ -1847,11 +1853,11 @@ void UpgradeFileAddFromDB(list_node* f_list,char* filename,MYSQL *_db,int ota_mo
 
     info->ota_mode = 1;//模式
     strcpy(info->new_file_name,filename);//新文件名
-    info->new_file_len = file_length_bytes;//新文件长度
-    info->ota_file_len = file_length_bytes;//升级文件长度
+    info->new_file_len = (uint32_t)file_length_bytes;//新文件长度
+    info->ota_file_len = (uint32_t)file_length_bytes;//升级文件长度
     info->new_file_crc = crc_value;//新文件crc32校验
     info->ota_file_crc = info->new_file_crc;//升级文件crc32校验
-    ota_file_total_packets = file_length_bytes/ota_file_packet_len + 1;
+    ota_file_total_packets = (uint32_t)file_length_bytes/ota_file_packet_len + 1;
     info->ota_file_total_packets = ota_file_total_packets;//升级文件总包数
     info->ota_file_packet_len = ota_file_packet_len;//升级文件每包数据长度
     sprintf(logstr,"新文件名:%s,新文件长度:%d,升级文件长度:%d,升级文件总包数:%d,升级文件每包数据长度:%d,新文件crc32校验:%X,升级文件crc32校验:%X \n",
@@ -1861,7 +1867,7 @@ void UpgradeFileAddFromDB(list_node* f_list,char* filename,MYSQL *_db,int ota_mo
   //差分
   else
   {
-    ota_length_bytes = file_length_bytes;
+    ota_length_bytes = (unsigned int)file_length_bytes;
     memcpy(info->buf,fp,ota_length_bytes);
     crc_value = get_crc32(0,info->buf,ota_length_bytes);//ota文件crc校验
 
@@ -1952,11 +1958,11 @@ void GetUpgradeFileFromDB(char *mac,MYSQL *_db,char* filename)
  ******************************************************************/
 unsigned char *readfile_from_db(const char *filename, MYSQL *_db,int *file_length_bytes)
 {
-  MYSQL_RES* res = NULL;
-  MYSQL_ROW row;
+  //MYSQL_RES* res = NULL;
+  //MYSQL_ROW row;
   char sql[512] = {0};
   int ret = 0;
-  unsigned long *lengths;
+  //unsigned long *lengths;
   unsigned char* data;
   MYSQL_BIND result;
 
@@ -1994,7 +2000,7 @@ unsigned char *readfile_from_db(const char *filename, MYSQL *_db,int *file_lengt
     return NULL;
   }
   log("readfile_from_db函数,total_length=%lu\n", total_length);
-  while (start<(int)total_length)
+  while (start<total_length)
   {
     result.buffer = (data+start);
     if(total_length < 1024)
@@ -2042,15 +2048,15 @@ int32_t calc_crc32_from_buff(const char *buff,int len, uint32_t *uiCrcValue)
   int index = 0;
   while(remain>0)
   {
-    if(remain > size)
+    if(remain > (int)size)
     {
-      crc = get_crc32(crc, buff + index, size);
+      crc = get_crc32(crc, (uint8_t*)(buff + index), size);
       index += size;
-      remain = remain-size;
+      remain = remain-(int)size;
     }
     else
     {
-      crc = get_crc32(crc, buff + index, remain);
+      crc = get_crc32(crc, (uint8_t*)(buff + index), remain);
       index += remain;
       remain = 0;
     }

+ 2 - 2
core/core.h

@@ -357,7 +357,7 @@ void GetUpgradeFileFromDB(char *mac,MYSQL *_db,char* filename);
  *	Editor:			cc
  *	Description:	扫描网关设备数据
  ******************************************************************/
-int CoreAlarmGateway();
+int CoreAlarmGateway(void);
 
 /******************************************************************
  *	Function Name:	core_alarm_gateway
@@ -367,7 +367,7 @@ int CoreAlarmGateway();
  *	Editor:			cc
  *	Description:	扫描节点设备数据
  ******************************************************************/
-int CoreAlarmNode();
+int CoreAlarmNode(void);
 
 /******************************************************************
  *	Function Name:	ElectricAlarm

+ 1 - 1
core/handle.request.c

@@ -343,5 +343,5 @@ int  do_SCR_OTA(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
                     " gateway_mac = '%s'",upgrade_status,rate_progress,number,mac);
     excuteSql(_db,updateSql);
 
-    return 0;
+    return ret;
 }

+ 3 - 1
include/debug.h

@@ -55,7 +55,9 @@
 
 #if defined(DEBUG_ON) || defined(LOG_ON) || defined(ERRLOG_ON)
 #include <stdio.h>
-#include<time.h>
+#define _XOPEN_SOURCE
+#define __USE_XOPEN
+#include <time.h>
 //获取系统时间年月日 时分秒
 // char* GetCurrentTimes(void);
 

+ 6 - 1
include/decode.h

@@ -1,9 +1,13 @@
 #ifndef DECODE_H_
 #define DECODE_H_
 #include <pthread.h>
-#include <sys/time.h>
+//#include <sys/time.h>
+#define _XOPEN_SOURCE
+#define __USE_XOPEN
+#include <time.h>
 #include <errno.h>
 #include <sys/syscall.h>
+//#include <math.h>
 
 #include "status.h"
 #include "globle.h"
@@ -14,6 +18,7 @@
 #include "data.h"  // add by YellowBug 2012-8-1
 #include "mysql.sdk.h"
 
+
 #define DECODE_DATA_FILE "decode.file"
 
 /******************************************************************

+ 0 - 2
lib/decode.c

@@ -42,7 +42,6 @@ void AlarmGateway(void)
 {
 	unsigned int nowTime;
 	//static MYSQL *_db = NULL;
-	int ret = 0;
 	unsigned int lastHeartTime = 0;
 
 	log("This is in AlarmGateway decode! pid=%d  tid=%ld  lwpid=%lu\n", getpid(), pthread_self(), syscall(SYS_gettid));
@@ -80,7 +79,6 @@ void AlarmNode(void)
 	//static MYSQL *_db = NULL;
 	int ret = 0;
 	unsigned int lastHeartTime = 0;
-
 	log("This is in AlarmNode decode! pid=%d  tid=%ld  lwpid=%lu\n", getpid(), pthread_self(), syscall(SYS_gettid));
 
 	while (1)