server.py 7.37 KB
Newer Older
Vladislav Rykov's avatar
Vladislav Rykov committed
1
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
2
import psycopg2
Vladislav Rykov's avatar
Vladislav Rykov committed
3
import bcrypt
4
import misc
Vladislav Rykov's avatar
Vladislav Rykov committed
5
import dao.user.user as ud
Vladislav Rykov's avatar
Vladislav Rykov committed
6
import dao.application.application as ad
7
import dao.device.device as dd
Vladislav Rykov's avatar
Vladislav Rykov committed
8
import dao.pend.pend as pend
Vladislav Rykov's avatar
Vladislav Rykov committed
9
import dao.data.data as data
Vladislav Rykov's avatar
Vladislav Rykov committed
10
11
import binascii

12

13
APP_KEY_LEN = 8
Vladislav Rykov's avatar
Vladislav Rykov committed
14
DATA_DOWNLOAD_DIR = 'data'
15
16

server = Flask(__name__, template_folder='templates/')
Vladislav Rykov's avatar
Vladislav Rykov committed
17

18
@server.route('/')
19
def index():
Vladislav Rykov's avatar
Vladislav Rykov committed
20
    if 'name' in session and len(session['name']) > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
21
22
        ah = ad.ApplicationDao()
        apps = ah.get_list(session['name'].encode('utf-8'))
Vladislav Rykov's avatar
Vladislav Rykov committed
23
24

        session.pop('appkey', None)
Vladislav Rykov's avatar
Vladislav Rykov committed
25
        print('apps: ', apps)
26
27
28
29
        if apps[0]:
            return render_template('index.html', apps=apps[1])
        else:
            return render_template('index.html', feedback=apps[1])
30
31
32
33



@server.route('/signup', methods=['GET', 'POST'])
34
35
36
37
38
def signup():
    if request.method == 'GET':
        return render_template('signup.html')
    else: 
        username = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
39
        password = request.form['password'].encode('utf-8')
40
41
42
43
44

        if (username == '' or password == ''):
            feedback = 'Username or password fields cannot be empty'
            return render_template('signup.html', feedback=feedback)
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
45
46
47
48
            uh = ud.UserDao()
            res = uh.create(username, password)
            if (not res[0]):
                return render_template('signup.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
49
50
            else:
                session['name'] = username
51
        
Vladislav Rykov's avatar
Vladislav Rykov committed
52
                return redirect(url_for('index'))
53

54
55
56


@server.route('/login', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
57
def login():
Vladislav Rykov's avatar
Vladislav Rykov committed
58
59
60
61
    if request.method == 'GET':
        return render_template('login.html')
    else: 
        username = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
62
        password = request.form['password'].encode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
63
64
65
66
67

        if (username == '' or password == ''):
            feedback = 'Username or password fields cannot be empty'
            return render_template('login.html', feedback=feedback)
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
68
69
70
71
            uh = ud.UserDao()
            res = uh.get(username, password)
            if (not res[0]):
                return render_template('login.html', feedback=msg[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
72
73
            else:
                session['name'] = username
Vladislav Rykov's avatar
Vladislav Rykov committed
74
        
Vladislav Rykov's avatar
Vladislav Rykov committed
75
76
77
                return redirect(url_for('index'))


78
79

@server.route('/logout')
Vladislav Rykov's avatar
Vladislav Rykov committed
80
81
82
def logout():
    session.clear()
    return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
83
84


85
86
87
88
89
90
91
92
93

@server.route('/new-app')
def new_application():
    return render_template('new-app.html')



@server.route('/app', methods=['GET', 'POST'])
def app():
Vladislav Rykov's avatar
Vladislav Rykov committed
94
    ah = ad.ApplicationDao()
95
    if request.method == 'GET':
96
        dh = dd.DeviceDao()
Vladislav Rykov's avatar
Vladislav Rykov committed
97
98
            
        session['appkey'] = request.args.get('appkey')
Vladislav Rykov's avatar
Vladislav Rykov committed
99

Vladislav Rykov's avatar
Vladislav Rykov committed
100
        app = ah.get(session['appkey'])
101
        devs = dh.get_list(app[1][1])
Vladislav Rykov's avatar
Vladislav Rykov committed
102
        
103
104
        print('devs : ', devs)
        return render_template('app.html', app=app[1], devs=devs[1])
105
106
107
108
109
    else:
        if request.form['appname'] == '':
            error = 'Application name cannot be empty.'
            return render_template('new-app.html', feedback=error)
        else:
110
111
            appkey = misc.rand_str(APP_KEY_LEN)
            res = ah.create(request.form['appname'], appkey, session['name'], request.form['appdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
112
            
Vladislav Rykov's avatar
Vladislav Rykov committed
113
114
115
            if not res[0]:
                return render_template('new-app.html', feedback=res[1])
            
116
117
118
119
120
121
122
            dh = dd.DeviceDao()
            res = dh.create_table(appkey)
            
            if not res[0]:
                ah.delete(appkey)
                return render_template('new-app.html', feedback=res[1])
            
Vladislav Rykov's avatar
Vladislav Rykov committed
123
            return redirect(url_for('index'))
124

125
126
127
128
129
130
@server.route('/delete-app')
def delete_app():
    dh = dd.DeviceDao()
    devs = dh.get_list(session['appkey'])
    
    for dev in devs[1]:
Vladislav Rykov's avatar
Vladislav Rykov committed
131
        data.delete_table(session['appkey'], dev[1])
132
133
134
135
136
137
138
139
140
141
    
    dh.delete_table(session['appkey'])
    
    ah = ad.ApplicationDao()
    res = ah.delete(session['appkey'])
    
    if not res[0]:
        return redirect(url_for('app'))
    else:
        return redirect(url_for('index'))
142

Vladislav Rykov's avatar
Vladislav Rykov committed
143
144
145
146
147
148
149
150
151
152
@server.route('/add-dev')
def new_dev():
    dh = dd.DeviceDao()
    dev_list = dh.get_list(session['appkey'])
    
    print('dev list : ', dev_list)

    if not dev_list[0]:
        return render_template('add-dev.html', feedback=dev_list[1])
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
153
        return render_template('add-dev.html', free_ids=misc.prep_id_range(dev_list[1]))
Vladislav Rykov's avatar
Vladislav Rykov committed
154
155
156
157
158
159
160
 


@server.route('/dev', methods=['GET', 'POST'])
def dev():
    dh = dd.DeviceDao()
    if request.method == 'GET':
161
        dev = dh.get(session['appkey'], request.args.get('id'))
Vladislav Rykov's avatar
Vladislav Rykov committed
162
163
        ltup = 'recently'

Vladislav Rykov's avatar
Vladislav Rykov committed
164
165
166
        session['devid'] = dev[1][1]
        session['devname'] = dev[1][0]

167
        return render_template('dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
Vladislav Rykov's avatar
Vladislav Rykov committed
168
169
    else:
        res = dh.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
170

Vladislav Rykov's avatar
Vladislav Rykov committed
171
172
173
        if not res[0]:
            return render_template('add-dev.html', feedback=res[1])
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
174
            res = data.create_table(session['appkey'], request.form['devid'])
175
176
177
178
179
            
            if not res[0]:
                dh.delete(session['appkey'], request.form['devid'])
                return render_template('add-dev.html', feedback=res[1])
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
180
                return redirect(url_for('app', appkey=session['appkey']))
Vladislav Rykov's avatar
Vladislav Rykov committed
181
182


Vladislav Rykov's avatar
Vladislav Rykov committed
183
184
185
186
187
@server.route('/dev-conf', methods=['GET', 'POST'])
def dev_conf():
    if request.method == 'GET':
        return render_template('dev-conf.html', devname=session['devname'])
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
188
        
Vladislav Rykov's avatar
Vladislav Rykov committed
189
190
191
192
193
        argslen = len(request.form['arg']) + 1
        args = bytearray(argslen + 2)
        args[0] = int(request.form['confid'])
        args[1] = argslen
        
Vladislav Rykov's avatar
Vladislav Rykov committed
194
        bstr = bytes(request.form['arg'])
Vladislav Rykov's avatar
Vladislav Rykov committed
195
        i = 0
Vladislav Rykov's avatar
Vladislav Rykov committed
196
        while i < argslen - 1:
Vladislav Rykov's avatar
Vladislav Rykov committed
197
198
199
            args[2+i] = bstr[i]
            i += 1

Vladislav Rykov's avatar
Vladislav Rykov committed
200
201
202
203
204
205
206
207
        base64_args = binascii.b2a_base64(args).decode('utf-8')

        pend.create(session['appkey'], session['devid'], base64_args)

        #print('msg = ', args)
        #print('base64 = ', base64_args)
        #print(type(request.form['arg'].encode('utf-8')))
        #print(request.form['arg'].encode('utf-8'))
Vladislav Rykov's avatar
Vladislav Rykov committed
208
209
        
        return redirect(url_for('dev', id=session['devid']))
Vladislav Rykov's avatar
Vladislav Rykov committed
210

Vladislav Rykov's avatar
Vladislav Rykov committed
211

Vladislav Rykov's avatar
Vladislav Rykov committed
212
213
214
@server.route('/delete-dev')
def delete_dev():
    dh = dd.DeviceDao()
Vladislav Rykov's avatar
Vladislav Rykov committed
215
216
    data.delete_table(session['appkey'], session['devid'])
    res = dh.delete(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
217
218
219
220

    return redirect(url_for('app', appkey=session['appkey']))


Vladislav Rykov's avatar
Vladislav Rykov committed
221
222
@server.route('/dev-data')
def dev_data():
Vladislav Rykov's avatar
Vladislav Rykov committed
223
224
225
    last = data.get_last_n(session['appkey'], session['devid'], 5)  
    count = data.get_count(session['appkey'], session['devid'])

Vladislav Rykov's avatar
Vladislav Rykov committed
226
227
    #print(last)
    #print(count)
Vladislav Rykov's avatar
Vladislav Rykov committed
228
229

    return render_template('dev-data.html', data=last[1], total=count[1][0])
Vladislav Rykov's avatar
Vladislav Rykov committed
230

Vladislav Rykov's avatar
Vladislav Rykov committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
@server.route('/data-csv')
def data_csv():
    dumpd = data.get_all(session['appkey'], session['devid'])

    fn = session['appkey']+ '_' +str(session['devid'])+ '.csv'

    with open(DATA_DOWNLOAD_DIR+'/'+fn, 'w') as f: 
        for d in dumpd[1][0][2]:
            f.write(d)
            f.write(',')
        f.write('\n')
        
        for row in dumpd[1]:
            for v in row[2]:
                f.write(str(row[2][v]))
                f.write(',')
            f.write('\n')
    
    return send_from_directory(DATA_DOWNLOAD_DIR, fn, as_attachment=True)
        

252
if __name__ == '__main__':
253
254
255
256
    server.secret_key = 'sdjfklsjf^$654sd^#sPH'
    server.run(debug = True, host='0.0.0.0')