feature: pcapconverter measures the time for cablecheck and precharge

This commit is contained in:
uhi22 2023-05-13 00:01:23 +02:00
parent 01c7c069fd
commit d017ca65e5

View file

@ -32,13 +32,35 @@ import json
# The path where the script will search for pcap files:
directory = '../temp'
# stop the evaluation after this number of packets. Set to zero to have no limit.
nLimitNumberOfPackets = 1000
def getManufacturerFromMAC(strMAC):
# Examples based on https://macvendors.com/, and own experience
if (strMAC[0:5]=="ec:a2"):
return "Kempower"
if (strMAC[0:8]=="dc:44:27"):
return "Tesla"
if (strMAC[0:8]=="ce:25:1a"):
return "Alpitronic"
if (strMAC[0:8]=="1a:a9:8e"):
return "Alpitronic"
if (strMAC[0:8]=="e8:eb:1b"):
return "Microchip (maybe ABB)"
if (strMAC[0:5]=="18:d7"):
return "(maybe Siemens)"
return "(unknown vendor)"
# Examples of the decoder result:
#"EVSEPresentVoltage.Multiplier": "0",
#"EVSEPresentVoltage.Value": "318",
#"EVSEPresentVoltage.Unit": "V",
#"DC_EVStatus.EVRESSSOC": "53",
def convertPcapToTxt(inputFileName):
global nLimitNumberOfPackets
global directory
cap = pyshark.FileCapture(inputFileName, display_filter="ipv6")
fileOut = open(inputFileName + '.decoded.txt', 'w')
print("# generated by pcapConverter.py", file=fileOut)
@ -46,13 +68,20 @@ def convertPcapToTxt(inputFileName):
fileOutValues = open(inputFileName + '.values.txt', 'w')
print("# generated by pcapConverter.py", file=fileOutValues)
print("# https://github.com/uhi22/pyPLC", file=fileOutValues)
fileOutStatistics = open(directory + '/pcap_statistics.txt', 'a')
print("# statistics for " + inputFileName, file=fileOutStatistics)
# Example how to access the data:
#print(cap)
#print(cap[0])
#print(cap[1])
#print(dir(cap[1]))
#print(dir(cap[1].eth))
#print(cap[1].eth.src) # the source MAC address
#print(cap[1].sniff_time) # readable time
#print(cap[1].sniff_timestamp) # epoch time
t1CableCheckBegin = 0
t2PreChargeBegin = 0
t3CurrentDemandBegin = 0
numberOfPackets=0
for packet in cap:
numberOfPackets+=1
@ -72,6 +101,26 @@ def convertPcapToTxt(inputFileName):
#print(decoded)
print(sHeader, file=fileOut)
print(decoded, file=fileOut)
if (decoded.find("SessionSetupReq")>0):
if ((t1CableCheckBegin>0) and (t2PreChargeBegin>t1CableCheckBegin) and (t3CurrentDemandBegin>t2PreChargeBegin)):
print("charger MAC " + chargerMAC + " " + getManufacturerFromMAC(chargerMAC))
timeForCableCheck = t2PreChargeBegin - t1CableCheckBegin
timeForPreCharge = t3CurrentDemandBegin - t2PreChargeBegin
print("timeForCableCheck= " + ("%.3f" % timeForCableCheck))
print("timeForPreCharge= " + ("%.3f" % timeForPreCharge))
print(chargerMAC + ";" + getManufacturerFromMAC(chargerMAC) + ";" + \
"timeForCableCheck;" + ("%.3f" % timeForCableCheck) + ";" + \
"timeForPreCharge; " + ("%.3f" % timeForPreCharge), file=fileOutStatistics)
t1CableCheckBegin = 0
t2PreChargeBegin = 0
t3CurrentDemandBegin = 0
if (decoded.find("CableCheckReq")>0) and (t1CableCheckBegin==0):
t1CableCheckBegin = float(packet.sniff_timestamp)
chargerMAC = str(packet.eth.dst)
if (decoded.find("PreChargeReq")>0) and (t2PreChargeBegin==0):
t2PreChargeBegin = float(packet.sniff_timestamp)
if (decoded.find("CurrentDemandReq")>0) and (t3CurrentDemandBegin==0):
t3CurrentDemandBegin = float(packet.sniff_timestamp)
try:
y = json.loads(decoded)
try:
@ -90,8 +139,24 @@ def convertPcapToTxt(inputFileName):
pass
if ((numberOfPackets % 100)==0):
print(str(numberOfPackets) + " packets")
#if (numberOfPackets>=1000):
# break
if ((nLimitNumberOfPackets>0) and (numberOfPackets>=nLimitNumberOfPackets)):
break
# Statistics of the timing:
#print("t1CableCheckBegin " + str(t1CableCheckBegin))
#print("t2PreChargeBegin " + str(t2PreChargeBegin))
#print("t3CurrentDemandBegin " + str(t3CurrentDemandBegin))
if ((t1CableCheckBegin>0) and (t2PreChargeBegin>t1CableCheckBegin) and (t3CurrentDemandBegin>t2PreChargeBegin)):
print("charger MAC " + chargerMAC + " " + getManufacturerFromMAC(chargerMAC))
timeForCableCheck = t2PreChargeBegin - t1CableCheckBegin
timeForPreCharge = t3CurrentDemandBegin - t2PreChargeBegin
print("timeForCableCheck= " + ("%.3f" % timeForCableCheck))
print("timeForPreCharge= " + ("%.3f" % timeForPreCharge))
print(chargerMAC + ";" + getManufacturerFromMAC(chargerMAC) + ";" + \
"timeForCableCheck;" + ("%.3f" % timeForCableCheck) + ";" + \
"timeForPreCharge; " + ("%.3f" % timeForPreCharge), file=fileOutStatistics)
fileOutStatistics.close()
fileOut.close()
fileOutValues.close()