Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app_ui.zip
Binary file not shown.
Binary file removed app_ui/__pycache__/app.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
148 changes: 86 additions & 62 deletions app_ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from bson.json_util import dumps, loads
import sensorCatalogueRegistration as sensorFunc
import sensorInstanceRegistation as sensorInstanceFunc
import getData as gD

address_dict = {"Lapataganj" : { "lat" :"167","long" :"196"}}
building_dict = {"Lapataganj" : ["Gorisaria and grandsons Garments Group"]}
Expand Down Expand Up @@ -63,7 +64,7 @@ def login():
return render_template('login.html')

# get the form data
username = request.form['username']
username = request.form['username']
password = request.form['password']

remember_me = False
Expand Down Expand Up @@ -191,25 +192,10 @@ def fetch_place_id():
print(place_id_list)
return place_id_list

def fetch_applications():
application_details_collection = "test_zip_file_upload"
cluster = MongoClient(db_url)
db = cluster[db_name]
collection = db[application_details_collection]
result = collection_to_json(collection)
result = json.loads(result)
application_list = []
for row in result:
fname = row['filename']
fname = fname[:-4]
application_list.append(fname)
return application_list

@app.route('/schedule-application')
def blank_page():
place_id_list = fetch_place_id()
application = fetch_applications()
return render_template('schedule-application.html', user=current_user, place_id=place_id_list, applications = application)
return render_template('schedule-application.html', user=current_user, place_id=place_id_list)


@app.route('/profile')
Expand Down Expand Up @@ -257,7 +243,7 @@ def correct_phone(phone):
if(re.search(regex,phone)):
return True
else:
return False
return False

@app.route('/scheduling_request/', methods=["POST"])
@login_required
Expand Down Expand Up @@ -296,7 +282,7 @@ def scheduling_request():
# print(notify_user)

days = list()

if request.form.get("mon"):
days.append("Monday")
if request.form.get("tue"):
Expand All @@ -314,7 +300,7 @@ def scheduling_request():

# print(days)

data = {
data = {
application_name : {
"user_id" : current_user.username,
"application_name" : application_name,
Expand Down Expand Up @@ -342,36 +328,34 @@ def scheduling_request():
requests.post('http://scheduler:13337/schedule_request',json=data)
return render_template('index.html', user=current_user)

def validateJSON(json_path):
def validateJSON(jsonData):
try:
f = open(json_path)
data = json.load(f)
json.loads(jsonData)
except ValueError as err:
return False
return True

def check_format(uploaded_file, app_path, app_name, file_path):
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(app_path)

app_name = app_name[:-4]

# check src file, exists or not.
src_path = os.path.join(app_path, app_name, "src")
src_path = app_path+"/"+app_name+"/src"
if not os.path.exists(src_path):
return jsonify({"status" : "src folder missing"})


# check app_config.
config_filepath = os.path.join(app_path, app_name, "app_config.json")
config_filepath = app_path+"/"+app_name + "/app_config.json"
if not os.path.exists(config_filepath):
return jsonify({"status" : "app_config.json missing"})

# validate json
if not validateJSON(config_filepath):
return jsonify({"status" : "Invalid JSON file", "json_path": config_filepath})
return jsonify({"status" : "Invalid JSON file"})

return True

def create_connection_mongo_cloud():
cluster = MongoClient(db_url)
db = cluster[db_name]
Expand All @@ -380,11 +364,6 @@ def create_connection_mongo_cloud():
fs = GridFS(db2)
return fs, coll

def remove_file_uploads(app_path, app_name, file_path):
os.remove(file_path)
directory = 'rm -r' + os.path.join(app_path,app_name)
os.system(directory)

@app.route('/uploads/', methods=["POST"])
@login_required
def upload_file():
Expand All @@ -396,24 +375,28 @@ def upload_file():
if uploaded_file.filename != '':
file_path = os.path.join(app_path, app_name)
uploaded_file.save(file_path)

format_status = check_format(uploaded_file, app_path, app_name, file_path)

format_status = check_format(uploaded_file, app_path, app_name, file_path)
if format_status == True:
fs, coll = create_connection_mongo_cloud()
with open(file_path, "rb") as fp:
encoded = Binary(fp.read())
flink = fs.put(encoded, filename = app_name)
coll.insert_one({"filename": app_name, "file": flink })

os.remove(file_path)
return json.dumps({'status': 'Zip uploaded successfully'}), 200
pass
else:
remove_file_uploads(app_path, app_name, file_path)
return format_status

fs, coll = create_connection_mongo_cloud()

with open(file_path, "rb") as fp:
encoded = Binary(fp.read())
flink = fs.put(encoded, filename = app_name)

coll.insert_one({"filename": app_name, "file": flink })

os.remove(file_path)

return json.dumps({'status': 'Zip uploaded successfully'}), 200

#### end routes ####


# required function for loading the right user
@login_manager.user_loader
def load_user(id):
Expand Down Expand Up @@ -504,11 +487,13 @@ def get_json_template():
address_dict['floor'] = "None"
address_dict['room_no'] = "None"
sensor_catalogue_config["sensor_name"] = "None"
sensor_catalogue_config["sensor_type"] = "None"
sensor_catalogue_config["sensor_type_data_type"] = "None"
sensor_catalogue_config['sensor_geolocation'] = {"lat" : "None", "long" : "None" }
sensor_catalogue_config['sensor_address'] = address_dict
sensor_catalogue_config['sensor_data_storage_details'] = temp_dict
sensor_catalogue_config['sensor_api'] = "None"
sensor_catalogue_config['has_controller'] = "None"
sensor_catalogue_config['place_id'] = "None"
sensor_info['sensor_catalogue_config'] = sensor_catalogue_config
return sensor_info

Expand All @@ -518,14 +503,17 @@ def get_json_template():
def catalogue_registeration_request():
sensor_name = request.form['sensor_name']
sensor_type = request.form['sensor_type']
has_controller = request.form['sensor_controller']
print("SensorName = ",sensor_name)
print("SensorType = ",sensor_type)
print("Has Controller = ",has_controller)
sensor_info = {}
user = ""
sensor_info = get_json_template()
sensor_info["user_id"]=current_user.username
sensor_info["sensor_catalogue_config"]["sensor_name"] = sensor_name
sensor_info["sensor_catalogue_config"]["sensor_type"] = sensor_type
sensor_info["sensor_catalogue_config"]["sensor_type_data_type"] = sensor_type
sensor_info["sensor_catalogue_config"]["has_controller"] = has_controller
filepath = 'static/uploads/'+'sensor_catalogue_registration.json'
with open(filepath,'w') as f:
json.dump(sensor_info,f)
Expand All @@ -534,42 +522,78 @@ def catalogue_registeration_request():
# requests.post('http://127.0.0.1:5005/sensorCatalogueRegistration/addSensorType',json=sensor_info)
return render_template('index.html', user=current_user)

def update_attr(sensor_reg_info, attr, value):
if value != '':
sensor_reg_info["sensor_address"][attr] = value
else:
sensor_reg_info["sensor_address"][attr] = "None"
return sensor_reg_info

@app.route("/sensorsSelect" , methods=['GET', 'POST'])
def test():
select_sensor = request.form.get('comp_select')
select_area = request.form.get('area_select')
select_building = request.form.get('building_select')
select_floor = request.form.get('floor_select')
select_room = request.form.get('room_select')
select_area = request.form['area']
select_building = request.form['building']
select_floor = request.form['floor']
select_room = request.form['room']
place_id = request.form['place_id']
lat = request.form['lat']
long = request.form['long']
sensor_info = get_json_template()
sensor_data = sensorInstanceFunc.get_sensor_info(select_sensor)
print('sensor_data - ')
print(sensor_data)
sensor_reg_info = sensor_info["sensor_catalogue_config"]
sensor_reg_info["place_id"] = "GGG_"+select_floor+"_"+select_room
sensor_reg_info["place_id"] = place_id
sensor_reg_info["sensor_name"] = sensor_data["sensor_name"]
sensor_reg_info["sensor_type"] = sensor_data["sensor_type"]
sensor_reg_info["sensor_geolocation"] = address_dict[select_area]
sensor_reg_info["sensor_address"]["area"] = select_area
sensor_reg_info["sensor_address"]["building"] = select_building
sensor_reg_info["sensor_address"]["floor"] = select_floor
sensor_reg_info["sensor_address"]["room_no"] = select_room
sensor_reg_info["sensor_type_data_type"] = sensor_data["sensor_type_data_type"]
sensor_reg_info["has_controller"] = sensor_data["has_controller"]
if lat != '':
sensor_reg_info["sensor_geolocation"]["lat"] = lat
else:
sensor_reg_info["sensor_geolocation"]["lat"] = "167"
if long != '':
sensor_reg_info["sensor_geolocation"]["long"] = long
else:
sensor_reg_info["sensor_geolocation"]["long"] = "196"
sensor_reg_info = update_attr(sensor_reg_info,"area",select_area)
sensor_reg_info = update_attr(sensor_reg_info,"building",select_building)
sensor_reg_info = update_attr(sensor_reg_info,"floor",select_floor)
sensor_reg_info = update_attr(sensor_reg_info,"room_no",select_room)
sensor_reg_info["sensor_data_storage_details"]["kafka"]["broker_ip"] = sensor_data["user_id"]
sensor_reg_info["sensor_data_storage_details"]["kafka"]["topic"] = "abc"
sensor_reg_info["sensor_data_storage_details"]["kafka"]["topic"] = "None"
sensor_json = {}
sensor_json["user_id"] = current_user.username
sensor_json["sensor_reg_config"] = sensor_reg_info
filepath = 'static/uploads/'+'sensor_instance_registration.json'
print('Sensor_json file - ',sensor_info)
with open(filepath,'w') as f:
json.dump(sensor_json,f)

results= sensorInstanceFunc.fun2(sensor_json)
return render_template('index.html', user=current_user)

@app.route('/user-notifications', methods=["GET","POST"])
def user_notifications():
return render_template('user-notifications.html', user=current_user)

@app.route('/print_user_notifications', methods=["GET","POST"])
def print_user_notifications():
result = gD.getData("IAS_test_1", "sensor_catalogue")
return result

@app.route('/controller-notifications', methods=["GET","POST"])
def controller_notifications():
return render_template('controller-notifications.html', user=current_user)

@app.route('/print_controller_notifications', methods=["GET","POST"])
def print_controller_notifications():
result = gD.getData("IAS_test_1", "sensor_catalogue")
return result

if __name__ == "__main__":
# change to app.run(host="0.0.0.0"), if you want other machines to be able to reach the webserver.
# db.create_all()
# call apurva's api and
# call apurva's api and
app.run(port=9999, threaded=True, host='0.0.0.0')
# app.run(host="localhost",port=5005)
# app.run(host="localhost",port=5005)
Binary file modified app_ui/database.db
Binary file not shown.
27 changes: 27 additions & 0 deletions app_ui/getData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

from pymongo import MongoClient
from bson.json_util import dumps, loads


dburl = "mongodb://apurva:user123@cluster0-shard-00-00.p4xv2.mongodb.net:27017,cluster0-shard-00-01.p4xv2.mongodb.net:27017,cluster0-shard-00-02.p4xv2.mongodb.net:27017/IAS_test_1?ssl=true&replicaSet=atlas-auz41v-shard-0&authSource=admin&retryWrites=true&w=majority"
# db_name = "IAS_test_1"
# collection_name = "sensor_catalogue"

def collection_to_json(col):

cursor = col.find()
list_cur = list(cursor)
json_data = dumps(list_cur)
return json_data

def getData(db_name, collection_name):
cluster = MongoClient(dburl)
db = cluster[db_name]
collection = db[collection_name]
result = collection_to_json(collection)
return result



# result = getData("IAS_test_1", "sensor_catalogue")
# print('Result = ', result)
Binary file added app_ui/models/__pycache__/Users.cpython-36.pyc
Binary file not shown.
Binary file added app_ui/models/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions app_ui/sensorCatalogueRegistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def fun2(data):
return msg
user_id = incoming_data["user_id"]
config = incoming_data["sensor_catalogue_config"]
print("Config before deleting - ",config)
config = remove_unnecessary_data(config)
print("Config - ",config)
cluster = MongoClient(dburl)
Expand Down
43 changes: 32 additions & 11 deletions app_ui/static/downloads/template.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
{
"myapp" : {
"user_id" : "divyansh",
"application_name" : "Application1",
"user_id" : "",
"application_name" : "",
"algorithms" : {
"algo1" : {
"isScheduled" : false,
"algorithm1" : {
"isScheduled" : true,
"schedule" : {
"time" : {
"startTimes" : ["00:45:00", "00:45:15"],
"durations" : ["00:00:20", "00:00:15"]
"startTimes" : ["", ""],
"durations" : ["", ""]
},
"days" : ["Monday", "Thursday", "Friday", "Saturday", "Sunday"]
"days" : ["Thursday", "lorem ipsum"]
},
"action" : {
"user_display" : "Results",
"sensor_manager" : [{"sensor_id1" : "command1"}],
"notify_user" : []
"user_display" : "lorem ipsum",
"sensor_manager" : [{"sensor_id1" : "lorem ipsum"}],
"notify_user" : {}
},
"sensors": {
"sensor1" : {
"latitude" : "1234",
"longitude" : "1234"
},
"sensor2" : {
"latitude" : "12",
"longitude" : "122"
}
}
},
"algorithm2" : {
"isScheduled" : false,
"schedule" : {
"time" : {
"startTimes" : ["13:15:20"],
"durations" : ["00:00:08"]
},
"days" : ["Monday", "Thursday", "Friday", "Saturday", "Sunday"]
},
"place_id" : "GGG_ground_floor_1"
"action" : {},
"sensors" : {}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app_ui/static/uploads/sensor_catalogue_registration.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"user_id": "abhishek_2", "sensor_catalogue_config": {"sensor_name": "Temp1", "sensor_type": "Temperature", "sensor_geolocation": {"lat": "None", "long": "None"}, "sensor_address": {"area": "None", "building": "None", "floor": "None", "room_no": "None"}, "sensor_data_storage_details": {"kafka": {"broker_ip": "None", "topic": "None"}, "mongo_db": {"ip": "None", "port": "None", "passwd": "None", "document_name": "None"}}, "sensor_api": "None"}}
{"user_id": "shreyaIas20", "sensor_catalogue_config": {"sensor_name": "temp_sensor4", "sensor_type_data_type": "int", "sensor_geolocation": {"lat": "None", "long": "None"}, "sensor_address": {"area": "None", "building": "None", "floor": "None", "room_no": "None"}, "sensor_data_storage_details": {"kafka": {"broker_ip": "None", "topic": "None"}, "mongo_db": {"ip": "None", "port": "None", "passwd": "None", "document_name": "None"}}, "sensor_api": "None", "has_controller": "yes", "place_id": "None"}}
1 change: 1 addition & 0 deletions app_ui/static/uploads/sensor_instance_registration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"user_id": "shreyaIas20", "sensor_reg_config": {"sensor_name": "stitch_setting", "sensor_type_data_type": "int", "sensor_geolocation": {"lat": "3", "long": "5"}, "sensor_address": {"area": "None", "building": "None", "floor": "None", "room_no": "None"}, "sensor_data_storage_details": {"kafka": {"broker_ip": "apurva", "topic": "None"}, "mongo_db": {"ip": "None", "port": "None", "passwd": "None", "document_name": "None"}}, "sensor_api": "None", "has_controller": "yes", "place_id": "place_40"}}
Loading