-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
141 lines (122 loc) · 4.48 KB
/
client.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
########################################################
#Client
########################################################
################ packages ##############################
import re
import os
import ast
import requests
import json
import csv
import collections
from random import randint, uniform
from flask_api import status
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import IntegrityError, OperationalError, SQLAlchemyError
from flask import Flask, jsonify, request, abort, redirect, url_for, session, Response, _request_ctx_stack, render_template
#################### init ###############################
app = Flask(__name__)
app.config['SECRET_KEY'] = "client api"
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
session_options = {'autocommit':False, 'autoflush':False}
ADMIN = "http://127.0.0.1:9999"
PORT = "5000"
################# function ######################
def get_location():
return [12.9331912453213, 77.54340132194]
# return [uniform(12.996800557961398, 12.91390750578042), uniform(77.45285487041474,77.59344553813935)]
################# apis ##########################
@app.route('/ping', methods=['POST'])
def getLocation():
req = request.get_json()
loc = get_location()
payload = {'location': loc}
return jsonify(payload)
@app.route('/')
def home():
return render_template('login.html')
@app.route('/processLogin', methods=['POST'])
def processLogin():
req = request.get_json()
device_id = req['client_id']
session['current_user'] = device_id
session['user_available'] = True
location = get_location()
payload = {
'device_id':device_id,
'port': PORT,
'location' : location
}
response = (requests.post(ADMIN+'/register', data = json.dumps(payload), headers = headers))
# print(response, type(response))
# ret = {}
# ret['redirecturl'] = '/dashboard'
# ret['msg'] = response['msg']
return jsonify({"status": "OK"})
@app.route('/dashboard', methods=['GET'])
def dashboard():
if(session['user_available']):
return render_template('client_dashboard.html')
return redirect(url_for('logout'))
@app.route('/updateDashboard', methods=['GET'])
def processDashboard():
if(session['user_available']):
location = get_location()
payload = {
'device_id': session['current_user'],
'port': PORT,
'location' : location
}
response = (requests.post(ADMIN+'/nearestGFS', data = json.dumps(payload), headers = headers)).json()
return jsonify(response['results'])
return redirect(url_for('logout'))
@app.route('/subscribe', methods=['POST'])
def subscribe():
if(session['user_available']):
req = request.get_json()
location = get_location()
payload = {
'device_id': session['current_user'],
'port': PORT,
'location' : location,
'geof_id' : req['geof_id']
}
response = (requests.post(ADMIN+'/subscribe', data = json.dumps(payload), headers = headers)).json()
return jsonify(response['msg'])
return redirect(url_for('logout'))
@app.route('/unsubscribe', methods=['POST'])
def unsubscribe():
if(session['user_available']):
req = request.get_json()
location = get_location()
payload = {
'device_id': session['current_user'],
'port': PORT,
'location' : location,
'geof_id' : req['geof_id'],
}
response = (requests.post(ADMIN+'/unsubscribe', data = json.dumps(payload), headers = headers)).json()
return jsonify(response['msg'])
return redirect(url_for('logout'))
@app.route('/partofGF', methods=['GET'])
def partofGF():
if(session['user_available']):
location = get_location()
payload = {
'device_id': session['current_user'],
'port': PORT,
'location' : location
}
response = (requests.post(ADMIN+'/partofGF', data = json.dumps(payload), headers = headers)).json()
return jsonify(response['result'])
return redirect(url_for('logout'))
@app.route('/logout')
def logout():
session.clear()
session['user_available'] = False
session['current_user'] = ""
return redirect(url_for('home'))
if __name__ == '__main__':
app.debug=True
app.run(port=int(PORT))