Module_OcppBackend.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. extern void SetOcppConnStatus(uint8_t status);
  63. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  64. pthread_mutex_t lock_sentData = PTHREAD_MUTEX_INITIALIZER;
  65. //struct json_object *parsed_json;
  66. //extern struct node Node;
  67. //==========================================
  68. // Function prototype
  69. //==========================================
  70. void trim(char *s);
  71. int mystrcmp(char *p1,char *p2);
  72. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  73. void getSubStr(char *dest, char* src, char *split, int idx);
  74. void split(char **arr, char *str, const char *del);
  75. int strpos(char *source, char *substr, int skip);
  76. int strposs(char *source, char *substr, int idx);
  77. char *random_uuid( char buf[37] );
  78. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
  79. char * strchr(const char *p, int ch);
  80. void ReceivedMessage(void *in, size_t len);
  81. struct StartTime
  82. {
  83. unsigned int connect;
  84. unsigned int bootNotification;
  85. }startTime;
  86. #if 0
  87. unsigned char *SendBuffer;
  88. int SendBufLen=(1024*4);//(1024*3);
  89. #endif
  90. int SendBufLen=(1024*4);//(1024*3);
  91. unsigned char SendBuffer[1024*4]={0};
  92. static int ConnectionEstablished=0;
  93. static int TransactionMessageResend = 1;
  94. static int TransactionQueueInterval = 10;//3; // TransactionMessageAttempts
  95. static int TransactionQueueNum = 0;
  96. static int OfflineTransaction = 0;
  97. int defaultWaitingTime = 10; //10 second
  98. extern int BootNotificationInterval;
  99. //char guid[37];
  100. //map_t hashMap;
  101. //data_struct_t* mapItem; --- remove for temporally
  102. //data_struct_t mapItem[0]={0};
  103. sqlite3 *db;
  104. char *errMsg = NULL;
  105. static char *createsql = "CREATE TABLE IF NOT EXISTS log_buffer("
  106. "idx integer primary key,"
  107. "user_id text,"
  108. "cmd_sn text,"
  109. "charger_id text,"
  110. "gun_type text,"
  111. "gun_no text,"
  112. "rfid_no text,"
  113. "stime text,"
  114. "etime text,"
  115. "time_len text,"
  116. "s_soc text,"
  117. "e_soc text,"
  118. "stop_reason text,"
  119. "power text,"
  120. "meter_before text,"
  121. "meter_after text,"
  122. "charge_price text,"
  123. "reserve text,"
  124. "surplus_before text,"
  125. "surplus_after text,"
  126. "service_price text,"
  127. "is_pay text,"
  128. "charge_strategy text,"
  129. "charge_parameter text,"
  130. "vin text,"
  131. "vehicle_no text,"
  132. "start_method text,"
  133. "card_type text,"
  134. "is_upload text,"
  135. "guid text UNIQUE,"
  136. "is_buf2OK text);";
  137. #if 0
  138. static char *createsChargingRecordsql = "CREATE TABLE IF NOT EXISTS ChargingRecord("
  139. "idx integer primary key,"
  140. "gun_type text,"
  141. "connectorId text,"
  142. "idTag text,"
  143. "transactionId text,"
  144. "stime text,"
  145. "etime text,"
  146. "time_len text,"
  147. "s_soc text,"
  148. "e_soc text,"
  149. "stop_reason text,"
  150. "power text,"
  151. "meter_before text,"
  152. "meter_after text,"
  153. "reservationId text,"
  154. "guid text UNIQUE);";
  155. //"is_buf2OK text);";
  156. #endif
  157. static char *sqlOcppAuthCache = "create table if not exists ocpp_auth_cache (idx integer primary key,"
  158. "idtag text UNIQUE,"
  159. "parent_idtag text,"
  160. "expir_date text,"
  161. "status text);";
  162. static char *sqlOcppAuthLocal = "create table if not exists ocpp_auth_local (idx integer primary key,"
  163. "idtag text UNIQUE,"
  164. "parent_idtag text,"
  165. "expir_date text,"
  166. "status text,"
  167. "version text);";
  168. #if 0
  169. 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')";
  170. #endif
  171. int DiffTimeb(struct timeb ST, struct timeb ET)
  172. {
  173. //return milli-second
  174. unsigned int StartTime,StopTime;
  175. StartTime=(unsigned int)ST.time;
  176. StopTime=(unsigned int)ET.time;
  177. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  178. }
  179. //=================================
  180. // Common routine
  181. //=================================
  182. void trim(char *s)
  183. {
  184. int i=0, j, k, l=0;
  185. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  186. i++;
  187. j = strlen(s)-1;
  188. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  189. j--;
  190. if(i==0 && j==strlen(s)-1) { }
  191. else if(i==0) s[j+1] = '\0';
  192. else {
  193. for(k=i; k<=j; k++) s[l++] = s[k];
  194. s[l] = '\0';
  195. }
  196. }
  197. int mystrcmp(char *p1,char *p2)
  198. {
  199. while(*p1==*p2)
  200. {
  201. if(*p1=='\0' || *p2=='\0')
  202. break;
  203. p1++;
  204. p2++;
  205. }
  206. if(*p1=='\0' && *p2=='\0')
  207. return(PASS);
  208. else
  209. return(FAIL);
  210. }
  211. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  212. {
  213. strncpy(dest, src + start, cnt);
  214. dest[cnt] = 0;
  215. }
  216. void getSubStr(char *dest, char* src, char *split, int idx)
  217. {
  218. int start = (strposs(src,",",idx)+1);
  219. int cnt = (strposs(src,",",idx+1)-2)-(strposs(src,",",idx)+1);
  220. strncpy(dest, src + start, cnt);
  221. dest[cnt] = 0;
  222. }
  223. void split(char **arr, char *str, const char *del)
  224. {
  225. char *s = strtok(str, del);
  226. while(s != NULL)
  227. {
  228. *arr++ = s;
  229. s = strtok(NULL, del);
  230. }
  231. }
  232. int strpos(char *source, char *substr, int skip)
  233. {
  234. char stack[strlen(source)];
  235. strncpy(stack, source+skip, strlen(source)-skip);
  236. char *p = strstr(stack, substr);
  237. if (p)
  238. return p - stack+skip;
  239. return -1;
  240. }
  241. int strposs(char *source, char *substr, int idx)
  242. {
  243. char stack[strlen(source)];
  244. int result=0;
  245. int count=0;
  246. while(count<=idx)
  247. {
  248. memset(stack,0,sizeof stack);
  249. strncpy(stack, source+result, strlen(source)-result);
  250. int loc = strcspn(stack, substr);
  251. if(loc>0)
  252. result += (loc+1);
  253. else
  254. result = -1;
  255. count++;
  256. }
  257. return result;
  258. }
  259. char *random_uuid( char buf[37] )
  260. {
  261. const char *c = "89ab";
  262. char *p = buf;
  263. int n;
  264. for( n = 0; n < 16; ++n )
  265. {
  266. int b = rand()%255;
  267. switch( n )
  268. {
  269. case 6:
  270. sprintf(p, "4%x", b%15 );
  271. break;
  272. case 8:
  273. sprintf(p, "%c%x", c[rand()%strlen(c)], b%15 );
  274. break;
  275. default:
  276. sprintf(p, "%02x", b);
  277. break;
  278. }
  279. p += 2;
  280. switch( n )
  281. {
  282. case 3:
  283. case 5:
  284. case 7:
  285. case 9:
  286. *p++ = '-';
  287. break;
  288. }
  289. }
  290. *p = 0;
  291. return buf;
  292. }
  293. //==========================================
  294. // Web socket tranceive routine
  295. //==========================================
  296. int SendData(struct lws *wsi)
  297. {
  298. int n;
  299. int len;
  300. unsigned char out[LWS_SEND_BUFFER_PRE_PADDING + 4096 + LWS_SEND_BUFFER_POST_PADDING] = {0};
  301. len = strlen((char *)SendBuffer);
  302. if(len == 0)
  303. return 0;
  304. memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
  305. DEBUG_OCPPMESSAGE_INFO("out + LWS_SEND_BUFFER_PRE_PADDING = %s\n", out + LWS_SEND_BUFFER_PRE_PADDING);
  306. n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
  307. memset(SendBuffer, 0, len);
  308. return n;
  309. }
  310. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
  311. {
  312. switch (reason)
  313. {
  314. case LWS_CALLBACK_PROTOCOL_INIT:
  315. #ifdef SystemLogMessage
  316. DEBUG_INFO("LWS_CALLBACK_PROTOCOL_INIT\n");
  317. #endif
  318. break;
  319. case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH:
  320. #ifdef SystemLogMessage
  321. DEBUG_INFO("LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH\n");
  322. #endif
  323. break;
  324. case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
  325. #ifdef SystemLogMessage
  326. DEBUG_INFO("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
  327. #endif
  328. break;
  329. case LWS_CALLBACK_WSI_DESTROY:
  330. #ifdef SystemLogMessage
  331. DEBUG_INFO("LWS_CALLBACK_WSI_DESTROY\n");
  332. #endif
  333. server_sign = FALSE;
  334. break;
  335. case LWS_CALLBACK_LOCK_POLL:
  336. #ifdef SystemLogMessage
  337. //DEBUG_INFO("LWS_CALLBACK_LOCK_POLL\n");
  338. #endif
  339. break;
  340. case LWS_CALLBACK_ADD_POLL_FD:
  341. #ifdef SystemLogMessage
  342. DEBUG_INFO("LWS_CALLBACK_ADD_POLL_FD\n");
  343. #endif
  344. break;
  345. case LWS_CALLBACK_DEL_POLL_FD:
  346. #ifdef SystemLogMessage
  347. DEBUG_INFO("LWS_CALLBACK_DEL_POLL_FD\n");
  348. #endif
  349. break;
  350. case LWS_CALLBACK_UNLOCK_POLL:
  351. #ifdef SystemLogMessage
  352. //DEBUG_INFO("LWS_CALLBACK_UNLOCK_POLL\n");
  353. #endif
  354. break;
  355. case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
  356. #ifdef SystemLogMessage
  357. //DEBUG_INFO("LWS_CALLBACK_CHANGE_MODE_POLL_FD\n");
  358. #endif
  359. break;
  360. case LWS_CALLBACK_WSI_CREATE:
  361. #ifdef SystemLogMessage
  362. DEBUG_INFO("LWS_CALLBACK_WSI_CREATE\n");
  363. #endif
  364. break;
  365. case LWS_CALLBACK_GET_THREAD_ID:
  366. //#ifdef SystemLogMessage
  367. //DEBUG_INFO("LWS_CALLBACK_GET_THREAD_ID\n");
  368. //#endif
  369. break;
  370. case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
  371. #ifdef SystemLogMessage
  372. DEBUG_INFO("LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER\n");
  373. #endif
  374. break;
  375. case LWS_CALLBACK_CLIENT_ESTABLISHED://3
  376. #ifdef SystemLogMessage
  377. DEBUG_INFO("LWS_CALLBACK_CLIENT_ESTABLISHED\n");
  378. #endif
  379. //connected
  380. ConnectionEstablished=1;
  381. break;
  382. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
  383. #ifdef Debug
  384. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR:%s\n",in);
  385. #endif
  386. #ifdef SystemLogMessage
  387. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
  388. #endif
  389. //disconnected
  390. ConnectionEstablished=0;
  391. break;
  392. case LWS_CALLBACK_CLOSED://4
  393. #ifdef SystemLogMessage
  394. DEBUG_INFO("LWS_CALLBACK_CLOSED\n");
  395. ConnectionEstablished=0;
  396. #endif
  397. //disconnected
  398. break;
  399. case LWS_CALLBACK_CLIENT_WRITEABLE://10
  400. //if(need to send message and its relevant data already store into SendBuffer)
  401. #ifdef SystemLogMessage
  402. DEBUG_INFO("LWS_CALLBACK_CLIENT_WRITEABLE\n");
  403. #endif
  404. SendData(wsi);
  405. break;
  406. case LWS_CALLBACK_CLIENT_RECEIVE://8
  407. ((char *)in)[len] = '\0';
  408. #ifdef SystemLogMessage
  409. DEBUG_INFO("LWS_CALLBACK_CLIENT_RECEIVE, RX length: %d\n", (int)len);
  410. #endif
  411. DEBUG_OCPPMESSAGE_INFO("%s\n", (char *)in);
  412. ReceivedMessage(in, len);
  413. break;
  414. default:
  415. #ifdef Debug
  416. DEBUG_INFO("OCPP16Callback:reason=%d\n", reason);
  417. #endif
  418. break;
  419. }
  420. return 0;
  421. }
  422. static struct lws_protocols protocols[] = {
  423. {
  424. "ocpp1.6",
  425. OCPP16Callback,
  426. 10240,
  427. 10240,
  428. },
  429. {
  430. "ocpp1.6",
  431. OCPP16Callback,
  432. 10240,
  433. 10240,
  434. },
  435. {
  436. NULL, NULL, 0 /* End of list */
  437. }
  438. };
  439. int ConnectWsServer()
  440. {
  441. int result = PASS;
  442. struct lws_context_creation_info ContextInfo;
  443. struct lws_client_connect_info ConnInfo;
  444. int use_ssl=0;
  445. //lws_set_log_level(LLL_PARSER | LLL_HEADER, NULL);
  446. if(context!=NULL)
  447. lws_context_destroy(context);
  448. memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
  449. ContextInfo.port = CONTEXT_PORT_NO_LISTEN;
  450. ContextInfo.iface = NULL;
  451. ContextInfo.ssl_private_key_password = NULL;
  452. ContextInfo.ssl_cert_filepath = NULL;
  453. ContextInfo.ssl_private_key_filepath = NULL;
  454. ContextInfo.ssl_ca_filepath = "/root/cacert.pem";
  455. ContextInfo.ssl_cipher_list = NULL; //use default one
  456. ContextInfo.gid = -1;
  457. ContextInfo.uid = -1;
  458. if(use_ssl)
  459. ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  460. ContextInfo.protocols = protocols;
  461. ContextInfo.timeout_secs = 9999;//30;
  462. //if(ping pong enabled)
  463. ContextInfo.ws_ping_pong_interval = 30;//0 for none, else interval in seconds
  464. context = lws_create_context(&ContextInfo);
  465. if (context == NULL)
  466. {
  467. #ifdef SystemLogMessage
  468. DEBUG_ERROR("lws_create_context NG");
  469. #endif
  470. result = FAIL;
  471. }
  472. memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
  473. // fill up below information
  474. ConnInfo.context = context;
  475. 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";
  476. ConnInfo.port = GetOcppPort();//2012;//8080;
  477. ConnInfo.path=(const char *)GetOcppPath();//"/ocpp/RDTEST103";//"/ocpp/OCTT_1";//"/RDTEST103";//"/0591201511030003";//"/RDTEST103";
  478. ConnInfo.host=lws_canonical_hostname(context);
  479. ConnInfo.origin="origin";
  480. ConnInfo.protocol = protocols[1].name;
  481. ConnInfo.ietf_version_or_minus_one = -1;
  482. if(use_ssl)
  483. ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;;
  484. wsi_client = lws_client_connect_via_info(&ConnInfo);
  485. if (!wsi_client)
  486. {
  487. #ifdef SystemLogMessage
  488. DEBUG_ERROR("lws_client_connect_via_info NG");
  489. #endif
  490. result = FAIL;
  491. }
  492. return result;
  493. }
  494. #if 0
  495. struct _node {
  496. char uuid[37];
  497. char data[2000];
  498. struct _node *next;//struct node *next;
  499. }node;
  500. typedef struct _node *pnode;
  501. struct _node staticNodeArray[100]={0};
  502. #endif
  503. //void createq();
  504. int showfront(char *uuid, char *data);
  505. int addq(char *uuid, char *data) ;
  506. int delq();
  507. int sentqueue();
  508. #if 0
  509. int storequeue();
  510. int readfiletoqueue();
  511. #endif
  512. void CheckTransactionPacket(char *uuid);
  513. int queue_operation(int type, char *frontUUID, char *frontData);
  514. #if 0
  515. pnode front, rear;
  516. void createq() {
  517. //front = rear = (pnode)malloc(sizeof(node)); --- remove temporally
  518. //front->next = rear->next = NULL;
  519. front = rear = NULL;
  520. }
  521. #endif
  522. int showfront(char *uuid, char *data) {
  523. FILE *fp;
  524. int result = FALSE; // 1: TRUE 0:FALSE
  525. char str[1200]={0};
  526. char sstr[50]={ 0 };//sstr[200]={ 0 };
  527. int c = 0;
  528. char *loc;
  529. char rmFileCmd[100]={0};
  530. struct stat stats;
  531. stat("../Storage/OCPP", &stats);
  532. // Check for directory existence
  533. if (S_ISDIR(stats.st_mode) == 1)
  534. {
  535. //DEBUG_INFO("\n OCPP directory exist \n");
  536. }
  537. else
  538. {
  539. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  540. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  541. system(rmFileCmd);
  542. }
  543. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  544. if((access("../Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  545. {
  546. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  547. }
  548. else
  549. {
  550. DEBUG_INFO("TransactionRelatedQueue not exist\n");
  551. FILE *log = fopen("../Storage/OCPP/TransactionRelatedQueue", "w+");
  552. if(log == NULL)
  553. {
  554. DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  555. return 0;
  556. }
  557. else
  558. {
  559. fclose(log);
  560. }
  561. }
  562. /* opening file for reading */
  563. fp = fopen("../Storage/OCPP/TransactionRelatedQueue" , "r");
  564. if(fp == NULL) {
  565. DEBUG_INFO("Error opening TransactionRelatedQueue file");
  566. return FALSE;
  567. }
  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. }
  1040. usleep(5000);
  1041. }
  1042. pthread_exit(NULL); //
  1043. return 0;
  1044. }
  1045. void CheckTransactionPacket(char *uuid)
  1046. {
  1047. char frontUUID[100]={0};
  1048. char frontData[1200]={0};
  1049. int queueNotEmpty = 0;
  1050. int cmpResult = 0;
  1051. queue_operation(0,"","");//showqueue(); ---> remove temporally
  1052. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  1053. if(queueNotEmpty == 1)
  1054. {
  1055. cmpResult = strcmp(frontUUID, uuid);
  1056. //cmpResult = strcmp(frontData, uuid);
  1057. if (cmpResult == 0)
  1058. {
  1059. DEBUG_INFO("TransactionPacket Compare All right!\n");
  1060. queue_operation(2,"","");//delq(); ---> remove temporally
  1061. }
  1062. }
  1063. }
  1064. /* type: 0 (showqueue ); type: 1(showfront); type: 2(delq) type: 3 (sentqueue) type: 4 (addq) type: 5(store queue to /Storage/OCPP/ )*/
  1065. int queue_operation(int type, char *frontUUID, char *frontData)
  1066. {
  1067. pthread_mutex_unlock(&lock_sentData);
  1068. pthread_mutex_lock(&lock_sentData);
  1069. int result=0;
  1070. if(type == 0) // show items in queue
  1071. result = showqueue();
  1072. else if(type == 1) // show first item
  1073. result = showfront(frontUUID, frontData);
  1074. else if(type == 2) // delete item
  1075. result = delq();
  1076. else if(type == 3) // sent items in queue
  1077. result = sentqueue();
  1078. else if(type == 4) // add items to the queue
  1079. result = addq(frontUUID, frontData);
  1080. pthread_mutex_unlock(&lock_sentData);
  1081. return result;
  1082. }
  1083. char * strchr(const char *p, int ch)
  1084. {
  1085. char c;
  1086. c = ch;
  1087. for (;; ++p) {
  1088. if (*p == c)
  1089. return ((char *)p);
  1090. if (*p == '\0')
  1091. return (NULL);
  1092. }
  1093. /* NOTREACHED */
  1094. return NULL;
  1095. }
  1096. int removeMessageSentFile(void)
  1097. {
  1098. char rmFileCmd[100]={0};
  1099. struct stat stats;
  1100. //
  1101. stat("../Storage/OCPP", &stats);
  1102. // Check for directory existence
  1103. if (S_ISDIR(stats.st_mode) == 1)
  1104. {
  1105. //DEBUG_INFO("\n OCPP directory exist \n");
  1106. }
  1107. else
  1108. {
  1109. DEBUG_INFO("\n directory not exist, create dir \n");
  1110. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  1111. system(rmFileCmd);
  1112. }
  1113. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1114. if((access("../Storage/OCPP/MessageSent",F_OK))!=-1)
  1115. {
  1116. DEBUG_INFO("MessageSent file exist.\n");
  1117. sprintf(rmFileCmd,"rm -f %s","../Storage/OCPP/MessageSent");
  1118. system(rmFileCmd);
  1119. }
  1120. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1121. return 0;
  1122. }
  1123. #define SystemLogMessage
  1124. //================================================
  1125. // Main process
  1126. //================================================
  1127. int main(void)
  1128. {
  1129. char rmFileCmd[100]={0};
  1130. struct stat stats;
  1131. pthread_t t;
  1132. #ifdef SystemLogMessage
  1133. DEBUG_INFO("Initialization...\n");
  1134. #endif
  1135. if(ProcessShareMemory()== FAIL)
  1136. {
  1137. return FAIL;
  1138. }
  1139. //Create OCPP dir
  1140. stat("../Storage/OCPP", &stats);
  1141. // Check for directory existence
  1142. if (S_ISDIR(stats.st_mode) == 1)
  1143. {
  1144. //DEBUG_INFO("\n OCPP directory exist \n");
  1145. }
  1146. else
  1147. {
  1148. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  1149. sprintf(rmFileCmd,"mkdir -p %s","../Storage/OCPP");
  1150. system(rmFileCmd);
  1151. }
  1152. memset(&rmFileCmd, 0, sizeof rmFileCmd);
  1153. //Create Process: Resend Transaction
  1154. pthread_create(&t, NULL, processTransactionQueue, NULL); //
  1155. sqlite3_config(SQLITE_CONFIG_URI,1);
  1156. if(sqlite3_open("file:/../Storage/OCPP/charger.db", &db))
  1157. {
  1158. #ifdef Debug
  1159. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1160. #endif
  1161. sqlite3_close( db );
  1162. exit(0);
  1163. }
  1164. else
  1165. {
  1166. #ifdef Debug
  1167. DEBUG_INFO( "Opened database successfully\n");
  1168. #endif
  1169. }
  1170. /* 建立 Table log buffer */
  1171. int rc =sqlite3_exec(db, createsql, 0, 0, &errMsg);
  1172. if (SQLITE_OK != rc)
  1173. {
  1174. #ifdef Debug
  1175. DEBUG_INFO( "Create log buffer table error message: %s\n", errMsg);
  1176. #endif
  1177. return 0;
  1178. }
  1179. else
  1180. {
  1181. #ifdef Debug
  1182. DEBUG_INFO( "Opened log buffer table successfully\n");
  1183. #endif
  1184. }
  1185. /* 建立 Table OcppAuthCache */
  1186. rc =sqlite3_exec(db, sqlOcppAuthCache, 0, 0, &errMsg);
  1187. if (SQLITE_OK != rc)
  1188. {
  1189. #ifdef Debug
  1190. DEBUG_INFO( "Create OcppAuthCache error message: %s\n", errMsg);
  1191. #endif
  1192. return 0;
  1193. }
  1194. else
  1195. {
  1196. #ifdef Debug
  1197. DEBUG_INFO( "Opened OcppAuthCache table successfully\n");
  1198. #endif
  1199. }
  1200. /* 建立 Table OcppAuthLocal */
  1201. rc =sqlite3_exec(db, sqlOcppAuthLocal, 0, 0, &errMsg);
  1202. if (SQLITE_OK != rc)
  1203. {
  1204. #ifdef Debug
  1205. DEBUG_INFO( "Create Table OcppAuthLocal error %s\n",errMsg);
  1206. #endif
  1207. return 0;
  1208. }
  1209. else
  1210. {
  1211. #ifdef Debug
  1212. DEBUG_INFO( "Opened OcppAuthLocal table successfully\n");
  1213. #endif
  1214. }
  1215. initialConfigurationTable();
  1216. removeMessageSentFile();
  1217. //queryAllData();
  1218. OCPP_get_TableAuthlocalAllData();
  1219. for(;;)
  1220. {
  1221. while(ConnectionEstablished==0)
  1222. {
  1223. SetOcppConnStatus(FALSE);
  1224. if((time((time_t*)NULL)-startTime.connect)>=60)
  1225. {
  1226. #ifdef Debug
  1227. DEBUG_INFO("Execute ConnectWsServer\n");
  1228. #endif
  1229. ConnectWsServer();
  1230. startTime.connect=time((time_t*)NULL);
  1231. }
  1232. lws_service(context, 10000);//timeout_ms
  1233. }
  1234. if(( (BootNotificationInterval != 0 && ((time((time_t*)NULL)-startTime.bootNotification)>=BootNotificationInterval) ) || ((time((time_t*)NULL)-startTime.bootNotification)>=defaultWaitingTime) ) && ((server_sign == FALSE)/*|| (server_pending == TRUE)*/))
  1235. {
  1236. //hashmapForMessageNew();
  1237. sendBootNotificationRequest();
  1238. startTime.bootNotification=time((time_t*)NULL);
  1239. }
  1240. if(server_sign == TRUE)
  1241. {
  1242. if(sendbuffer == 1)
  1243. {
  1244. queue_operation(3, "", "");//sentqueue()
  1245. sendbuffer = 0;
  1246. }
  1247. // Check System Value
  1248. CheckSystemValue();
  1249. SetOcppConnStatus(TRUE);
  1250. }
  1251. lws_service(context, 100);//timeout_ms
  1252. }
  1253. pthread_join(t, NULL); // ���ݤl��������槹��
  1254. //hashmapForMessageFree();
  1255. return FAIL;
  1256. }