-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.py
463 lines (394 loc) · 19.2 KB
/
Log.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import time
import sys
import glob,json
import os
from datetime import datetime
import threading
from Serial import Serialport
from Tkinter import *
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Signature import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from hashlib import md5
from uuid import getnode as get_mac
class Logger():
def __init__(self, Platform):
"""This is for encry the data and decry
"""
self.Platform = Platform
self.UploadKey = 9600
self.TempKey="5475731930WireLA5475731930WireLA"
#self.AutoSendState=0
self.AutoCommandList={}
self.ModemLogFile=None
self.GPSLogState = False
self.ModemLogState=False
self.GPSLogFile=None
self.MAC=get_mac()
self.Version=1.0
self.RSAPrivateKey=None
self.username=None
#address the LogSetup.conf here
if os.path.exists('.//Setting//LogSetup.conf'):
print 'LogSetup.conf exists'
fd = open('.//Setting//LogSetup.conf', 'r')
variables = fd.readline().strip('\n')
for line in fd.readlines():
variables += "&"+line.strip('\n')
# print 'variables:'+variables
self.SplitTokenAndSave(variables)
else:
self.username='this is pc'
if os.path.exists("priv.pem"):
print "get RSAsignature key from file"
fd = open('priv.pem', 'r')
self.RSAPrivateKey = fd.read()
fd.close()
else:
print "get RSAsignature default key"
self.RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\n\
MIIJJwIBAAKCAgEAqHdahhigAFn9eNEPIeIp3aXQWMcGM6GttGxhlDxKMGnxC13A\n\
a4JPO3uI2mDDuRMAx1CyXm9VKEoDN7lf3rXWoRz+Ez7DBw0luzx2WOwKCsxP+x6x\n\
MxmmHB611L/prr3L2FW5ta4oBOJJQz6LxGsQmiobSmj0UYNFOIOuVCkIQI6TClg1\n\
PXqk5H7J6A8twQg/GNzgrUlv553Bl1fKVNTMq6cd4IYU9gOIkgwgRs6SwZNyERwz\n\
He/ZBDmBfFgorMUbDdSdMF8In/F/ljGLqgDFl2VQXzKni7yCEpesFBeLx3XWcVmC\n\
vD+meV4cN/Ob2PUT3lYlEqEpY7M+UGBW55rXSA1i2QaA1JX+RwhXBxSJcGyyhbeO\n\
OUNqFNXhn9eHkCg2ovaR5bI4stEpeMvq02VOhf8zpYp1m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39jjOzZR8qHPF9iZJkkmpRUL3VQphDIqba8jh2vBqH2wTjSDIYvY\n\
thG3gB1ndeQ/Ju5ptGqr7cIA01DCEpSTxqIY5ARQ3bfCFYMD202HhndfEszGxJCR\n\
WXTxmoHOu/Sz22J+r5at9oawAdO5i0/BPGiMlr90bS0Cp2X02NlbkGUqqph/hcHG\n\
PxEU+IYv9BUKa8VyN3v7z0Rr6v6KaexzzZJ/BPcP/Iu/YyHOeF80ln+CxYcseJCX\n\
P9NQZg2Tnb0rJVmnDArgD53FGc86dpg37GYV6Y16xynWFpGCHF3sF42IuANrlSAL\n\
NtHZ1xoJAJA0J5GI5YNiaCCIkGWH/+sCggEAUAM/qiLAwrXdyW/pNKLm9wj8E6KL\n\
+Jqr1UQV+ficFgv0lr5XUTNlFV5c/K5W9crKaHl7wclu0j1ZwI6enbDUiH5Qu/Pt\n\
cmVc+mXATBqcRcboSbLF945cSKl+i3sF+64gXsiQibnSBSR9FcRHylfUcblx63zR\n\
N5383r8R+1B9V7PZf74jLlHiTk4BxKjuMFmXvt2vqine8oRoUjwGZyLiEvOpYMm/\n\
9TYFQtPA4pqv56ghy77w4hYHj6gqxxLuYWu1O6nu9qbWhM0qwF55BcpewCJEyD7e\n\
Ruje7gPSGvWNgjjVvmiZbDRQbsyVDF65TmRik5jlzYiHBvdaR12rZvYW1g==\n\
-----END RSA PRIVATE KEY-----\n"
print "self.RSAPrivateKey"+self.RSAPrivateKey
def derive_key_and_iv(self,password, salt, key_length, iv_length):
d = d_i = ''
while len(d) < key_length + iv_length:
d_i = md5(d_i + self.TempKey + salt).digest()
d += d_i
return d[:key_length], d[key_length:key_length+iv_length]
def Tencrypt(self,in_file, out_file, key_length=32):
bs = AES.block_size
salt = Random.new().read(bs - len('Salted__'))
key, iv = self.derive_key_and_iv(self.TempKey, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
out_file.write('Salted__' + salt)
finished = False
while not finished:
chunk = in_file.read(1024 * bs)
if len(chunk) == 0 or len(chunk) % bs != 0:
padding_length = (bs - len(chunk) % bs) or bs
chunk += padding_length * chr(padding_length)
finished = True
out_file.write(cipher.encrypt(chunk))
def Tdecrypt(self,in_file, out_file, key_length=32):
bs = AES.block_size
salt = in_file.read(bs)[len('Salted__'):]
key, iv = self.derive_key_and_iv(self.TempKey, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
next_chunk = ''
finished = False
while not finished:
chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))
if len(next_chunk) == 0:
padding_length = ord(chunk[-1])
chunk = chunk[:-padding_length]
finished = True
out_file.write(chunk)
def RSAsignature(self,in_file, out_file):
"""create RSA signature
"""
# Load private key
priv_key = RSA.importKey(self.RSAPrivateKey)
# Create PKCS1 handler
priv_cipher = PKCS1_v1_5.new(priv_key)
# Read test file
message = in_file.read()
# Create SHA1 hash object
h = SHA.new(message)
print 'hash',h
# write signature into file
out_file.write(priv_cipher.sign(h))
def SetGPSLogState(self,state):
self.GPSLogState=state
print "Log.GPSLogState:",self.GPSLogState
def SetModemLogState(self,state):
self.ModemLogState=state
print "Log.ModemLogState:",self.ModemLogState
def SetAutoSendState(self,state):
"""function.py will set this variable
"""
self.AutoSendState=state
def SetAutoCommandList(self,list):
self.AutoCommandList.clear()
self.AutoCommandList=list.copy()
def SetScrollText(self,frame):
"""get the GUI fram
"""
self.txt=frame
def SetModemLogFile(self,file,mode):
"""open or close the file
mode1: open
mode2: close
"""
if mode==1:
myUTCtime = datetime.strftime(datetime.utcnow(),'%Y-%m-%d %H-%M-%S')
self.ModemLogFile = open(".//Log//"+myUTCtime+"-"+file,"w")
else:
if self.ModemLogFile != None :
filename=self.ModemLogFile.name
self.ModemLogFile.close()
#self.Encrypt_file(filename,2)
# with open(filename, 'rb') as in_file, open(filename+'.enc', 'wb') as out_file:
# self.Tencrypt(in_file, out_file)
self.ModemLogFile = None
def SetGPSLogFile(self,mode):
"""open or close the log
mode1: open
mode2: close
"""
if mode==1:
myUTCtime = datetime.strftime(datetime.utcnow(),'%Y-%m-%d %H-%M-%S')
self.GPSLogFile = open(".//Log//"+myUTCtime+"-GPS","w")
else:
if self.GPSLogFile != None :
filename=self.GPSLogFile.name
self.GPSLogFile.close()
#self.Encrypt_file(filename,2)
# with open(filename, 'rb') as in_file, open(filename+'.enc', 'wb') as out_file:
# self.Tencrypt(in_file, out_file)
self.GPSLogFile = None
def StartAutoThread(self):
self.AutoThread=threading.Thread(target=self.AutoSendThread)
self.AutoThread.start()
def StartGPSThread(self):
self.GPSThread=threading.Thread(target=self.GPSLogThread)
self.GPSThread.start()
def Setcommandserial(self,serial):
"""get serial controll fomr function.py
"""
self.commandserial=serial
def SetGPSserial(self,serial):
self.GPSserial=serial
def SetAutoSendTimeInterval(self,interval):
self.AutoSendTimeInterval=interval
def ReadUserInPut(self,input):
"""get user input from function
"""
output = self.commandserial.SendSerialCommand(mess)
self.txt.insert(END,"UserInput:"+mess+"\nOutput:"+output+"\r\n")
self.txt.yview(END)
"""if self.ModemLogFile != None:
myUTCtime = datetime.strftime(datetime.utcnow(),'%Y-%m-%d %H-%M-%S')
self.ModemLogFile.write(myUTCtime+":"+output+"\r\n")"""
def AutoSendThread(self):
while self.AutoSendState:
for mess in self.AutoCommandList.keys():
output = self.commandserial.SendSerialCommand(mess+"\r")
self.txt.insert(END,"UserInput:"+mess+"\nOutput:"+output+"\r\n")
if self.Platform is "win":
self.txt.insert(END,"\r")
self.txt.insert(END,"\n")
self.txt.yview(END)
if self.ModemLogFile != None:
dt = datetime.now()
sec_since_epoch = time.mktime(dt.timetuple()) + dt.microsecond/1000000.0
myUTCtime = sec_since_epoch * 1000
#myUTCtime = datetime.strftime(datetime.utcnow(),'%Y-%m-%d %H-%M-%S')
self.ModemLogFile.write(str(int(myUTCtime))+":"+output)
time.sleep(0.002)
time.sleep(self.AutoSendTimeInterval)
pass
def GPSLogThread(self):
"""log the gps data from serial by threading
TODO make sure the serial can readline
"""
#print "GPSLogThread GPSLogState:",self.GPSLogState," ModemLogState:",self.ModemLogState
while self.GPSLogState and self.ModemLogState :
#print "in thread"
output = self.GPSserial.GetSerialReadline()
if len(output) >10:
#print output
dt = datetime.now()
sec_since_epoch = time.mktime(dt.timetuple()) + dt.microsecond/1000000.0
myUTCtime = sec_since_epoch * 1000
self.GPSLogFile.write(str(dt)+","+output)
#self.GPSLogFile.write(str(int(myUTCtime))+","+output)
def ToJson(self,signal,gps,modem):
"""parse the signal and gps into one json file
assume the gps log and signal are both as same timestamp formate
"""
GPSFile=None
GPSLines=None
self.modem=modem
if gps!=None:
#self.Decrypt_file(".//Upload//"+gps+'.enc')
#with open(".//Upload//"+gps+'.enc', 'rb') as in_file, open(".//Upload//"+gps, 'wb') as out_file:
# self.Tdecrypt(in_file, out_file)
GPSFile=open(".//Upload//"+gps,"r")
#GPSLines=GPSFile.readlines()
gpsstate=1
#SignalFile = open('.//Log//'+signal,'r')
#SignalLines=SignalFile.readlines()
WriteFile = open('.//Upload//'+signal,'w')
timestamp=None
jsonArray=[]
jsonElement={}
#self.Decrypt_file('.//Log//'+signal+'.enc')
#with open('.//Log//'+signal+'.enc', 'rb') as in_file, open('.//Log//'+signal, 'wb') as out_file:
# self.Tdecrypt(in_file, out_file)
with open('.//Log//'+signal) as SignalFile:
CommandResultList = self.modem.GetCommandResultList()#TODO need to dynamic change the modem
print 'open ',signal,CommandResultList
for SignalLine in SignalFile:
#print "SignalLine:",SignalLine
if len(SignalLine) >15:#remove empty line
SignalLine = SignalLine.strip()
SignalLine=SignalLine.replace(':',',')
SignalLine=SignalLine.replace('"','')
# SignalLine=SignalLine.replace('/','')
line=SignalLine.split(',')#first is timestamp,second is command, other is result
if line[1] not in CommandResultList or len(line)<3:
#if is not a command result, pass to next one
continue
if timestamp == None:
#initial the time stamp
jsonElement.clear()
jsonElement=self.JsonReset()
#print jsonElement
timestamp= long(float(line[0])) #get minisceond
if GPSFile!=None:
#if GPS is available, check the time is correct or not
GPSLine=GPSFile.readline()
GPSLine=GPSLine.split(',')
GPStime = long(float(GPSLine[0]))/1000#use second to divide the data,not minisceond
#print 'GPS time=',GPStime
while timestamp/1000 > GPStime:
# if the GPS time is less than signal. then search the right one
GPSLine=GPSFile.readline()
if not GPSLine:#end of file, no data
GPSFile.close()
GPSFile=None
GPSLine=None
break
GPSLine=GPSLine.split(',')
GPStime = long(float(GPSLine[0]))/1000
if long(float(line[0]))/1000 != timestamp/1000:#next timestamp
timestamp=long(line[0])
jsonArray.append(jsonElement.copy())
jsonElement.clear()
jsonElement=self.JsonReset()
command = CommandResultList[line[1]] #todo need to dynamic detect the right command list, now only get the choose one
jsonElement['CellularInfo'][0]['Time']=timestamp
#print timestamp,jsonElement['Time']
for index, response in enumerate(command):
#index start with 0
if line[index+2]=='D':
#don't care
continue
if response in jsonElement['CellularInfo'][0]:
jsonElement['CellularInfo'][0][response]=str(line[index+2])
else:
jsonElement['other'][0][response]=line[index+2]
if GPSFile != None:
#if GPS is available, check the time is correct or not
#print timestamp/1000,GPStime
while timestamp/1000 > GPStime:
# if the GPS time is less than signal. then search the right one
GPSLine=GPSFile.readline()
if not GPSLine:#end of file, no data
GPSFile.close()
GPSFile=None
GPStime=None
break
GPSLine=GPSLine.split(',')
GPStime = long(float(GPSLine[0]))/1000
if timestamp/1000 == GPStime:#check if is in the same sceond. don't care minisceond
jsonElement['Lat']=GPSLine[1]
jsonElement['Lng']=GPSLine[3]
#print line
jsonArray.append(jsonElement.copy()) #last data
json.dump(jsonArray, WriteFile,indent=2)
#SignalFile.close()
WriteFile.close()
with open('.//Upload//'+signal, 'rb') as in_file, open('.//Upload//'+signal+'.sig', 'wb') as out_file:
self.RSAsignature(in_file, out_file)
if GPSFile != None:
GPSFile.close()
#os.rename('.//Log//'+signal,'.//Log//'+signal+'-Uploaded')
def JsonReset(self):
jsonElement={}
jsonElement.clear()
seq = ("AppicationType", "ApplicationVersion", "Account","IMEI","Lat", "Lng","CellularInfo","other","equipmentId")
jsonElement=dict.fromkeys(seq)
jsonElement["AppicationType"]=self.commandserial.GetPlatformOS()
jsonElement["ApplicationVersion"]=self.Version
jsonElement["Account"]=self.username
jsonElement["equipmentId"]=self.MAC
jsonElement["CellularInfo"]=[]
cellseq=( "Time","CellID","CellMCC","CellMNC","CellPCI","CellTAC","RSSI","SINR","RSRQ","RSRP")
cell=dict.fromkeys(cellseq)
jsonElement["CellularInfo"].append(cell)
jsonElement["other"]=[]
jsonElement["other"].append({})
return jsonElement
def GetModemLogFile(self):
return self.ModemLogFile
def GetGPSLogFile(self):
return self.GPSLogFile
def SaveLogSetupConfig(self, variables):
self.SplitTokenAndSave(variables)
outfile = open('.//Setting//LogSetup.conf', 'w')
outfile.write('username='+self.username)
outfile.close()
pass
def SplitTokenAndSave(self, variables):
token = variables.split("&")
for t in token:
print 't=',t
SysToken = t.split("=")
if( SysToken[0] == 'username' ):
print 'SysToken[0]',SysToken[0]
self.username = str(SysToken[1])
pass
def GetUserName(self):
return self.username