Skip to content

Commit

Permalink
Merge pull request #693 from NextCenturyCorporation/restrictToOneTab
Browse files Browse the repository at this point in the history
MCS-1945 [Webenabled] Restrict to one tab
  • Loading branch information
rartiss55 authored Sep 14, 2023
2 parents dee7f72 + 520c0ad commit 9b0f7b4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
33 changes: 22 additions & 11 deletions webenabled/mcsweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def clean_request_data(request, is_json=False):
return data


def get_mcs_interface(request, label):
def get_mcs_interface(request, label, on_exit=False):
# Do we know who this is?
uniq_id_str = request.cookies.get("uniq_id")

Expand All @@ -63,14 +63,17 @@ def get_mcs_interface(request, label):
else:
app.logger.info("MCS interface is unavailable")

letters = string.ascii_lowercase
uniq_id_str = ''.join(random.choice(letters) for i in range(10))
app.logger.info(f"{label}: new user: {uniq_id_str}")
# skip for exit_unity route, since in that case, we don't
# need to start a new interface/controller if one isn't found
if (on_exit is False):
letters = string.ascii_lowercase
uniq_id_str = ''.join(random.choice(letters) for i in range(10))
app.logger.info(f"{label}: new user: {uniq_id_str}")

# Don't recognize, create new mcs interface
mcs_interface = MCSInterface(uniq_id_str)
mcs_interface.start_mcs()
session[uniq_id_str] = mcs_interface
# Don't recognize, create new mcs interface
mcs_interface = MCSInterface(uniq_id_str)
mcs_interface.start_mcs()
session[uniq_id_str] = mcs_interface

return mcs_interface, uniq_id_str

Expand Down Expand Up @@ -135,10 +138,18 @@ def handle_keypress():
@app.route("/exit_unity", methods=["POST"])
def exit_unity():
app.logger.info("=" * 30)
mcs_interface, unique_id = get_mcs_interface(request, "Exit Unity")
mcs_interface, unique_id = get_mcs_interface(
request, "Exit Unity", on_exit=True)
if mcs_interface is None:
app.logger.warn("Cannot load MCS interface")
return
error_msg = ("Cannot find MCS interface to exit. If you attempted "
"to quit on startup/initial page load, you will "
"likely have to kill the MCS controller process "
"manually.")
app.logger.warn(error_msg)
resp = jsonify(
error_msg=error_msg
)
return resp

controller_pid = mcs_interface.get_controller_pid()

Expand Down
36 changes: 33 additions & 3 deletions webenabled/templates/mcs_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
flex-direction: row;
}

#another-tab-err {
display: none;
flex-direction: column;
}

div#header h1 {
height: 80px;
line-height: 80px;
Expand Down Expand Up @@ -232,7 +237,13 @@
<div id="header">
<h1>Machine Common Sense</h1>
</div>
<div id="another-tab-err">
<div class="section">
<p>Error: It looks like MCS is already running in a separate tab. Please close this tab.</p>
</div>
</div>
<div id="content-wrapper">

<div class="left-panel column">
<div id="scene-nav-info" class="section">
<p><strong>Scenes</strong>
Expand Down Expand Up @@ -367,10 +378,25 @@ <h1>Machine Common Sense</h1>
"objectImageCoordsY": 200
}
var hasValidInput = true
var isMainWindow = true;
loadingSpinner(false)
loadController()
checkForExistingTab()

sceneSelect.addEventListener('change', select_scene, false);
function checkForExistingTab() {
let doesTabExist = localStorage.getItem("mainMcsTabIsOpen")

if(doesTabExist === null) {
localStorage.setItem("mainMcsTabIsOpen", true)

loadController()

sceneSelect.addEventListener('change', select_scene, false);
} else {
isMainWindow = false;
document.getElementById("another-tab-err").style.display = "flex"
document.getElementById("content-wrapper").style.display = "none"
}
}

function eventHandleScenes() {
sceneSelect = document.getElementById("scenes-dropdown");
Expand Down Expand Up @@ -654,9 +680,13 @@ <h1>Machine Common Sense</h1>


function cleanup(e) {
resp = fetch("{{url_for('exit_unity')}}",
let mainTabOpened = localStorage.getItem("mainMcsTabIsOpen")
if(mainTabOpened && isMainWindow) {
localStorage.clear()
resp = fetch("{{url_for('exit_unity')}}",
{method: 'POST', body: JSON.stringify(this.innerHTML)})
.then(parseJsonResponse)
}
}

window.addEventListener('beforeunload', this.cleanup, false);
Expand Down

0 comments on commit 9b0f7b4

Please sign in to comment.