Skip to content
Snippets Groups Projects
Commit 57e041e2 authored by Arafat NOUREDDINE's avatar Arafat NOUREDDINE
Browse files

Remove python test script test_switch_mca_mapping.py

Add python test script test_switch_mca_map.py
parent 47b7c82f
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
#########################################################
# Arafat NOUREDDINE
# 2014/11/19
# Purpose: Test XiaDxp - Command LoadConfigurationFile
# Modified: Added enable/disable file output feature
#########################################################
import os
import sys
import PyTango
import time
import datetime
# Global variable to control file output
ENABLE_OUTPUT_FILE = True
#------------------------------------------------------------------------------
# Log to file and console
#------------------------------------------------------------------------------
def log(*args, **kwargs):
"""
Logs messages to both the console and a file (if enabled).
Uses the global variable ENABLE_OUTPUT_FILE to determine if file output is active.
"""
# Always print to the console without moving to the next line
print(*args, **kwargs)
# Write to the file if ENABLE_OUTPUT_FILE is True
if ENABLE_OUTPUT_FILE:
with open("output.txt", "a") as f:
print(*args, **kwargs, file=f)
#------------------------------------------------------------------------------
# Custom exception for build errors
#------------------------------------------------------------------------------
class BuildError(Exception):
pass
#------------------------------------------------------------------------------
# Load configuration file
#------------------------------------------------------------------------------
def load(proxy, alias):
"""
Loads a configuration file on the Tango device and logs the process.
:param proxy: Tango device proxy.
:param alias: Alias of the configuration file to load.
"""
try:
log('\nLoadConfigFile(', alias, ') \n------------------')
# Record start time and device state
start_load_time = datetime.datetime.now()
log(start_load_time.isoformat(), ' - ', proxy.state())
# Load the configuration file
proxy.LoadConfigFile(alias)
# Record time after the call of LoadConfigFile
log(datetime.datetime.now().isoformat(), ' - ', proxy.state())
# Wait for the device to finish loading
timeout = 60 # 60 seconds timeout
start_loop_time = time.time()
state = proxy.state()
while state == PyTango.DevState.DISABLE:
if time.time() - start_loop_time > timeout:
log("\nTimeout: Device did not transition to STANDBY state.")
break
state = proxy.state()
if state == PyTango.DevState.STANDBY:
break
log('.', end='', flush=True) # Affiche un point sans nouvelle ligne
time.sleep(1)
# Record end time and device state
end_load_time = datetime.datetime.now()
log('\n'+end_load_time.isoformat(), ' - ', proxy.state())
duration_ms = (end_load_time - start_load_time).total_seconds() * 1e3
log(f"[duration : {duration_ms:.0f} ms]")
time.sleep(5)
except PyTango.DevFailed as e:
log(f"Tango error while loading configuration: {e}")
except Exception as e:
log(f"Unexpected error: {e}")
#------------------------------------------------------------------------------
# Display usage information
#------------------------------------------------------------------------------
def usage():
"""
Displays usage information and exits the program.
"""
log("Usage: [python] test_load_switch_mca_map.py <my/device/proxy> <mca_alias> <map_alias> <nb_loops> <enable_output_file>")
sys.exit(1)
#------------------------------------------------------------------------------
# Main function to run the program
#------------------------------------------------------------------------------
def run(proxy_name='tmp/test/xia_memory_leak', mca_alias="MCA", map_alias="MAP", nb_loops=1, enable_output_file=True):
"""
Runs the main program logic.
:param proxy_name: Name of the Tango device proxy.
:param mca_alias: Alias for the MCA configuration.
:param map_alias: Alias for the MAP configuration.
:param nb_loops: Number of loops to run.
:param enable_output_file: Whether to enable file output.
"""
global ENABLE_OUTPUT_FILE
ENABLE_OUTPUT_FILE = enable_output_file # Update the global variable
# Log program inputs
log('\nProgram inputs :\n--------------')
log('proxy_name\t\t = ', proxy_name)
log('MCA alias\t\t = ', mca_alias)
log('MAP alias\t\t = ', map_alias)
log('nb_loops\t\t = ', nb_loops)
log('enable_output_file\t = ', enable_output_file)
# Connect to the Tango device
proxy = PyTango.DeviceProxy(proxy_name)
log('\nConfigure Device attributes :\n--------------')
proxy.Stop()
nb_loops = int(nb_loops)
alias = '1'
log('\n')
try:
current_loop = 0
while current_loop < nb_loops:
log('\n========================================================')
log('Loop : ', current_loop, end=' ')
log('\n========================================================')
# Alternate between MCA and MAP aliases
if current_loop % 2 == 0:
alias = mca_alias
else:
alias = map_alias
# Load the configuration
load(proxy, alias)
current_loop += 1
time.sleep(1)
# Check device state
state = proxy.state()
if state != PyTango.DevState.STANDBY:
raise BuildError(f'FAIL: Acquisition ended with state ({state})')
log('Device is in FAULT!')
log('\nProgram outputs :\n--------------')
except Exception as err:
sys.stderr.write('--------------\nERROR :\n--------------\n%s\n' % err)
#------------------------------------------------------------------------------
# Main entry point
#------------------------------------------------------------------------------
if __name__ == "__main__":
# Check if the correct number of arguments is provided
if len(sys.argv) < 6:
usage()
else:
# Parse command-line arguments
proxy_name = sys.argv[1]
mca_alias = sys.argv[2]
map_alias = sys.argv[3]
nb_loops = sys.argv[4]
enable_output_file = sys.argv[5].lower() in ('true', '1', 'yes') # Convert to boolean
# Run the program
run(proxy_name, mca_alias, map_alias, int(nb_loops), enable_output_file)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment