aboutsummaryrefslogtreecommitdiff
path: root/scripts/mqtt_monitor_systemlock.sh
blob: 92ce98744bb6ef13b3c2f5b4b5dde81fb13c7349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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" )