📄 spalarm.c
字号:
#include "spalarm.h"
int debug = 0;
int g_DBisEror = -1;
unsigned int TotalBandNum;
unsigned int SPDevNum[4];
int read_ethset(void);
pthread_attr_t thread_attr;
pthread_t Alarm_Analysis_Thread;
//判断闰年的函数
int IsLeap(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else return 0;
}
//每个月有多少天
int DayOfMonth(int year,int month)
{
if(month==1||month==3||month==5||month==7||month==8||
month==10||month==12)
return 31;
else if(month==2)
{
if(IsLeap(year)) return 29;
else return 28;
}
else
return 30;
}
//用int初始化struct tm
void int_to_tm(struct tm * t, int y, int m, int d, int h, int min, int s, int wd)
{
t->tm_year = y - 1900;
t->tm_mon = m - 1;
t->tm_mday = d;
t->tm_hour = h;
t->tm_min = min;
t->tm_sec = s;
t->tm_wday = wd;
t->tm_isdst = 0;
}
//用char初始化struct tm
void char_to_tm(struct tm * t,char *t_char)
{
int year, month, day, hour, min, sec, weekday;
sscanf(t_char, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &min, &sec);
weekday = week_no(year, month, day);
int_to_tm(t, year, month, day, hour, min, sec, weekday);
}
//将struct tm 用字符串形式表示
void tm_to_char(char *t_char, struct tm *t)
{
sprintf(t_char, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year+1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
}
//比较(字符串)时间先后
int comparetime(char *time1, char *time2)
{
struct tm t1, t2;
time_t t3, t4;
double ret;
char_to_tm(&t1, time1);
char_to_tm(&t2, time2);
t3 = mktime(&t1);
t4 = mktime(&t2);
ret = difftime(t3, t4);
if (ret > 0)
return 1;
else if(ret == 0)
return 0;
else
return -1;
}
void add_day(struct tm *t, int n)
{
time_t ct;
ct = mktime(t);
ct += n*24*60*60-1;
struct tm *t_result;
t_result = localtime(&ct);
t->tm_year = t_result->tm_year;
t->tm_mon = t_result->tm_mon;
t->tm_mday = t_result->tm_mday;
t->tm_hour = t_result->tm_hour;
t->tm_min = t_result->tm_min;
t->tm_sec = t_result->tm_sec;
t->tm_wday = t_result->tm_wday;
t->tm_yday = t_result->tm_yday;
t->tm_isdst = 0;
}
void less_day(struct tm *t, int n)
{
time_t ct;
ct = mktime(t);
ct -= n*24*60*60-1;
struct tm *t_result;
t_result = localtime(&ct);
t->tm_year = t_result->tm_year;
t->tm_mon = t_result->tm_mon;
t->tm_mday = t_result->tm_mday;
t->tm_hour = t_result->tm_hour;
t->tm_min = t_result->tm_min;
t->tm_sec = t_result->tm_sec;
t->tm_wday = t_result->tm_wday;
t->tm_yday = t_result->tm_yday;
t->tm_isdst = 0;
}
void less_now_day(struct tm *t, int n)
{
time_t ct;
ct = mktime(t);
ct -= n*24*60*60;
ct -=(t->tm_hour*60*60+t->tm_min*60+t->tm_sec);
struct tm *t_result;
t_result = localtime(&ct);
t->tm_year = t_result->tm_year;
t->tm_mon = t_result->tm_mon;
t->tm_mday = t_result->tm_mday;
t->tm_hour = t_result->tm_hour;
t->tm_min = t_result->tm_min;
t->tm_sec = t_result->tm_sec;
t->tm_wday = t_result->tm_wday;
t->tm_yday = t_result->tm_yday;
t->tm_isdst = 0;
}
void get_weekarea_head(char *time_b, char *week_b, char *week_e)
{
struct tm t;
char_to_tm(&t, time_b);
t.tm_hour=0;
t.tm_min = 0;
t.tm_sec = 0;
t.tm_isdst = 0;
sprintf(week_b, "%04d-%02d-%02d 00:00:00", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
if (t.tm_wday == 0)
{
sprintf(week_e, "%04d-%02d-%02d 23:59:59", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
}
else
{
add_day(&t, 8-t.tm_wday);
sprintf(week_e, "%04d-%02d-%02d 23:59:59", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
}
}
void get_montharea_head(char *time_b, char *mon_b, char *mon_e)
{
struct tm t;
char_to_tm(&t, time_b);
sprintf(mon_b, "%04d-%02d-%02d 00:00:00", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
int day = DayOfMonth(t.tm_year+1900, t.tm_mon+1);
sprintf(mon_e, "%04d-%02d-%02d 23:59:59", t.tm_year+1900, t.tm_mon+1, day);
}
void get_yeararea_head(char *time_b, char *year_b, char *year_e)
{
struct tm t;
char_to_tm(&t, time_b);
sprintf(year_b, "%04d-%02d-%02d 00:00:00", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
sprintf(year_e, "%04d-12-31 23:59:59", t.tm_year+1900);
}
void get_weekarea_tail(char *time_e, char *week_b, char *week_e)
{
struct tm t;
char_to_tm(&t, time_e);
t.tm_hour=23;
t.tm_min = 59;
t.tm_sec = 59;
t.tm_isdst = 0;
sprintf(week_e, "%04d-%02d-%02d 23:59:59", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
if (t.tm_wday == 0)
{
sprintf(week_b, "%04d-%02d-%02d 00:00:00", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
}
else
{
less_day(&t, t.tm_wday);
sprintf(week_b, "%04d-%02d-%02d 00:00:00", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
}
}
void get_montharea_tail(char *time_e, char *mon_b, char *mon_e)
{
struct tm t;
char_to_tm(&t, time_e);
sprintf(mon_e, "%04d-%02d-%02d 23:59:59", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
sprintf(mon_b, "%04d-%02d-01 00:00:00", t.tm_year+1900, t.tm_mon+1);
}
void get_yeararea_tail(char *time_e, char *year_b, char *year_e)
{
struct tm t;
char_to_tm(&t, time_e);
sprintf(year_e, "%04d-%02d-%02d 23:59:59", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
sprintf(year_b, "%04d-01-01 00:00:00", t.tm_year+1900);
}
void get_weekarea_now(char *time_now, char *week_b, char *week_e)
{
struct tm t;
char_to_tm(&t, time_now);
strcpy(week_e, time_now);
if (t.tm_wday == 0)
less_now_day(&t, 7-1);
else
less_now_day(&t, t.tm_wday-1);
sprintf(week_b, "%04d-%02d-%02d 00:00:00", t.tm_year+1900, t.tm_mon+1, t.tm_mday);
}
void get_montharea_now(char *time_now, char *mon_b, char *mon_e)
{
struct tm t;
char_to_tm(&t, time_now);
strcpy(mon_e, time_now);
sprintf(mon_b, "%04d-%02d-01 00:00:00", t.tm_year+1900, t.tm_mon+1);
}
void get_yeararea_now(char *time_b, char *year_b, char *year_e)
{
struct tm t;
char_to_tm(&t, now);
strcpy(year_e, now);
sprintf(year_b, "%04d-01-01 00:00:00", t.tm_year+1900);
}
void get_timeby_week(int i, char *begin, char *end)
{
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_weekarea_head(sp_control_table[i].time_b, begin, end);
if (comparetime(begin, now)<=0 && comparetime(now, end)<=0)
{
memcpy(end, now, strlen(now));
return ;
}
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_weekarea_tail(sp_control_table[i].time_e, begin, end);
if (comparetime(begin, now)<=0 && comparetime(now, end)<=0)
{
memcpy(end, now, strlen(now));
return ;
}
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_weekarea_now(now, begin, end);
}
void get_timeby_month(int i, char *begin, char *end)
{
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_montharea_head(sp_control_table[i].time_b, begin, end);
if (comparetime(begin, now)<=0 && comparetime(now, end)<=0)
{
memcpy(end, now, strlen(now));
return ;
}
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_montharea_tail(sp_control_table[i].time_e, begin, end);
if (comparetime(begin, now)<=0 && comparetime(now, end)<=0)
if (comparetime(begin, now)<=0 && comparetime(now, end)<=0)
{
memcpy(end, now, strlen(now));
return ;
}
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_montharea_now(now, begin, end);
}
void get_timeby_year(int i, char *begin, char *end)
{
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_yeararea_head(sp_control_table[i].time_b, begin, end);
if (comparetime(begin, now)<=0 && comparetime(now, end)<=0)
{
memcpy(end, now, strlen(now));
return ;
}
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_yeararea_tail(sp_control_table[i].time_e, begin, end);
if (comparetime(begin, now)<=0 && comparetime(now, end)<=0)
{
memcpy(end, now, strlen(now));
return ;
}
memset(begin, 0, sizeof(begin));
memset(end, 0, sizeof(end));
get_yeararea_now(now, begin, end);
}
void get_time_begin_end(int type, int i, char *begin, char *end)
{
switch (type)//判断统计方式 0:每周 1:每月 2:每年
{
case 0://每周
get_timeby_week(i, begin, end);
break;
case 1://每月
get_timeby_month(i, begin, end);
break;
case 2://每年
get_timeby_year(i, begin, end);
break;
default:
break;
}
}
//判断给定日期是星期几
int week_no (int y , int m , int d )
{
/* 输入 y 年 ,m 月,d 日
* 返回值 为星期几
*/
int x ;
int first_data[]={0,6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
x = first_data[m] + d -1 ;
x += (y -2000 + 3 )/4 ;
x += y - 2000 ;
if (!( y % 4 ) && (m > 2 ))
x++;
return (x%7);
}
/*
**@brief 读取配置文件
**@param *filename 类型 char 需要打开的文件名,允许使用绝对路径
**@param *scetion 类型 char 主节点
**@param *item 类型 char 主节点
**@param *scetion 类型 char 关键字
**@param *value 类型 char 取出的值
**@param *Default 类型 char 缺省值
**@return int
*/
int ReadConfig( char *filename, char *scetion, char *item, char *value, char *Default )
{
FILE *fp;
char readStr[200], tmpstr[ 31 ];
int findScetion = 0;
if( ( fp = fopen(filename, "r") ) == NULL ) {
strcpy(value, Default);
return -1;
}
while(!feof(fp)) {
memset( readStr, 0x00, sizeof( readStr ) );
fgets( readStr, 80, fp );
readStr[strlen(readStr)-1] = 0; // 去掉换行符 '0x0A'
memset(tmpstr, 0, sizeof(tmpstr));
if (readStr[0] == '[') {
findScetion = 0;
memcpy(tmpstr, readStr + 1, strlen(scetion) );
if (memcmp(scetion, tmpstr, strlen(tmpstr) ) == 0)
findScetion = 1;
}
else{
if (findScetion == 0)
continue;
if (memcmp(readStr, item, strlen(item)) == 0) {
strcpy(value, readStr + strlen(item) + 1);
break;
}
}
}
fclose(fp);
if (findScetion == 0) {
if ( Default != NULL ) {
strcpy(value, Default);
return 1;
}
return -1;
}
return 1;
}
/**
**@breif 得到系统时间
**@param *ctime 类型 char 时间
**@return void
*/
inline void get_ctime(char *ctime)
{
time_t ct;
struct tm *mtm = 0;
ct = time(NULL);
mtm = localtime(& ct);
sprintf(ctime, "%04d-%02d-%02d %02d:%02d:%02d", mtm->tm_year + 1900, mtm->tm_mon + 1, mtm->tm_mday, mtm->tm_hour, mtm->tm_min, mtm->tm_sec);
//if(mtm)
// free(mtm);
return;
}
void int_tests(char *name, int i)
{
char temp[50];
char temp1[30];
memset(temp, 0, sizeof(temp));
memset(temp1, 0, sizeof(temp1));
strcat(temp, name);
sprintf(temp1, " is : %d\n", i);
strcat(temp, temp1);
tests(temp);
}
void mytests(char *test)
{
tests(test);
}
/**
**@breif 记录调试信息
**@param *test 类型 char 调试信息
**@return void
*/
void tests(char *test)
{
/*
if(0==debug)
return ;
*/
return;
FILE *fp ;
char ss[30] ;
fp = fopen("/debug_spalarm.txt", "a") ;
get_ctime(ss) ;
fputs(ss, fp) ;
fputs("\n", fp) ;
fputs(test, fp) ;
fputs("\n", fp) ;
fclose(fp) ;
}
int base64(char *s,char *d,int sl)
{
char CharSet[64]=
{
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'
};
unsigned char In[3];
unsigned char Out[4];
int cnt=0;
int i = 0;
if(sl==0)return 0;
int dl=0;
for(i=0;i<sl;i=i+3)
{
if((sl-i)>=3)
{
In[0]=*s;
In[1]=*(s+1);
In[2]=*(s+2);
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
Out[2]=(In[1]&0x0f)<<2|(In[2]&0xc0)>>6;
Out[3]=In[2]&0x3f;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)=CharSet[Out[2]];
*(d+3)=CharSet[Out[3]];
s+=3;
d+=4;
}
else if((sl-i)==1)
{
In[0]=*s;
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|0;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)='=';
*(d+3)='=';
s+=1;
d+=4;
}
else if((sl-i)==2)
{
In[0]=*s;
In[1]=*(s+1);
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
Out[2]=(In[1]&0x0f)<<2|0;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)=CharSet[Out[2]];
*(d+3)='=';
s+=2;
d+=4;
}
cnt+=4;
dl+=4;
}
*d='\0';
return dl;
}
int CheckNetMask(char mask[16])
{
int returnvalue=-1;
char maskA[3];
char maskB[3];
char maskC[3];
char maskD[3];
char netmask[16];
int suffix[3];
int i=0,j=0;
int mamk_a,mask_b,mask_c,mask_d;
memset(maskA,'0',sizeof(maskA));
memset(maskB,'0',sizeof(maskB));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -