aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/mqtt_monitor_systemlock.sh42
1 files changed, 42 insertions, 0 deletions
diff --git a/scripts/mqtt_monitor_systemlock.sh b/scripts/mqtt_monitor_systemlock.sh
new file mode 100755
index 0000000..92ce987
--- /dev/null
+++ b/scripts/mqtt_monitor_systemlock.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# Automatically lock/unlock system with bluetooth based presence detection
+# using monitor on raspi and mosquitto MQTT client
+#
+# ./mqtt_monitor_systemlock.sh [BROKER] [TOPIC]
+
+
+readonly THRESHHOLD=50
+LAST_CONFIDENCE=100
+
+readonly DEPENDS=( mosquitto_sub jq xset xsecurelock )
+for prog in "${DEPENDS[@]}"; do
+ if ! which $prog > /dev/null 2>&1; then
+ echo $prog not found
+ exit 1
+ fi
+done
+
+while read msg; do
+ if ! CONFIDENCE=$(jq -r ".confidence" <<< "$msg"; exit $?); then
+ echo JQ parse error.
+ exit 1
+ fi
+
+ if [ $CONFIDENCE -lt $THRESHHOLD -a $LAST_CONFIDENCE -ge $THRESHHOLD ]; then
+ echo [$(date +%d.%m.%Y\ %H:%M)] OFF
+
+ XSECURELOCK_PASSWORD_PROMPT=kaomoji xsecurelock 2> /dev/null &
+ LOCK_PID=$!
+
+ xset dpms force standby
+ elif [ $CONFIDENCE -gt $THRESHHOLD -a $LAST_CONFIDENCE -le $THRESHHOLD ]; then
+ echo [$(date +%d.%m.%Y\ %H:%M)] ON
+
+ kill $LOCK_PID
+
+ xset dpms force on
+ fi
+
+ LAST_CONFIDENCE=$CONFIDENCE
+done < <( mosquitto_sub -h "$1" -t "$2" )