-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.py
291 lines (241 loc) · 9.18 KB
/
Function.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
import time
import sys
import glob
import threading,json
import ftplib
import os
from datetime import datetime
from Serial import Serialport
from Modem import ModemCommand
from Serial import Serialport
from GPS import GPSCommand
from datetime import datetime
from Log import Logger
from Tkinter import *
count =0
class Function():
def __init__(self):
"""
"""
self.OPENFILE = 1;
self.CLOSEFILE = 2;
self.commandserial = Serialport()
self.GPSserial = Serialport()
self.GPSLogState = False
self.GPSThread=None
self.ModemLogState=False
self.ModemLogFile=None
self.ModemThread=None
self.AutoSendState=None
self.AutoCommandList={}
self.AutoSendTimeInterval=1 #second
self.ModemCommandList={}
self.UploadConfigList={}
self.UploadFileList={}
self.Platform = self.commandserial.GetPlatform()
self.modem = ModemCommand()
self.GPSControll = GPSCommand(self.Platform)
self.logger=Logger(self.Platform)
def GetPlatform(self):
return self.Platform
def SetModemLog(self,state):
"""log the modem and the gps same time
"""
if state ==1 :
self.ModemLogState = True
self.logger.SetModemLogState(True)
else:
self.ModemLogState = False
self.logger.SetModemLogState(False)
print "Function.ModemLogState:",self.ModemLogState
def SetScrollText(self,frame):
self.logger.SetScrollText(frame)
self.txt=frame
def ParseFileToJson(self, file):
"""cahnge the upload file into json format(both of the signal and gps)
determine gps need to add in json or not
"""
# SignalFileList = self.UploadFileList.keys()
# for file in SignalFileList:
print 'ParseFileToJson with file:',file
GPSFile = self.CheckGPS(file)
self.logger.ToJson(file,GPSFile,self.modem)
# self.ToJson(file,GPSFile)
def SetAutoSendState(self, state):
"""Set AutoSendState
"""
if state == 1:
self.AutoSendState =True
self.logger.SetAutoSendState(True)
if len(self.AutoCommandList.keys() )==0:
raise EnvironmentError('No command choosen')
print self.AutoCommandList.keys()
if self.ModemLogState :
modem = self.modem.GetModemName()
self.logger.SetModemLogFile(modem,self.OPENFILE)
if self.GPSLogState:
self.logger.SetGPSLogFile(self.OPENFILE)
self.logger.StartGPSThread()
print "start gps thread"
self.logger.SetAutoCommandList(self.AutoCommandList)
self.logger.SetAutoSendTimeInterval(self.AutoSendTimeInterval)
self.logger.StartAutoThread()
print "start autosend thread"
else:
ModemLogFile = self.logger.GetModemLogFile()
GPSLogFile = self.logger.GetGPSLogFile()
self.logger.SetModemLogFile(None,self.CLOSEFILE)
self.logger.SetGPSLogFile(self.CLOSEFILE)
if ModemLogFile != None:
fileindex = ModemLogFile.name.rfind('/')
filename = ModemLogFile.name[fileindex+1:] #remove the directory's name
print "filename:"+filename
self.ParseFileToJson(filename)
self.AutoSendState =False
self.logger.SetAutoSendState(False)
self.logger.SetGPSLogState(False)
print "Set AutoSendState and GPSLogState to False"
print "Close Log file"
#self.AutoCommandList.clear()
def SetGPSLog(self,state):
"""only set the GPS state and close the file
"""
if state ==1 :
self.logger.SetGPSLogState(True)
self.GPSLogState = True
else:
self.GPSLogState = False
self.logger.SetGPSLogState(False)
print "Function.GPSLogState:",self.GPSLogState
def GetAutoSendConfig(self):
"""Read info from AutoSendConfig
"""
#self.modem.GetModemCommandList
CommandList=[1,2,3,4,5,6,7]
print "function ",self.ModemCommandList
return self.AutoSendTimeInterval*1000, self.ModemCommandList.keys()
pass
def SetModemSerialPort(self,port):
"""Set ModemSerial
"""
self.commandserial.SetSerialPort(port)
self.logger.Setcommandserial(self.commandserial)
pass
def SetGPSSerialPort(self,port):
"""Set the GPS serial port
"""
self.GPSserial.SetSerialPort(port)
self.logger.SetGPSserial(self.GPSserial)
def GetModemList(self):
"""return the supporeted modem list
"""
return self.modem.GetModemList()
def SetModem(self,modem):
"""Set which modem will be use
"""
print "SetModem"
self.ModemCommandList.clear()
self.ModemCommandList=self.modem.SetChooseModem(modem)
def GetModemSerialPort(self):
"""Get Modem serial list
"""
return self.commandserial.GetSerialPortList()
pass
def GetSerialPort(self):
"""Get serial list
"""
return self.commandserial.GetSerialPortList()
pass
def GetUploadConfig(self):
"""Read info from UploadConfig
"""
self.UploadConfigList.clear()
file = open('.//Setting//Upload.conf','r')
for line in file.read().splitlines():
content = line.split(':')
self.UploadConfigList[content[0]] = content[1]
#print line
return self.UploadConfigList
pass
def GetUploadFileList(self):
"""Get the available upload file list
"""
filelist = []
for file in os.listdir('.//Upload'):
#if not ( file.endswith('GPS') or file.endswith('-Uploaded') or file.endswitch):
if not file.endswith('GPS') and not file.endswith('.sig'):
filelist.append(file)
print file
return filelist
pass
def CheckGPS(self,file):
"""check the upload file need gps or not
"""
lasttag=file.rfind('-')
GPSfile=file[:lasttag]+'-GPS'
print 'CheckGPS with ',GPSfile
for gpslist in os.listdir('.//Log'):
if gpslist==GPSfile:
self.GPSControll.ValidGPSFile(gpslist)
return GPSfile
return None
def UploadFile(self):
"""upload the file to the server
"""
UploadConfig = self.GetUploadConfig()
# self.ParseFileToJson()
#session = ftplib.FTP(UploadConfig['Server IP'],UploadConfig['Server ID'],UploadConfig['Server PW'])
ftp = ftplib.FTP()
ftp.connect(UploadConfig['Server IP'], 21)
ftp.login(UploadConfig['Server ID'],UploadConfig['Server PW'])
print "Logging in"
for filename in self.UploadFileList.keys():
print 'upload file:',filename
try:
#ploadfile=open('.//Upload//'+filename,'rb')
with open('.//Upload//'+filename,'rb') as uploadfile, open('.//Upload//'+filename+'.sig','rb') as uploadfileSIG:
ftp.cwd('/file/data/')
ftp.storbinary("STOR " + filename, uploadfile) # send the file
ftp.cwd('/file/sig/')
ftp.storbinary("STOR " + filename+'.sig', uploadfileSIG)
print 'uploaded ',filename
except IOError:
print "failed to upload"
print 'close ftp'
ftp.close()
self.UploadFileList.clear()
def SetUploadFile(self,command,state):
"""set which file will be uploaded
"""
if command in self.UploadFileList:
if state == 0:
del self.UploadFileList[command]
else:
if state ==1:
self.UploadFileList[command]=state
print "self.UploadFileList:",self.UploadFileList
def SetAutoSendCommand(self,command,state):
"""Set which command will be auto
"""
if command in self.AutoCommandList:
if state == 0:
del self.AutoCommandList[command]
else:
if state ==1:
self.AutoCommandList[command]=state
print self.AutoCommandList
def SetUserInPut(self,mess):
"""Send the input to the serial port
"""
self.logger.Setcommandserial(self.commandserial)
self.logger.ReadUserInPut(mess)
# 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 LoginSuccess(self, username):
variables = 'username='+username
self.logger.SaveLogSetupConfig(variables)
pass