Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor some lewis emulator files #151

Merged
merged 6 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lewis_emulators/danfysik/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _initialize_data(self):
Sets the initial state of the device.
"""
self.comms_initialized = False
self.device_available = True
self.connected = True

self.field = 0
self.field_sp = 0
Expand Down
1 change: 1 addition & 0 deletions lewis_emulators/danfysik/interfaces/dfkps_8000.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Danfysik8000StreamInterface(CommonStreamInterface, StreamInterface):
CmdBuilder("init_comms").escape("UNLOCK").build(),
]

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_status(self):
"""
Expand Down
5 changes: 4 additions & 1 deletion lewis_emulators/danfysik/interfaces/dfkps_8500.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Danfysik8500StreamInterface(CommonStreamInterface, StreamInterface):
CmdBuilder("set_slew_rate").escape("W").arg(r"[1-3]", argument_mapping=int).spaces().int().eos().build()
]

@conditional_reply("device_available")
@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_status(self):
"""
Expand Down Expand Up @@ -79,14 +79,17 @@ def get_status(self):
def set_address(self, value):
self.device.set_address(value)

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_address(self):
return "{:03d}".format(self.address)

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_slew_rate(self, dac_num):
return self.device.get_slew_rate(dac_num)

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def set_slew_rate(self, dac_num, slew_rate_value):
self.device.set_slew_rate(dac_num, slew_rate_value)
1 change: 1 addition & 0 deletions lewis_emulators/danfysik/interfaces/dfkps_8800.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Danfysik8800StreamInterface(CommonStreamInterface, StreamInterface):
CmdBuilder("init_comms").escape("ADR 000").build(),
]

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_status(self):
"""
Expand Down
5 changes: 4 additions & 1 deletion lewis_emulators/danfysik/interfaces/dfkps_9X00.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Danfysik9X00StreamInterface(CommonStreamInterface, StreamInterface):
CmdBuilder("set_slew_rate").escape("W").arg(r"[1-3]", argument_mapping=int).spaces().int().eos().build()
]

@conditional_reply("device_available")
@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_status(self):
"""
Expand Down Expand Up @@ -77,14 +77,17 @@ def get_status(self):
def set_address(self, value):
self.device.set_address(value)

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_address(self):
return "{:03d}".format(self.address)

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_slew_rate(self, dac_num):
return self.device.get_slew_rate(dac_num)

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def set_slew_rate(self, dac_num, slew_rate_value):
self.device.set_slew_rate(dac_num, slew_rate_value)
20 changes: 15 additions & 5 deletions lewis_emulators/danfysik/interfaces/dfkps_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from lewis.utils.command_builder import CmdBuilder
from lewis.utils.replies import conditional_reply

if_available = conditional_reply("device_available")


@has_log
Expand All @@ -21,6 +20,7 @@ class CommonStreamInterface(object):
in_terminator = "\r"
out_terminator = ""


commands = [
CmdBuilder("get_voltage").escape("AD 2").eos().build(),
CmdBuilder("set_polarity").escape("PO ").arg(r"\+|-").eos().build(),
Expand All @@ -42,47 +42,56 @@ def handle_error(self, request, error):
"""
self.log.error("An error occurred at request " + repr(request) + ": " + repr(error))

@conditional_reply("device_available")

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_current(self):
return int(round(self.device.get_current()))

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def set_current(self, value):
self.device.set_current(value)

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_last_setpoint(self):
return int(round(self.device.get_last_setpoint()))

@conditional_reply("device_available")
@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_voltage(self):
return int(round(self.device.get_voltage()))

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def unlock(self):
"""
Unlock the device. Implementation could be put in in future.
"""


@conditional_reply("connected")
@conditional_reply("comms_initialized")
def get_polarity(self):
return "-" if self.device.negative_polarity else "+"

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def set_polarity(self, polarity):
assert polarity in ["+", "-"]
self.device.negative_polarity = polarity == "-"

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def set_power_off(self):
self.device.power = False

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def set_power_on(self):
self.device.power = True

@conditional_reply("connected")
@conditional_reply("comms_initialized")
def reset_device(self):
self.device.reset()
Expand All @@ -92,7 +101,8 @@ def get_status(self):
"""
Respond to the get_status command.
"""
@if_available

@conditional_reply("connected")
def init_comms(self):
"""
Initialize comms of device
Expand Down
2 changes: 1 addition & 1 deletion lewis_emulators/edwardstic/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _initialize_data(self):
self._gauge_pressure = 0.0
self._gauge_units = GaugeUnits.Pa

self.is_connected = True
self.connected = True

def _get_state_handlers(self):
return {
Expand Down
30 changes: 15 additions & 15 deletions lewis_emulators/edwardstic/interfaces/stream_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def handle_error(self, request, error):

self.log.info("An error occurred at request {}: {}".format(request, error))

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_set_standby(self, switch):
self._device.turbo_set_standby(switch)

return "*C908 0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_get_standby(self):
return_string = "=V908 {stdby_state};0;0"

Expand All @@ -119,22 +119,22 @@ def turbo_get_standby(self):

return return_string.format(stdby_state=standby_state)

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_start_stop(self, switch):
self.log.info("turbo start stop command received")
self._device.turbo_start_stop(switch)

return "*C904 0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def get_turbo_state(self):
state_string = "=V904 {turbo_state};{alert};{priority}"

return state_string.format(turbo_state=reverse_dict_lookup(PUMPSTATES_MAP, self._device.turbo_pump),
alert=self._device.turbo_alert,
priority=PRIORITYSTATES_MAP[self._device.turbo_priority])

@conditional_reply("is_connected")
@conditional_reply("connected")
def get_turbo_status(self):
output_string = "*C904 {state};{alert};{priority}"

Expand All @@ -144,43 +144,43 @@ def get_turbo_status(self):

return output_string.format(state=state, alert=alert, priority=priority)

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_get_speed(self):
return "=V905 1;0;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_get_sft(self):
return "=S905 1;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_get_power(self):
return "=V906 1;0;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_get_norm(self):
return "=V907 4;0;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def turbo_get_cycle(self):
return "=V909 1;0;0;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def backing_get_status(self):
return "=V910 1;0;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def backing_start_stop(self, switch):
return "*C910 0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def backing_get_speed(self):
return "=V911 1;0;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def backing_get_power(self):
return "=V912 1;0;0"

@conditional_reply("is_connected")
@conditional_reply("connected")
def get_gauge(self, gauge_id):
state_string = "=V91{gauge_id} {pressure};{units};{gauge_state};{alert};{priority}"

Expand Down
2 changes: 1 addition & 1 deletion lewis_emulators/fzj_dd_fermi_chopper/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _initialize_data(self):
self.error_on_set_magnetic_bearing = None
self.error_on_set_drive_mode = None

self.disconnected = False
self.connected = True

def _get_state_handlers(self):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from lewis.adapters.stream import StreamInterface
from lewis.core.logging import has_log
from lewis.utils.replies import conditional_reply

from lewis.utils.command_builder import CmdBuilder

Expand Down Expand Up @@ -41,6 +42,7 @@ def handle_error(self, request, error):

self.log.error("An error occurred at request " + repr(request) + ": " + repr(error))

@conditional_reply("connected")
def set_frequency(self, chopper_name, frequency):

"""
Expand All @@ -52,9 +54,6 @@ def set_frequency(self, chopper_name, frequency):

Returns: OK or error
"""

if self._device.disconnected:
return None
if self._device.error_on_set_frequency is None:
self._device.frequency_setpoint = frequency * self._device.frequency_reference
reply = "{chopper_name}OK".format(chopper_name=chopper_name)
Expand All @@ -64,6 +63,7 @@ def set_frequency(self, chopper_name, frequency):
self.log.info(reply)
return reply

@conditional_reply("connected")
def set_phase(self, chopper_name, phase):

"""
Expand All @@ -75,9 +75,6 @@ def set_phase(self, chopper_name, phase):

Returns: OK or error
"""

if self._device.disconnected:
return None
if self._device.error_on_set_phase is None:
self._device.phase_setpoint = phase
reply = "{chopper_name}OK".format(chopper_name=chopper_name)
Expand All @@ -87,6 +84,7 @@ def set_phase(self, chopper_name, phase):
self.log.info(reply)
return reply

@conditional_reply("connected")
def set_magnetic_bearing(self, chopper_name, magnetic_bearing):

"""
Expand All @@ -98,9 +96,6 @@ def set_magnetic_bearing(self, chopper_name, magnetic_bearing):

Returns: OK or error
"""

if self._device.disconnected:
return None
if self._device.error_on_set_magnetic_bearing is None:
# Lookup the bool representation of the string
inverted_on_off_dict = {str_val: bool_val for (bool_val, str_val) in ON_OFF.items()}
Expand All @@ -112,6 +107,7 @@ def set_magnetic_bearing(self, chopper_name, magnetic_bearing):
self.log.info(reply)
return reply

@conditional_reply("connected")
def set_drive_mode(self, chopper_name, drive_mode):

"""
Expand All @@ -123,9 +119,6 @@ def set_drive_mode(self, chopper_name, drive_mode):

Returns: OK or error
"""

if self._device.disconnected:
return None
if self._device.error_on_set_drive_mode is None:
# Lookup the bool representation of the string
inverted_start_stop_dict = {str_val: bool_val for (bool_val, str_val) in START_STOP.items()}
Expand All @@ -137,6 +130,7 @@ def set_drive_mode(self, chopper_name, drive_mode):
self.log.info(reply)
return reply

@conditional_reply("connected")
def get_magnetic_bearing_status(self, chopper_name):

"""
Expand All @@ -147,12 +141,10 @@ def get_magnetic_bearing_status(self, chopper_name):

Returns: magnetic bearing status
"""

if self._device.disconnected:
return None
device = self._device
return "{0:3s};MBON?;{}".format(device.chopper_name, self._device.magnetic_bearing_status)

@conditional_reply("connected")
def get_all_status(self, chopper_name):

"""
Expand All @@ -165,7 +157,7 @@ def get_all_status(self, chopper_name):
"""

device = self._device
if self._device.disconnected or chopper_name != device.chopper_name:
if chopper_name != device.chopper_name:
return None

values = [
Expand Down Expand Up @@ -203,5 +195,4 @@ def get_all_status(self, chopper_name):
]

status_string = ";".join(values)

return status_string