#
# $Id:$
#
# Written by Trent D'Hooge and Makia Minich
#
# Should support the following 24-plug APC Switched Rack PDU models:
#   AP7930, AP7932
#   AP7940, AP7941
#   AP7960, AP7961, AP7968, AP7990, AP7991, AP7998 
#   AP7951, AP7952, AP7953, AP7954
#
# - Firmware: APP v2.6.5 / AOS v2.6.4
#
specification "apcpdu" {
	timeout 	10  

	plug name { "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" }

	script login {
		expect "\n"
		expect "\rUser Name : "
		send "apc\r\n"
		expect "\rPassword  : "
		send "apc\r\n"
		expect "> "
		send "1\r\n"  # device manager menu
		expect "> "
		send "3\r\n"  # outlet control/configuration
		expect "to continue..."
		send "\r\n"
		expect "> "
	}
	script logout {
		send "\033"
		expect "> "
		send "\033"
		expect "> "
		send "4\r\n"
	}
	script status_all {
                send "\r\n"  # refresh
                send "\r\n"  # to continue... (after plug 22)
                foreachplug {
                        expect "([0-9])*[^\r\n]*(ON|OFF)\r\n"
                        setplugstate $1 $2 on="ON" off="OFF"
                }
                expect "> "
        }
	script on {
		send "%s\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "7\r\n" # cancel any pending delays
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "1\r\n" # immediate on
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "to continue..."
		send "\r\n"
		expect "> "
	}
	script on_all {
		send "25\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "7\r\n" # cancel any pending delays
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "4\r\n" # delayed on
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "to continue..."
		send "\r\n"
		expect "> "
	}
	script off {
		send "%s\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "7\r\n" # cancel any pending delays
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "2\r\n" # immediate off
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "to continue..."
		send "\r\n"
		expect "> "
	}
	script off_all {
		send "25\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "7\r\n" # cancel any pending delays
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "5\r\n" # delayed off
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "to continue..."
		send "\r\n"
		expect "> "
	}
# If outlet is initially off, "immediate reboot" (4) will leave it off.
# This doesn't match PowerMan's semantics for cycle, therefore do explicit
# immediate off + immediate on.
	script cycle {
		send "%s\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "7\r\n" # cancel any pending delays
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "2\r\n" # immediate off
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		delay 4

		send "1\r\n" # immediate on
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "to continue..."
		send "\r\n"
	}
	script cycle_all {
		send "25\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "7\r\n" # cancel any pending delays
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "5\r\n" # delayed off
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		delay 4

		send "4\r\n" # delayed on
		expect "to cancel : "
		send "YES\r\n"
		expect "to continue..."
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "to continue..."
		send "\r\n"
		expect "> "
	}
}
