-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.py
184 lines (165 loc) · 4.65 KB
/
sms.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
import MDM
import MOD
import sys
FTPSERVER_ADDR = ''
FTPUSERNAME = ''
FTPPASSWORD = ''
DirName = ''
FILENAME = ''
def delsms():
b = MOD.watchdogReset()
print 'deleting msg'
MDM.send('at+cmgd=1,4\r', 0)
res = MDM_waitfor('OK',20)
if(res == -1):
print 'error deleting sms'
print res
def SmsSetup():
b = MOD.watchdogReset()
a = MDM.send('AT+CMGF=1\r', 0)
trovato = MDM_waitfor('OK', 10)
a = MDM.send('AT+CNMI=2,1\r', 0)
trovato = MDM_waitfor('OK', 10)
a = MDM.send('AT+CLIP=1\r', 0)
trovato = MDM_waitfor('OK', 10)
def LookforSMS(txt):
b = MOD.watchdogReset()
foundPos = -1
strtxt = ''
strd2 = ''
strd3 = ''
foundPos = txt.find('+CMGR:')
if(foundPos != -1):
strtxt = txt[foundPos+5:]
print strtxt
else:
print 'error pos'
return strtxt
findendl = strtxt.rfind('"')
if(findendl != -1):
strd2 = strtxt[findendl+1:]
print strd2
else:
print 'error endl'
return strd2
findok = strd2.find('OK')
if(findok != -1):
strd3 = strd2[:findok-2]
print strd3
else:
print 'error: Ok not found looking for SMS'
return strd2
return strd3
def ExtractInfo(textSms):
# FOTA:ftp.innovations.com:[email protected]:TEST@1234:telit:FOTA_TEST.pyo:OK
global FILENAME
b = MOD.watchdogReset()
global FTPSERVER_ADDR
global FTPUSERNAME
global FTPPASSWORD
global DirName
infoList = textSms.split(':')
n = len(infoList)
print 'text sms is ',textSms
if n == 7 :
FTPSERVER_ADDR = infoList[1].strip()
FTPUSERNAME = infoList[2].strip()
FTPPASSWORD = infoList[3].strip()
DirName = infoList[4].strip()
FILENAME = infoList[5].strip()
print 'server info',FTPSERVER_ADDR, FTPUSERNAME, FTPPASSWORD, DirName, FILENAME
return 1
else :
delsms()
return -1
# waiting function
def Wait(sec):
timer = MOD.secCounter()
timerstop = timer + sec
b = MOD.watchdogReset()
while timer < timerstop:
timer = MOD.secCounter()
def MDM_receive(timeout):
res = ''
start = MOD.secCounter()
b = MOD.watchdogReset()
while (MOD.secCounter() - start < timeout):
res = res + MDM.read()
return res
def MDM_waitfor(value, timeout):
res = ''
found = -1
b = MOD.watchdogReset()
start = MOD.secCounter()
while (MOD.secCounter() - start < timeout):
res = res + MDM.read()
found = res.find(value)
if(found != -1):
print res
return found
print res
return found
def MDM_receiveUntil(value,timeout):
res = ''
found = -1
b = MOD.watchdogReset()
start = MOD.secCounter()
while (MOD.secCounter() - start < timeout):
res = res + MDM.read()
found = res.find(value)
if(found != -1):
return res
return res
def main_sms():
try :
SmsSetup()
SMSindex = checkForFotaMessagePos()
if SMSindex != '-1' and SMSindex != '' :
cmgr = 'AT+CMGR=' + SMSindex + '\r'
print 'cmgr output ',cmgr
a = MDM.send(cmgr, 0)
SMStext = MDM_receiveUntil('OK',10)
print 'smstext',SMStext
TxtSMS = LookforSMS(SMStext)
print 'textsms', TxtSMS
if(TxtSMS != ''):
ret = ExtractInfo(TxtSMS)
if ret == 1 :
return 1
return -1
except Exception, e:
print 'Got exce in sms'
delsms()
return -1
def checkForFotaMessagePos():
SMSindex = '-1'
b = MOD.watchdogReset()
a = MDM.send('AT+CMGL="ALL"\r',5)
msgList = MDM_receiveUntil('OK',10)
print msgList
if msgList != '' :
print 'msglist',msgList
allMsgList = msgList.split('+CMGL')
listLen = len(allMsgList)
if msgList.find('FOTA')!= -1 :
fotaMsgCounter = 0
fotaMsg = ''
for msg in allMsgList :
if msg.find('FOTA') != -1 :
fotaMsgCounter = fotaMsgCounter + 1
fotaMsg = msg
print 'fota msg is ',fotaMsg
if fotaMsgCounter > 1 :
delsms()
else :
c = fotaMsg.find(':') + 1
d= fotaMsg.find(',')
SMSindex = fotaMsg[c:d].strip()
print 'smsindex is',SMSindex
return SMSindex
else :
if listLen > 10 :
print 'deleting unwanted sms'
delsms()
return '-1'
print 'SMS imported'