termux-api-package/scripts/termux-sensor

115 lines
3.1 KiB
Plaintext
Raw Normal View History

2018-04-02 01:26:48 +02:00
#!/data/data/com.termux/files/usr/bin/bash
set -e -u
SCRIPTNAME=termux-sensor
show_usage () {
echo "Usage: $SCRIPTNAME"
echo "Get information about types of sensors as well as live data"
echo " -h, help Show this help"
echo " -a, all Listen to all sensors (WARNING! may have battery impact)"
echo " -c, cleanup Perform cleanup (release sensor resources)"
echo " -l, list Show list of available sensors"
echo " -s, sensors [,,,] Sensors to listen to (can contain just partial name)"
echo " -d, delay [ms] Delay time in milliseconds before receiving new sensor update"
2018-06-26 04:50:07 +02:00
echo " -n, limit [num] Number of times to read sensor(s) (default: continuous) (min: 1)"
2018-04-02 01:26:48 +02:00
exit 0
}
# ensure we properly release resources if needed on ^C
trap handle_interrupt SIGTERM SIGINT
if [ $# -eq 0 ]; then
echo "No arguments supplied!"
show_usage
exit 1;
fi
SENSOR_FLAG=1
ALL_SENSORS_FLAG=2
LIST_FLAG=4
CLEANUP_FLAG=8
DELAY_FLAG=16
2018-06-26 04:50:07 +02:00
LIMIT_FLAG=32
2018-04-02 01:26:48 +02:00
FLAGS=0
handle_interrupt () {
echo "Caught interrupt.. Finishing..."
cleanup
}
cleanup () {
if [ $((FLAGS & (ALL_SENSORS_FLAG | SENSOR_FLAG))) != 0 ]; then
echo "Performing sensor cleanup"
call_api -a cleanup
2018-04-02 01:26:48 +02:00
else
echo "No cleanup necessary"
fi
}
get_sensors () {
if [ $# -gt 1 ]; then
usage_error "Sensor arg should be a comma delimited string!"
fi
}
get_delay () {
if [ $# -gt 1 ]; then
usage_error "Too many arguments for -d delay"
elif ! [[ $1 =~ ^[0-9]+$ ]]; then
usage_error "Illegal argument! -d delay arg should be a number!"
fi
}
2018-06-26 04:50:07 +02:00
get_limit () {
if [ $# -gt 1 ]; then
usage_error "Too many arguments for -n limit"
elif ! [[ $1 =~ ^[0-9]+$ ]]; then
usage_error "Illegal argument! -n limit arg should be a number!"
fi
}
2018-04-02 01:26:48 +02:00
call_api () {
2018-06-08 03:54:19 +02:00
/data/data/com.termux/files/usr/libexec/termux-api Sensor "$@"
2018-04-02 01:26:48 +02:00
}
set_flag () {
FLAGS=$((FLAGS | $1));
}
usage_error () {
echo "ERROR: $@"
show_usage
exit 1
}
PARAMS=()
2018-06-26 04:50:07 +02:00
while getopts :h,a,c,l,s:d:n: option
2018-04-02 01:26:48 +02:00
do
2018-06-08 03:54:19 +02:00
case "$option" in
h) show_usage ;;
2018-06-08 03:55:16 +02:00
a) set_flag $ALL_SENSORS_FLAG; PARAMS+=(-a sensors --ez all true);;
c) set_flag $CLEANUP_FLAG; PARAMS=(-a cleanup) ;;
l) set_flag $LIST_FLAG; PARAMS=(-a list) ;;
s) set_flag $SENSOR_FLAG; get_sensors "$OPTARG"; PARAMS+=(-a sensors --es sensors "$OPTARG") ;;
d) set_flag $DELAY_FLAG; get_delay $OPTARG; PARAMS+=(--ei delay $OPTARG) ;;
2018-06-26 04:50:07 +02:00
n) set_flag $LIMIT_FLAG; get_limit $OPTARG; PARAMS+=(--ei limit $OPTARG) ;;
2018-06-08 03:54:19 +02:00
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
esac
2018-04-02 01:26:48 +02:00
done
shift $((OPTIND -1))
# Validate options were set properly
if [ $((FLAGS & CLEANUP_FLAG)) -ne 0 ] && [ $FLAGS -ne $CLEANUP_FLAG ]; then
2018-06-08 03:54:19 +02:00
usage_error "No other options should be specified with -c cleanup!"
2018-04-02 01:26:48 +02:00
elif [ $((FLAGS & LIST_FLAG)) -ne 0 ] && [ $FLAGS -ne $LIST_FLAG ]; then
2018-06-08 03:54:19 +02:00
usage_error "No other options should be specified with -l list!"
2018-04-02 01:26:48 +02:00
elif [ $((FLAGS & DELAY_FLAG)) -ne 0 ] && [ $((FLAGS & (ALL_SENSORS_FLAG | SENSOR_FLAG))) -eq 0 ]; then
2018-06-08 03:54:19 +02:00
usage_error "Use -s <sensors> or -a option when using -d delay!"
2018-04-02 01:26:48 +02:00
elif [ $((FLAGS & ALL_SENSORS_FLAG)) -ne 0 ] && [ $((FLAGS & SENSOR_FLAG)) -ne 0 ]; then
2018-06-08 03:54:19 +02:00
usage_error "Listed sensors will be ignored with -a all option!"
2018-04-02 01:26:48 +02:00
fi
call_api "${PARAMS[@]}"