From c0a471877875403bb3f4036bcdba0350a3fee857 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Wed, 3 Jul 2019 22:54:03 +0200 Subject: [PATCH] print a warning when locking/unlocking via SSH when door is already locked/unlocked --- opt/afra_door/close.py | 8 +++++++- opt/afra_door/lock_server.js | 20 +++++++++++++++++--- opt/afra_door/open.py | 8 +++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/opt/afra_door/close.py b/opt/afra_door/close.py index 6f6a3b1..a01bc56 100755 --- a/opt/afra_door/close.py +++ b/opt/afra_door/close.py @@ -5,4 +5,10 @@ shared_secret = f.read() print("Locking door...") -requests.get("http://127.0.0.1:8001/lock?shared_secret={}".format(shared_secret)) + +# Try to lock door +response = requests.get("http://127.0.0.1:8001/lock?shared_secret={}".format(shared_secret)) + +if response.status_code != 200: + print(response.text) + diff --git a/opt/afra_door/lock_server.js b/opt/afra_door/lock_server.js index 9d41c32..dcf48cb 100755 --- a/opt/afra_door/lock_server.js +++ b/opt/afra_door/lock_server.js @@ -78,15 +78,29 @@ http.createServer(function (req, res) { if (q.searchParams.get("shared_secret") === shared_secret){ if (q.pathname === "/unlock"){ - res.write("Opening lock") + if (status_door() === "UNLOCKED") { + // Status code 409 CONFLICT + res.statusCode = 409; + res.write("Door is already UNLOCKED! Try to unlock anyway..."); + } + else { + res.write("Opening lock...") + } unlock_door() } else if (q.pathname === "/lock"){ - res.write("Closing lock") + if (status_door() === "LOCKED") { + // Status code 409 CONFLICT + res.statusCode = 409; + res.write("Door is already LOCKED! Try to lock anyway..."); + } + else { + res.write("Closing lock...") + } lock_door() } else if (q.pathname === "/toggle"){ - res.write("Toggling lock (old status: " + status_door() + ")"); + res.write("Toggling lock (current status: " + status_door() + ")"); toggle_door(); } } diff --git a/opt/afra_door/open.py b/opt/afra_door/open.py index 73a1261..6e9d35e 100755 --- a/opt/afra_door/open.py +++ b/opt/afra_door/open.py @@ -5,4 +5,10 @@ shared_secret = f.read() print("Welcome to AfRA, opening door...") -requests.get("http://127.0.0.1:8001/unlock?shared_secret={}".format(shared_secret)) + +# Try to unlock door +response = requests.get("http://127.0.0.1:8001/unlock?shared_secret={}".format(shared_secret)) + +if response.status_code != 200: + print(response.text) +