Module_OcppBackend.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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; // 1:offline 0: online
  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_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. //DEBUG_INFO("Message: %s\n", (char *)in);
  410. #endif
  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. printf("test\n");
  568. if( fgets (str, 1200, fp)!=NULL ) {
  569. /* writing content to stdout */
  570. if (str[0] == '\n')
  571. {
  572. DEBUG_INFO("It is a blank line");
  573. fclose(fp);
  574. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  575. sprintf(rmFileCmd,"rm -f %s","../Storage/OCPP/TransactionRelatedQueue");
  576. system(rmFileCmd);
  577. result = FALSE;
  578. return result;
  579. }
  580. else
  581. {
  582. puts(str);
  583. /*********************uuid***************/
  584. loc = strstr(str, "\"");
  585. memset(sstr ,0, sizeof(sstr) );
  586. c = 0;
  587. while (loc[1+c] != '\"')
  588. {
  589. sstr[c] = loc[1+c];
  590. c++;
  591. }
  592. sstr[c] = '\0';
  593. DEBUG_INFO("\n uuid:%s", sstr);
  594. DEBUG_INFO("\n data:%s", str);
  595. strcpy(uuid,sstr);
  596. strcpy(data,str);
  597. result = TRUE;
  598. }
  599. //return 1;
  600. }
  601. else
  602. {
  603. DEBUG_INFO("queue is null\n");
  604. strcpy(uuid,"");
  605. strcpy(data,"");
  606. result = FALSE;
  607. //return 0;
  608. }
  609. fclose(fp);
  610. return result;
  611. #if 0
  612. pnode p;
  613. p = front;
  614. if(p)
  615. {
  616. //printf("姓名 %s 電話 %s\n", p->name, p->phone);
  617. printf("\n uuid:%s", p->uuid);
  618. printf("\n data:%s", p->data);
  619. strcpy(uuid,p->uuid);
  620. strcpy(data,p->data);
  621. return 1;
  622. }
  623. else
  624. {
  625. printf("queue is null\n");
  626. strcpy(uuid,"");
  627. strcpy(data,"");
  628. return 0;
  629. }
  630. #endif
  631. }
  632. int addq(char *uuid, char *data) {
  633. FILE *outfile;
  634. char rmFileCmd[100]={0};
  635. struct stat stats;
  636. stat("../Storage/OCPP", &stats);
  637. DEBUG_INFO("addq\n");
  638. // Check for directory existence
  639. if (S_ISDIR(stats.st_mode) == 1)
  640. {
  641. DEBUG_INFO("\n OCPP directory exist \n");
  642. }
  643. else
  644. {
  645. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  646. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  647. system(rmFileCmd);
  648. }
  649. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  650. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  651. {
  652. DEBUG_INFO("TransactionRelatedQueue exist.\n");
  653. }
  654. else
  655. {
  656. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  657. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  658. if(log == NULL)
  659. {
  660. DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  661. return 0;
  662. }
  663. else
  664. {
  665. fclose(log);
  666. }
  667. }
  668. // open file for writing
  669. outfile = fopen ("../Storage/OCPP/TransactionRelatedQueue", "a");
  670. DEBUG_INFO("data=%s\n",data);
  671. fputs(data, outfile);
  672. fputs("\n", outfile);
  673. fclose (outfile);
  674. TransactionQueueNum = TransactionQueueNum + 1;
  675. return 0;
  676. #if 0
  677. //pnode p = (pnode )malloc(sizeof(node)); --- remove temporally
  678. int nodeCurrentFreeIndex =0;
  679. for (int i = 0; i < 100; i++) // Worst case 100+1
  680. if (staticNodeArray[i].uuid[0] == 0) // Worst case 100
  681. nodeCurrentFreeIndex = i;
  682. pnode p = &staticNodeArray[nodeCurrentFreeIndex];
  683. memset(p, 0, sizeof(node));
  684. if(p)
  685. {
  686. strcpy(p->uuid, uuid);
  687. strcpy(p->data, data);
  688. //printf("請輸入姓名 ");
  689. //scanf("%s", p->name);
  690. //printf("請輸入電話 ");
  691. //scanf("%s", p->phone);
  692. p->next = NULL;
  693. if(rear)
  694. {
  695. rear->next = p;
  696. rear = p;
  697. }
  698. else
  699. {
  700. front = rear = p;
  701. }
  702. }
  703. else
  704. {
  705. printf("無法取得記憶體空間新增資料\n");
  706. }
  707. #endif
  708. }
  709. int delq() {
  710. char tempfile[] = "../Storage/OCPP/temp.json";
  711. FILE *infile;
  712. FILE *outfile;
  713. int resultRename=0;
  714. char filename[60]={0};
  715. char rmFileCmd[100]={0};
  716. struct stat stats;
  717. stat("../Storage/OCPP", &stats);
  718. // Check for directory existence
  719. if (S_ISDIR(stats.st_mode) == 1)
  720. {
  721. DEBUG_INFO("\n OCPP directory exist \n");
  722. }
  723. else
  724. {
  725. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  726. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  727. system(rmFileCmd);
  728. }
  729. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  730. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  731. {
  732. DEBUG_INFO("TransactionRelatedQueue exist.\n");
  733. }
  734. else
  735. {
  736. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  737. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  738. if(log == NULL)
  739. {
  740. DEBUG_INFO("log is NULL\n");
  741. return 0;
  742. }
  743. else
  744. {
  745. fclose(log);
  746. }
  747. }
  748. // open file for writing
  749. strcpy(filename, "../Storage/OCPP/TransactionRelatedQueue");
  750. infile = fopen ("../Storage/OCPP/TransactionRelatedQueue", "r");
  751. outfile = fopen (tempfile, "w");
  752. /*检测到文件结束标识返回1,否则返回0。*/
  753. //DEBUG_INFO("feof(infile) =%d\n",feof(infile));
  754. int c;
  755. c = fgetc(infile);
  756. //printf("file c:%d\n",c);
  757. rewind(infile);
  758. if(c == EOF)
  759. {
  760. DEBUG_INFO("TransactionRelatedQueue is NULL\n");
  761. fclose(infile);
  762. fclose(outfile);
  763. sprintf(rmFileCmd,"rm -f %s",tempfile);
  764. system(rmFileCmd);
  765. }
  766. else
  767. {
  768. char buf[1200]={0};
  769. int i = 0;
  770. //DEBUG_INFO("Orignal File is not NULL\n");
  771. while (fgets(buf, sizeof(buf), infile) != NULL)
  772. {
  773. //printf("Orignal File get strings \n");
  774. buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
  775. if(i==0)
  776. {
  777. TransactionQueueNum = TransactionQueueNum - 1;
  778. TransactionQueueInterval = 0;
  779. TransactionMessageResend = 1;
  780. }
  781. if(i != 0)
  782. fprintf(outfile,"%s\n", buf);
  783. i = i + 1;
  784. }
  785. fclose(infile);
  786. fclose(outfile);
  787. sprintf(rmFileCmd,"rm -f %s",filename);
  788. system(rmFileCmd);
  789. resultRename = rename(tempfile, filename);
  790. if(resultRename == 0)
  791. {
  792. DEBUG_INFO("TransactionRelatedQueue file renamed successfully");
  793. }
  794. else
  795. {
  796. DEBUG_INFO("Error: unable to rename the TransactionRelatedQueue file");
  797. }
  798. }
  799. return 0;
  800. #if 0
  801. pnode p;
  802. if(front)
  803. {
  804. p = front;
  805. if(front->next)
  806. front = front->next;
  807. else
  808. front = rear = NULL;
  809. printf("delete uuid: %s data: %s\n", p->uuid, p->data);
  810. //free(p); --- remove temporally
  811. //new
  812. memset(p, 0, sizeof(node));
  813. p=NULL;
  814. }
  815. else
  816. {
  817. printf("queue is Empty\n");
  818. printf("\n delete: queue is null");
  819. }
  820. return 0;
  821. #endif
  822. }
  823. int showqueue() {
  824. char rmFileCmd[100]={0};
  825. struct stat stats;
  826. stat("../Storage/OCPP", &stats);
  827. // Check for directory existence
  828. if (S_ISDIR(stats.st_mode) == 1)
  829. {
  830. DEBUG_INFO("\n OCPP directory exist \n");
  831. }
  832. else
  833. {
  834. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  835. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  836. system(rmFileCmd);
  837. }
  838. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  839. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  840. {
  841. DEBUG_INFO("TransactionRelatedQueue exist.\n");
  842. }
  843. else
  844. {
  845. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  846. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  847. if(log == NULL)
  848. {
  849. DEBUG_INFO("log is NULL\n");
  850. return 0;
  851. }
  852. else
  853. {
  854. fclose(log);
  855. }
  856. }
  857. FILE *fp = fopen("../Storage/OCPP/TransactionRelatedQueue", "r");
  858. char line[1200]={0};
  859. // check if file exist (and you can open it) or not
  860. if (fp == NULL) {
  861. DEBUG_INFO("can open file TransactionRelatedQueue!");
  862. return 0;
  863. }
  864. while(fgets(line, sizeof line, fp) != NULL) {
  865. DEBUG_INFO("%s\n", line);
  866. }
  867. fclose(fp);
  868. return 0;
  869. #if 0
  870. pnode p;
  871. p = front;
  872. while(p)
  873. {
  874. printf("uuid: %s data: %s\n", p->uuid, p->data);
  875. p = p->next;
  876. }
  877. return 0;
  878. #endif
  879. }
  880. int sentqueue(){
  881. FILE *fp;
  882. int result = FALSE; // 1: TRUE 0:FALSE
  883. char str[1200]={0};
  884. char rmFileCmd[100]={0};
  885. struct stat stats;
  886. DEBUG_INFO("sentqueue\n");
  887. stat("../Storage/OCPP", &stats);
  888. // Check for directory existence
  889. if (S_ISDIR(stats.st_mode) == 1)
  890. {
  891. DEBUG_INFO("\n OCPP directory exist \n");
  892. }
  893. else
  894. {
  895. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  896. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  897. system(rmFileCmd);
  898. }
  899. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  900. /* opening file for reading */
  901. fp = fopen("../Storage/OCPP/TransactionRelatedQueue" , "r");
  902. if(fp == NULL) {
  903. DEBUG_INFO("Error opening file");
  904. return FALSE;
  905. }
  906. if( fgets (str, 1200, fp)!=NULL ) {
  907. /* writing content to stdout */
  908. //puts(str);
  909. DEBUG_INFO("\n uuid:%s", "");
  910. DEBUG_INFO("\n data:%s", str);
  911. LWS_Send(str);
  912. result = TRUE;
  913. //return 1;
  914. }
  915. else
  916. {
  917. DEBUG_INFO("queue is null\n");
  918. result = FALSE;
  919. //return 0;
  920. }
  921. fclose(fp);
  922. DEBUG_INFO("sentqueue end\n");
  923. return result;
  924. #if 0
  925. pnode p;
  926. p = front;
  927. printf("sentqueue\n");
  928. while(p)
  929. {
  930. printf("uuid: %s data: %s\n", p->uuid, p->data);
  931. LWS_Send(p->data);
  932. p = p->next;
  933. }
  934. printf("sentqueue end\n");
  935. return 0;
  936. #endif
  937. }
  938. #if 0
  939. int storequeue(){
  940. //write queue to flash
  941. unsigned int i,Chk;
  942. unsigned char *ptr = NULL;
  943. int fd,wrd;
  944. // Save factory default setting value to flash backup setting block
  945. fd = open("./Storage/OCPP/queue.txt", O_RDWR);
  946. if (fd < 0)
  947. {
  948. #ifdef SystemLogMessage
  949. DEBUG_ERROR("open ./Storage/OCPP/queue.txt NG");
  950. #endif
  951. //free(ptr);
  952. return 0;
  953. }
  954. wrd=write(fd, &front, sizeof(node));
  955. if(wrd!=(sizeof(node)))
  956. {
  957. #ifdef SystemLogMessage
  958. DEBUG_ERROR("write ./Storage/OCPP/queue.txt NG");
  959. #endif
  960. //free(ptr);
  961. return 0;
  962. }
  963. close(fd);
  964. close(wrd);
  965. return 0;
  966. }
  967. int readfiletoqueue(){
  968. FILE *infile;
  969. //pnode p = (pnode )malloc(sizeof(node));
  970. struct _node Pnode;
  971. memset(&Pnode, 0, sizeof(node));
  972. // Open person.dat for reading
  973. infile = fopen ("./Storage/OCPP/queue.txt", "r");
  974. if (infile == NULL)
  975. {
  976. fprintf(stderr, "\nError opening file\n");
  977. exit (1);
  978. }
  979. // read file contents till end of file
  980. while(fread(&Pnode, sizeof(node), 1, infile))
  981. printf ("uuid = %s name = %s\n", Pnode.uuid,
  982. Pnode.data);
  983. // close file
  984. fclose (infile);
  985. // free(p);
  986. return 0;
  987. }
  988. #endif
  989. //
  990. void* processTransactionQueue(void* data) {
  991. char frontUUID[100] ={0};
  992. char frontData[1200/*1024*4*/] ={0};
  993. int queueNotEmpty = 0;
  994. while(1)
  995. {
  996. if(FirstHeartBeatResponse() == 1)
  997. {
  998. memset(frontUUID, 0, sizeof(frontUUID));
  999. memset(frontData, 0, sizeof(frontData));
  1000. queueNotEmpty = 0;
  1001. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  1002. if((queueNotEmpty == 1) && (GetOcppConnStatus() == 1))
  1003. {
  1004. if((OfflineTransaction == 1) && (TransactionQueueNum != 0))
  1005. {
  1006. TransactionQueueInterval = 2;
  1007. sleep(TransactionQueueInterval);
  1008. sendbuffer = 1;
  1009. }
  1010. else
  1011. {
  1012. OfflineTransaction = 0;
  1013. if(TransactionMessageResend < TransactionMessageAttemptsGet()) //
  1014. {
  1015. //DEBUG_INFO("processTransactionQueue queue is not empty!\n");
  1016. TransactionQueueInterval= TransactionMessageRetryIntervalGet()* TransactionMessageResend;
  1017. sleep(TransactionQueueInterval);
  1018. sendbuffer = 1;
  1019. TransactionMessageResend = TransactionMessageResend + 1;
  1020. }
  1021. else
  1022. {
  1023. queue_operation(2,"",""); //// delete item
  1024. TransactionQueueInterval = 0;
  1025. TransactionMessageResend = 1;
  1026. sleep(TransactionQueueInterval);
  1027. }
  1028. }
  1029. }
  1030. else if((queueNotEmpty == 1) && (GetOcppConnStatus() == 0))
  1031. {
  1032. OfflineTransaction = 1;
  1033. }
  1034. else
  1035. {
  1036. TransactionQueueInterval = 0;//10;
  1037. sleep(TransactionQueueInterval);
  1038. }
  1039. #if 0
  1040. if(TransactionQueueNum != 0)
  1041. {
  1042. TransactionQueueInterval = 1;
  1043. }
  1044. else
  1045. #endif
  1046. {
  1047. // TransactionQueueInterval = 10;
  1048. }
  1049. //TransactionQueueInterval = TransactionMessageAttemptsGet();
  1050. //sleep(TransactionQueueInterval); // sleep for 30 seconds
  1051. }
  1052. }
  1053. pthread_exit(NULL); //
  1054. return 0;
  1055. }
  1056. void CheckTransactionPacket(char *uuid)
  1057. {
  1058. char frontUUID[100]={0};
  1059. char frontData[1200]={0};
  1060. int queueNotEmpty = 0;
  1061. int cmpResult = 0;
  1062. queue_operation(0,"","");//showqueue(); ---> remove temporally
  1063. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  1064. if(queueNotEmpty == 1)
  1065. {
  1066. cmpResult = strcmp(frontUUID, uuid);
  1067. //cmpResult = strcmp(frontData, uuid);
  1068. if (cmpResult == 0)
  1069. {
  1070. DEBUG_INFO("TransactionPacket Compare All right!\n");
  1071. queue_operation(2,"","");//delq(); ---> remove temporally
  1072. }
  1073. }
  1074. }
  1075. /* type: 0 (showqueue ); type: 1(showfront); type: 2(delq) type: 3 (sentqueue) type: 4 (addq) type: 5(store queue to /Storage/OCPP/ )*/
  1076. int queue_operation(int type, char *frontUUID, char *frontData)
  1077. {
  1078. pthread_mutex_unlock(&lock_sentData);
  1079. pthread_mutex_lock(&lock_sentData);
  1080. int result=0;
  1081. if(type == 0) // show items in queue
  1082. result = showqueue();
  1083. else if(type == 1) // show first item
  1084. result = showfront(frontUUID, frontData);
  1085. else if(type == 2) // delete item
  1086. result = delq();
  1087. else if(type == 3) // sent items in queue
  1088. result = sentqueue();
  1089. else if(type == 4) // add items to the queue
  1090. result = addq(frontUUID, frontData);
  1091. pthread_mutex_unlock(&lock_sentData);
  1092. return result;
  1093. }
  1094. char * strchr(const char *p, int ch)
  1095. {
  1096. char c;
  1097. c = ch;
  1098. for (;; ++p) {
  1099. if (*p == c)
  1100. return ((char *)p);
  1101. if (*p == '\0')
  1102. return (NULL);
  1103. }
  1104. /* NOTREACHED */
  1105. return NULL;
  1106. }
  1107. int removeMessageSentFile(void)
  1108. {
  1109. char rmFileCmd[100]={0};
  1110. struct stat stats;
  1111. //
  1112. stat("../Storage/OCPP", &stats);
  1113. // Check for directory existence
  1114. if (S_ISDIR(stats.st_mode) == 1)
  1115. {
  1116. DEBUG_INFO("\n OCPP directory exist \n");
  1117. }
  1118. else
  1119. {
  1120. DEBUG_INFO("\n directory not exist, create dir \n");
  1121. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  1122. system(rmFileCmd);
  1123. }
  1124. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1125. if((access("../Storage/OCPP/MessageSent",F_OK))!=-1)
  1126. {
  1127. DEBUG_INFO("MessageSent file exist.\n");
  1128. sprintf(rmFileCmd,"rm -f %s","../Storage/OCPP/MessageSent");
  1129. system(rmFileCmd);
  1130. }
  1131. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1132. return 0;
  1133. }
  1134. #define SystemLogMessage
  1135. //================================================
  1136. // Main process
  1137. //================================================
  1138. int main(void)
  1139. {
  1140. pthread_t t;
  1141. #ifdef SystemLogMessage
  1142. DEBUG_INFO("Initialization...\n");
  1143. #endif
  1144. if(ProcessShareMemory()== FAIL)
  1145. {
  1146. return FAIL;
  1147. }
  1148. //Create Process: Resend Transaction
  1149. pthread_create(&t, NULL, processTransactionQueue, NULL); //
  1150. sqlite3_config(SQLITE_CONFIG_URI,1);
  1151. if(sqlite3_open("file:/../Storage/OCPP/charger.db", &db))
  1152. {
  1153. #ifdef Debug
  1154. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1155. #endif
  1156. sqlite3_close( db );
  1157. exit(0);
  1158. }
  1159. else
  1160. {
  1161. #ifdef Debug
  1162. DEBUG_INFO( "Opened database successfully\n");
  1163. #endif
  1164. }
  1165. /* 建立 Table log buffer */
  1166. int rc =sqlite3_exec(db, createsql, 0, 0, &errMsg);
  1167. if (SQLITE_OK != rc)
  1168. {
  1169. #ifdef Debug
  1170. DEBUG_INFO( "Create log buffer table error message: %s\n", errMsg);
  1171. #endif
  1172. return 0;
  1173. }
  1174. else
  1175. {
  1176. #ifdef Debug
  1177. DEBUG_INFO( "Opened log buffer table successfully\n");
  1178. #endif
  1179. }
  1180. /* 建立 Table OcppAuthCache */
  1181. rc =sqlite3_exec(db, sqlOcppAuthCache, 0, 0, &errMsg);
  1182. if (SQLITE_OK != rc)
  1183. {
  1184. #ifdef Debug
  1185. DEBUG_INFO( "Create OcppAuthCache error message: %s\n", errMsg);
  1186. #endif
  1187. return 0;
  1188. }
  1189. else
  1190. {
  1191. #ifdef Debug
  1192. DEBUG_INFO( "Opened OcppAuthCache table successfully\n");
  1193. #endif
  1194. }
  1195. /* 建立 Table OcppAuthLocal */
  1196. rc =sqlite3_exec(db, sqlOcppAuthLocal, 0, 0, &errMsg);
  1197. if (SQLITE_OK != rc)
  1198. {
  1199. #ifdef Debug
  1200. DEBUG_INFO( "Create Table OcppAuthLocal error %s\n",errMsg);
  1201. #endif
  1202. return 0;
  1203. }
  1204. else
  1205. {
  1206. #ifdef Debug
  1207. DEBUG_INFO( "Opened OcppAuthLocal table successfully\n");
  1208. #endif
  1209. }
  1210. initialConfigurationTable();
  1211. removeMessageSentFile();
  1212. //queryAllData();
  1213. OCPP_get_TableAuthlocalAllData();
  1214. for(;;)
  1215. {
  1216. while(ConnectionEstablished==0)
  1217. {
  1218. if((time((time_t*)NULL)-startTime.connect)>=60)
  1219. {
  1220. #ifdef Debug
  1221. DEBUG_INFO("Execute ConnectWsServer\n");
  1222. #endif
  1223. ConnectWsServer();
  1224. startTime.connect=time((time_t*)NULL);
  1225. }
  1226. lws_service(context, 10000);//timeout_ms
  1227. }
  1228. if(( (BootNotificationInterval != 0 && ((time((time_t*)NULL)-startTime.bootNotification)>=BootNotificationInterval) ) || ((time((time_t*)NULL)-startTime.bootNotification)>=defaultWaitingTime) ) && ((server_sign == FALSE)/*|| (server_pending == TRUE)*/))
  1229. {
  1230. //hashmapForMessageNew();
  1231. sendBootNotificationRequest();
  1232. startTime.bootNotification=time((time_t*)NULL);
  1233. }
  1234. if(server_sign == TRUE)
  1235. {
  1236. if(sendbuffer == 1)
  1237. {
  1238. queue_operation(3, "", "");//sentqueue()
  1239. sendbuffer = 0;
  1240. }
  1241. // Check System Value
  1242. CheckSystemValue();
  1243. }
  1244. lws_service(context, 100);//timeout_ms
  1245. }
  1246. pthread_join(t, NULL); // ���ݤl��������槹��
  1247. //hashmapForMessageFree();
  1248. return FAIL;
  1249. }