-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·120 lines (105 loc) · 3.27 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
import time
from flask import Flask, abort, request
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')
app.wsgi_app = ProxyFix(app.wsgi_app)
db = SQLAlchemy(app)
@app.before_request
def before_request():
pass
@app.after_request
def after_request(response):
return response
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/<uuid>/upload_log', methods=['POST'])
def upload_file(uuid):
uuid = uuid.replace('-', '_')
create_table_cmd = 'CREATE TABLE IF NOT EXISTS %s_app_usage_logs(\
id INT NOT NULL AUTO_INCREMENT, \
datetime DATETIME NOT NULL, \
latitude DOUBLE, \
longitude DOUBLE, \
location_acc FLOAT, \
speed FLOAT, \
activity VARCHAR(12), \
activity_conf INT, \
illumination FLOAT, \
mobile_connection BOOLEAN, \
wifi_connection BOOLEAN, \
wifi_ap_num INT, \
battery_power FLOAT, \
application VARCHAR(96) NOT NULL, \
PRIMARY KEY(id));' % uuid
db.session.execute(create_table_cmd)
file = request.files['log_file']
if file:
for line in file.stream.readlines():
values = line.replace('\n', '')
try:
insert_log_cmd = 'INSERT INTO %s_app_usage_logs \
(datetime, latitude, longitude, location_acc, speed, \
activity, activity_conf, \
illumination, mobile_connection, wifi_connection, wifi_ap_num, \
battery_power, application) \
VALUES (%s);' % (uuid, values)
db.session.execute(insert_log_cmd)
except:
print insert_log_cmd
db.session.commit()
return 'Success!'
@app.route('/<uuid>/register_app', methods=['GET'])
def register_app(uuid):
uuid = uuid.replace('-', '_')
app = request.args['app']
try:
create_table_cmd = 'CREATE TABLE IF NOT EXISTS %s_installed_apps(\
id INT NOT NULL AUTO_INCREMENT, \
application VARCHAR(96) NOT NULL, \
start_date DATETIME, \
end_date DATETIME, \
PRIMARY KEY(id), \
UNIQUE KEY(application));' % uuid
db.session.execute(create_table_cmd)
except:
abort(404)
print create_table_cmd
try:
insert_app_cmd = 'INSERT INTO %s_installed_apps \
(application, start_date) VALUES ("%s", "%s");' % \
(uuid, app, time.strftime('%Y-%m-%d %H:%M:%S'))
db.session.execute(insert_app_cmd)
db.session.commit()
return request.args['app'] + " is registered!"
except:
print insert_app_cmd
abort(404)
@app.route('/<uuid>/unregister_app', methods=['GET'])
def unregister_app(uuid):
uuid = uuid.replace('-', '_')
app = request.args['app']
try:
update_cmd = 'UPDATE %s_installed_apps SET end_date = "%s" \
WHERE application = "%s"' % \
(uuid, time.strftime('%Y-%m-%d %H:%M:%S'), app)
db.session.execute(update_cmd)
db.session.commit()
return request.args['app'] + " is unregistered!"
except:
print update_cmd
return request.args['app'] + "unregistration failed."
@app.route('/test_post', methods=['POST'])
def test_post():
for att in request.files:
print att
return 'Success!'
import os
from werkzeug.utils import secure_filename
def save_file(file):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if __name__ == '__main__':
app.run()