浏览代码

[Add][Modularization][Module_OcppBackend]

2021.08.20 / Folus Wen

Actions:
1. TTIA info communication implement.
2. define.h add TTIA configuration struct.

Files:
1. As follow commit history

Image version: D0.00.XX.XXXX.XX
Image checksum: XXXXXXXX

Hardware PWB P/N : XXXXXXX
Hardware Version : XXXXXXX
FolusWen 3 年之前
父节点
当前提交
4495cf622c

+ 267 - 0
EVSE/Modularization/ocppfiles/MessageHandler.c

@@ -405,6 +405,272 @@ static char * UnitOfMeasureStr[] = {
 	MACROSTR(K),
 	MACROSTR(Percent)
 };
+//=========================================
+// TTIA routine
+//=========================================
+int sendTTIAInfo(uint8_t gun_index)
+{
+	static int sn = 0;
+
+	int result = FAIL;
+	struct hostent *server;
+	int udp_socket_fd;
+	struct sockaddr_in dest_addr = {0};
+
+	uint8_t tx_buf[80] = {0};
+	uint8_t logBuf[256]= {0};
+	uint8_t tempIndex, socStart, socEnd;
+	uint16_t PresentChargedEnergy;
+	struct tm tmStart, tmEnd;
+
+	DEBUG_INFO("sendTTIAInfo...\n");
+
+	if(gunType[gun_index] == GUN_TYPE_CHAdeMO)
+	{
+		if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
+		{
+			tempIndex = ((gun_index==2) ? 1: 0);
+		}
+		else
+		{
+			tempIndex = gun_index;
+		}
+
+		for (int index = 0; index < CHAdeMO_QUANTITY; index++)
+		{
+			if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == tempIndex)
+			{
+				socStart = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].EvBatteryStartSoc;
+				socEnd = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].EvBatterySoc;
+				PresentChargedEnergy = (uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].PresentChargedEnergy*10);
+			}
+		}
+	}
+	else if(gunType[gun_index] == GUN_TYPE_CCS)
+	{
+
+		if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
+		{
+			tempIndex = ((gun_index==2) ? 1: 0);
+		}
+		else
+		{
+			tempIndex = gun_index;
+		}
+
+		for (int index = 0; index < CCS_QUANTITY; index++)
+		{
+			if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == tempIndex)
+			{
+				socStart = ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].EvBatteryStartSoc;
+				socEnd = ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].EvBatterySoc;
+				PresentChargedEnergy = (uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].PresentChargedEnergy*10);
+			}
+		}
+	}
+	else if(gunType[gun_index] == GUN_TYPE_GBT)
+	{
+
+		if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
+		{
+			tempIndex = ((gun_index==2) ? 1: 0);
+		}
+		else
+		{
+			tempIndex = gun_index;
+		}
+
+
+		for (int index = 0; index < GB_QUANTITY; index++)
+		{
+			if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == tempIndex)
+			{
+				socStart = ShmSysConfigAndInfo->SysInfo.GbChargingData[index].EvBatteryStartSoc;
+				socEnd = ShmSysConfigAndInfo->SysInfo.GbChargingData[index].EvBatterySoc;
+				PresentChargedEnergy = (uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[index].PresentChargedEnergy*10);
+			}
+		}
+
+	}
+	else if(gunType[gun_index] == GUN_TYPE_DO)
+	{
+		tempIndex = gun_index;
+
+		for (int index = 0; index < GENERAL_GUN_QUANTITY; index++)
+		{
+			if (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.Index == tempIndex)
+			{
+				socStart = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.EvBatteryStartSoc;
+				socEnd = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.EvBatterySoc;
+				PresentChargedEnergy = (uint16_t)(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.PresentChargedEnergy*10);
+			}
+		}
+	}
+	else
+	{
+		if(ShmSysConfigAndInfo->SysConfig.ModelName[0]=='D')
+		{
+			tempIndex = 2;
+		}
+		else
+		{
+			tempIndex = gun_index;
+		}
+
+		for (int index = 0; index < AC_QUANTITY; index++)
+		{
+			if (ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index == tempIndex)
+			{
+				socStart = ShmSysConfigAndInfo->SysInfo.AcChargingData[index].EvBatteryStartSoc;
+				socEnd = ShmSysConfigAndInfo->SysInfo.AcChargingData[index].EvBatterySoc;
+				PresentChargedEnergy = (uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[index].PresentChargedEnergy*10);
+			}
+		}// END
+	}
+
+	// Message assembly
+	sprintf((char*)&tx_buf[0], "APTS");													// Protocol ID
+	tx_buf[4] = 0x02;																	// Protocol Version
+	tx_buf[5] = 0xb0;																	// Message ID
+	tx_buf[6] = ShmSysConfigAndInfo->SysConfig.TTIA_Info.busVenderId & 0xff;			// CustomerID low byte
+	tx_buf[7] = (ShmSysConfigAndInfo->SysConfig.TTIA_Info.busVenderId>>8) & 0xff;		// CustomerID high byte
+	tx_buf[8] = 0x00;																	// CarID low byte
+	tx_buf[9] = 0x00;																	// CarID high byte
+	tx_buf[10] = 0x00;																	// ID Storage
+	tx_buf[11] = 0x00;																	// Driver lowest byte
+	tx_buf[12] = 0x00;																	// Driver 2nd byte
+	tx_buf[13] = 0x00;																	// Driver 3rd byte
+	tx_buf[14] = 0x00;																	// Driver highest byte
+	tx_buf[15] = sn & 0xff;																// Sequence SN low byte
+	tx_buf[16] = (sn>>8) & 0xff;														// Sequence SN high byte
+	tx_buf[17] = 0x00;																	// Reserve
+	tx_buf[18] = 60 & 0xff;																// Payload length low byte
+	tx_buf[19] = (60>>8) & 0xff;														// Payload length high byte
+
+	// EVSE provider
+	memcpy((char*)&tx_buf[20], ShmSysConfigAndInfo->SysConfig.TTIA_Info.EquipmentProvider, 15);
+
+	// EVSE provider
+	tx_buf[35] = ShmSysConfigAndInfo->SysConfig.TTIA_Info.TransportationCompanyNo;
+
+	// EVSE id
+	tx_buf[36] = ShmSysConfigAndInfo->SysConfig.TTIA_Info.ChargeBoxId;
+
+	// Connector id
+	tx_buf[37] = gun_index;
+
+	// EVSE station
+	memcpy((char*)&tx_buf[38], ShmSysConfigAndInfo->SysConfig.TTIA_Info.evseStation, 15);
+
+	// Vehicle SN
+	memcpy(&tx_buf[53], &ShmOCPP16Data->StartTransaction[gun_index].IdTag, 8);
+
+	// Mileage
+	tx_buf[61] = 0x00;																	// lowest byte
+	tx_buf[62] = 0x00;																	// 2nd byte
+	tx_buf[63] = 0x00;																	// highest byte
+
+	// Start date time 6 bytes
+	if((sscanf((char*)ShmOCPP16Data->StartTransaction[gun_index].Timestamp, "%4d-%2d-%2dT%2d:%2d:%2d", &tmStart.tm_year, &tmStart.tm_mon, &tmStart.tm_mday, &tmStart.tm_hour, &tmStart.tm_min, &tmStart.tm_sec) == 6))
+	{
+		tx_buf[64] = (tmStart.tm_year-2000);
+		tx_buf[65] = tmStart.tm_mon;
+		tx_buf[66] = tmStart.tm_mday;
+		tx_buf[67] = tmStart.tm_hour;
+		tx_buf[68] = tmStart.tm_min;
+		tx_buf[69] = tmStart.tm_sec;
+	}
+
+	// Stop date time 6 bytes
+	if((sscanf((char*)ShmOCPP16Data->StopTransaction[gun_index].Timestamp, "%4d-%2d-%2dT%2d:%2d:%2d", &tmEnd.tm_year, &tmEnd.tm_mon, &tmEnd.tm_mday, &tmEnd.tm_hour, &tmEnd.tm_min, &tmEnd.tm_sec) == 6))
+	{
+		tx_buf[70] = (tmEnd.tm_year-2000);
+		tx_buf[71] = tmEnd.tm_mon;
+		tx_buf[72] = tmEnd.tm_mday;
+		tx_buf[73] = tmEnd.tm_hour;
+		tx_buf[74] = tmEnd.tm_min;
+		tx_buf[75] = tmEnd.tm_sec;
+	}
+
+	// Session energy 2 bytes
+	tx_buf[76] = PresentChargedEnergy & 0xff;
+	tx_buf[77] = (PresentChargedEnergy>>8) & 0xff;
+
+	// Start SOC 1 byte
+	tx_buf[78] = socStart;
+
+	// Stop SOC 1 byte
+	tx_buf[79] = socEnd;
+
+
+
+	// Open socket
+	udp_socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
+	if(udp_socket_fd == -1)
+	{
+		DEBUG_ERROR("UDP socket create failed!\n");
+	}
+	else
+	{
+		// Get server DNS entry
+		server = gethostbyname((char*)ShmSysConfigAndInfo->SysConfig.TTIA_Info.server_addr);
+		if (server == NULL)
+		{
+			DEBUG_ERROR("ERROR, no such host as %s\n", ShmSysConfigAndInfo->SysConfig.TTIA_Info.server_addr);
+		}
+		else
+		{
+			// Build the server's Internet address
+			bzero((char *) &dest_addr, sizeof(dest_addr));
+			dest_addr.sin_family = AF_INET;
+			bcopy((char *)server->h_addr, (char *)&dest_addr.sin_addr.s_addr, server->h_length);
+			dest_addr.sin_port = htons(ShmSysConfigAndInfo->SysConfig.TTIA_Info.server_port);
+
+			if(sendto(udp_socket_fd, tx_buf, strlen((char*)tx_buf), 0, &dest_addr, sizeof(dest_addr)) >= 0)
+			{
+				for(uint8_t idx=0;idx<ARRAY_SIZE(tx_buf);idx++)
+				{
+					sprintf((char*)logBuf, "%s %02X", logBuf, tx_buf[idx]);
+				}
+				DEBUG_TTIA_INFO("Message raw: %s\n", logBuf);
+				DEBUG_TTIA_INFO("OCPP Transaction ID: %d\n", ShmOCPP16Data->StopTransaction[gun_index].TransactionId);
+				DEBUG_TTIA_INFO("----- Header -----\n");
+				DEBUG_TTIA_INFO("ProtocolID: APTS\n");
+				DEBUG_TTIA_INFO("ProtocolVer: 0x02\n");
+				DEBUG_TTIA_INFO("MessageID: 0xB0\n");
+				DEBUG_TTIA_INFO("CustomerID: 0x%04X\n", ShmSysConfigAndInfo->SysConfig.TTIA_Info.busVenderId);
+				DEBUG_TTIA_INFO("CarID: 0x0000\n");
+				DEBUG_TTIA_INFO("IDStorage: 0x00\n");
+				DEBUG_TTIA_INFO("DriverID: 0x00000000\n");
+				DEBUG_TTIA_INFO("Sequence: %d\n", sn);
+				DEBUG_TTIA_INFO("Reserved: 0x00\n");
+				DEBUG_TTIA_INFO("Len: 80\n");
+				DEBUG_TTIA_INFO("----- Payload -----\n");
+				DEBUG_TTIA_INFO("EquipmentProvider: %s\n", ShmSysConfigAndInfo->SysConfig.TTIA_Info.EquipmentProvider);
+				DEBUG_TTIA_INFO("TransportationCompanyNo: %d\n", ShmSysConfigAndInfo->SysConfig.TTIA_Info.TransportationCompanyNo);
+				DEBUG_TTIA_INFO("ChargeBoxId: %d\n", ShmSysConfigAndInfo->SysConfig.TTIA_Info.ChargeBoxId);
+				DEBUG_TTIA_INFO("Gun index: %d\n", gun_index);
+				DEBUG_TTIA_INFO("evseStation: %s\n", ShmSysConfigAndInfo->SysConfig.TTIA_Info.evseStation);
+				DEBUG_TTIA_INFO("EV vehicle: %s\n", ShmOCPP16Data->StartTransaction[gun_index].IdTag);
+				DEBUG_TTIA_INFO("EV mileage: 000000\n");
+				DEBUG_TTIA_INFO("Session start data time: %s\n", ShmOCPP16Data->StartTransaction[gun_index].Timestamp);
+				DEBUG_TTIA_INFO("Session stop data time: %s\n", ShmOCPP16Data->StopTransaction[gun_index].Timestamp);
+				DEBUG_TTIA_INFO("Session charged energy (kwh): %f\n", (PresentChargedEnergy/10.0));
+				DEBUG_TTIA_INFO("Session start SOC: %d\n", socStart);
+				DEBUG_TTIA_INFO("Session end SOC: %d\n", socEnd);
+				DEBUG_TTIA_INFO("==================================\n");
+				sn++;
+								result = PASS;
+			}
+			else
+			{
+				DEBUG_ERROR("UDP sent out fail.\n");
+			}
+		}
+	}
+
+	return result;
+}
 
 //=========================================
 // Save configuration
@@ -3482,6 +3748,7 @@ void CheckSystemValue(void)
 		{
 			ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = 0;
 			sendStopTransactionRequest(gun_index);
+			if(ShmSysConfigAndInfo->SysConfig.TTIA_Info.isEnableTTIA && (gunType[gun_index] != GUN_TYPE_AC) )sendTTIAInfo(gun_index);
 			clientTime.StopTransaction = time((time_t*)NULL);
 		}
 

+ 31 - 0
EVSE/Modularization/ocppfiles/SystemLogMessage.c

@@ -62,3 +62,34 @@ int StoreOcppMsg(const char *fmt, ...)
 #endif
 	return rc;
 }
+
+int StoreTTIAMsg(const char *fmt, ...)
+{
+	char Buf[65536+256];
+	char buffer[65536];
+	//char Buf[4096+256];
+	//char buffer[4096];
+	time_t CurrentTime;
+	struct tm *tm;
+	struct timeval tv;
+	va_list args;
+
+	va_start(args, fmt);
+	int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
+	va_end(args);
+	memset(Buf,0,sizeof(Buf));
+	CurrentTime = time(NULL);
+	tm=localtime(&CurrentTime);
+	gettimeofday(&tv, NULL); // get microseconds, 10^-6
+
+	sprintf(Buf,"echo -n \'[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\' >>  /Storage/OCPP/[%04d.%02d]TTIAMessageLog",
+			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
+			buffer,
+			tm->tm_year+1900,tm->tm_mon+1);
+	system((const char*)Buf);
+
+#ifdef ConsloePrintLog
+	printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
+#endif
+	return rc;
+}

+ 2 - 0
EVSE/Modularization/ocppfiles/SystemLogMessage.h

@@ -6,8 +6,10 @@
 #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
 #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
 #define DEBUG_OCPPMESSAGE_INFO(format, args...) StoreOcppMsg("[%s:%d][%s][Info] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
+#define DEBUG_TTIA_INFO(format, args...) StoreTTIAMsg("[%s:%d][%s][Info] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
 
 int StoreLogMsg(const char *fmt, ...);
 int StoreOcppMsg(const char *fmt, ...);
+int StoreTTIAMsg(const char *fmt, ...);
 
 #endif

+ 13 - 0
EVSE/Projects/define.h

@@ -401,6 +401,18 @@ struct Schedule
 	unsigned char   isTriggerStop;    					// 0: disable; 1: enable
 };
 
+struct TTIA
+{
+	unsigned char	server_addr[512];					// Target server address
+	unsigned int	server_port;						// Target server port
+	unsigned int	busVenderId;						// Bus vender id, 0~65535
+	unsigned char	EquipmentProvider[16];				// EVSE vender max 15 bytes
+	unsigned char	TransportationCompanyNo;			// Data provider company number
+	unsigned char 	ChargeBoxId;						// 1 byte
+	unsigned char	evseStation[16];					// UTF-8 15 bytes, Chinese 5 words
+	unsigned char	isEnableTTIA:1;						// TTIA function enable
+};
+
 struct LocalSharingInfo
 {
     unsigned short      AvailableShargingCurrent;       // 0 ~ rating value amp,  Synchronize from local sharing server
@@ -512,6 +524,7 @@ struct SysConfigData
 	unsigned char           isEnableLocalPowerSharging; //0: Disable power sharing  1: Enable power sharing
 	unsigned char           StopChargingByButton;       //0: Disable  1: Enable
 	struct LCD_OVERRIDE		LcdOveride;					// LCD override info
+	struct TTIA				TTIA_Info;					// TTIA configuration struct
 
     /************PowerCabinet************/
     WiringInfoData          WiringInfo;