123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- #include "Module_OcppBackend.h"
- typedef enum boolean { FALSE, TRUE } BOOL;
- static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
- int MessageSent_add(char *uuid, char *data)
- {
- FILE *outfile;
- char rmFileCmd[100]={0};
- struct stat stats;
- char tempstring[100]={0};
- stat("/Storage/OCPP", &stats);
-
- if (S_ISDIR(stats.st_mode) == 1)
- {
-
- }
- else
- {
- printf("\n directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(&rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
- {
-
- }
- else
- {
- printf("MessageSent not exist\n");
- FILE *log = fopen("/Storage/OCPP/MessageSent", "w+");
- if(log == NULL)
- {
- printf("Can't Create File MessageSent \n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
-
- outfile = fopen ("/Storage/OCPP/MessageSent", "a");
- sprintf(tempstring,"%s,%s\n", uuid,data);
- fputs(tempstring, outfile);
- fclose (outfile);
-
- return 1;
- }
- int MessageSent_get(char *uuid, char *data)
- {
- FILE *fp;
- int result = FALSE;
- char str[1200]={0};
- char sstr[50]={ 0 }, datastr[30]={0};
-
- int c = 0;
- char *loc;
- char rmFileCmd[100]={0};
- struct stat stats;
- stat("/Storage/OCPP", &stats);
-
- if (S_ISDIR(stats.st_mode) == 1)
- {
-
- }
- else
- {
- printf("\n directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(&rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
- {
-
- }
- else
- {
- printf("MessageSent not exist\n");
- FILE *log = fopen("/Storage/OCPP/MessageSent", "w+");
- if(log == NULL)
- {
- printf("Can't Create File MessageSent \n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
-
- fp = fopen("/Storage/OCPP/MessageSent" , "r");
- if(fp == NULL) {
- printf("Error opening file");
- return FALSE;
- }
- c = fgetc(fp);
-
- rewind(fp);
- if(c == EOF)
- {
- printf("MessageSent is null\n");
-
- strcpy(data,"");
- result = FALSE;
- }
- else
- {
- result = FALSE;
- while (fgets (str, 1200, fp)!=NULL)
- {
-
- str[strlen(str) - 1] = '\0';
-
-
-
-
- int d = 0;
- while (str[d] != ',')
- {
- sstr[d] = str[d];
- d=d+ 1;
- }
- sstr[d] = '\0';
-
-
- if(strcmp(sstr, uuid) == 0)
- {
-
- loc = strstr(str, ",");
- memset(sstr ,0, sizeof(sstr) );
- int e = 0;
- while (loc[1+e] != '\0')
- {
- datastr[e] = loc[1+e];
- e++;
- }
- datastr[e] = '\0';
-
-
- strcpy(data,datastr);
- result = TRUE;
- break;
- }
- }
- }
- fclose(fp);
- return result;
- }
- int MessageSent_remove(char *uuid, char *data)
- {
- char tempfile[] = "/Storage/OCPP/temp1.json";
- FILE *infile;
- FILE *outfile;
- int resultRename=0;
- char filename[60]={0};
- char rmFileCmd[100]={0};
- char tempstring[100]={0};
- struct stat stats;
- stat("../Storage/OCPP", &stats);
- if (S_ISDIR(stats.st_mode) == 1)
- {
-
- }
- else
- {
- printf("\n directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
- system(rmFileCmd);
- }
- memset(&rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
- {
-
- }
- else
- {
- printf("MessageSent not exist\n");
- FILE *log = fopen("/Storage/OCPP/MessageSent", "w+");
- if(log == NULL)
- {
- printf("log is NULL\n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- sprintf(tempstring,"%s,%s", uuid,data);
- strcpy(filename, "/Storage/OCPP/MessageSent");
- infile = fopen ("/Storage/OCPP/MessageSent", "r");
- outfile = fopen (tempfile, "w");
- int c;
- c = fgetc(infile);
- rewind(infile);
- if(c == EOF)
- {
- printf("MessageSent is NULL\n");
- fclose(infile);
- fclose(outfile);
- sprintf(rmFileCmd,"rm -f %s",tempfile);
- system(rmFileCmd);
- }
- else
- {
- char buf[1200]={0};
-
-
- while (fgets(buf, sizeof(buf), infile) != NULL)
- {
-
- buf[strlen(buf) - 1] = '\0';
-
-
-
- if(strcmp(tempstring, buf)== 0)
- {
-
- }
- else
- {
- fprintf(outfile,"%s\n", buf);
- }
-
- }
- fclose(infile);
- fclose(outfile);
- sprintf(rmFileCmd,"rm -f %s",filename);
- system(rmFileCmd);
- resultRename = rename(tempfile, filename);
- if(resultRename == 0)
- {
-
- }
- else
- {
- printf("Error: unable to rename the file");
- }
- }
- return 0;
- }
- int hashmap_operation(int type, char *uuid, char *data)
- {
- pthread_mutex_lock(&m);
- int result=0;
- if(type == HASH_OP_ADD)
- result = MessageSent_add(uuid, data);
- else if(type == HASH_OP_GET)
- result = MessageSent_get(uuid,data);
- else if(type == HASH_OP_REMOVE)
- result = MessageSent_remove(uuid, data);
- pthread_mutex_unlock(&m);
- return result;
- }
|