forked from pajikos/sms-gammu-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
146 lines (117 loc) · 4.23 KB
/
run.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
import os
import sys
from flask import Flask, request
from flask_httpauth import HTTPBasicAuth
from flask_restful import reqparse, Api, Resource, abort
from support import load_user_data, init_state_machine, retrieveAllSms, deleteSms, encodeSms
from gammu import GSMNetworks
pin = os.getenv('PIN', None)
ssl = os.getenv('SSL', False)
port = os.getenv('PORT', '5000')
user_data = load_user_data()
machine = init_state_machine(pin)
app = Flask(__name__)
#app.debug = True
api = Api(app)
auth = HTTPBasicAuth()
@auth.verify_password
def verify(username, password):
if not (username and password):
return False
return user_data.get(username) == password
class Sms(Resource):
def __init__(self, sm):
self.parser = reqparse.RequestParser()
self.parser.add_argument('text')
self.parser.add_argument('number')
self.parser.add_argument('smsc')
self.parser.add_argument('unicode')
self.machine = sm
#@auth.login_required
def get(self):
allSms = retrieveAllSms(machine)
list(map(lambda sms: sms.pop("Locations"), allSms))
return allSms
#@auth.login_required
def post(self):
args = self.parser.parse_args()
if args['text'] is None or args['number'] is None:
abort(404, message="Parameters 'text' and 'number' are required.")
smsinfo = {
"Class": -1,
"Unicode": args.get('unicode') if args.get('unicode') else False,
"Entries": [
{
"ID": "ConcatenatedTextLong",
"Buffer": args['text'],
}
],
}
messages = []
for number in args.get("number").split(','):
for message in encodeSms(smsinfo):
message["SMSC"] = {'Number': args.get("smsc")} if args.get("smsc") else {'Location': 1}
message["Number"] = number
messages.append(message)
print(message, file=sys.stderr)
result = [machine.SendSMS(message) for message in messages]
return {"status": 200, "message": str(result)}, 200
class Signal(Resource):
def __init__(self, sm):
self.machine = sm
def get(self):
return machine.GetSignalQuality()
class Reset(Resource):
def __init__(self, sm):
self.machine = sm
def get(self):
machine.Reset(False)
return {"status":200, "message": "Reset done"}, 200
class Network(Resource):
def __init__(self, sm):
self.machine = sm
def get(self):
network = machine.GetNetworkInfo()
network["NetworkName"] = GSMNetworks.get(network["NetworkCode"], 'Unknown')
return network
class GetSms(Resource):
def __init__(self, sm):
self.machine = sm
@auth.login_required
def get(self):
allSms = retrieveAllSms(machine)
sms = {"Date": "", "Number": "", "State": "", "Text": ""}
if len(allSms) > 0:
sms = allSms[0]
deleteSms(machine, sms)
sms.pop("Locations")
return sms
class SmsById(Resource):
def __init__(self, sm):
self.machine = sm
@auth.login_required
def get(self, id):
allSms = retrieveAllSms(machine)
self.abort_if_id_doesnt_exist(id, allSms)
sms = allSms[id]
sms.pop("Locations")
return sms
def delete(self, id):
allSms = retrieveAllSms(machine)
self.abort_if_id_doesnt_exist(id, allSms)
deleteSms(machine, allSms[id])
return '', 204
def abort_if_id_doesnt_exist(self, id, allSms):
if id < 0 or id >= len(allSms):
abort(404, message = "Sms with id '{}' not found".format(id))
api.add_resource(Sms, '/sms', resource_class_args=[machine])
api.add_resource(SmsById, '/sms/<int:id>', resource_class_args=[machine])
api.add_resource(Signal, '/signal', resource_class_args=[machine])
api.add_resource(Network, '/network', resource_class_args=[machine])
api.add_resource(GetSms, '/getsms', resource_class_args=[machine])
api.add_resource(Reset, '/reset', resource_class_args=[machine])
if __name__ == '__main__':
if ssl:
app.run(port=port, host="0.0.0.0", ssl_context=('/ssl/cert.pem', '/ssl/key.pem'))
else:
app.run(port=port, host="0.0.0.0")