|
@@ -916,272 +916,199 @@ int showqueue()
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
-int sentqueue(){
|
|
|
+int sentqueue()
|
|
|
+{
|
|
|
+ int result = FAIL;
|
|
|
+ struct stat stats;
|
|
|
FILE *fp;
|
|
|
- int result = FALSE; // 1: TRUE 0:FALSE
|
|
|
- int temptransactionId = 0, gettransactionId = 0;
|
|
|
- int tempconnectorId = 0;
|
|
|
- //int gunIndex = 0;
|
|
|
- char guid[37]={0};
|
|
|
- char tempdata[65]={0};
|
|
|
- char key_value[65]={0};
|
|
|
- int IsStopTransaction = FALSE;
|
|
|
- //int IsconnectorIdNULL = FALSE;
|
|
|
- //int IsIdtagNULL = FALSE;
|
|
|
+ json_object *obj = NULL;
|
|
|
+ json_object *objPayload = NULL;
|
|
|
+ json_object *objData = NULL;
|
|
|
+
|
|
|
+ char cmd[128];
|
|
|
char str[QUEUE_MESSAGE_LENGTH]={0};
|
|
|
- char strcomposite[QUEUE_MESSAGE_LENGTH]={0};
|
|
|
- char rmFileCmd[100]={0};
|
|
|
- char connectorStr[2]={0};
|
|
|
- struct stat stats;
|
|
|
- char sstr[28]={0};
|
|
|
- unsigned char IdtagStr[20]={0};
|
|
|
- unsigned char timestampStr[30]={0};
|
|
|
- int tempmeterStart = 0;
|
|
|
- int tempreservationId = 0;
|
|
|
- int c = 0;
|
|
|
- char *loc;
|
|
|
+ char queueData[QUEUE_MESSAGE_LENGTH]={0};
|
|
|
+ char payload[QUEUE_MESSAGE_LENGTH]={0};
|
|
|
+ char key_value[65]={0};
|
|
|
+ char hashData[65]={0};
|
|
|
+ char action[32];
|
|
|
+ char guid[37];
|
|
|
+ uint8_t connectorId;
|
|
|
|
|
|
DEBUG_INFO("Sent queue.\n");
|
|
|
|
|
|
- stat("/Storage/OCPP_PH", &stats);
|
|
|
+ stat("/Storage/OCPP", &stats);
|
|
|
|
|
|
// Check for directory existence
|
|
|
- if (S_ISDIR(stats.st_mode) == 1)
|
|
|
- {
|
|
|
- //DEBUG_INFO("\n OCPP directory exist \n");
|
|
|
- }
|
|
|
- else
|
|
|
+ if (S_ISDIR(stats.st_mode) != 1)
|
|
|
{
|
|
|
//DEBUG_INFO("\n OCPP directory not exist, create dir \n");
|
|
|
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP_PH");
|
|
|
- system(rmFileCmd);
|
|
|
+ sprintf(cmd, "mkdir -p /Storage/OCPP");
|
|
|
+ system(cmd);
|
|
|
}
|
|
|
|
|
|
- memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
|
|
|
-
|
|
|
- /* opening file for reading */
|
|
|
- fp = fopen("/Storage/OCPP_PH/TransactionRelatedQueue" , "r");
|
|
|
- if(fp == NULL) {
|
|
|
- DEBUG_INFO("Error opening file");
|
|
|
- return FALSE;
|
|
|
+ if((fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r")) == NULL)
|
|
|
+ {
|
|
|
+ DEBUG_ERROR("Error opening file");
|
|
|
}
|
|
|
-
|
|
|
- if( fgets (str, QUEUE_MESSAGE_LENGTH, fp)!=NULL ) {
|
|
|
-
|
|
|
- //---- writing content to stdout ---//
|
|
|
-
|
|
|
- //*********************Start: StopTransaction***************************/
|
|
|
- loc = strstr(str, "StopTransaction");
|
|
|
- c = 0;
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- if(loc != NULL)
|
|
|
- {
|
|
|
- IsStopTransaction = TRUE;
|
|
|
- }
|
|
|
-
|
|
|
- memset(connectorStr,0,ARRAY_SIZE(connectorStr));
|
|
|
- strncpy(connectorStr, str, 1);
|
|
|
- tempconnectorId = atoi(connectorStr);
|
|
|
- //*********************End: StopTransaction***************************/
|
|
|
-
|
|
|
- #if 0
|
|
|
- //*********************Start:connectorId***************************/
|
|
|
- loc = strstr(str, "connectorId");
|
|
|
- c = 0;
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- if(loc != NULL)
|
|
|
- {
|
|
|
- while (loc[strlen("connectorId")+2+c] != ',')
|
|
|
- {
|
|
|
- sstr[c] = loc[strlen("connectorId")+2+c];
|
|
|
- c++;
|
|
|
- }
|
|
|
- sstr[c] = '\0';
|
|
|
- tempconnectorId = atoi(sstr);
|
|
|
- }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // IsconnectorIdNULL = TRUE;
|
|
|
- // }
|
|
|
- //*********************End:connectorId***************************/
|
|
|
- #endif
|
|
|
-
|
|
|
- //*********************Start:idTag***************************/
|
|
|
- loc = strstr(str, "idTag");
|
|
|
- c = 0;
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- if(loc != NULL)
|
|
|
- {
|
|
|
- while (loc[3+strlen("idTag")+c] != '\"')
|
|
|
- {
|
|
|
- sstr[c] = loc[3+strlen("idTag")+c];
|
|
|
- c++;
|
|
|
- }
|
|
|
- sstr[c] = '\0';
|
|
|
- strcpy((char*)IdtagStr, sstr);
|
|
|
- }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // IsIdtagNULL = TRUE;
|
|
|
- // }
|
|
|
- //*********************End:idTag***************************/
|
|
|
-
|
|
|
-
|
|
|
- //*********************Start: StartTransaction***************************/
|
|
|
- loc = strstr(str, "StartTransaction");
|
|
|
- c = 0;
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- if(loc != NULL)
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // parse message content
|
|
|
+ if(fgets(str, QUEUE_MESSAGE_LENGTH, fp) != NULL)
|
|
|
{
|
|
|
- // [2,0200000000000000000000000001584415776,StartTransaction,{connectorId:1,idTag:123,meterStart:100,reservationId:0,timestamp:2020-03-17T03:29:36Z}]
|
|
|
- //DEBUG_INFO("\n sent queue StartTransaction\n");
|
|
|
- if(tempconnectorId > 0)
|
|
|
- {
|
|
|
- sprintf(tempdata, "StartTransaction,%d", (tempconnectorId-1));
|
|
|
- }
|
|
|
+ // parse connectorId
|
|
|
+ connectorId = (str[0]-0x30);
|
|
|
+ memcpy(&queueData, &str[2], strlen(str)-2);
|
|
|
|
|
|
- //GUID
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- c=0;
|
|
|
- while (str[6+c] != '\"')
|
|
|
+ obj = json_tokener_parse(queueData);
|
|
|
+ if(!is_error(obj))
|
|
|
{
|
|
|
- sstr[c] = str[6+c];
|
|
|
- c++;
|
|
|
- }
|
|
|
- sstr[c] = '\0';
|
|
|
- strcpy(guid, sstr);
|
|
|
-
|
|
|
-
|
|
|
- //Idtag
|
|
|
- loc = strstr(str, "idTag");
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- c=0;
|
|
|
- while (loc[3+strlen("idTag")+c] != '\"')
|
|
|
- {
|
|
|
- sstr[c] = loc[3+strlen("idTag")+c];
|
|
|
- c++;
|
|
|
- }
|
|
|
- sstr[c] = '\0';
|
|
|
- strcpy((char*)IdtagStr, sstr);
|
|
|
-
|
|
|
- //meterStart
|
|
|
- loc = strstr(str, "meterStart");
|
|
|
- c = 0;
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- if(loc != NULL)
|
|
|
- {
|
|
|
- while (loc[strlen("meterStart")+2+c] != ',')
|
|
|
+ sprintf(guid, "%s", json_object_get_string(json_object_array_get_idx(obj, 1)));
|
|
|
+ sprintf(action, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
|
|
|
+ sprintf(payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 3), JSON_C_TO_STRING_PLAIN));
|
|
|
+ objPayload = json_tokener_parse(payload);
|
|
|
+ if(!is_error(objPayload))
|
|
|
{
|
|
|
- sstr[c] = loc[strlen("meterStart")+2+c];
|
|
|
- c++;
|
|
|
- }
|
|
|
- sstr[c] = '\0';
|
|
|
- tempmeterStart = atoi(sstr);
|
|
|
-
|
|
|
- }
|
|
|
+ if(strstr(action, "StartTransaction") != NULL)
|
|
|
+ {
|
|
|
+ if(hashmap_operation(HASH_OP_GET, guid, key_value) == TRUE)
|
|
|
+ {
|
|
|
+ //DEBUG_INFO("\n 1. sent queue guid=%s\n",guid);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ char idtag[21]={0};
|
|
|
+ char timestamp[36]={0};
|
|
|
+ int meterStart=0;
|
|
|
+ int reservationId=-1;
|
|
|
+ sprintf(hashData, "StartTransaction,%d", (connectorId-1));
|
|
|
+ hashmap_operation(HASH_OP_ADD, guid, hashData);
|
|
|
+
|
|
|
+ if(json_object_object_get(objPayload, "idTag") != NULL)
|
|
|
+ {
|
|
|
+ sprintf(idtag, "%s", json_object_get_string(json_object_object_get(objPayload, "idTag")));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(json_object_object_get(objPayload, "meterStart") != NULL)
|
|
|
+ {
|
|
|
+ meterStart = json_object_get_int(json_object_object_get(objPayload, "meterStart"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(json_object_object_get(objPayload, "reservationId") != NULL)
|
|
|
+ {
|
|
|
+ reservationId = json_object_get_int(json_object_object_get(objPayload, "reservationId"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(json_object_object_get(objPayload, "timestamp") != NULL)
|
|
|
+ {
|
|
|
+ sprintf(timestamp, "%s", json_object_get_string(json_object_object_get(objPayload, "timestamp")));
|
|
|
+ }
|
|
|
+
|
|
|
+ FillStartTransaction(connectorId, (unsigned char*)idtag, meterStart, reservationId, (unsigned char*)timestamp);
|
|
|
+ //DEBUG_INFO("\n 2. sent queue guid=%s\n",guid);
|
|
|
+ }
|
|
|
+
|
|
|
+ LWS_Send((char*)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
|
|
|
+ json_object_put(objPayload);
|
|
|
+ }
|
|
|
+ else if((strstr(action, "MeterValues") != NULL) || strstr(action, "StopTransaction") != NULL)
|
|
|
+ {
|
|
|
+ char idtag[21]={0};
|
|
|
+ int transactionId_org=0;
|
|
|
+ int transactionId_map=0;
|
|
|
+
|
|
|
+ if(json_object_object_get(objPayload, "transactionId") != NULL)
|
|
|
+ {
|
|
|
+ transactionId_org = json_object_get_int(json_object_object_get(objPayload, "transactionId"));
|
|
|
+ if(json_object_object_get(objPayload, "idTag") != NULL)
|
|
|
+ {
|
|
|
+ sprintf(idtag, "%s", json_object_get_string(json_object_object_get(objPayload, "idTag")));
|
|
|
+ }
|
|
|
+
|
|
|
+ //Get IdTag from StartTransaction , store to StartTransactionIdTagTemp, For StopTransaction usage in Queue (StartTransaction. StopTransaction user id different), get actual TransactionId
|
|
|
+ GetStartTransactionIdTag(connectorId-1);
|
|
|
+ transactionId_map = GetTransactionId(connectorId, (unsigned char*)idtag, ((strstr(action, "StopTransaction") != NULL)?YES:NO));
|
|
|
+
|
|
|
+ DEBUG_INFO("queue map transactionId = %d\n", transactionId_map);
|
|
|
+ DEBUG_INFO("original connectorId = %d\n", connectorId);
|
|
|
+ DEBUG_INFO("original transactionId = %d\n", transactionId_org);
|
|
|
+ DEBUG_INFO("original IdtagStr = %s\n", "");
|
|
|
+ if((transactionId_map != 0)&&(transactionId_org != transactionId_map))
|
|
|
+ {
|
|
|
+ //replace transactionId
|
|
|
+ json_object_object_add(objPayload, "transactionId", json_object_new_int(transactionId_map));
|
|
|
+ }
|
|
|
+ else if((transactionId_map == 0) && (transactionId_org == 0))
|
|
|
+ {
|
|
|
+ //replace transactionId
|
|
|
+ transactionId_map = GetStartTransactionId(connectorId-1);
|
|
|
+ json_object_object_add(objData, "idTx", json_object_new_int(transactionId_map));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ transactionId_map = transactionId_org;
|
|
|
+ }
|
|
|
+ DEBUG_INFO("Final transactionId = %d\n", transactionId_map);
|
|
|
+ }
|
|
|
+
|
|
|
+ json_object_array_put_idx(obj, 3, objPayload);
|
|
|
+ LWS_Send((char*)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
|
|
|
+
|
|
|
+ if(strstr(action, "StopTransaction") != NULL)
|
|
|
+ queueOpInfo.stopTransactionId = transactionId_map;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ int transactionId_org=0;
|
|
|
+ int transactionId_map=0;
|
|
|
+
|
|
|
+ if(strstr(json_object_get_string(json_object_object_get(objPayload, "messageId")), "ConnectorUnplugged") != NULL)
|
|
|
+ {
|
|
|
+ objData = json_tokener_parse(json_object_get_string(json_object_object_get(objPayload, "data")));
|
|
|
+ if(!is_error(objData))
|
|
|
+ {
|
|
|
+ transactionId_org = json_object_get_int(json_object_object_get(objData, "idTx"));
|
|
|
+
|
|
|
+
|
|
|
+ //Get IdTag from StartTransaction , store to StartTransactionIdTagTemp, For StopTransaction usage in Queue (StartTransaction. StopTransaction user id different), get actual TransactionId
|
|
|
+ GetStartTransactionIdTag(connectorId-1);
|
|
|
+ transactionId_map = GetTransactionId(connectorId, (unsigned char*)"", NO);
|
|
|
+
|
|
|
+ DEBUG_INFO("queue map transactionId = %d\n", transactionId_map);
|
|
|
+ DEBUG_INFO("original connectorId = %d\n", connectorId);
|
|
|
+ DEBUG_INFO("original transactionId = %d\n", transactionId_org);
|
|
|
+ DEBUG_INFO("original IdtagStr = %s\n", "");
|
|
|
+ if((transactionId_map != 0)&&(transactionId_org != transactionId_map))
|
|
|
+ {
|
|
|
+ //replace transactionId
|
|
|
+ json_object_object_add(objData, "idTx", json_object_new_int(transactionId_map));
|
|
|
+ }
|
|
|
+ else if((transactionId_map == 0) && (transactionId_org == 0))
|
|
|
+ {
|
|
|
+ //replace transactionId
|
|
|
+ json_object_object_add(objData, "idTx", json_object_new_int(GetStartTransactionId(connectorId-1)));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ transactionId_map = transactionId_org;
|
|
|
+ }
|
|
|
+ DEBUG_INFO("Final transactionId = %d\n", transactionId_map);
|
|
|
+
|
|
|
+ json_object_object_add(objPayload, "data", json_object_new_string(json_object_to_json_string_ext(objData, JSON_C_TO_STRING_PLAIN)));
|
|
|
+ json_object_array_put_idx(obj, 3, objPayload);
|
|
|
+ LWS_Send((char*)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- //reservationId
|
|
|
- loc = strstr(str, "reservationId");
|
|
|
- c = 0;
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- if(loc != NULL)
|
|
|
- {
|
|
|
- while (loc[strlen("reservationId")+2+c] != ',')
|
|
|
- {
|
|
|
- sstr[c] = loc[strlen("reservationId")+2+c];
|
|
|
- c++;
|
|
|
+ result = PASS;
|
|
|
}
|
|
|
- sstr[c] = '\0';
|
|
|
- tempreservationId = atoi(sstr);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //timestamp
|
|
|
- loc = strstr(str, "timestamp");
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- c=0;
|
|
|
- while (loc[3+strlen("timestamp")+c] != '\"')
|
|
|
- {
|
|
|
- sstr[c] = loc[3+strlen("timestamp")+c];
|
|
|
- c++;
|
|
|
- }
|
|
|
- sstr[c] = '\0';
|
|
|
- strcpy((char*)timestampStr, sstr);
|
|
|
-
|
|
|
-
|
|
|
- if(hashmap_operation(HASH_OP_GET, guid, key_value) == TRUE)
|
|
|
- {
|
|
|
- //DEBUG_INFO("\n 1. sent queue guid=%s\n",guid);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- hashmap_operation(HASH_OP_ADD, guid, tempdata);
|
|
|
- FillStartTransaction(tempconnectorId, IdtagStr, tempmeterStart, tempreservationId, timestampStr);
|
|
|
- //DEBUG_INFO("\n 2. sent queue guid=%s\n",guid);
|
|
|
}
|
|
|
+ json_object_put(obj);
|
|
|
}
|
|
|
- //*********************End: StartTransaction***************************/
|
|
|
-
|
|
|
-
|
|
|
- //****************transactionId********************/
|
|
|
- c=0;
|
|
|
- loc = strstr(str, "transactionId");
|
|
|
- memset(sstr ,0, ARRAY_SIZE(sstr) );
|
|
|
- if(loc != NULL)
|
|
|
- {
|
|
|
- // Only MeterValue with transactionId & StopTransaction will arrive here
|
|
|
- while ((loc[strlen("transactionId")+2+c] != '}') && (loc[strlen("transactionId")+2+c] != ','))
|
|
|
- {
|
|
|
- sstr[c] = loc[strlen("transactionId")+2+c];
|
|
|
- c++;
|
|
|
- }
|
|
|
-
|
|
|
- sstr[c] = '\0';
|
|
|
- temptransactionId = atoi(sstr);
|
|
|
-
|
|
|
-
|
|
|
- //Get IdTag from StartTransaction , store to StartTransactionIdTagTemp, For StopTransaction usage in Queue (StartTransaction. StopTransaction user id different), get actual TransactionId
|
|
|
- GetStartTransactionIdTag(tempconnectorId-1);
|
|
|
- gettransactionId = GetTransactionId(tempconnectorId, IdtagStr, IsStopTransaction);
|
|
|
-
|
|
|
- DEBUG_INFO("queue map transactionId = %d\n", gettransactionId);
|
|
|
- DEBUG_INFO("original connectorId = %d\n", tempconnectorId);
|
|
|
- DEBUG_INFO("original transactionId = %d\n", temptransactionId);
|
|
|
- DEBUG_INFO("original IdtagStr = %s\n", IdtagStr);
|
|
|
- if((gettransactionId != 0)&&(temptransactionId != gettransactionId))
|
|
|
- {
|
|
|
- //replace transactionId of metervalue or stopTransaction
|
|
|
- strncpy(strcomposite,str, (loc-str)+2+strlen("transactionId"));
|
|
|
- sprintf(strcomposite+((loc-str)+2+strlen("transactionId")),"%d",gettransactionId);
|
|
|
- strcat(strcomposite, loc+strlen("transactionId")+2+c); // 把 字串中transactionId後面的字串串接到 strcomposite後面
|
|
|
- LWS_Send(strcomposite+2); // skip 2 bytes String -> Connector ID,
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- LWS_Send(str+2); // skip 2 bytes String -> Connector ID
|
|
|
- gettransactionId = temptransactionId;
|
|
|
- }
|
|
|
-
|
|
|
- DEBUG_INFO("Final transactionId = %d\n", gettransactionId);
|
|
|
- if(IsStopTransaction == TRUE)//if((IsStopTransaction == TRUE)&&(gettransactionId != 0))
|
|
|
- {
|
|
|
- queueOpInfo.stopTransactionId = gettransactionId;
|
|
|
- //SetTransactionIdZero(gettransactionId);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- // MeterValue without transactionId & StartTransaction arrive here
|
|
|
- LWS_Send(str+2);
|
|
|
- }
|
|
|
-
|
|
|
- result = TRUE;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- result = FALSE;
|
|
|
}
|
|
|
fclose(fp);
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
|