#!/bin/bash

# Settings

NASIP="ADRES_IP_QNAP"             # NAS IP Address

PASSWORD="tu_twoje_haslo_do_usługi_snmp_na_qnap"             # SNMP Password

DOMO_IP="ADRES_IP_DOMOTICZA"             # Domoticz IP Address

DOMO_PORT="8080"         # Domoticz Port

NAS_IDX="91"             # Virtual switch NAS STATUS

INPUT_VOLTAGE_IDX="50"                 # napięcie wejściowe na UPS

OUTPUT_VOLTAGE_IDX="51"             # napięcie wyjściowe z ups

BATTERY_VOLTAGE_IDX="52"             # napięcie baterii w ups

UPS_TEMP_IDX="53"             # temperatura UPS

BATTERY_CHARGE_IDX="54"                 # naładowanie baterii

# Check if NAS in online

PINGTIME=`ping -c 1 -q $NASIP | awk -F"/" '{print $5}' | xargs`

echo $PINGTIME

if expr "$PINGTIME" '>' 0

then

            #curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=devices&rid=$NAS_IDX" | grep "Status" | grep "On" > /dev/null

    if [ $? -eq 0 ] ; then

                                # NAS already ON

            echo "NAS already ON"

                                # INPUT VOLTAGE

                                # upsc qnapups@$NASIP:3493 > dane_ups.txt             #zapis danych do pliku

                                # InVOLTAGE=`grep input.voltage: dane_ups.txt | cut -c16-`             #wyciągnięcie z pliku napięcia wejściowego

                                # echo $InVOLTAGE tee voltage.txt                     #wyświetlenie na ekranie i zapis do pliku (samo wyśw. na ekranie to echo

                                InVOLTAGE=`upsc qnapups@$NASIP:3493 | grep input.voltage: | cut -c16-` #bez zapisu do pliku wyciągnięcie napięcia wejściowego

                                # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$INPUT_VOLTAGE_IDX&nvalue=0&svalue=$InVOLTAGE"

                                # OUTPUT VOLTAGE

                                OutVOLTAGE=`upsc qnapups@$NASIP:3493 | grep output.voltage: | cut -c17-`

                                # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$OUTPUT_VOLTAGE_IDX&nvalue=0&svalue=$OutVOLTAGE"

                                # BATTERY VOLTAGE

                                BatteryVOLTAGE=`upsc qnapups@$NASIP:3493 | grep battery.voltage: | cut -c18-`

                                # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$BATTERY_VOLTAGE_IDX&nvalue=0&svalue=$BatteryVOLTAGE"

                                # UPS TEMPERATURE

                                UPSTemperature=`upsc qnapups@$NASIP:3493 | grep ups.temperature: | cut -c18-`

                                 # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_TEMP_IDX&nvalue=0&svalue=$UPSTemperature"

                               # BATTERY_CHARGE

                                BatteryCHARGE=`upsc qnapups@$NASIP:3493 | grep battery.charge: | cut -c17-`

                          # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$BATTERY_CHARGE_IDX&nvalue=0&svalue=$BatteryCHARGE"

    else

                                # NAS ON

            echo "NAS ON"

                            # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$NAS_IDX&switchcmd=On"

                        # INPUT VOLTAGE

                                InVOLTAGE=`upsc qnapups@$NASIP:3493 | grep input.voltage: | cut -c16-`

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$INPUT_VOLTAGE_IDX&nvalue=0&svalue=$InVOLTAGE"

                    # OUTPUT VOLTAGE

                                 OutVOLTAGE=`upsc qnapups@$NASIP:3493 | grep output.voltage: | cut -c17-`

                    # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$OUTPUT_VOLTAGE_IDX&nvalue=0&svalue=$OutVOLTAGE"

                    # BATTERY VOLTAGE

                                BatteryVOLTAGE=`upsc qnapups@$NASIP:3493 | grep battery.voltage: | cut -c18-`

                # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$BATTERY_VOLTAGE_IDX&nvalue=0&svalue=$BatteryVOLTAGE"

                # UPS TEMPERATURE

                                UPSTemperature=`upsc qnapups@$NASIP:3493 | grep ups.temperature: | cut -c18-`

                # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_TEMP_IDX&nvalue=0&svalue=$UPSTemperature"

                # BATTERY_CHARGE

                                BatteryCHARGE=`upsc qnapups@$NASIP:3493 | grep battery.charge: | cut -c17-`

                # Send data

            curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$BATTERY_CHARGE_IDX&nvalue=0&svalue=$BatteryCHARGE"

    fi

else

            #curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=devices&rid=$NAS_IDX" | grep"Status" | grep "Off" > /dev/null

            # NAS uitgeschakeld

    if [ $? -eq 0 ] ; then

        echo "NAS already OFF"

        exit

    else

        echo "NAS OFF"

              # Send data

                    #curl -s -i -H "Accept: application/json""http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$NAS_IDX&switchcmd=Off"

    fi

fi