-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathj1708_decode.py
165 lines (138 loc) · 4.81 KB
/
j1708_decode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import serial, time, re, csv
from j1708 import Message, checksum
def to_dec(string):
array = []
n = 2
array = [string[i:i+n] for i in range(0,len(string),n)]
array = [int(x, 16) for x in array]
return array
'''
def checksum(array):
checksum = 0
for x in range(0,(len(array)-1)):
checksum = checksum+array[x]
checksum = checksum & 255
return checksum
'''
def slice_on_packages(array):
messages = []
pStart = 0
pEnd = 21
lastEnd = pEnd
while pStart < (len(array)-3):
while (pEnd-pStart)>3:
sub_array = array[pStart:pEnd]
if checksum(sub_array)==sub_array[-1]:
mess = Message(sub_array[0],
sub_array[0:len(sub_array)],
sub_array[-1],
pStart,
len(sub_array))
#if len(sub_array)!=0:
messages.append(mess)
#print(sub_array)
lastEnd = pEnd
pEnd = pEnd - 1
pStart = pStart + 1
pEnd = pStart + 21
return messages
def slice_message(messages, start, end):
return messages[start:end]
def check_message_exist(message):
maxPossibleLen = 21
while maxPossibleLen > 0:
if checksum(message) == message[-1]
maxPossibleLen -= 1
return True
return False
def slice_on_packages2(array):
messages = []
pStart = 0
pEnd = 21
while pStart < (len(array)-3):
mess = slice_message(array,pStart, pEnd)
if check_message_exist(mess):
def compaction(messages):
#Searching for message wich repeats more then 5 times.
#If near this message exist other message
#and founded messsage contains the first element of that message
#then delete this message
for item in messages:
if messages.count(item) == 1:
ind = messages.index(item)
del messages[ind]
return messages
def find_lost_packages(messages, input_dump):
lost_packages = []
for item in range(0,len(messages)-1):
startP = messages[item].pos + messages[item].length
endP = messages[item+1].pos -1
if startP == endP or endP < startP:
continue
lost = input_dump[startP:endP]
#print startP,endP
lost = Message(
lost[0],
lost,
"WRONG!",
startP,
len(lost)
)
lost_packages.append(lost)
#lost_packages = [cleanlist.append(x) for x in lost_packages if x not in cleanlist]
return lost_packages
def write_in_log(log_file, data):
writedContent = 0
for item in range(0,len(data)):
writedContent = writedContent+len(data[item].content)
log_file.write("Pos: "+str(data[item].pos)+
" \tPosEnd: "+str(data[item].pos+data[item].length)+
" \tLength: "+str(data[item].length)+
" \tMID: "+str(data[item].mid)+" "+
" \tCHECKSUM: " +str(data[item].checksum)+
" \tContent: " +str(data[item].content)+
"\n")
return writedContent
def save_messages_to_csv(messages):
messages_dump = open("messages_dump.txt","wb")
writer = csv.writer(messages_dump,
delimiter=';',
quotechar='|',
quoting=csv.QUOTE_MINIMAL)
for item in messages:
writer.writerow(
[str(item.mid)]+
[str(item.pos)]+
[str(item.length)]+
[str(item.checksum)]+
[str(item.content)]
)
messages_dump.close()
input_dump = open("dump_10_08.txt","r")
dump_array = input_dump.readlines()
dump_array = str(dump_array)
dump_array = re.sub('[^A-Za-z0-9]+', '', dump_array)
dump_array = to_dec(dump_array)
inputDumpLen =len(dump_array)
print("Length of input dump: "+str(inputDumpLen))
input_dump.close()
messages = slice_on_packages(dump_array)
output = open("decode.txt","w")
contentLen = 0
messages = compaction(messages)
messages.sort(key=lambda x:x.pos)
#messages = [i for i in messages if i.content[0]!=0]
#messages = [i for i in messages if i.content[0]!=255]
#write messages to file
save_messages_to_csv(messages)
#np.savetxt('messages_dump.csv',messages,delimeter=';',)
write_in_log(output, messages)
output.write("\n\n\n\t\t\t\t\t\t\t================= LOST PACKAGES =================\n\n\n")
lost_pkg = find_lost_packages(messages, dump_array)
write_in_log(output, lost_pkg)
output.close()
#messages = compaction(messages)
#lost = find_lost_packages(messages, dump_array)
#print(len(lost))
print("Length of output dump: "+str(contentLen))
print("Missed bytes total: "+ str(inputDumpLen - contentLen))