main control script

This commit is contained in:
justmarvinn 2025-06-08 12:58:12 +02:00
parent 75b816eba9
commit b88dfa1bb9

213
usr/bin/t3hs/t3hs_sim800 Executable file
View File

@ -0,0 +1,213 @@
#!/bin/bash
set -e
print_usage() {
echo "Usage: $0 {healthcheck|{gpio|mqtt} {on|off}}"
}
healthcheck() {
# DNS names also available
local iface="${1:-ppp0}"
local addr="${2:-1.1.1.1}"
local packets="${3:-5}"
local timeout="${4:-10}"
if ! ping -I $iface -c $packets -W $timeout $addr > /dev/null 2>&1; then
echo "Healthcheck failed"
return 1
else
echo "Healthcheck passed"
return 0
fi
}
gpio_on() {
local pin="$1"
local iface="${2:-ppp0}"
if [[ -z $pin ]]; then
echo "pin number must be provided to \`gpio_on\` function"
exit 1
fi
if ip link show $iface > /dev/null 2>&1; then
echo "Interface $iface already exists"
exit 1
fi
if [ ! -d "/sys/class/gpio/gpio{pin}" ]; then
echo "$pin" > /sys/class/gpio/export
fi
echo out > "/sys/class/gpio/gpio$pin/direction"
echo 1 > "/sys/class/gpio/gpio$pin/active_low"
echo 1 > "/sys/class/gpio/gpio$pin/value"
pon t3hs_sim800
}
gpio_off() {
local pin="$1"
if [[ -z $pin ]]; then
echo "pin number must be provided to \`gpio_off\` function"
exit 1
fi
poff t3hs_sim800
echo 0 > "/sys/class/gpio/gpio$pin/value"
echo "$pin" > /sys/class/gpio/unexport
}
mqtt_on() {
local topic="$1"
local host="$2"
local port="$3"
local username="$4"
local password="$5"
local iface="${6:-ppp0}"
if ip link show $iface > /dev/null 2>&1; then
echo "Interface $iface already exists"
exit 1
fi
args=""
if [[ -z $topic ]]; then
echo "topic must be provided to \`mqtt_on\` function"
exit 1
else
args+=" --topic $topic"
echo "topic: $topic"
fi
[[ -n $host ]] && args+=" --host $host" && echo "host: $host"
[[ -n $port ]] && args+=" --port $port" && echo "port: $port"
[[ -n $username ]] && args+=" --username $username" && echo "username: $username"
[[ -n $password ]] && args+=" --password $password" && echo "password: ***"
payload=$(cat <<EOF
{
"data": {
"msgid": 1344633989,
"srcid": "11223344",
"dstid": "FFFFFFFF",
"time": "2025-04-13T08:59:09.691276Z",
"msgtype": 1,
"payloadtype": 100,
"payloadname": "notset",
"payload": {
"applytype": 0,
"variables": {
"gsm_power": 0
}
}
},
"signature": "ADB3996D6792E696BB0D04137AF8BCB3883E501312E8EE13874D376B28E5C6CC"
}
EOF
)
args+=" --stdin-file"
echo "message payload:"
echo "$payload"
echo "$payload" | mosquitto_pub $args
pon t3hs_sim800
}
mqtt_off() {
local topic="$1"
local host="$2"
local port="$3"
local username="$4"
local password="$5"
local iface="${6:-ppp0}"
if ip link show $iface > /dev/null 2>&1; then
echo "Interface $iface already exists"
exit 1
fi
args=""
if [[ -z $topic ]]; then
echo "topic must be provided to \`mqtt_off\` function"
exit 1
else
args+=" --topic $topic"
echo "topic: $topic"
fi
[[ -n $host ]] && args+=" --host $host" && echo "host: $host"
[[ -n $port ]] && args+=" --port $port" && echo "port: $port"
[[ -n $username ]] && args+=" --username $username" && echo "username: $username"
[[ -n $password ]] && args+=" --password $password" && echo "password: ***"
payload=$(cat <<EOF
{
"data": {
"msgid": 1344633989,
"srcid": "11223344",
"dstid": "FFFFFFFF",
"time": "2025-04-13T08:59:09.691276Z",
"msgtype": 1,
"payloadtype": 100,
"payloadname": "notset",
"payload": {
"applytype": 0,
"variables": {
"gsm_power": 1
}
}
},
"signature": "ADB3996D6792E696BB0D04137AF8BCB3883E501312E8EE13874D376B28E5C6CC"
}
EOF
)
args+=" --stdin-file"
echo "message payload:"
echo "$payload"
echo "$payload" | mosquitto_pub $args
poff t3hs_sim800
}
# common settings file for t3hs_sim800 package
source /etc/t3hs/sim800
case $# in
1)
if [[ "$1" == "healthcheck" ]]; then
healthcheck "$IFACE" "$PING_ADDR" "$PING_PACKETS" "$PING_TIMEOUT"
exit $?
else
print_usage
exit 1
fi
;;
2)
case "$1,$2" in
gpio,on)
gpio_on "$GPIO_PIN" "$IFACE"
exit $?
;;
gpio,off)
gpio_off "$GPIO_PIN"
exit $?
;;
mqtt,on)
mqtt_on "$MQTT_TOPIC" "$MQTT_HOST" "$MQTT_PORT" "$MQTT_USERNAME" "$MQTT_PASSWORD" "$IFACE"
exit $?
;;
mqtt,off)
mqtt_off "$MQTT_TOPIC" "$MQTT_HOST" "$MQTT_PORT" "$MQTT_USERNAME" "$MQTT_PASSWORD"
exit $?
;;
*)
print_usage
exit 1
;;
esac
;;
*)
print_usage
;;
esac