Module_OcppBackend.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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. #if 0
  1039. if(TransactionQueueNum != 0)
  1040. {
  1041. TransactionQueueInterval = 1;
  1042. }
  1043. else
  1044. #endif
  1045. {
  1046. // TransactionQueueInterval = 10;
  1047. }
  1048. //TransactionQueueInterval = TransactionMessageAttemptsGet();
  1049. //sleep(TransactionQueueInterval); // sleep for 30 seconds
  1050. }
  1051. }
  1052. pthread_exit(NULL); //
  1053. return 0;
  1054. }
  1055. void CheckTransactionPacket(char *uuid)
  1056. {
  1057. char frontUUID[100]={0};
  1058. char frontData[1200]={0};
  1059. int queueNotEmpty = 0;
  1060. int cmpResult = 0;
  1061. queue_operation(0,"","");//showqueue(); ---> remove temporally
  1062. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  1063. if(queueNotEmpty == 1)
  1064. {
  1065. cmpResult = strcmp(frontUUID, uuid);
  1066. //cmpResult = strcmp(frontData, uuid);
  1067. if (cmpResult == 0)
  1068. {
  1069. DEBUG_INFO("TransactionPacket Compare All right!\n");
  1070. queue_operation(2,"","");//delq(); ---> remove temporally
  1071. }
  1072. }
  1073. }
  1074. /* type: 0 (showqueue ); type: 1(showfront); type: 2(delq) type: 3 (sentqueue) type: 4 (addq) type: 5(store queue to /Storage/OCPP/ )*/
  1075. int queue_operation(int type, char *frontUUID, char *frontData)
  1076. {
  1077. pthread_mutex_unlock(&lock_sentData);
  1078. pthread_mutex_lock(&lock_sentData);
  1079. int result=0;
  1080. if(type == 0) // show items in queue
  1081. result = showqueue();
  1082. else if(type == 1) // show first item
  1083. result = showfront(frontUUID, frontData);
  1084. else if(type == 2) // delete item
  1085. result = delq();
  1086. else if(type == 3) // sent items in queue
  1087. result = sentqueue();
  1088. else if(type == 4) // add items to the queue
  1089. result = addq(frontUUID, frontData);
  1090. pthread_mutex_unlock(&lock_sentData);
  1091. return result;
  1092. }
  1093. char * strchr(const char *p, int ch)
  1094. {
  1095. char c;
  1096. c = ch;
  1097. for (;; ++p) {
  1098. if (*p == c)
  1099. return ((char *)p);
  1100. if (*p == '\0')
  1101. return (NULL);
  1102. }
  1103. /* NOTREACHED */
  1104. return NULL;
  1105. }
  1106. int removeMessageSentFile(void)
  1107. {
  1108. char rmFileCmd[100]={0};
  1109. struct stat stats;
  1110. //
  1111. stat("../Storage/OCPP", &stats);
  1112. // Check for directory existence
  1113. if (S_ISDIR(stats.st_mode) == 1)
  1114. {
  1115. DEBUG_INFO("\n OCPP directory exist \n");
  1116. }
  1117. else
  1118. {
  1119. DEBUG_INFO("\n directory not exist, create dir \n");
  1120. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  1121. system(rmFileCmd);
  1122. }
  1123. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1124. if((access("../Storage/OCPP/MessageSent",F_OK))!=-1)
  1125. {
  1126. DEBUG_INFO("MessageSent file exist.\n");
  1127. sprintf(rmFileCmd,"rm -f %s","../Storage/OCPP/MessageSent");
  1128. system(rmFileCmd);
  1129. }
  1130. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1131. return 0;
  1132. }
  1133. #define SystemLogMessage
  1134. //================================================
  1135. // Main process
  1136. //================================================
  1137. int main(void)
  1138. {
  1139. char rmFileCmd[100]={0};
  1140. struct stat stats;
  1141. pthread_t t;
  1142. #ifdef SystemLogMessage
  1143. DEBUG_INFO("Initialization...\n");
  1144. #endif
  1145. if(ProcessShareMemory()== FAIL)
  1146. {
  1147. return FAIL;
  1148. }
  1149. //Create OCPP dir
  1150. stat("../Storage/OCPP", &stats);
  1151. // Check for directory existence
  1152. if (S_ISDIR(stats.st_mode) == 1)
  1153. {
  1154. DEBUG_INFO("\n OCPP directory exist \n");
  1155. }
  1156. else
  1157. {
  1158. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  1159. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  1160. system(rmFileCmd);
  1161. }
  1162. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1163. //Create Process: Resend Transaction
  1164. pthread_create(&t, NULL, processTransactionQueue, NULL); //
  1165. sqlite3_config(SQLITE_CONFIG_URI,1);
  1166. if(sqlite3_open("file:/../Storage/OCPP/charger.db", &db))
  1167. {
  1168. #ifdef Debug
  1169. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1170. #endif
  1171. sqlite3_close( db );
  1172. exit(0);
  1173. }
  1174. else
  1175. {
  1176. #ifdef Debug
  1177. DEBUG_INFO( "Opened database successfully\n");
  1178. #endif
  1179. }
  1180. /* 建立 Table log buffer */
  1181. int rc =sqlite3_exec(db, createsql, 0, 0, &errMsg);
  1182. if (SQLITE_OK != rc)
  1183. {
  1184. #ifdef Debug
  1185. DEBUG_INFO( "Create log buffer table error message: %s\n", errMsg);
  1186. #endif
  1187. return 0;
  1188. }
  1189. else
  1190. {
  1191. #ifdef Debug
  1192. DEBUG_INFO( "Opened log buffer table successfully\n");
  1193. #endif
  1194. }
  1195. /* 建立 Table OcppAuthCache */
  1196. rc =sqlite3_exec(db, sqlOcppAuthCache, 0, 0, &errMsg);
  1197. if (SQLITE_OK != rc)
  1198. {
  1199. #ifdef Debug
  1200. DEBUG_INFO( "Create OcppAuthCache error message: %s\n", errMsg);
  1201. #endif
  1202. return 0;
  1203. }
  1204. else
  1205. {
  1206. #ifdef Debug
  1207. DEBUG_INFO( "Opened OcppAuthCache table successfully\n");
  1208. #endif
  1209. }
  1210. /* 建立 Table OcppAuthLocal */
  1211. rc =sqlite3_exec(db, sqlOcppAuthLocal, 0, 0, &errMsg);
  1212. if (SQLITE_OK != rc)
  1213. {
  1214. #ifdef Debug
  1215. DEBUG_INFO( "Create Table OcppAuthLocal error %s\n",errMsg);
  1216. #endif
  1217. return 0;
  1218. }
  1219. else
  1220. {
  1221. #ifdef Debug
  1222. DEBUG_INFO( "Opened OcppAuthLocal table successfully\n");
  1223. #endif
  1224. }
  1225. initialConfigurationTable();
  1226. removeMessageSentFile();
  1227. //queryAllData();
  1228. OCPP_get_TableAuthlocalAllData();
  1229. for(;;)
  1230. {
  1231. while(ConnectionEstablished==0)
  1232. {
  1233. if((time((time_t*)NULL)-startTime.connect)>=60)
  1234. {
  1235. #ifdef Debug
  1236. DEBUG_INFO("Execute ConnectWsServer\n");
  1237. #endif
  1238. ConnectWsServer();
  1239. startTime.connect=time((time_t*)NULL);
  1240. }
  1241. lws_service(context, 10000);//timeout_ms
  1242. }
  1243. if(( (BootNotificationInterval != 0 && ((time((time_t*)NULL)-startTime.bootNotification)>=BootNotificationInterval) ) || ((time((time_t*)NULL)-startTime.bootNotification)>=defaultWaitingTime) ) && ((server_sign == FALSE)/*|| (server_pending == TRUE)*/))
  1244. {
  1245. //hashmapForMessageNew();
  1246. sendBootNotificationRequest();
  1247. startTime.bootNotification=time((time_t*)NULL);
  1248. }
  1249. if(server_sign == TRUE)
  1250. {
  1251. if(sendbuffer == 1)
  1252. {
  1253. queue_operation(3, "", "");//sentqueue()
  1254. sendbuffer = 0;
  1255. }
  1256. // Check System Value
  1257. CheckSystemValue();
  1258. }
  1259. lws_service(context, 100);//timeout_ms
  1260. }
  1261. pthread_join(t, NULL); // ���ݤl��������槹��
  1262. //hashmapForMessageFree();
  1263. return FAIL;
  1264. }