#
# $Id: apc.dev 866 2008-05-21 20:42:37Z garlick $
#
# APC MasterSwitch Plus 
#
# Firmware rev may not be the best indicator of whether you need "apc"
# or "apcnew" scripts.  We have one data point for "apc":
#
# - Firmware: APP v2.0.0 / AOS v2.5.4
#
specification "apc" {
	timeout 	10  

	plug name { "1" "2" "3" "4" "5" "6" "7" "8" }

	script login {
		expect "\n"
		expect "User Name : "
		send "apc\r\n"
		expect "Password  : "
		send "apc\r\n"
		expect "> "
		send "1\r\n"  # device manager menu
		expect "> "
		send "1\r\n"  # select master switch plus 1 
		expect "> "
	}
	script logout {
		send "4\r\n"
	}
	script status_all {
		send "\r\n"  # refresh
		foreachplug {
			expect "([0-9]+)-[^\n]*(ON|OFF)[^\n]*\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 "8\r\n" # cancel any pending delays
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "1\r\n" # immediate on
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "> "
	}
	script on_all {
		send "9\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "8\r\n" # cancel any pending delays
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "1\r\n" # immediate on
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

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

		send "8\r\n" # cancel any pending delays
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "3\r\n" # immediate off
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "> "
	}
	script off_all {
		send "9\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "8\r\n" # cancel any pending delays
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "3\r\n" # immediate off
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		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 "8\r\n" # cancel any pending delays
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "3\r\n" # immediate off
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		delay 4

		send "1\r\n" # immediate on
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "> "
	}
	script cycle_all {
		send "9\r\n"
		expect "> "
		send "1\r\n" # select outlet control
		expect "> "

		send "8\r\n" # cancel any pending delays
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "3\r\n" # immediate off
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		delay 4

		send "1\r\n" # immediate on
		expect "Enter 'YES' to continue or <ENTER> to cancel : "
		send "YES\r\n"
		expect "Success\r\n"
		send "\r\n"
		expect "> "

		send "\033"
		expect "> "
		send "\033"
		expect "> "
	}
}
