-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt-suspend-this-machine.sh
More file actions
executable file
·171 lines (141 loc) · 3.34 KB
/
mqtt-suspend-this-machine.sh
File metadata and controls
executable file
·171 lines (141 loc) · 3.34 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
MQTT_HOST="${HA_MQTT_HOST}"
MQTT_PASS="${HA_MQTT_PASSWORD:?Missing HA_MQTT_PASSWORD value}"
MQTT_USER="${HA_MQTT_USER:?Missing HA_MQTT_USER value}"
MQTT_TOPIC=""
compare_date_from_message() {
MSG_DATE_PART=$(get_date_part_from_message "$1")
if [[ "$MSG_DATE_PART" == "" ]]; then
echo "Failed to extract date part from message: $1."
exit 6
fi
DATE_NOW=$(date +%s)
MSG_DATE=$(date -d "$MSG_DATE_PART" +%s)
if [[ $DATE_NOW -le $MSG_DATE ]]; then
echo "Date in message is in the future."
return 1
else
return 0
fi
}
get_date_part_from_message() {
echo "$1" | grep -o " after .*" | cut -d " " -f 3
}
get_mqtt_message() {
mosquitto_sub \
--host "$MQTT_HOST" \
--topic "$MQTT_TOPIC" \
--username "$MQTT_USER" \
--pw "$MQTT_PASS" \
-C 1 \
-W 10
}
handle_suspend() {
IS_DELAY="$1"
if [ "$IS_DELAY" -ne 0 ]; then
AFTER_DATE="$(date -d "+10 minute" --iso-8601=minutes)"
publish_mqtt_message "suspend after $AFTER_DATE"
else
write_log "Suspending now."
sudo systemctl suspend
fi
}
print_usage() {
echo ""
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " -H, --host HOST MQTT host to connect to"
echo ""
echo " -t, --topic TOPIC MQTT topic to subscribe to"
}
publish_mqtt_message() {
write_log "Publishing message: $1"
mosquitto_pub \
--host "$MQTT_HOST" \
--topic "$MQTT_TOPIC" \
--username "$MQTT_USER" \
--pw "$MQTT_PASS" \
--message "$1" \
--qos 0 \
--retain
}
when_suspend_after() {
compare_date_from_message "$1"
COMPARE=$?
if [ $COMPARE -eq 1 ]; then
exit 0
fi
write_log "Suspend delay time elapsed."
handle_suspend 0
}
when_suspend_enabled() {
write_log "Suspend enabled. Setting delay."
handle_suspend 1
}
write_log() {
echo "$(date +%Y-%m-%dT%H:%M:%S) $1"
}
if ! command -v mosquitto_sub >/dev/null 2>&1 ; then
write_log "mosquitto_sub not found. Please install mosquitto-clients."
exit 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-H|--host)
MQTT_HOST="$2"
shift
shift
;;
-t|--topic)
MQTT_TOPIC="$2"
shift
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
write_log "Unknown option: $1"
print_usage
exit 2
;;
esac
done
if [[ "$MQTT_HOST" == "" ]]; then
write_log "Missing MQTT host."
print_usage
exit 2
fi
if [[ "$MQTT_TOPIC" == "" ]]; then
write_log "Missing MQTT topic."
print_usage
exit 2
fi
MSG="$(get_mqtt_message)"
write_log "Received message: $MSG"
if [ $? -ne 0 ]; then
write_log "Failed to subscribe to MQTT topic '$MQTT_TOPIC' on host '$MQTT_HOST'."
exit 3
fi
if [[ "$MSG" == "" ]]; then
write_log "No message received."
exit 4
fi
if [[ "$MSG" == disabled* ]]; then
write_log "Suspend disabled. Nothing to do."
exit 0
fi
if [[ "$MSG" == enabled* ]]; then
when_suspend_enabled
exit 0
fi
if [[ "$MSG" == "suspend after"* ]]; then
when_suspend_after "$MSG"
exit 0
fi
if [[ "$MSG" == * ]]; then
write_log "Unknown message: $MSG"
exit 5
fi