aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-08-02 02:26:03 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-08-02 02:26:03 +0200
commitf2ac15921368a5e421f4e42e4317aff7ee1c7a1e (patch)
treefc3e807b81001b54b6d0370a3c06429ef090312f /scripts
parent73bac9700939f9b9a05d195b30a834006ac95224 (diff)
downloaddotfiles-f2ac15921368a5e421f4e42e4317aff7ee1c7a1e.tar.gz
add mqtt_monitor_systemlock.sh
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" )