Module_OcppBackend.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/socket.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <linux/sockios.h>
  13. #include <linux/socket.h>
  14. #include <arpa/inet.h>
  15. #include <netinet/in.h>
  16. #include <unistd.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <termios.h>
  23. #include <errno.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include <ctype.h>
  28. #include <ifaddrs.h>
  29. #include "libwebsockets.h"
  30. #include <lws_config.h>
  31. #include "hashmap.h"
  32. #include "SystemLogMessage.h"
  33. #include "ShareMemory.h"
  34. #include <pthread.h>
  35. #include "MessageHandler.h"
  36. #include "sqlite3.h"
  37. #if 0
  38. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  39. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  40. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  41. #endif
  42. #define Debug
  43. //#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  44. #define PASS 1
  45. #define FAIL -1
  46. typedef enum boolean { FALSE, TRUE } BOOL;
  47. struct lws *wsi_client;
  48. struct lws_context *context;
  49. static int sendbuffer = 0;
  50. extern int server_sign;
  51. //extern int TransactionMessageAttemptsValue;
  52. //extern int TransactionMessageRetryIntervalValue;
  53. //extern int server_pending;
  54. //extern struct OCPP16Data *ShmOCPP16Data;
  55. extern void CheckSystemValue(void);
  56. //extern int TransactionMessageAttemptsGet(void);
  57. extern int FirstHeartBeatResponse(void);
  58. extern void OCPP_get_TableAuthlocalAllData(void);
  59. extern int TransactionMessageAttemptsGet(void);
  60. extern int TransactionMessageRetryIntervalGet(void);
  61. extern int GetOcppConnStatus(void);
  62. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  63. pthread_mutex_t lock_sentData = PTHREAD_MUTEX_INITIALIZER;
  64. //struct json_object *parsed_json;
  65. //extern struct node Node;
  66. //==========================================
  67. // Function prototype
  68. //==========================================
  69. void trim(char *s);
  70. int mystrcmp(char *p1,char *p2);
  71. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  72. void getSubStr(char *dest, char* src, char *split, int idx);
  73. void split(char **arr, char *str, const char *del);
  74. int strpos(char *source, char *substr, int skip);
  75. int strposs(char *source, char *substr, int idx);
  76. char *random_uuid( char buf[37] );
  77. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
  78. char * strchr(const char *p, int ch);
  79. void ReceivedMessage(void *in, size_t len);
  80. struct StartTime
  81. {
  82. unsigned int connect;
  83. unsigned int bootNotification;
  84. }startTime;
  85. #if 0
  86. unsigned char *SendBuffer;
  87. int SendBufLen=(1024*4);//(1024*3);
  88. #endif
  89. int SendBufLen=(1024*4);//(1024*3);
  90. unsigned char SendBuffer[1024*4]={0};
  91. static int ConnectionEstablished=0;
  92. static int TransactionMessageResend = 1;
  93. static int TransactionQueueInterval = 10;//3; // TransactionMessageAttempts
  94. static int TransactionQueueNum = 0;
  95. static int OfflineTransaction = 0;
  96. int defaultWaitingTime = 10; //10 second
  97. extern int BootNotificationInterval;
  98. //char guid[37];
  99. //map_t hashMap;
  100. //data_struct_t* mapItem; --- remove for temporally
  101. //data_struct_t mapItem[0]={0};
  102. sqlite3 *db;
  103. char *errMsg = NULL;
  104. static char *createsql = "CREATE TABLE IF NOT EXISTS log_buffer("
  105. "idx integer primary key,"
  106. "user_id text,"
  107. "cmd_sn text,"
  108. "charger_id text,"
  109. "gun_type text,"
  110. "gun_no text,"
  111. "rfid_no text,"
  112. "stime text,"
  113. "etime text,"
  114. "time_len text,"
  115. "s_soc text,"
  116. "e_soc text,"
  117. "stop_reason text,"
  118. "power text,"
  119. "meter_before text,"
  120. "meter_after text,"
  121. "charge_price text,"
  122. "reserve text,"
  123. "surplus_before text,"
  124. "surplus_after text,"
  125. "service_price text,"
  126. "is_pay text,"
  127. "charge_strategy text,"
  128. "charge_parameter text,"
  129. "vin text,"
  130. "vehicle_no text,"
  131. "start_method text,"
  132. "card_type text,"
  133. "is_upload text,"
  134. "guid text UNIQUE,"
  135. "is_buf2OK text);";
  136. #if 0
  137. static char *createsChargingRecordsql = "CREATE TABLE IF NOT EXISTS ChargingRecord("
  138. "idx integer primary key,"
  139. "gun_type text,"
  140. "connectorId text,"
  141. "idTag text,"
  142. "transactionId text,"
  143. "stime text,"
  144. "etime text,"
  145. "time_len text,"
  146. "s_soc text,"
  147. "e_soc text,"
  148. "stop_reason text,"
  149. "power text,"
  150. "meter_before text,"
  151. "meter_after text,"
  152. "reservationId text,"
  153. "guid text UNIQUE);";
  154. //"is_buf2OK text);";
  155. #endif
  156. static char *sqlOcppAuthCache = "create table if not exists ocpp_auth_cache (idx integer primary key,"
  157. "idtag text UNIQUE,"
  158. "parent_idtag text,"
  159. "expir_date text,"
  160. "status text);";
  161. static char *sqlOcppAuthLocal = "create table if not exists ocpp_auth_local (idx integer primary key,"
  162. "idtag text UNIQUE,"
  163. "parent_idtag text,"
  164. "expir_date text,"
  165. "status text,"
  166. "version text);";
  167. #if 0
  168. static char *sqlInitialOcppAuthLocal = "insert or replace into ocpp_auth_local(idtag, parent_idtag, expir_date, status, version) values('PaHdImHiOnNG','none','2099-12-31T23:59:59.999Z','Accepted','0')";
  169. #endif
  170. int DiffTimeb(struct timeb ST, struct timeb ET)
  171. {
  172. //return milli-second
  173. unsigned int StartTime,StopTime;
  174. StartTime=(unsigned int)ST.time;
  175. StopTime=(unsigned int)ET.time;
  176. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  177. }
  178. //=================================
  179. // Common routine
  180. //=================================
  181. void trim(char *s)
  182. {
  183. int i=0, j, k, l=0;
  184. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  185. i++;
  186. j = strlen(s)-1;
  187. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  188. j--;
  189. if(i==0 && j==strlen(s)-1) { }
  190. else if(i==0) s[j+1] = '\0';
  191. else {
  192. for(k=i; k<=j; k++) s[l++] = s[k];
  193. s[l] = '\0';
  194. }
  195. }
  196. int mystrcmp(char *p1,char *p2)
  197. {
  198. while(*p1==*p2)
  199. {
  200. if(*p1=='\0' || *p2=='\0')
  201. break;
  202. p1++;
  203. p2++;
  204. }
  205. if(*p1=='\0' && *p2=='\0')
  206. return(PASS);
  207. else
  208. return(FAIL);
  209. }
  210. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  211. {
  212. strncpy(dest, src + start, cnt);
  213. dest[cnt] = 0;
  214. }
  215. void getSubStr(char *dest, char* src, char *split, int idx)
  216. {
  217. int start = (strposs(src,",",idx)+1);
  218. int cnt = (strposs(src,",",idx+1)-2)-(strposs(src,",",idx)+1);
  219. strncpy(dest, src + start, cnt);
  220. dest[cnt] = 0;
  221. }
  222. void split(char **arr, char *str, const char *del)
  223. {
  224. char *s = strtok(str, del);
  225. while(s != NULL)
  226. {
  227. *arr++ = s;
  228. s = strtok(NULL, del);
  229. }
  230. }
  231. int strpos(char *source, char *substr, int skip)
  232. {
  233. char stack[strlen(source)];
  234. strncpy(stack, source+skip, strlen(source)-skip);
  235. char *p = strstr(stack, substr);
  236. if (p)
  237. return p - stack+skip;
  238. return -1;
  239. }
  240. int strposs(char *source, char *substr, int idx)
  241. {
  242. char stack[strlen(source)];
  243. int result=0;
  244. int count=0;
  245. while(count<=idx)
  246. {
  247. memset(stack,0,sizeof stack);
  248. strncpy(stack, source+result, strlen(source)-result);
  249. int loc = strcspn(stack, substr);
  250. if(loc>0)
  251. result += (loc+1);
  252. else
  253. result = -1;
  254. count++;
  255. }
  256. return result;
  257. }
  258. char *random_uuid( char buf[37] )
  259. {
  260. const char *c = "89ab";
  261. char *p = buf;
  262. int n;
  263. for( n = 0; n < 16; ++n )
  264. {
  265. int b = rand()%255;
  266. switch( n )
  267. {
  268. case 6:
  269. sprintf(p, "4%x", b%15 );
  270. break;
  271. case 8:
  272. sprintf(p, "%c%x", c[rand()%strlen(c)], b%15 );
  273. break;
  274. default:
  275. sprintf(p, "%02x", b);
  276. break;
  277. }
  278. p += 2;
  279. switch( n )
  280. {
  281. case 3:
  282. case 5:
  283. case 7:
  284. case 9:
  285. *p++ = '-';
  286. break;
  287. }
  288. }
  289. *p = 0;
  290. return buf;
  291. }
  292. //==========================================
  293. // Web socket tranceive routine
  294. //==========================================
  295. int SendData(struct lws *wsi)
  296. {
  297. int n;
  298. int len;
  299. unsigned char out[LWS_SEND_BUFFER_PRE_PADDING + 4096 + LWS_SEND_BUFFER_POST_PADDING] = {0};
  300. len = strlen((char *)SendBuffer);
  301. if(len == 0)
  302. return 0;
  303. memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
  304. DEBUG_OCPPMESSAGE_INFO("out + LWS_SEND_BUFFER_PRE_PADDING = %s\n", out + LWS_SEND_BUFFER_PRE_PADDING);
  305. n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
  306. memset(SendBuffer, 0, len);
  307. return n;
  308. }
  309. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
  310. {
  311. switch (reason)
  312. {
  313. case LWS_CALLBACK_PROTOCOL_INIT:
  314. #ifdef SystemLogMessage
  315. DEBUG_INFO("LWS_CALLBACK_PROTOCOL_INIT\n");
  316. #endif
  317. break;
  318. case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH:
  319. #ifdef SystemLogMessage
  320. DEBUG_INFO("LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH\n");
  321. #endif
  322. break;
  323. case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
  324. #ifdef SystemLogMessage
  325. DEBUG_INFO("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
  326. #endif
  327. break;
  328. case LWS_CALLBACK_WSI_DESTROY:
  329. #ifdef SystemLogMessage
  330. DEBUG_INFO("LWS_CALLBACK_WSI_DESTROY\n");
  331. #endif
  332. server_sign = FALSE;
  333. break;
  334. case LWS_CALLBACK_LOCK_POLL:
  335. #ifdef SystemLogMessage
  336. //DEBUG_INFO("LWS_CALLBACK_LOCK_POLL\n");
  337. #endif
  338. break;
  339. case LWS_CALLBACK_ADD_POLL_FD:
  340. #ifdef SystemLogMessage
  341. DEBUG_INFO("LWS_CALLBACK_ADD_POLL_FD\n");
  342. #endif
  343. break;
  344. case LWS_CALLBACK_DEL_POLL_FD:
  345. #ifdef SystemLogMessage
  346. DEBUG_INFO("LWS_CALLBACK_DEL_POLL_FD\n");
  347. #endif
  348. break;
  349. case LWS_CALLBACK_UNLOCK_POLL:
  350. #ifdef SystemLogMessage
  351. //DEBUG_INFO("LWS_CALLBACK_UNLOCK_POLL\n");
  352. #endif
  353. break;
  354. case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
  355. #ifdef SystemLogMessage
  356. //DEBUG_INFO("LWS_CALLBACK_CHANGE_MODE_POLL_FD\n");
  357. #endif
  358. break;
  359. case LWS_CALLBACK_WSI_CREATE:
  360. #ifdef SystemLogMessage
  361. DEBUG_INFO("LWS_CALLBACK_WSI_CREATE\n");
  362. #endif
  363. break;
  364. case LWS_CALLBACK_GET_THREAD_ID:
  365. //#ifdef SystemLogMessage
  366. //DEBUG_INFO("LWS_CALLBACK_GET_THREAD_ID\n");
  367. //#endif
  368. break;
  369. case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
  370. #ifdef SystemLogMessage
  371. DEBUG_INFO("LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER\n");
  372. #endif
  373. break;
  374. case LWS_CALLBACK_CLIENT_ESTABLISHED://3
  375. #ifdef SystemLogMessage
  376. DEBUG_INFO("LWS_CALLBACK_CLIENT_ESTABLISHED\n");
  377. #endif
  378. //connected
  379. ConnectionEstablished=1;
  380. break;
  381. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
  382. #ifdef Debug
  383. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR:%s\n",in);
  384. #endif
  385. #ifdef SystemLogMessage
  386. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
  387. #endif
  388. //disconnected
  389. ConnectionEstablished=0;
  390. break;
  391. case LWS_CALLBACK_CLOSED://4
  392. #ifdef SystemLogMessage
  393. DEBUG_INFO("LWS_CALLBACK_CLOSED\n");
  394. ConnectionEstablished=0;
  395. #endif
  396. //disconnected
  397. break;
  398. case LWS_CALLBACK_CLIENT_WRITEABLE://10
  399. //if(need to send message and its relevant data already store into SendBuffer)
  400. #ifdef SystemLogMessage
  401. DEBUG_INFO("LWS_CALLBACK_CLIENT_WRITEABLE\n");
  402. #endif
  403. SendData(wsi);
  404. break;
  405. case LWS_CALLBACK_CLIENT_RECEIVE://8
  406. ((char *)in)[len] = '\0';
  407. #ifdef SystemLogMessage
  408. DEBUG_INFO("LWS_CALLBACK_CLIENT_RECEIVE, RX length: %d\n", (int)len);
  409. #endif
  410. DEBUG_OCPPMESSAGE_INFO("%s\n", (char *)in);
  411. ReceivedMessage(in, len);
  412. break;
  413. default:
  414. #ifdef Debug
  415. DEBUG_INFO("OCPP16Callback:reason=%d\n", reason);
  416. #endif
  417. break;
  418. }
  419. return 0;
  420. }
  421. static struct lws_protocols protocols[] = {
  422. {
  423. "ocpp1.6",
  424. OCPP16Callback,
  425. 10240,
  426. 10240,
  427. },
  428. {
  429. "ocpp1.6",
  430. OCPP16Callback,
  431. 10240,
  432. 10240,
  433. },
  434. {
  435. NULL, NULL, 0 /* End of list */
  436. }
  437. };
  438. int ConnectWsServer()
  439. {
  440. int result = PASS;
  441. struct lws_context_creation_info ContextInfo;
  442. struct lws_client_connect_info ConnInfo;
  443. int use_ssl=0;
  444. //lws_set_log_level(LLL_PARSER | LLL_HEADER, NULL);
  445. if(context!=NULL)
  446. lws_context_destroy(context);
  447. memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
  448. ContextInfo.port = CONTEXT_PORT_NO_LISTEN;
  449. ContextInfo.iface = NULL;
  450. ContextInfo.ssl_private_key_password = NULL;
  451. ContextInfo.ssl_cert_filepath = NULL;
  452. ContextInfo.ssl_private_key_filepath = NULL;
  453. ContextInfo.ssl_ca_filepath = "/root/cacert.pem";
  454. ContextInfo.ssl_cipher_list = NULL; //use default one
  455. ContextInfo.gid = -1;
  456. ContextInfo.uid = -1;
  457. if(use_ssl)
  458. ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  459. ContextInfo.protocols = protocols;
  460. ContextInfo.timeout_secs = 9999;//30;
  461. //if(ping pong enabled)
  462. ContextInfo.ws_ping_pong_interval = 30;//0 for none, else interval in seconds
  463. context = lws_create_context(&ContextInfo);
  464. if (context == NULL)
  465. {
  466. #ifdef SystemLogMessage
  467. DEBUG_ERROR("lws_create_context NG");
  468. #endif
  469. result = FAIL;
  470. }
  471. memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
  472. // fill up below information
  473. ConnInfo.context = context;
  474. ConnInfo.address=(const char *)GetOcppServerURL();//"172.17.40.13";//"192.168.137.1";//"192.168.201.202";//"172.1.0.109";//"172.17.40.13";//"192.168.201.202";//"192.168.10.125";
  475. ConnInfo.port = GetOcppPort();//2012;//8080;
  476. ConnInfo.path=(const char *)GetOcppPath();//"/ocpp/RDTEST103";//"/ocpp/OCTT_1";//"/RDTEST103";//"/0591201511030003";//"/RDTEST103";
  477. ConnInfo.host=lws_canonical_hostname(context);
  478. ConnInfo.origin="origin";
  479. ConnInfo.protocol = protocols[1].name;
  480. ConnInfo.ietf_version_or_minus_one = -1;
  481. if(use_ssl)
  482. ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;;
  483. wsi_client = lws_client_connect_via_info(&ConnInfo);
  484. if (!wsi_client)
  485. {
  486. #ifdef SystemLogMessage
  487. DEBUG_ERROR("lws_client_connect_via_info NG");
  488. #endif
  489. result = FAIL;
  490. }
  491. return result;
  492. }
  493. #if 0
  494. struct _node {
  495. char uuid[37];
  496. char data[2000];
  497. struct _node *next;//struct node *next;
  498. }node;
  499. typedef struct _node *pnode;
  500. struct _node staticNodeArray[100]={0};
  501. #endif
  502. //void createq();
  503. int showfront(char *uuid, char *data);
  504. int addq(char *uuid, char *data) ;
  505. int delq();
  506. int sentqueue();
  507. #if 0
  508. int storequeue();
  509. int readfiletoqueue();
  510. #endif
  511. void CheckTransactionPacket(char *uuid);
  512. int queue_operation(int type, char *frontUUID, char *frontData);
  513. #if 0
  514. pnode front, rear;
  515. void createq() {
  516. //front = rear = (pnode)malloc(sizeof(node)); --- remove temporally
  517. //front->next = rear->next = NULL;
  518. front = rear = NULL;
  519. }
  520. #endif
  521. int showfront(char *uuid, char *data) {
  522. FILE *fp;
  523. int result = FALSE; // 1: TRUE 0:FALSE
  524. char str[1200]={0};
  525. char sstr[50]={ 0 };//sstr[200]={ 0 };
  526. int c = 0;
  527. char *loc;
  528. char rmFileCmd[100]={0};
  529. struct stat stats;
  530. stat("../Storage/OCPP", &stats);
  531. // Check for directory existence
  532. if (S_ISDIR(stats.st_mode) == 1)
  533. {
  534. //DEBUG_INFO("\n OCPP directory exist \n");
  535. }
  536. else
  537. {
  538. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  539. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  540. system(rmFileCmd);
  541. }
  542. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  543. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  544. {
  545. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  546. }
  547. else
  548. {
  549. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  550. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  551. if(log == NULL)
  552. {
  553. DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  554. return 0;
  555. }
  556. else
  557. {
  558. fclose(log);
  559. }
  560. }
  561. /* opening file for reading */
  562. fp = fopen("../Storage/OCPP/TransactionRelatedQueue" , "r");
  563. if(fp == NULL) {
  564. DEBUG_INFO("Error opening TransactionRelatedQueue file");
  565. return FALSE;
  566. }
  567. if( fgets (str, 1200, fp)!=NULL ) {
  568. /* writing content to stdout */
  569. if (str[0] == '\n')
  570. {
  571. DEBUG_INFO("It is a blank line");
  572. fclose(fp);
  573. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  574. sprintf(rmFileCmd,"rm -f %s","../Storage/OCPP/TransactionRelatedQueue");
  575. system(rmFileCmd);
  576. result = FALSE;
  577. return result;
  578. }
  579. else
  580. {
  581. puts(str);
  582. /*********************uuid***************/
  583. loc = strstr(str, "\"");
  584. memset(sstr ,0, sizeof(sstr) );
  585. c = 0;
  586. while (loc[1+c] != '\"')
  587. {
  588. sstr[c] = loc[1+c];
  589. c++;
  590. }
  591. sstr[c] = '\0';
  592. DEBUG_INFO("\n uuid:%s", sstr);
  593. DEBUG_INFO("\n data:%s", str);
  594. strcpy(uuid,sstr);
  595. strcpy(data,str);
  596. result = TRUE;
  597. }
  598. //return 1;
  599. }
  600. else
  601. {
  602. //DEBUG_INFO("queue is null\n");
  603. strcpy(uuid,"");
  604. strcpy(data,"");
  605. result = FALSE;
  606. //return 0;
  607. }
  608. fclose(fp);
  609. return result;
  610. #if 0
  611. pnode p;
  612. p = front;
  613. if(p)
  614. {
  615. //printf("姓名 %s 電話 %s\n", p->name, p->phone);
  616. printf("\n uuid:%s", p->uuid);
  617. printf("\n data:%s", p->data);
  618. strcpy(uuid,p->uuid);
  619. strcpy(data,p->data);
  620. return 1;
  621. }
  622. else
  623. {
  624. printf("queue is null\n");
  625. strcpy(uuid,"");
  626. strcpy(data,"");
  627. return 0;
  628. }
  629. #endif
  630. }
  631. int addq(char *uuid, char *data) {
  632. FILE *outfile;
  633. char rmFileCmd[100]={0};
  634. struct stat stats;
  635. stat("../Storage/OCPP", &stats);
  636. DEBUG_INFO("addq\n");
  637. // Check for directory existence
  638. if (S_ISDIR(stats.st_mode) == 1)
  639. {
  640. //DEBUG_INFO("\n OCPP directory exist \n");
  641. }
  642. else
  643. {
  644. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  645. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  646. system(rmFileCmd);
  647. }
  648. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  649. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  650. {
  651. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  652. }
  653. else
  654. {
  655. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  656. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  657. if(log == NULL)
  658. {
  659. DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  660. return 0;
  661. }
  662. else
  663. {
  664. fclose(log);
  665. }
  666. }
  667. // open file for writing
  668. outfile = fopen ("../Storage/OCPP/TransactionRelatedQueue", "a");
  669. DEBUG_INFO("data=%s\n",data);
  670. fputs(data, outfile);
  671. fputs("\n", outfile);
  672. fclose (outfile);
  673. TransactionQueueNum = TransactionQueueNum + 1;
  674. return 0;
  675. #if 0
  676. //pnode p = (pnode )malloc(sizeof(node)); --- remove temporally
  677. int nodeCurrentFreeIndex =0;
  678. for (int i = 0; i < 100; i++) // Worst case 100+1
  679. if (staticNodeArray[i].uuid[0] == 0) // Worst case 100
  680. nodeCurrentFreeIndex = i;
  681. pnode p = &staticNodeArray[nodeCurrentFreeIndex];
  682. memset(p, 0, sizeof(node));
  683. if(p)
  684. {
  685. strcpy(p->uuid, uuid);
  686. strcpy(p->data, data);
  687. //printf("請輸入姓名 ");
  688. //scanf("%s", p->name);
  689. //printf("請輸入電話 ");
  690. //scanf("%s", p->phone);
  691. p->next = NULL;
  692. if(rear)
  693. {
  694. rear->next = p;
  695. rear = p;
  696. }
  697. else
  698. {
  699. front = rear = p;
  700. }
  701. }
  702. else
  703. {
  704. printf("無法取得記憶體空間新增資料\n");
  705. }
  706. #endif
  707. }
  708. int delq() {
  709. char tempfile[] = "../Storage/OCPP/temp.json";
  710. FILE *infile;
  711. FILE *outfile;
  712. int resultRename=0;
  713. char filename[60]={0};
  714. char rmFileCmd[100]={0};
  715. struct stat stats;
  716. stat("../Storage/OCPP", &stats);
  717. // Check for directory existence
  718. if (S_ISDIR(stats.st_mode) == 1)
  719. {
  720. //DEBUG_INFO("\n OCPP directory exist \n");
  721. }
  722. else
  723. {
  724. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  725. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  726. system(rmFileCmd);
  727. }
  728. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  729. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  730. {
  731. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  732. }
  733. else
  734. {
  735. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  736. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  737. if(log == NULL)
  738. {
  739. DEBUG_INFO("log is NULL\n");
  740. return 0;
  741. }
  742. else
  743. {
  744. fclose(log);
  745. }
  746. }
  747. // open file for writing
  748. strcpy(filename, "../Storage/OCPP/TransactionRelatedQueue");
  749. infile = fopen ("../Storage/OCPP/TransactionRelatedQueue", "r");
  750. outfile = fopen (tempfile, "w");
  751. /*检测到文件结束标识返回1,否则返回0。*/
  752. //DEBUG_INFO("feof(infile) =%d\n",feof(infile));
  753. int c;
  754. c = fgetc(infile);
  755. //printf("file c:%d\n",c);
  756. rewind(infile);
  757. if(c == EOF)
  758. {
  759. DEBUG_INFO("TransactionRelatedQueue is NULL\n");
  760. fclose(infile);
  761. fclose(outfile);
  762. sprintf(rmFileCmd,"rm -f %s",tempfile);
  763. system(rmFileCmd);
  764. }
  765. else
  766. {
  767. char buf[1200]={0};
  768. int i = 0;
  769. //DEBUG_INFO("Orignal File is not NULL\n");
  770. while (fgets(buf, sizeof(buf), infile) != NULL)
  771. {
  772. //printf("Orignal File get strings \n");
  773. buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
  774. if(i==0)
  775. {
  776. TransactionQueueNum = TransactionQueueNum - 1;
  777. TransactionQueueInterval = 0;
  778. TransactionMessageResend = 1;
  779. }
  780. if(i != 0)
  781. fprintf(outfile,"%s\n", buf);
  782. i = i + 1;
  783. }
  784. fclose(infile);
  785. fclose(outfile);
  786. sprintf(rmFileCmd,"rm -f %s",filename);
  787. system(rmFileCmd);
  788. resultRename = rename(tempfile, filename);
  789. if(resultRename == 0)
  790. {
  791. DEBUG_INFO("TransactionRelatedQueue file renamed successfully");
  792. }
  793. else
  794. {
  795. DEBUG_INFO("Error: unable to rename the TransactionRelatedQueue file");
  796. }
  797. }
  798. return 0;
  799. #if 0
  800. pnode p;
  801. if(front)
  802. {
  803. p = front;
  804. if(front->next)
  805. front = front->next;
  806. else
  807. front = rear = NULL;
  808. printf("delete uuid: %s data: %s\n", p->uuid, p->data);
  809. //free(p); --- remove temporally
  810. //new
  811. memset(p, 0, sizeof(node));
  812. p=NULL;
  813. }
  814. else
  815. {
  816. printf("queue is Empty\n");
  817. printf("\n delete: queue is null");
  818. }
  819. return 0;
  820. #endif
  821. }
  822. int showqueue() {
  823. char rmFileCmd[100]={0};
  824. struct stat stats;
  825. stat("../Storage/OCPP", &stats);
  826. // Check for directory existence
  827. if (S_ISDIR(stats.st_mode) == 1)
  828. {
  829. //DEBUG_INFO("\n OCPP directory exist \n");
  830. }
  831. else
  832. {
  833. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  834. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  835. system(rmFileCmd);
  836. }
  837. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  838. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  839. {
  840. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  841. }
  842. else
  843. {
  844. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  845. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  846. if(log == NULL)
  847. {
  848. DEBUG_INFO("log is NULL\n");
  849. return 0;
  850. }
  851. else
  852. {
  853. fclose(log);
  854. }
  855. }
  856. FILE *fp = fopen("../Storage/OCPP/TransactionRelatedQueue", "r");
  857. char line[1200]={0};
  858. // check if file exist (and you can open it) or not
  859. if (fp == NULL) {
  860. DEBUG_INFO("can open file TransactionRelatedQueue!");
  861. return 0;
  862. }
  863. while(fgets(line, sizeof line, fp) != NULL) {
  864. DEBUG_INFO("%s\n", line);
  865. }
  866. fclose(fp);
  867. return 0;
  868. #if 0
  869. pnode p;
  870. p = front;
  871. while(p)
  872. {
  873. printf("uuid: %s data: %s\n", p->uuid, p->data);
  874. p = p->next;
  875. }
  876. return 0;
  877. #endif
  878. }
  879. int sentqueue(){
  880. FILE *fp;
  881. int result = FALSE; // 1: TRUE 0:FALSE
  882. char str[1200]={0};
  883. char rmFileCmd[100]={0};
  884. struct stat stats;
  885. DEBUG_INFO("sentqueue\n");
  886. stat("../Storage/OCPP", &stats);
  887. // Check for directory existence
  888. if (S_ISDIR(stats.st_mode) == 1)
  889. {
  890. //DEBUG_INFO("\n OCPP directory exist \n");
  891. }
  892. else
  893. {
  894. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  895. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  896. system(rmFileCmd);
  897. }
  898. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  899. /* opening file for reading */
  900. fp = fopen("../Storage/OCPP/TransactionRelatedQueue" , "r");
  901. if(fp == NULL) {
  902. DEBUG_INFO("Error opening file");
  903. return FALSE;
  904. }
  905. if( fgets (str, 1200, fp)!=NULL ) {
  906. /* writing content to stdout */
  907. //puts(str);
  908. DEBUG_INFO("\n uuid:%s", "");
  909. DEBUG_INFO("\n data:%s", str);
  910. LWS_Send(str);
  911. result = TRUE;
  912. //return 1;
  913. }
  914. else
  915. {
  916. DEBUG_INFO("queue is null\n");
  917. result = FALSE;
  918. //return 0;
  919. }
  920. fclose(fp);
  921. DEBUG_INFO("sentqueue end\n");
  922. return result;
  923. #if 0
  924. pnode p;
  925. p = front;
  926. printf("sentqueue\n");
  927. while(p)
  928. {
  929. printf("uuid: %s data: %s\n", p->uuid, p->data);
  930. LWS_Send(p->data);
  931. p = p->next;
  932. }
  933. printf("sentqueue end\n");
  934. return 0;
  935. #endif
  936. }
  937. #if 0
  938. int storequeue(){
  939. //write queue to flash
  940. unsigned int i,Chk;
  941. unsigned char *ptr = NULL;
  942. int fd,wrd;
  943. // Save factory default setting value to flash backup setting block
  944. fd = open("./Storage/OCPP/queue.txt", O_RDWR);
  945. if (fd < 0)
  946. {
  947. #ifdef SystemLogMessage
  948. DEBUG_ERROR("open ./Storage/OCPP/queue.txt NG");
  949. #endif
  950. //free(ptr);
  951. return 0;
  952. }
  953. wrd=write(fd, &front, sizeof(node));
  954. if(wrd!=(sizeof(node)))
  955. {
  956. #ifdef SystemLogMessage
  957. DEBUG_ERROR("write ./Storage/OCPP/queue.txt NG");
  958. #endif
  959. //free(ptr);
  960. return 0;
  961. }
  962. close(fd);
  963. close(wrd);
  964. return 0;
  965. }
  966. int readfiletoqueue(){
  967. FILE *infile;
  968. //pnode p = (pnode )malloc(sizeof(node));
  969. struct _node Pnode;
  970. memset(&Pnode, 0, sizeof(node));
  971. // Open person.dat for reading
  972. infile = fopen ("./Storage/OCPP/queue.txt", "r");
  973. if (infile == NULL)
  974. {
  975. fprintf(stderr, "\nError opening file\n");
  976. exit (1);
  977. }
  978. // read file contents till end of file
  979. while(fread(&Pnode, sizeof(node), 1, infile))
  980. printf ("uuid = %s name = %s\n", Pnode.uuid,
  981. Pnode.data);
  982. // close file
  983. fclose (infile);
  984. // free(p);
  985. return 0;
  986. }
  987. #endif
  988. //
  989. void* processTransactionQueue(void* data) {
  990. char frontUUID[100] ={0};
  991. char frontData[1200/*1024*4*/] ={0};
  992. int queueNotEmpty = 0;
  993. while(1)
  994. {
  995. if(FirstHeartBeatResponse() == 1)
  996. {
  997. memset(frontUUID, 0, sizeof(frontUUID));
  998. memset(frontData, 0, sizeof(frontData));
  999. queueNotEmpty = 0;
  1000. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  1001. if((queueNotEmpty == 1) && (GetOcppConnStatus() == 1))
  1002. {
  1003. if((OfflineTransaction == 1) && (TransactionQueueNum != 0))
  1004. {
  1005. TransactionQueueInterval = 2;
  1006. sleep(TransactionQueueInterval);
  1007. sendbuffer = 1;
  1008. }
  1009. else
  1010. {
  1011. OfflineTransaction = 0;
  1012. if(TransactionMessageResend < TransactionMessageAttemptsGet()) //
  1013. {
  1014. //DEBUG_INFO("processTransactionQueue queue is not empty!\n");
  1015. TransactionQueueInterval= TransactionMessageRetryIntervalGet()* TransactionMessageResend;
  1016. sleep(TransactionQueueInterval);
  1017. sendbuffer = 1;
  1018. TransactionMessageResend = TransactionMessageResend + 1;
  1019. }
  1020. else
  1021. {
  1022. queue_operation(2,"",""); //// delete item
  1023. TransactionQueueInterval = 0;
  1024. TransactionMessageResend = 1;
  1025. sleep(TransactionQueueInterval);
  1026. }
  1027. }
  1028. }
  1029. else if((queueNotEmpty == 1) && (GetOcppConnStatus() == 0))
  1030. {
  1031. OfflineTransaction = 1;
  1032. }
  1033. else
  1034. {
  1035. TransactionQueueInterval = 0;//10;
  1036. sleep(TransactionQueueInterval);
  1037. }
  1038. }
  1039. usleep(5000);
  1040. }
  1041. pthread_exit(NULL); //
  1042. return 0;
  1043. }
  1044. void CheckTransactionPacket(char *uuid)
  1045. {
  1046. char frontUUID[100]={0};
  1047. char frontData[1200]={0};
  1048. int queueNotEmpty = 0;
  1049. int cmpResult = 0;
  1050. queue_operation(0,"","");//showqueue(); ---> remove temporally
  1051. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  1052. if(queueNotEmpty == 1)
  1053. {
  1054. cmpResult = strcmp(frontUUID, uuid);
  1055. //cmpResult = strcmp(frontData, uuid);
  1056. if (cmpResult == 0)
  1057. {
  1058. DEBUG_INFO("TransactionPacket Compare All right!\n");
  1059. queue_operation(2,"","");//delq(); ---> remove temporally
  1060. }
  1061. }
  1062. }
  1063. /* type: 0 (showqueue ); type: 1(showfront); type: 2(delq) type: 3 (sentqueue) type: 4 (addq) type: 5(store queue to /Storage/OCPP/ )*/
  1064. int queue_operation(int type, char *frontUUID, char *frontData)
  1065. {
  1066. pthread_mutex_unlock(&lock_sentData);
  1067. pthread_mutex_lock(&lock_sentData);
  1068. int result=0;
  1069. if(type == 0) // show items in queue
  1070. result = showqueue();
  1071. else if(type == 1) // show first item
  1072. result = showfront(frontUUID, frontData);
  1073. else if(type == 2) // delete item
  1074. result = delq();
  1075. else if(type == 3) // sent items in queue
  1076. result = sentqueue();
  1077. else if(type == 4) // add items to the queue
  1078. result = addq(frontUUID, frontData);
  1079. pthread_mutex_unlock(&lock_sentData);
  1080. return result;
  1081. }
  1082. char * strchr(const char *p, int ch)
  1083. {
  1084. char c;
  1085. c = ch;
  1086. for (;; ++p) {
  1087. if (*p == c)
  1088. return ((char *)p);
  1089. if (*p == '\0')
  1090. return (NULL);
  1091. }
  1092. /* NOTREACHED */
  1093. return NULL;
  1094. }
  1095. int removeMessageSentFile(void)
  1096. {
  1097. char rmFileCmd[100]={0};
  1098. struct stat stats;
  1099. //
  1100. stat("../Storage/OCPP", &stats);
  1101. // Check for directory existence
  1102. if (S_ISDIR(stats.st_mode) == 1)
  1103. {
  1104. //DEBUG_INFO("\n OCPP directory exist \n");
  1105. }
  1106. else
  1107. {
  1108. DEBUG_INFO("\n directory not exist, create dir \n");
  1109. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  1110. system(rmFileCmd);
  1111. }
  1112. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1113. if((access("../Storage/OCPP/MessageSent",F_OK))!=-1)
  1114. {
  1115. DEBUG_INFO("MessageSent file exist.\n");
  1116. sprintf(rmFileCmd,"rm -f %s","../Storage/OCPP/MessageSent");
  1117. system(rmFileCmd);
  1118. }
  1119. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1120. return 0;
  1121. }
  1122. #define SystemLogMessage
  1123. //================================================
  1124. // Main process
  1125. //================================================
  1126. int main(void)
  1127. {
  1128. char rmFileCmd[100]={0};
  1129. struct stat stats;
  1130. pthread_t t;
  1131. #ifdef SystemLogMessage
  1132. DEBUG_INFO("Initialization...\n");
  1133. #endif
  1134. if(ProcessShareMemory()== FAIL)
  1135. {
  1136. return FAIL;
  1137. }
  1138. //Create OCPP dir
  1139. stat("../Storage/OCPP", &stats);
  1140. // Check for directory existence
  1141. if (S_ISDIR(stats.st_mode) == 1)
  1142. {
  1143. //DEBUG_INFO("\n OCPP directory exist \n");
  1144. }
  1145. else
  1146. {
  1147. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  1148. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  1149. system(rmFileCmd);
  1150. }
  1151. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1152. //Create Process: Resend Transaction
  1153. pthread_create(&t, NULL, processTransactionQueue, NULL); //
  1154. sqlite3_config(SQLITE_CONFIG_URI,1);
  1155. if(sqlite3_open("file:/../Storage/OCPP/charger.db", &db))
  1156. {
  1157. #ifdef Debug
  1158. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1159. #endif
  1160. sqlite3_close( db );
  1161. exit(0);
  1162. }
  1163. else
  1164. {
  1165. #ifdef Debug
  1166. DEBUG_INFO( "Opened database successfully\n");
  1167. #endif
  1168. }
  1169. /* 建立 Table log buffer */
  1170. int rc =sqlite3_exec(db, createsql, 0, 0, &errMsg);
  1171. if (SQLITE_OK != rc)
  1172. {
  1173. #ifdef Debug
  1174. DEBUG_INFO( "Create log buffer table error message: %s\n", errMsg);
  1175. #endif
  1176. return 0;
  1177. }
  1178. else
  1179. {
  1180. #ifdef Debug
  1181. DEBUG_INFO( "Opened log buffer table successfully\n");
  1182. #endif
  1183. }
  1184. /* 建立 Table OcppAuthCache */
  1185. rc =sqlite3_exec(db, sqlOcppAuthCache, 0, 0, &errMsg);
  1186. if (SQLITE_OK != rc)
  1187. {
  1188. #ifdef Debug
  1189. DEBUG_INFO( "Create OcppAuthCache error message: %s\n", errMsg);
  1190. #endif
  1191. return 0;
  1192. }
  1193. else
  1194. {
  1195. #ifdef Debug
  1196. DEBUG_INFO( "Opened OcppAuthCache table successfully\n");
  1197. #endif
  1198. }
  1199. /* 建立 Table OcppAuthLocal */
  1200. rc =sqlite3_exec(db, sqlOcppAuthLocal, 0, 0, &errMsg);
  1201. if (SQLITE_OK != rc)
  1202. {
  1203. #ifdef Debug
  1204. DEBUG_INFO( "Create Table OcppAuthLocal error %s\n",errMsg);
  1205. #endif
  1206. return 0;
  1207. }
  1208. else
  1209. {
  1210. #ifdef Debug
  1211. DEBUG_INFO( "Opened OcppAuthLocal table successfully\n");
  1212. #endif
  1213. }
  1214. initialConfigurationTable();
  1215. removeMessageSentFile();
  1216. //queryAllData();
  1217. OCPP_get_TableAuthlocalAllData();
  1218. for(;;)
  1219. {
  1220. while(ConnectionEstablished==0)
  1221. {
  1222. if((time((time_t*)NULL)-startTime.connect)>=60)
  1223. {
  1224. #ifdef Debug
  1225. DEBUG_INFO("Execute ConnectWsServer\n");
  1226. #endif
  1227. ConnectWsServer();
  1228. startTime.connect=time((time_t*)NULL);
  1229. }
  1230. lws_service(context, 10000);//timeout_ms
  1231. }
  1232. if(( (BootNotificationInterval != 0 && ((time((time_t*)NULL)-startTime.bootNotification)>=BootNotificationInterval) ) || ((time((time_t*)NULL)-startTime.bootNotification)>=defaultWaitingTime) ) && ((server_sign == FALSE)/*|| (server_pending == TRUE)*/))
  1233. {
  1234. //hashmapForMessageNew();
  1235. sendBootNotificationRequest();
  1236. startTime.bootNotification=time((time_t*)NULL);
  1237. }
  1238. if(server_sign == TRUE)
  1239. {
  1240. if(sendbuffer == 1)
  1241. {
  1242. queue_operation(3, "", "");//sentqueue()
  1243. sendbuffer = 0;
  1244. }
  1245. // Check System Value
  1246. CheckSystemValue();
  1247. }
  1248. lws_service(context, 100);//timeout_ms
  1249. }
  1250. pthread_join(t, NULL); // ���ݤl��������槹��
  1251. //hashmapForMessageFree();
  1252. return FAIL;
  1253. }