diff --git a/app_ui.zip b/app_ui.zip new file mode 100644 index 0000000..ba31d68 Binary files /dev/null and b/app_ui.zip differ diff --git a/app_ui/__pycache__/app.cpython-38.pyc b/app_ui/__pycache__/app.cpython-38.pyc deleted file mode 100644 index 7c75719..0000000 Binary files a/app_ui/__pycache__/app.cpython-38.pyc and /dev/null differ diff --git a/app_ui/__pycache__/sensorCatalogueRegistration.cpython-38.pyc b/app_ui/__pycache__/sensorCatalogueRegistration.cpython-38.pyc deleted file mode 100644 index 7d10ebc..0000000 Binary files a/app_ui/__pycache__/sensorCatalogueRegistration.cpython-38.pyc and /dev/null differ diff --git a/app_ui/__pycache__/sensorInstanceRegistation.cpython-38.pyc b/app_ui/__pycache__/sensorInstanceRegistation.cpython-38.pyc deleted file mode 100644 index 8e2e8b2..0000000 Binary files a/app_ui/__pycache__/sensorInstanceRegistation.cpython-38.pyc and /dev/null differ diff --git a/app_ui/app.py b/app_ui/app.py index f122bc8..bd37a0b 100644 --- a/app_ui/app.py +++ b/app_ui/app.py @@ -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"]} @@ -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 @@ -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') @@ -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 @@ -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"): @@ -314,7 +300,7 @@ def scheduling_request(): # print(days) - data = { + data = { application_name : { "user_id" : current_user.username, "application_name" : application_name, @@ -342,10 +328,9 @@ 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 @@ -353,25 +338,24 @@ def validateJSON(json_path): 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] @@ -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(): @@ -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): @@ -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 @@ -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) @@ -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) \ No newline at end of file + # app.run(host="localhost",port=5005) diff --git a/app_ui/database.db b/app_ui/database.db index 1e75dc0..0c68fab 100644 Binary files a/app_ui/database.db and b/app_ui/database.db differ diff --git a/app_ui/getData.py b/app_ui/getData.py new file mode 100644 index 0000000..3dcd8bd --- /dev/null +++ b/app_ui/getData.py @@ -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) diff --git a/app_ui/models/__pycache__/Users.cpython-36.pyc b/app_ui/models/__pycache__/Users.cpython-36.pyc new file mode 100644 index 0000000..497800f Binary files /dev/null and b/app_ui/models/__pycache__/Users.cpython-36.pyc differ diff --git a/app_ui/models/__pycache__/__init__.cpython-36.pyc b/app_ui/models/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..4f15fb5 Binary files /dev/null and b/app_ui/models/__pycache__/__init__.cpython-36.pyc differ diff --git a/app_ui/sensorCatalogueRegistration.py b/app_ui/sensorCatalogueRegistration.py index b0edb73..277a1a9 100644 --- a/app_ui/sensorCatalogueRegistration.py +++ b/app_ui/sensorCatalogueRegistration.py @@ -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) diff --git a/app_ui/static/downloads/template.json b/app_ui/static/downloads/template.json index 0a3e90b..7838f3e 100644 --- a/app_ui/static/downloads/template.json +++ b/app_ui/static/downloads/template.json @@ -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" : {} } } } diff --git a/app_ui/static/uploads/sensor_catalogue_registration.json b/app_ui/static/uploads/sensor_catalogue_registration.json index d4fab83..27f0480 100644 --- a/app_ui/static/uploads/sensor_catalogue_registration.json +++ b/app_ui/static/uploads/sensor_catalogue_registration.json @@ -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"}} \ No newline at end of file +{"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"}} \ No newline at end of file diff --git a/app_ui/static/uploads/sensor_instance_registration.json b/app_ui/static/uploads/sensor_instance_registration.json new file mode 100644 index 0000000..16dfb70 --- /dev/null +++ b/app_ui/static/uploads/sensor_instance_registration.json @@ -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"}} \ No newline at end of file diff --git a/app_ui/templates/controller-notifications.html b/app_ui/templates/controller-notifications.html new file mode 100644 index 0000000..3256e20 --- /dev/null +++ b/app_ui/templates/controller-notifications.html @@ -0,0 +1,41 @@ +{% extends 'site_template.html' %} + +{% block body %} + + +