#include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" #include "db.h" #include "functions.h" #include "expression.h" int evaluate_LAST(float *last,int itemid,int parameter) { DB_RESULT *result; char c[1024]; char *field; sprintf(c,"select lastvalue from items where itemid=%d and lastvalue is not null", itemid ); result = DBselect(c); if(result==NULL) { DBfree_result(result); return FAIL; } if(DBnum_rows(result)==0) { DBfree_result(result); return FAIL; } field = DBget_field(result,0,0); if( field == NULL ) { DBfree_result(result); return FAIL; } *last=atof(field); DBfree_result(result); return SUCCEED; } int evaluate_MIN(float *min,int itemid,int parameter) { DB_RESULT *result; char c[1024]; char *field; int now; now=time(NULL); sprintf(c,"select min(value) from history where clock>%d-%d and itemid=%d",now,parameter,itemid); result = DBselect(c); if(result==NULL) { syslog(LOG_NOTICE, "Result for MIN is empty" ); DBfree_result(result); return FAIL; } if(DBnum_rows(result)==0) { syslog( LOG_NOTICE, "Result for MIN is empty" ); DBfree_result(result); return FAIL; } field = DBget_field(result,0,0); if( field == NULL ) { syslog( LOG_NOTICE, "Result for MIN is empty" ); DBfree_result(result); return FAIL; } *min=atof(field); DBfree_result(result); return SUCCEED; } int evaluate_MAX(float *max,int itemid,int parameter) { DB_RESULT *result; char c[1024]; char *field; int now; now=time(NULL); sprintf(c,"select max(value) from history where clock>%d-%d and itemid=%d",now,parameter,itemid); result = DBselect(c); if(result==NULL) { DBfree_result(result); return FAIL; } if(DBnum_rows(result)==0) { DBfree_result(result); return FAIL; } field = DBget_field(result,0,0); if( field == NULL ) { DBfree_result(result); return FAIL; } *max=atof(field); DBfree_result(result); return SUCCEED; } int evaluate_PREV(float *prev,int itemid,int parameter) { DB_RESULT *result; char c[1024]; char *field; sprintf(c,"select prevvalue from items where itemid=%d and prevvalue is not null", itemid ); result = DBselect(c); if(result==NULL) { DBfree_result(result); return FAIL; } if(DBnum_rows(result)==0) { DBfree_result(result); return FAIL; } field = DBget_field(result,0,0); if( field == NULL ) { DBfree_result(result); return FAIL; } *prev=atof(field); DBfree_result(result); return SUCCEED; } int evaluate_DIFF(float *diff,int itemid,int parameter) { float prev,last; float tmp; if(evaluate_PREV(&prev,itemid,parameter) == FAIL) { return FAIL; } if(evaluate_LAST(&last,itemid,parameter) == FAIL) { return FAIL; } tmp=last-prev; if((tmp<0.000001)&&(tmp>-0.000001)) { *diff=0; } else { *diff=1; } return SUCCEED; } int evaluate_NODATA(float *nodata,int itemid,int parameter) { DB_RESULT *result; char c[1024]; char *field; int now; now=time(NULL); sprintf(c,"select value from history where itemid=%d and clock>%d-%d limit 1",itemid,now,parameter); result = DBselect(c); if(result==NULL) { DBfree_result(result); return FAIL; } if(DBnum_rows(result)==0) { DBfree_result(result); return FAIL; } field = DBget_field(result,0,0); *nodata=0; if( field == NULL ) { *nodata=1; } DBfree_result(result); return SUCCEED; } int update_functions( int itemid ) { FUNCTION function; DB_RESULT *result; char c[1024]; float value; int ret=SUCCEED; int i,rows; sprintf(c,"select function,parameter from functions where itemid=%d group by 1,2 order by 1,2",itemid ); result = DBselect(c); if(result==NULL) { syslog( LOG_NOTICE, "No functions to update."); DBfree_result(result); return SUCCEED; } rows=DBnum_rows(result); if(rows == 0) { syslog( LOG_NOTICE, "No functions to update."); DBfree_result(result); return SUCCEED; } for(i=0;ih_addr))->s_addr; servaddr_in.sin_port=htons(25); s=socket(AF_INET,SOCK_STREAM,0); if(s==0) perror("socket"); myaddr_in.sin_family = AF_INET; myaddr_in.sin_port=0; myaddr_in.sin_addr.s_addr=INADDR_ANY; if(connect(s,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in))==-1) perror("Connect"); c=(char *)malloc(1024); if(c==NULL) perror("Cannot allocate memory."); sprintf(c,"HELO %s\n",smtp_helo); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending HELO to mailserver."); i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i==-1) perror("Error receiving data answer on HELO reqest."); sprintf(c,"MAIL FROM: %s\n",smtp_email); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending MAIL FROM to mailserver."); i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i==-1) perror("Error receiving answer on MAIL FROM request."); sprintf(c,"RCPT TO: <%s>\n",mailto); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending RCPT TO to mailserver."); i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i==-1) perror("Error receiving answer on RCPT TO request."); sprintf(c,"DATA\nSubject: %s\n",mailsubject); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending DATA to mailserver."); i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i==-1) perror("Error receiving answer on DATA request."); sprintf(c,"%s\n",mailbody); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending MailBody to mailserver."); sprintf(c,".\n"); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending . to mailserver."); i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); sprintf(c,"\n"); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending \\n to mailserver."); i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i==-1) perror("Error receiving answer on \\n request."); sprintf(c,"QUIT\n"); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e==-1) perror("Error sending QUIT to mailserver."); close(s); free(c); } void send_to_user(int actionid,int userid,char *smtp_server,char *smtp_helo,char *smtp_email,char *subject,char *message) { MEDIA media; char c[1024]; DB_RESULT *result; int i,rows; int now; sprintf(c,"select type,sendto,active from media where userid=%d",userid); result = DBselect(c); rows=DBnum_rows(result); for(i=0;i