This repository has been archived by the owner on Apr 2, 2022. It is now read-only.
forked from Amansinghtech/Flask-IoT-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArms.py
333 lines (270 loc) · 10.7 KB
/
Arms.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from flask import Flask, render_template, jsonify, redirect, request
import json, database, base64
from random import choice
from datetime import datetime
import person
import os, binascii
app = Flask(__name__)
logged_in = {}
api_loggers = {}
mydb = database.db('aman', '192.168.56.102', 'hacker123', 'ARMS')
#test api key aGFja2luZ2lzYWNyaW1lYXNmc2FmZnNhZnNhZmZzYQ==
@app.route("/login", methods=['GET', 'POST'])
def login():
error = ""
if request.method == 'POST':
user = person.user(request.form['username'], request.form['password'])
if user.authenticated:
user.session_id = str(binascii.b2a_hex(os.urandom(15)))
logged_in[user.username] = {"object": user}
return redirect('/overview/{}/{}'.format(request.form['username'], user.session_id))
else:
error = "invalid Username or Passowrd"
return render_template('Login.htm', error=error)
#this links is for device 1
@app.route('/device1/<string:username>/<string:session>', methods=["GET", "POST"])
def Dashoboard():
user = {
"username" : "Aman Singh",
"image":"static/images/amanSingh.jpg"
}
devices = [
{"Dashboard" : "device1",
"deviceID": "Device1"
}
]
return render_template('device_dashboard.htm', title='Dashobard', user=user, devices=devices)
#this link is for the main dashboard of the website
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('home.htm', title='HOME - Landing Page')
@app.route('/overview/<string:username>/<string:session>', methods=['GET', 'POST'])
def overview(username, session):
global logged_in
if username in logged_in and (logged_in[username]['object'].session_id == session):
user = {
"username" : username,
"image":"/static/images/amanSingh.jpg",
"api": logged_in[username]["object"].api,
"session" : session
}
devices = [
{"Dashboard" : "device1",
"deviceID": "Device1"
}
]
return render_template('overview.htm', title='Overview', user=user, devices=devices)
else:
return redirect('/login')
#this location will get to the api setting
@app.route('/apisettings/<string:username>/<string:session>', methods=['GET', 'POST'])
def apisettings(username, session):
global logged_in
if username in logged_in and (logged_in[username]['object'].session_id == session):
user = {
"username" : username,
"image":"/static/images/amanSingh.jpg",
"api": logged_in[username]["object"].api,
"session" : session
}
devices = [
{"Dashboard" : "device1",
"deviceID": "Device1"
}
]
return render_template('api_settings.htm', title='API-Settings', user=user, devices=devices)
else:
return redirect('/login')
#this part is for the profile view
@app.route('/profile/<string:username>/<string:session>', methods=['GET', 'POST'])
def profile(username, session):
global logged_in
if username in logged_in and (logged_in[username]['object'].session_id == session):
user = {
"username" : username,
"image":"/static/images/amanSingh.jpg",
"api": logged_in[username]["object"].api,
"session" : session,
"firstname": logged_in[username]["object"].first,
"lastname": logged_in[username]["object"].last,
"email":logged_in[username]["object"].email,
"phone":logged_in[username]["object"].phone,
"lastlogin":logged_in[username]["object"].last_login,
}
devices = [
{"Dashboard" : "device1",
"deviceID": "ARMS12012"
}
]
return render_template('profile.htm', title='API-Settings', user=user, devices=devices)
else:
return redirect('/login')
@app.route('/logout/<string:username>/<string:session>', methods=['GET', 'POST'])
def logout(username, session):
global logged_in
if username in logged_in and (logged_in[username]['object'].session_id == session):
logged_in.pop(username)
# print("logged out")
return redirect('/')
else:
return redirect('/login')
#this is the testing for api
@app.route("/api/<string:apikey>/test", methods=["GET", "POST"])
def apitest (apikey):
return {"data":"working Fine Connected to the api server"}
#get all the devices information from the user
@app.route("/api/<string:apikey>/listdevices", methods=['GET', 'POST'])
def listdevices(apikey):
global api_loggers
global mydb
if not(apikey in api_loggers):
try:
query = "select username from users where api_key = '{}'".format(apikey)
mydb.cursor.execute(query)
username = mydb.cursor.fetchall()
username = username[0][0]
apiuser = person.user(username, "dummy")
apiuser.authenticated = True
devices_list = apiuser.get_devices()
api_loggers[apikey] = {"object" : apiuser}
return jsonify(devices_list)
except Exception as e:
print (e)
return jsonify({"data":"Oops Looks like api is not correct"})
else:
data = api_loggers[apikey]["object"].get_devices()
return jsonify (data)
randlist = [i for i in range(0, 100)]
@app.route('/api/<string:apikey>/deviceinfo/<string:deviceID>', methods=['GET', 'POST'])
def device_info (apikey, deviceID):
global api_loggers
global mydb
if not(apikey in api_loggers):
try:
query = "select username from users where api_key = '{}'".format(apikey)
mydb.cursor.execute(query)
username = mydb.cursor.fetchall()
username = username[0][0]
apiuser = person.user(username, "dummy")
apiuser.authenticated = True
data = apiuser.dev_info(deviceID)
api_loggers[apikey] = {"object" : apiuser}
#this part is hard coded so remove after fixing the issue
data = list(data)
data[2] = "Rosegarden"
return jsonify(data)
except Exception as e:
print (e)
return jsonify({"data":"Oops Looks like api is not correct"})
else:
data = api_loggers[apikey]["object"].dev_info(deviceID)
#this part is hard coded so remove after fixing the issue
data = list(data)
data[2] = "Rosegarden"
return jsonify (data)
@app.route('/api/<string:apikey>/fieldstat/<string:fieldname>', methods=['GET', 'POST'])
def fieldstat (apikey, fieldname):
global api_loggers
global mydb
if not(apikey in api_loggers):
try:
query = "select username from users where api_key = '{}'".format(apikey)
mydb.cursor.execute(query)
username = mydb.cursor.fetchall()
username = username[0][0]
apiuser = person.user(username, "dummy")
apiuser.authenticated = True
data = apiuser.field_values(fieldname)
api_loggers[apikey] = {"object" : apiuser}
return jsonify(data)
except Exception as e:
print (e)
return jsonify({"data":"Oops Looks like api is not correct"})
else:
data = api_loggers[apikey]["object"].field_values(fieldname)
return jsonify (data)
@app.route('/api/<string:apikey>/devicestat/<string:fieldname>/<string:deviceID>', methods=['GET', 'POST'])
def devicestat (apikey, fieldname, deviceID):
global api_loggers
global mydb
if not(apikey in api_loggers):
try:
query = "select username from users where api_key = '{}'".format(apikey)
mydb.cursor.execute(query)
username = mydb.cursor.fetchall()
username = username[0][0]
apiuser = person.user(username, "dummy")
apiuser.authenticated = True
data = apiuser.device_values(fieldname, deviceID)
api_loggers[apikey] = {"object" : apiuser}
return jsonify(data)
except Exception as e:
print (e)
return jsonify({"data":"Oops Looks like api is not correct"})
else:
data = api_loggers[apikey]["object"].device_values(fieldname, deviceID)
return jsonify (data)
@app.route('/api/<string:apikey>/update/<string:data>', methods=['GET','POST'])
def update_values(apikey, data):
global mydb
try:
data = decode(data)
output = mydb.get_apikeys()
if apikey in output:
if (len(data) == 6) and (type(data) is list):
fieldname = data[0]
deviceID = data[1]
temp = data[2]
humidity = data[3]
moisture = data[4]
light = data[5]
mydb.update_values(apikey, fieldname, deviceID, temp, humidity, moisture, light)
return ("Values Updated")
else:
return "Data Decoding Error!"
else:
return "Api key invalid"
except Exception as e:
print (e)
return jsonify({"data":"Oops Looks like api is not correct"})
@app.route("/api/<string:apikey>/temperature", methods=["GET", "POST"])
def get_temperature(apikey):
randData = choice(randlist)
time = datetime.now()
time = time.strftime("%H:%M:%S")
response = [time, randData]
return jsonify(response)
@app.route("/api/<string:apikey>/moisture", methods=["GET", "POST"])
def get_moisture(apikey):
randData = choice(randlist)
time = datetime.now()
time = time.strftime("%H:%M:%S")
response = [time, randData]
return jsonify(response)
@app.route("/api/<string:apikey>/humidity", methods=["GET", "POST"])
def get_humidity(apikey):
randData = choice(randlist)
time = datetime.now()
time = time.strftime("%H:%M:%S")
response = [time, randData]
return jsonify(response)
@app.route("/api/<string:apikey>/light", methods=["GET", "POST"])
def get_light(apikey):
randData = choice(randlist)
time = datetime.now()
time = time.strftime("%H:%M:%S")
response = [time, randData]
return jsonify(response)
def encode(data):
data = json.dumps(data)
message_bytes = data.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')
return base64_message
def decode(base64_message):
base64_bytes = base64_message.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')
return json.loads(message)
if __name__ == "__main__":
app.run(host="0.0.0.0", port = "80", debug=True)