#!/usr/bin/python

# Imports
import PyTango
import time

##############################################################################
# Utilities
##############################################################################

#
# Variables
#

OFF = PyTango.DevState.OFF  # PyTango OFF state
STANDBY = PyTango.DevState.STANDBY  # PyTango standby state
ON = PyTango.DevState.ON  # PyTango on state


#
# Functions
#

#------------------------------------------------------------------------------
# Wait for a delay
# argin :
#   delay : delay in seconds
# argout : none
def waitForDelay(delay):
	print "Wait for ", delay, " s ..."
	time.sleep(delay)


#------------------------------------------------------------------------------
# Print dome state and status
#
def logStateAndStatus():
	time.sleep(1)
	print "STATE = ", dome.state()
	time.sleep(1)
	print "STATUS = ", dome.status()


#------------------------------------------------------------------------------
# Wait for a state to be ready, in a specified delay
# Used to handle the anachronism
# argin :
#   state : the required state to wait for
#   delay : delay in seconds to wait; default is 1 second
# argout : none
def waitForState(state, delay=1):
	print "Wait for ", state, "state ..."
	waitForDelay(3)
	while dome.state() != state:
		waitForDelay(delay)
	# Check state and status
	print "Check state and status"
	logStateAndStatus()


##############################################################################


print "\n*****************************************************************"
print "INFOS"
print "--------"

# Get Tango database host
print "TANGO_HOST =", PyTango.ApiUtil.get_env_var("TANGO_HOST")

# PyTango version
print "PyTango version = ", PyTango.__version__

print "\n*****************************************************************"
print "DEVICE STATE"
print "--------"

# create a device object: get proxy on the dome device
print("Creating proxy to dome device...")
dome = PyTango.DeviceProxy("roomT/inf/dome")
print "Get green mode : ", dome.get_green_mode()


print "--------"
print "\n---> STATES and STATUS, PING"
print "-------------------------------------------"
# Get the state
print "Get the state"
print(dome.state())

print "Ping the device proxy"
print(dome.ping())

# Get the status
print "Get the status"
print(dome.status())

print "-------------------------------------------"
# Go to OFF state
print "Go to 'OFF' state"
print "is equivalent to send the 'Off()' command"
print(dome.Off())
waitForState(OFF)

print "-------------------------------------------"
# Go to STANDBY state
print "Go to 'STANDBY' state"
print "is equivalent to send the 'Standby()' command"
print(dome.Standby())
waitForState(STANDBY)

print "-------------------------------------------"
# Go to ON state
print "Go to 'ON' state"
print "is equivalent to send the 'On()' command"
print(dome.On())
waitForState(ON)

print "\n*****************************************************************"
print "DEVICE ATTRIBUTES"
print "--------"

# Read all attributes
print "Dump/read all attributes values:"
for i in dome.get_attribute_list():
	print "%s = %s" % (i, str(dome.__getattr__(i)))

print "--------"
print "\n---> READ attributes"
print "-------------------------------------------"

print "-------------------------------------------"
# Read azimuth attribute
print("Get azimuth attribute")
print "azimuth = ", dome.azimuth

print "-------------------------------------------"
# Read encoder attribute
print("Get encoder attribute")
print "encoder = ", dome.encoder

print "-------------------------------------------"
# Read shutterClosed attribute
print("Get shutterClosed attribute")
print "shutterClosed = ", dome.shutterClosed

print "-------------------------------------------"
# Read dewPoint attribute
print("Get dewPoint attribute")
print "dewPoint = ", dome.dewPoint

print "-------------------------------------------"
# Read SIM_FLAG attribute
print("Get SIM_FLAG attribute")
print "SIM_FLAG = ", dome.dewPoint

# Other attribute available
print "-------------------------------------------"
print "Other available attributes \n"
otherAvailableAttrList = ("atHome",
                           "atPark",
                           "internalTemperature",
                           "externalTemperature",
                           "atmosphericHumidity",
                           "atmosphericPressure",
                           "powerScopeDomeControler",
                           "powerSpectralCalib",
                           "powerFlatFieldCalib",
                           "powerFPUControllers",
                           "weatherInterlockActive",
                           "radioStrength",
                           "State",
                           "Status")
for i in otherAvailableAttrList:
	print "- ", i


print "--------"
print "\n---> WRITE attributes"
print "-------------------------------------------"
print "For most of them it is like 'commands'"

# Write azimuth attribute
print "-------------------------------------------"
print("Slew to azimuth => set azimuth attribute ...")
print "Set azimuth to 20 degree "
dome.azimuth = 20
waitForState(ON)
print "Check azimuth position : azimuth = ", dome.azimuth

# Write encoder attribute
print "-------------------------------------------"
print("Slew to encoder => set encoder attribute ...")
print "Set encoder to 165 "
dome.encoder = 165
waitForState(ON)
print "Check encoder position : encoder = ", dome.encoder

# Write shutterClosed attribute
print "-------------------------------------------"
print("Move shutter => set shutterClosed attribute ...")
# Open shutter
print "Open shutter => set shutterClosed to False "
dome.shutterClosed = False
waitForState(ON)
print "Check shutter position : ", dome.shutterClosed
# Close shutter
print "Close shutter => set shutterClosed to True "
dome.shutterClosed = True
waitForState(ON)
print "Check shutter position : ", dome.shutterClosed

# Write atHome attribute
print "-------------------------------------------"
print("Go at home => set atHome attribute to True ...")
print "Set atHome to True = "
dome.atHome = True
waitForState(ON)
print "Check atHome position : ", dome.atHome

# Write powerSpectralCalib attribute
print "-------------------------------------------"
print("Switch on/off spectral lamp => set powerSpectralCalib attribute ...")
print "Switch on spectral lamp => set powerSpectralCalib to True = "
dome.powerSpectralCalib = True
waitForState(ON)
print "Check lamp state : ", dome.powerSpectralCalib
print "Switch off spectral lamp => set powerSpectralCalib to False = "
dome.powerSpectralCalib = False
waitForState(ON)
print "Check lamp state : ", dome.powerSpectralCalib

# Other attribute available for writting
print "-------------------------------------------"
print("Other available attributes")
otherAvailableAttrList = ("atPark",
                           "SIM_FLAG",
                           "powerScopeDomeControler",
                           "powerFlatFieldCalib",
                           "powerFPUControllers",
                           "weatherInterlockActive")
for i in otherAvailableAttrList:
	print "- ", i


print "\n*****************************************************************"
print "DEVICE COMMANDS"
print "--------"

print "-------------------------------------------"
# Send Init command
print "Send Init command ..."
print "Init : ", dome.Init()
print "Check state"
logStateAndStatus()

print "-------------------------------------------"
# Send Off, Standby, On commands
print "Send Off, Standby, On commands ..."
print "is equivalent to go to Off, Standby, On states => see STATE section"

print "-------------------------------------------"
# Send State, Status commands
print "Send State, Status commands ..."
print "see STATE and STATUS section"
logStateAndStatus()

print "-------------------------------------------"
# Send Stop command
print "Send Stop command ..."
print "___________________"
print "Go to Off state ..."
print(dome.Off())
waitForState(OFF)
print "___________________"
print "Go to Standby state ..."
dome.Standby()
waitForState(STANDBY)

print "-------------------------------------------"
# Send Derotate command
print "Send derotate command ..."
print "Derotate : "
dome.Derotate()
waitForState(STANDBY)

print "___________________"
print "Go to On state ..."
dome.On()
waitForState(ON)
print "___________________"
print "Move to 10 degree"
dome.azimuth = 10
waitForState(ON)
print "Check azimuth : ", dome.azimuth
print "___________________"
print "Move to 190 degree"
dome.azimuth = 190
# Wait a little bit in order the motion starts
waitForDelay(10)
print "___________________"
print "Send Stop command ..."
print "Stop : "
dome.Stop()
waitForState(ON)

print "-------------------------------------------"
# Send Kill command
print "Send Kill command ..."
print "Kill : "
dome.Kill()
print "Check state  and status :"
logStateAndStatus()

# Other attribute available for writting
print "-------------------------------------------"
print("Other attribute available")
otherAvailableAttrList = ("CalibrateAzimuthEncoder",
                          "CalibrateInertia",
                          "ResetAzimuthEncoder",
                          "ResetEncoderCounter",
                          "RestoreDriverDefaults")
for i in otherAvailableAttrList:
	print "- ", i
