Skip to content

Commit

Permalink
Needs Tiago inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
isotuela committed Jun 7, 2023
1 parent 2e57f2c commit 5575a22
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import yaml

from lsst.ts.idl.enums import ATWhiteLight
from lsst.ts.observatory.control.remote_group import Usages


from lsst.ts import salobj

Expand All @@ -47,7 +49,7 @@ class PowerOnATCalSys(salobj.BaseScript):
"""

def __init__(self, index):
def __init__(self, index, add_remotes: bool = True):
super().__init__(
index=index,
descr="Power On AT Calibration System ",
Expand All @@ -56,6 +58,7 @@ def __init__(self, index):
self.white_light_source = None
self.monochromator = None

# White lamp config
self.timeout_lamp_warm_up = 60 * 15
self.cmd_timeout = 30

Expand All @@ -65,19 +68,24 @@ def __init__(self, index):

if self.white_light_source is None:
self.white_light_source = salobj.Remote(
domain=self.domain, name="ATWhiteLight"
domain=self.domain, name="ATWhiteLight",
intended_usage= None if add_remotes else Usages.DryTest,
log =self.log
)

if self.monochromator is None:
self.monochromator = salobj.Remote(
domain=self.domain, name="ATMonochromator"
domain=self.domain, name="ATMonochromator",
intended_usage= None if add_remotes else Usages.DryTest,
log=self.log
)

asyncio.gather(
self.white_light_source.start_task,
self.monochromator.start_task,
)


@classmethod
def get_schema(cls):
schema_yaml = """
Expand Down Expand Up @@ -161,8 +169,6 @@ async def configure(self, config):
# domain=self.domain, name="ATMonochromator"
# )



self.config = config

self.log.info("Configure completed")
Expand Down Expand Up @@ -202,7 +208,7 @@ async def start_chiller(self):
await self.white_light_source.cmd_setChillerTemperature.set_start(
temperature=self.chiller_temperature, timeout=self.cmd_timeout
)
await self.white_light_source.cmd_startChiller.set_start()
await self.white_light_source.cmd_startChiller.set(timeout=self.timeout_chiller_cool_down)

async def wait_for_chiller_temp_within_tolerance(self):
start_time_chill_time = time.time()
Expand Down Expand Up @@ -258,9 +264,6 @@ async def wait_for_lamp_to_warm_up(self):
self.log.info(
f"White Light Lamp is on after a warm up of {time.time() - start_time_lamp_warm_up} s"
)
else:
raise ValueError("White Light Lamp is not warming up or turning on")

else:
raise TimeoutError(
f"White Light Lamp failed to turn on after {self.timeout_lamp_warm_up} s"
Expand All @@ -272,15 +275,15 @@ async def configure_atmonochromator(self):
)

await self.monochromator.cmd_changeWavelength.set_start(
wavelength=self.wavelength
wavelength=self.wavelength, timeout=self.cmd_timeout
)

await self.monochromator.cmd_changeSlitWidth.set_start(
slit=1, slitWidth=self.entrance_slit_width
slit=1, slitWidth=self.entrance_slit_width, timeout=self.cmd_timeout
)

await self.monochromator.cmd_changeSlitWidth.set_start(
slit=2, slitWidth=self.exit_slit_width
slit=2, slitWidth=self.exit_slit_width, timeout=self.cmd_timeout
)

params = await self.get_monochromator_parameters()
Expand Down
227 changes: 127 additions & 100 deletions tests/test_auxtel_power_on_atcalsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
#
# You should have received a copy of the GNU General Public License

import asyncio
import logging
import random
import unittest
import types
import asyncio

# import pytest
# from lsst.ts.idl.enums import ATMonochromator, Script
# from lsst.ts import salobj
from lsst.ts import salobj

from lsst.ts import standardscripts
from lsst.ts.standardscripts.auxtel.calibrations import PowerOnATCalSys
from lsst.ts.idl.enums import ATWhiteLight
from lsst.ts.idl.enums import ATWhiteLight, ATMonochromator


random.seed(47) # for set_random_lsst_dds_partition_prefix
Expand All @@ -41,15 +41,23 @@
class TestPowerOnATCalSys(
standardscripts.BaseScriptTestCase, unittest.IsolatedAsyncioTestCase
):
def setUp(self) -> None:
self.remotes_needed = False

async def basic_make_script(self, index):
self.script = PowerOnATCalSys(index=index)
self.script = PowerOnATCalSys(index=index, add_remotes=False)

self.chiller_status = types.SimpleNamespace(chillerState="NOTREADY")
self.lamp_status = types.SimpleNamespace(
lampState=ATWhiteLight.LampBasicState.OFF
)
self.shutter_status = types.SimpleNamespace(
shutterState=ATWhiteLight.ShutterState.CLOSED
)
self.grating_status = types.SimpleNamespace(
gratingtype=ATMonochromator.Grating.BLUE
)

self.chiller_temp_within_tolerance = False
self.tel_chiller_temp = 30
self.lamp_status = types.SimpleNamespace(status=ATWhiteLight.LampBasicState.OFF)
self.wavelength_status = types.SimpleNamespace(wavelength=200)

self.slit_status = types.SimpleNamespace(width=0)

await self.configure_mocks()

Expand All @@ -61,122 +69,141 @@ async def configure_mocks(self):
self.script.white_light_source = unittest.mock.AsyncMock()
self.script.monochromator = unittest.mock.AsyncMock()

self.script.start_chiller = unittest.mock.AsyncMock()
self.script.open_white_light_shutter = unittest.mock.AsyncMock()
self.script.switch_lamp_on = unittest.mock.AsyncMock()
self.script.configure_atmonochromator = unittest.mock.AsyncMock()

# Configure mocks

# self.script.white_light_source.configure_mock(
# **{
# "cmd_setChillerTemperature.set_start.side_effect":
# self.start_chiller_temp,
# "cmd_startChiller.set_start.side_effect":
# self.mock_chiller_temp,
# "tel_ChillerTemp.side_effect":
# self.get_mock_chiller_temp,

# }
# )

# self.script.white_light_source.configure_mock(
# **{
# "cmd_turnLampOn.set_start": self.mock_lamp_temp,
# "evt_lampState.aget": self.get_lamp_status,
# "evt_lampState.next": self.get_lamp_status,
# }
# )
# self.script.wait_for_chiller_temp_within_tolerance =
# unittest.mock.AsyncMock(return_value=
# self.chiller_temp_within_tolerance
# )

# async def mock_lamp_temp(self, power, timeout):
# self._lamp_temp_task = asyncio.create_task(self._mock_lamp_temp())

# async def _mock_lamp_temp(self):
# self.lamp_status.status = ATWhiteLight.LampBasicState.WARMUP
# await asyncio.sleep(5.0)
# self.lamp_status.status = ATWhiteLight.LampBasicState.ON

# async def get_lamp_status(self, *args, **kwags):
# await asyncio.sleep(0.5)
# return self.lamp_status

# async def start_chiller_temp(self, temperature, timeout):
# self.tel_chiller_temp = 20.1

# async def mock_chiller_temp(self, temperature, timeout):
# self._chiller_temp_task = asyncio.create_task(
# self._mock_chiller_temp())

# async def _mock_chiller_temp(self):
# self.chiller_temp_within_tolerance = False
# await asyncio.sleep(10.0)
# self.chiller_temp_within_tolerance = True

# async def get_mock_chiller_temp(self):
# return types.SimpleNamespace(temperature=self.tel_chiller_temp)
self.script.white_light_source.configure_mock(
**{
"evt_summaryState.aget": self.mock_get_whitelightsource_summary_state,
"cmd_setChillerTemperature.set_start": self.mock_start_chiller_temp,
"cmd_startChiller.set": self.mock_chiller_temp,
"tel_ChillerTemp.aget": self.mock_get_chiller_status,
"com_openShutter.set_start": self.mock_open_shutter,
"cmd_turnLampOn.set_start": self.mock_lamp_temp,
"evt_lampState.next": self.mock_get_lamp_status,
}
)
self.script.monochromator.configure_mock(
**{
"evt_summaryState.aget": self.mock_get_monochromator_sumary_state,
"cmd_selectGrating.set_start": self.mock_change_grating,
"cmd_changeWavelength.set_start": self.mock_change_wavelength,
"cmd_changeSlitWidth.set_start": self.mock_change_slit_width,
}
)

# Chiller
async def mock_get_whitelightsource_summary_state(self):
return types.Simplenamespace(summaryState=salobj.State.ENABLED)

async def mock_start_chiller_temp(self):
self.start_chiller_temperature = 30

async def mock_chiller_temp(self):
self.chiller_status = types.SimpleNamespace(chillerState="NOTREADY")
await asyncio.sleep(5.0)
self.chiller_status = types.SimpleNamespace(chillerState="READY")

async def mock_get_chiller_status(self):
await asyncio.sleep(0.5)
return self.chiller_status

# Shutter
async def mock_open_shutter(self):
self.shutter_status = types.SimpleNamespace(
shutterState=ATWhiteLight.ShutterState.OPEN
)

# async def test_start_chiller_without_failures(self):
# async with self.make_script():
# await self.configure_mocks()
# Lamp

# await self.configure_script()
async def mock_lamp_temp(self):
self.lamp_status.status = ATWhiteLight.LampBasicState.WARMUP
await asyncio.sleep(10.0)
self.lamp_status.status = ATWhiteLight.LampBasicState.ON

# self.tel_chiller_temp = 20.2
async def mock_get_lamp_status(self):
await asyncio.sleep(0.5)
return self.lamp_status

# await self.script.start_chiller()
# Monochromator
async def mock_get_monochromator_summary_state(self):
return types.Simplenamespace(summaryState=salobj.State.ENABLED)

# # # Now the assertions
# self.script.white_light_source.cmd_setChillerTemperature.set_start.assert_awaited_with(
# temperature=self.script.chiller_temperature,
# timeout=self.script.timeout_chiller_cool_down,
# )
# self.script.white_light_source.cmd_startChiller.set_start.assert_awaited_once()
async def mock_change_grating(self):
self.grating_status = types.SimpleNamespace(
gratingState=self.script.grating
)

# self.script.wait_for_chiller_temp_within_tolerance.assert_awaited_once()
async def mock_change_wavelength(self):
self.wavelength_status = types.SimpleNamespace(
wavelength=self.script.wavelength
)

# # assert self.chiller_temp_within_tolerance == True
async def mock_change_slit_width(self):
self.slit_status = types.SimpleNamespace(
width=self.script.entrance_slit_width
)

async def test_switch_lamp_on(self):
async def test_run_without_without_failures(self):
async with self.make_script():
await self.configure_script()

await self.run_script()

# await self.lamp_status.status == ATWhiteLight.LampBasicState.ON
# Chiller
self.script.white_light_source.cmd_setChillerTemperature.set_start.assert_awaited_once_with(
temperature=self.script.chiller_temperature,
timeout=self.script.cmd_timeout,
)

await self.white_light_source.cmd_turnLampOn.set_start.assert_awaited_with(
temperature=self.script.whitelight_power,
timeout=self.script.timeout_lamp_warm_up,
self.script.white_light_source.cmd_startChiller.set.assert_awaited_once_with(
timeout=self.script.timeout_chiller_cool_down
)

await self.white_light_source.evt_lampState.aget.assert_awaited_once()
self.script.white_light_source.tel_chillerTemperatures.aget.assert_awaited_once()

await self.white_light_source.evt_lampState.next.assert_awaited_once()
# Shutter
self.script.white_light_source.cmd_openShutter.set_start.assert_awaited_once()

# async def test_set_chiller_temp_fail_on_timeout(self):
# async with self.setup_mocks():
# White lamp
self.white_light_source.cmd_turnLampOn.set_start.assert_awaited_with(
temperature=self.script.whitelight_power,
timeout=self.script.timeout_lamp_warm_up,
)
self.white_light_source.evt_lampState.next.assert_awaited_once_with(
flush=False, timeout=self.script.timeout_lamp_warm_up
)

# self.chiller_temperature_return_value = 22
# await self.script.set_chiller_temp()
# Monochromator configuration
self.monochromator.cmd_selectGrating.set_start.assert_awaited_once_with(
gratingType=self.script.grating, timeout=self.script.cmd_timeout
)

# await self.assert_execute_set_chiller_temp()
self.monochromator.cmd_changeWavelength.set_start.assert_awaited_once_with(
wavelength=self.script.wavelength, timeout=self.script.cmd_timeout
)

# async def test_run_without_without_failures(self):
# async with self.make_script():
# await self.configure_script()
self.monochromator.cmd_changeSlitWidth.set_start.assert_awaited_once_with(
slit=1,
slitWidth=self.script.entrance_slit_width,
timeout=self.script.cmd_timeout,
)

self.monochromator.cmd_changeSlitWidth.set_start.assert_awaited_once_with(
slit=2,
slitWidth=self.script.exit_slit_width,
timeout=self.script.cmd_timeout,
)

# await self.configure_mocks()
# Summary State
self.white_light_source.evt_summaryState.aget.assert_awaited_once_with()

# await self.script.arun()
self.monochromator.evt_summaryState.aget.assert_awaited_once_with()

# self.script.set_chiller_temp.assert_awaited_once()
# self.script.open_white_light_shutter.assert_awaited_once()
# self.script.switch_lamp_on.assert_awaited_once()
# self.script.configure_atmonochromator.assert_awaited_once()
assert self.chiller_status.chillerState == "READY"
assert self.lamp_status.lampState == ATWhiteLight.LampBasicState.ON
assert self.shutter_status.shutterState == ATWhiteLight.ShutterState.OPEN
assert self.grating_status.gratingType == ATMonochromator.Grating.MIRROR
# Assert wavelength and slitWidth?

async def test_executable(self):
scripts_dir = standardscripts.get_scripts_dir()
Expand Down

0 comments on commit 5575a22

Please sign in to comment.