-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmsweb.py
195 lines (165 loc) · 5.31 KB
/
smsweb.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
#!/usr/bin/env python
"""
smsweb : spy mongoDB connector
"""
import config
import os.path
import re
import time
import pymongo
from datetime import datetime
import serial
from messaging.sms import SmsSubmit
from messaging.sms import SmsDeliver
class SmsWeb(object):
def __init__(self, recipient=config.recipient, message=config.message):
self.recipient = recipient
self.content = message
def openser(self):
self.ser = serial.Serial(config.serial, 115200, timeout=config.timeout)
self.ser.flushInput()
self.ser.flushOutput()
self.SendCommand('ATZ\r',8)
self.SendCommand('AT+CMGF=0\r',16)
def opendb(self):
self.conn = pymongo.MongoClient(config.mongohost, config.mongoport)
self.db = self.conn.smsweb
def insertOutbox(self,rcpt,msg):
self.db.outbox
doc = {"rcpt":rcpt,"msg":msg,"timestamp":datetime.now()}
idProcess = self.db.outbox.insert_one(doc).inserted_id
ret = {"rcpt":rcpt,"msg":msg,"idProcess":idProcess}
return ret
def insertSentitem(self,rcpt,msg,stat):
self.db.sentitems
doc = {"rcpt":rcpt,"msg":msg,"timestamp":str(datetime.now()),"idProcess":self.idprocess,"stat":stat}
return self.db.sentitems.insert_one(doc).inserted_id
def insertInbox(self,data):
self.db.inbox
return self.db.inbox.insert_one(data).inserted_id
def getSentitem(self,id):
self.db.sentitems
return self.db.sentitems.find_one({"idProcess":id})
def getSentitems(self):
self.db.sentitems
return self.db.sentitems.find()
def getOutbox(self):
self.db.outbox
return self.db.outbox.find_one()
def getOutboxs(self):
self.db.outbox
return self.db.outbox.find()
def getInbox(self):
self.db.inbox
return self.db.inbox.find_one()
def removeOutbox(self,id):
self.db.outbox
return self.db.outbox.delete_many({"_id":id})
def dropOutbox(self):
self.db.drop_collection("outbox")
def rcpt(self, number):
self.recipient = number
def msg(self, message):
self.content = message
def idProcess(self,idprocess):
self.idprocess = idprocess
def sends(self):
rcptarr = re.split(',|;',self.recipient)
for num in rcptarr:
if num:
print '*Sending SMS to: '+num+' \n'
self.rcpt(num)
self.send()
def send(self):
self.pdu = SmsSubmit(self.recipient, self.content)
for xpdu in self.pdu.to_pdu():
command = 'AT+CMGS=%d\r' % xpdu.length
a = self.SendCommand(command,len(str(xpdu.length))+14)
command = '%s\x1a' % xpdu.pdu
b = self.SendCommand(command,len(xpdu.pdu)+20)
data = str(a)+str(b)
self.insertSentitem(self.recipient,self.content,data)
return data
def close(self):
self.ser.close()
def SendCommand(self,command,char,getline=True):
self.ser.write(command)
data = ''
if getline:
data=self.ReadLine(char)
return data
def ReadAll(self):
data = self.ser.readall()
return data
def ReadLine(self,char):
data = self.ser.read(char)
return data
def unreadMsg(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL=0\r\n'
data = self.SendCommand(command,16)
return data
def readMsg(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL=1\r\n'
self.ser.write(command)
cmd = self.ser.readline()
cmd += self.ser.readline()
head = self.ser.readline()
a = []
while "CMGL" in head:
data = self.ser.readline()
sms = SmsDeliver(data.rstrip())
idx = head.rstrip().split(',')[0].split(' ')[1]
self.insertInbox(sms.data)
#print idx
a.append(int(idx))
head = self.ser.readline()
return a
def allMsg(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL=4\r\n'
data = self.SendCommand(command,16)
return data
def deleteMsg(self, idx):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGD=%s\r\n' % idx
return self.SendCommand(command,18)
#self.ser.write(command)
#data = self.ser.readline()
#data += self.ser.readline()
#data += self.ser.readline()
#return data
def deleteMsgs(self, idx):
#self.ser.flushInput()
#self.ser.flushOutput()
for id in idx:
command = 'AT+CMGD=%s\r\n' % id
self.SendCommand(command,18)
def getMsg(self, idx):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGR=%s\r\n' % idx
self.ser.write(command)
data = self.ser.readline()
data += self.ser.readline()
data3 = self.ser.readline()
data = data+data3
if "CMGR" in data3:
data += self.ser.readline()
data += self.ser.readline()
data += self.ser.readline()
return data
def isRunning(self,pid):
path = "/proc/"+str(pid)
return os.path.exists(path)
#try:
# os.kill(pid,0)
#except OSError:
# return False
#else:
# return True