#!/bin/bash

# Utility to manage R2lab relays to control specific devices such as UP4000 mini-PC and QuectelHat
# Relays are raspberry Pi devices, each with 3 outlets
# Those relays are only controllable through dumb http requests.
# https://gitlab.inria.fr/slices-ri/resources/sites/sophianode/relay_control

# the script is typically called as:
#           operation    IP     chain# outlet#
# raspberry     on    relay-10   0      3
# raspberry    off    relay-10   0      3
# raspberry    status relay-10   0      3

COMMAND=$(basename $0)

function raspberry-status() {

    function help() {
        echo "$0 status: Retrieve the status of a specific R2lab relay"
        echo "Usage: $COMMAND status IP chain outlet#"
        echo -e "\twith chain being 0 (ignored anyway)"
        echo -e "\twith outlet# in [1:3]"
        exit 255
    }

    [[ "$#" == 2 ]] || help

    # provided but ignored
    local _chain="$1"; shift
    local outlet="$1"; shift
    [[ "${outlet}" -lt 1 || "${outlet}" -gt 3 ]] && help

    local url="http://$PDU_IP:8000/api/v1/relay/CH${outlet}/status"
    local status=$(http GET $url | jq -r .status )

    case "$status" in
    off)
        echo "${PDU_IP}@outlet-${outlet} ($DEVICE_NAME): OFF"
        return 1
        ;;
    on)
        echo "${PDU_IP}@outlet-${outlet} ($DEVICE_NAME): ON"
        return 0
        ;;
    *)
        echo "Could not retrieve ${PDU_IP}@outlet-${outlet} ($DEVICE_NAME) status, GET returned '$status'"
        echo "with url = $url"
        return 255
        ;;
    esac
}

function -raspberry-on-off() {
    local mode="$1"; shift    # 'on' or 'off'

    function help() {
        echo "$COMMAND $mode: switch $mode a specific raspberry outlet"
        echo "Usage: $COMMAND on IP chain outlet#"
        echo -e "\twith chain being 0 (ignored anyway)"
        echo -e "\twith outlet# in [1:3]"
        exit 255
    }

    [[ "$#" == 2 ]] || help

    # provided but ignored
    local _chain="$1"; shift
    local outlet="$1"; shift
    [[ "${outlet}" -lt 1 || "${outlet}" -gt 3 ]] && help

    raspberry-status $_chain ${outlet} >& /dev/null
    local current_status=$?

    case $mode in
    on)
        if [ "$current_status" -eq 0 ]; then
            echo "${PDU_IP}@outlet-${outlet} ($DEVICE_NAME) already ON"
            exit 0
        fi
        com=1
        ;;
    off)
        if [ "$current_status" -eq 1 ]; then
            echo "${PDU_IP}@outlet-${outlet} ($DEVICE_NAME) already OFF"
            exit 0
        fi
        com=0
        ;;
    esac
    local url="http://$PDU_IP:8000/api/v1/relay/CH${outlet}/$mode"
    http POST $url >/dev/null

}

function raspberry-on() {
    -raspberry-on-off on "$@"
}

function raspberry-off() {
    -raspberry-on-off off "$@"
}

function raspberry-probe() {
    echo "Probing raspberry $PDU_IP"
    for outlet in $(seq 1 3); do
        raspberry-status 0 ${outlet}
    done
}

function main() {

    function help() {
        echo "Usage: $0 status|on|off IP NAME chain outlet"
        exit 255
    }

    [[ "$#" -le 2 ]] && { help; exit 1; }
    local subcommand="$1"; shift
    PDU_IP="$1"; shift
    DEVICE_NAME="$1"; shift

    local function="raspberry-${subcommand}"
    $function "$@"
}

[[ "$1" == "--debug" ]] && {
    shift
    set -x
}
main "$@"