server.py 8.91 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

Vladislav Rykov's avatar
Vladislav Rykov committed
4
import dao.user.user as ud
Vladislav Rykov's avatar
Vladislav Rykov committed
5
import dao.application.application as ad
6
import dao.device.device as dd
Vladislav Rykov's avatar
Vladislav Rykov committed
7
import dao.pend.pend as pend
Vladislav Rykov's avatar
Vladislav Rykov committed
8
import dao.data.data as data
Vladislav Rykov's avatar
Vladislav Rykov committed
9
10
11

import misc

12
import os
Vladislav Rykov's avatar
Vladislav Rykov committed
13

14

15
APP_KEY_LEN = 8
Vladislav Rykov's avatar
Vladislav Rykov committed
16
DATA_DOWNLOAD_DIR = 'data'
17
18

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

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

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



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

        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
48
            res = ud.create(username, password)
Vladislav Rykov's avatar
Vladislav Rykov committed
49
50
            if (not res[0]):
                return render_template('signup.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
51
52
            else:
                session['name'] = username
53
        
Vladislav Rykov's avatar
Vladislav Rykov committed
54
                return redirect(url_for('index'))
55

56
57
58


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

        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
70
            res = ud.get(username, password)
Vladislav Rykov's avatar
Vladislav Rykov committed
71
72
            if (not res[0]):
                return render_template('login.html', feedback=msg[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
73
74
            else:
                session['name'] = username
Vladislav Rykov's avatar
Vladislav Rykov committed
75
        
Vladislav Rykov's avatar
Vladislav Rykov committed
76
77
78
                return redirect(url_for('index'))


79
80

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


86
87
88

@server.route('/new-app')
def new_application():
Vladislav Rykov's avatar
Vladislav Rykov committed
89
90
91
92
    if 'name' in session:
        return render_template('new-app.html')
    else:
        return redirect(url_for('index'))
93
94
95
96
97



@server.route('/app', methods=['GET', 'POST'])
def app():
Vladislav Rykov's avatar
Vladislav Rykov committed
98
99
    if 'name' in session:
        if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
100
            
Vladislav Rykov's avatar
Vladislav Rykov committed
101
            session['appkey'] = request.args.get('appkey')
Vladislav Rykov's avatar
Vladislav Rykov committed
102

Vladislav Rykov's avatar
Vladislav Rykov committed
103
            app = ad.get(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
104
            devs = dd.get_list(app[1][1])
Vladislav Rykov's avatar
Vladislav Rykov committed
105
        
Vladislav Rykov's avatar
Vladislav Rykov committed
106
107
108
109
110
111
112
113
114
            try:
                filelist = [f for f in os.listdir(DATA_DOWNLOAD_DIR) if f.startswith(session['appkey'])]
                for f in filelist:
                    os.remove(DATA_DOWNLOAD_DIR+'/'+f)
            except OSError:
                pass

           # print('devs : ', devs)
            return render_template('app.html', app=app[1], devs=devs[1])
115
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
116
117
118
119
120
            if request.form['appname'] == '':
                error = 'Application name cannot be empty.'
                return render_template('new-app.html', feedback=error)
            else:
                appkey = misc.rand_str(APP_KEY_LEN)
Vladislav Rykov's avatar
Vladislav Rykov committed
121
                res = ad.create(request.form['appname'], appkey, session['name'], request.form['appdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
122
            
Vladislav Rykov's avatar
Vladislav Rykov committed
123
124
                if not res[0]:
                    return render_template('new-app.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
125
            
Vladislav Rykov's avatar
Vladislav Rykov committed
126
                res = dd.create_table(appkey)
127
            
Vladislav Rykov's avatar
Vladislav Rykov committed
128
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
129
                    ad.delete(appkey)
Vladislav Rykov's avatar
Vladislav Rykov committed
130
                    return render_template('new-app.html', feedback=res[1])
131
            
Vladislav Rykov's avatar
Vladislav Rykov committed
132
133
134
                return redirect(url_for('index'))
    else:
        return redirect(url_for('index'))
135

136
137
@server.route('/delete-app')
def delete_app():
Vladislav Rykov's avatar
Vladislav Rykov committed
138
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
139
        devs = dd.get_list(session['appkey'])
140
    
Vladislav Rykov's avatar
Vladislav Rykov committed
141
142
        for dev in devs[1]:
            data.delete_table(session['appkey'], dev[1])
143
    
Vladislav Rykov's avatar
Vladislav Rykov committed
144
        dd.delete_table(session['appkey'])
145
    
Vladislav Rykov's avatar
Vladislav Rykov committed
146
        res = ad.delete(session['appkey'])
147
    
Vladislav Rykov's avatar
Vladislav Rykov committed
148
149
150
151
        if not res[0]:
            return redirect(url_for('app'))
        else:
            return redirect(url_for('index'))
152
153
    else:
        return redirect(url_for('index'))
154

Vladislav Rykov's avatar
Vladislav Rykov committed
155

Vladislav Rykov's avatar
Vladislav Rykov committed
156
157
@server.route('/add-dev')
def new_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
158
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
159
        dev_list = dd.get_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
160
    
Vladislav Rykov's avatar
Vladislav Rykov committed
161
    #print('dev list : ', dev_list)
Vladislav Rykov's avatar
Vladislav Rykov committed
162

Vladislav Rykov's avatar
Vladislav Rykov committed
163
164
165
166
        if not dev_list[0]:
            return render_template('add-dev.html', feedback=dev_list[1])
        else:
            return render_template('add-dev.html', free_ids=misc.prep_id_range(dev_list[1]))
Vladislav Rykov's avatar
Vladislav Rykov committed
167
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
168
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
169
170
171
172
173
 


@server.route('/dev', methods=['GET', 'POST'])
def dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
174
175
    if 'name' in session:
        if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
176
            dev = dd.get(session['appkey'], request.args.get('id'))
Vladislav Rykov's avatar
Vladislav Rykov committed
177

Vladislav Rykov's avatar
Vladislav Rykov committed
178
179
            session['devid'] = dev[1][1]
            session['devname'] = dev[1][0]
Vladislav Rykov's avatar
Vladislav Rykov committed
180
        
Vladislav Rykov's avatar
Vladislav Rykov committed
181
            last = data.get_last_n(session['appkey'], session['devid'], 1)
Vladislav Rykov's avatar
Vladislav Rykov committed
182
        
Vladislav Rykov's avatar
Vladislav Rykov committed
183
            ltup = 'Device have not sent data yet'
Vladislav Rykov's avatar
Vladislav Rykov committed
184

Vladislav Rykov's avatar
Vladislav Rykov committed
185
186
            if last[0]:
                ltup = last[1][0][1]
187

Vladislav Rykov's avatar
Vladislav Rykov committed
188
            return render_template('dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
Vladislav Rykov's avatar
Vladislav Rykov committed
189
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
190
            res = dd.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
191

192
193
194
            if not res[0]:
                return render_template('add-dev.html', feedback=res[1])
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
195
196
197
                res = data.create_table(session['appkey'], request.form['devid'])
            
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
198
                    dd.delete(session['appkey'], request.form['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
199
200
201
202
203
                    return render_template('add-dev.html', feedback=res[1])
                else:
                    return redirect(url_for('app', appkey=session['appkey']))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
204
205


Vladislav Rykov's avatar
Vladislav Rykov committed
206
207
@server.route('/dev-conf', methods=['GET', 'POST'])
def dev_conf():
Vladislav Rykov's avatar
Vladislav Rykov committed
208
209
210
211
    if 'name' in session and 'devid' in session:
        if request.method == 'GET':
            return render_template('dev-conf.html', devname=session['devname'])
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
212
        
Vladislav Rykov's avatar
Vladislav Rykov committed
213
214
215
216
            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
217
        
Vladislav Rykov's avatar
Vladislav Rykov committed
218
219
220
221
222
            bstr = bytes(request.form['arg'])
            i = 0
            while i < argslen - 1:
                args[2+i] = bstr[i]
                i += 1
Vladislav Rykov's avatar
Vladislav Rykov committed
223

Vladislav Rykov's avatar
Vladislav Rykov committed
224
            base64_args = binascii.b2a_base64(args).decode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
225

Vladislav Rykov's avatar
Vladislav Rykov committed
226
            pend.create(session['appkey'], session['devid'], base64_args)
Vladislav Rykov's avatar
Vladislav Rykov committed
227
228
229
230
231

        #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
232
        
Vladislav Rykov's avatar
Vladislav Rykov committed
233
234
235
            return redirect(url_for('dev', id=session['devid']))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
236

Vladislav Rykov's avatar
Vladislav Rykov committed
237

Vladislav Rykov's avatar
Vladislav Rykov committed
238
239
@server.route('/delete-dev')
def delete_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
240
241
    if 'name' in session and 'devid' in session:
        data.delete_table(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
242
        res = dd.delete(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
243

Vladislav Rykov's avatar
Vladislav Rykov committed
244
245
246
        return redirect(url_for('app', appkey=session['appkey']))
    else:
        return redirect(utl_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
247
248


Vladislav Rykov's avatar
Vladislav Rykov committed
249
250
@server.route('/dev-data')
def dev_data():
Vladislav Rykov's avatar
Vladislav Rykov committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    if 'name' in session and 'devid' in session:
        last = data.get_last_n(session['appkey'], session['devid'], 10)  
        count = data.get_count(session['appkey'], session['devid'])

        last_ctr = 10
        if count[1][0] < 10:
            last_ctr = count[1][0]

        #print(last)
        #print(count)
        if count[1][0] > 0:
            return render_template('dev-data.html', data=last[1], total=count[1][0], lastctr=last_ctr, devname=session['devname'])
        else:
            return render_template('dev-data.html', devname=session['devname'])
Vladislav Rykov's avatar
Vladislav Rykov committed
265
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
266
        return redirect(utl_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
267

Vladislav Rykov's avatar
Vladislav Rykov committed
268
269
@server.route('/data-csv')
def data_csv():
Vladislav Rykov's avatar
Vladislav Rykov committed
270
271
    if 'name' in session and 'devid' in session:
        dumpd = data.get_all(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
272

Vladislav Rykov's avatar
Vladislav Rykov committed
273
        fn = session['appkey']+ '_' +str(session['devid'])+ '.csv'
Vladislav Rykov's avatar
Vladislav Rykov committed
274

Vladislav Rykov's avatar
Vladislav Rykov committed
275
276
277
        with open(DATA_DOWNLOAD_DIR+'/'+fn, 'w') as f: 
            for d in dumpd[1][0][2]:
                f.write(d)
Vladislav Rykov's avatar
Vladislav Rykov committed
278
279
                f.write(',')
            f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
280
281
282
283
284
285
        
            for row in dumpd[1]:
                for v in row[2]:
                    f.write(str(row[2][v]))
                    f.write(',')
                f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
286
    
Vladislav Rykov's avatar
Vladislav Rykov committed
287
288
289
        return send_from_directory(DATA_DOWNLOAD_DIR, fn, as_attachment=True)
    else:
        return redirect(utl_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
290
291
        

292
if __name__ == '__main__':
293
294
295
296
    server.secret_key = 'sdjfklsjf^$654sd^#sPH'
    server.run(debug = True, host='0.0.0.0')