CommandTelemetryClient¶
- class lsst.ts.hexrotcomm.CommandTelemetryClient(*, log: Logger, ConfigClass: Callable[[], Structure], TelemetryClass: Callable[[], Structure], host: str, port: int, connect_callback: Callable[[BaseClientOrServer], Awaitable[None]], config_callback: Callable[[Self], Coroutine], telemetry_callback: Callable[[Self], Coroutine], connect_timeout: float = 10.0)¶
Bases:
ClientTCP/IP Client for a Moog CSC.
This client cannot be reconnected; once closed, you must create a new one.
- Parameters:
- log
logging.Logger Logger.
- ConfigClass
ctypes.Structure Class for configuration.
- TelemetryClass
ctypes.Structure Class for telemetry
- host
str IP address of CSC server.
- port
int Server port.
- connect_callbackcoroutine
Coroutine to call when a connection is made or dropped. The function receives one argument: this client.
- config_callbackcoroutine
Coroutine to call when configuration is read. The function receives one argument: this client.
- telemetry_callbackcoroutine
Coroutine to call when telemetry is read. The function receives one argument: this client.
- connect_timeout
float, optional Time limit for a connection to be made (seconds).
- log
- Raises:
- TypeError
If any of the callbacks is not a coroutine.
Notes
To start a client:
client = Client(...) await client.start_task
To stop the client:
await client.stop()
- Attributes:
- header
Header The most recently read header (which may be for config, telemetry, or unrecognized).
- config
ConfigClass The most recently read configuration. A null-constructed instance before configuration is read.
- configured_task
asyncio.Future A Future that is set to None when configuration is first read and processed, or to the exception if
config_callbackraises.- telemetry
TelemetryClass The most recently read telemetry. A null-constructed instance before telemetry is read.
- config_callbackcoroutine
The
config_callbackconstructor argument.- telemetry_callbackcoroutine
The
telemetry_callbackconstructor argument.- Plus the standard attributes of `lsst.ts.tcpip.Client`.
- header
Attributes Summary
Return True if self._reader and self._writer are connected.
Methods Summary
Close the connected client socket, if any, and set done_task done.
Call self.__connect_callback.
close()Close the socket connection and cancel all tasks.
Wait for next telemetry.
read(n)Read up to n bytes.
read_into(struct)Read binary data from a stream reader into a
ctypes.Structure.Read JSON data.
Read from the Moog controller.
read_str()Read and decode a terminated str; strip the terminator.
readexactly(n)Read exactly n bytes.
readline()Read a sequence of bytes ending with
\n.readuntil([separator])Read one line, where “line” is a sequence of bytes ending with
separator.run_command(command[, interrupt])Run a command and wait for acknowledgement.
start()Connect to the low-level controller.
write(data)Write data and call
drain.write_from(*structs)Write binary data from one or more
ctypes.Structures.write_json(data)Write data in JSON format.
write_str(line)Encode, terminate, and write a str.
writelines(lines)Write an iterable of bytes and call
drain.Attributes Documentation
- connected¶
Return True if self._reader and self._writer are connected.
Note: if the other end drops the connection and if you are not trying to read data (e.g. in a background loop), then it takes the operating system awhile to realize the connection is lost. So this can return true for some unknown time after the connection has been dropped.
Methods Documentation
- async basic_close() None¶
Close the connected client socket, if any, and set done_task done.
Like
closeexcept does not clearself.should_be_connected, nor cancelself._monitor_connection_task.
- async call_connect_callback() None¶
Call self.__connect_callback.
This is always safe to call. It only calls the callback function if that function is not None and if the connection state has changed since the last time this method was called.
- async next_telemetry() Structure¶
Wait for next telemetry.
- async read(n: int) bytes¶
Read up to n bytes.
- Parameters:
- n
int The number of bytes to read. If -1 then block until the other end closes its writer, then return all data seen.
- n
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
- async read_into(struct: Structure) None¶
Read binary data from a stream reader into a
ctypes.Structure.- Parameters:
- struct
ctypes.Structure Structure to set.
- struct
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
asyncio.IncompleteReadErrorIf EOF is reached before
nbytes can be read. Use theIncompleteReadError.partialattribute to get the partially read data.
- async read_json() Any¶
Read JSON data.
Read the data with
read_strand return the json-decoded result.- Returns:
- data
typing.Any Data decoded from JSON.
- data
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
asyncio.IncompleteReadErrorIf EOF is reached before the complete separator is found and the internal buffer is reset.
LimitOverrunErrorIf the amount of data read exceeds the configured stream lmit. The data is left in the internal buffer and can be read again.
TypeErrorIf the data are of a type that cannot be decoded from JSON.
json.JSONDecodeErrorIf the data cannot be decoded from JSON.
- async read_str() str¶
Read and decode a terminated str; strip the terminator.
Read until
self.terminator, strip the terminator, and decode the data asself.encodingwith strict error handling.- Returns:
- line
str Line of data, as a str with the terminator stripped.
- line
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
asyncio.IncompleteReadErrorIf EOF is reached before the complete separator is found and the internal buffer is reset.
LimitOverrunErrorIf the amount of data read exceeds the configured stream lmit. The data is left in the internal buffer and can be read again.
UnicodeErrorIf decoding fails.
- async readexactly(n: int) bytes¶
Read exactly n bytes.
- Parameters:
- n
int The number of bytes to read.
- n
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
asyncio.IncompleteReadErrorIf EOF is reached before
nbytes can be read. Use theIncompleteReadError.partialattribute to get the partially read data.
- async readline() bytes¶
Read a sequence of bytes ending with
\n.If EOF is received and
\nwas not found, the method returns partially read data.- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
- async readuntil(separator: bytes = b'\n') bytes¶
Read one line, where “line” is a sequence of bytes ending with
separator.Read data from the stream until separator is found.
On success, the data and separator will be removed from the internal buffer (consumed). Returned data will include the separator at the end.
See also
read_str, which is more convenient for most use cases.- Parameters:
- separator
bytes The desired separator. The default matches the standard library, rather than using
terminator.
- separator
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
asyncio.IncompleteReadErrorIf EOF is reached before the complete separator is found and the internal buffer is reset.
LimitOverrunErrorIf the amount of data read exceeds the configured stream lmit. The data is left in the internal buffer and can be read again.
- async run_command(command: Command, interrupt: bool = False) float¶
Run a command and wait for acknowledgement.
- Parameters:
- Returns:
- duration
float The expected duration of the command (seconds).
- duration
- Raises:
- ValueError
If
commandis not an instance ofstructs.Command.- ConnectionError
If not connected.
- asyncio.TimeoutError
If no acknowledgement is seen in time.
- lsst.ts.salobj.ExpectedError
If the command fails.
Notes
Holds the command lock until the reply for this command is seen (or the time limit is exceeded).
- async write(data: bytes) None¶
Write data and call
drain.- Parameters:
- data
bytes The data to write.
- data
- Raises:
ConnectionErrorIf
self.connectedfalse before writing begins.
- async write_from(*structs: Structure) None¶
Write binary data from one or more
ctypes.Structures.- Parameters:
- structs
list[ctypes.Structure] Structures to write.
- structs
- Raises:
ConnectionErrorIf
self.connectedfalse before writing begins.
- async write_json(data: Any) None¶
Write data in JSON format.
Encode the data as json and write the result with
write_str.- Parameters:
- data
any The data to be written. Typically a dict, but any json-encodable data is acceptable.
- data
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
UnicodeErrorIf encoding fails.
json.JSONEncodeErrorIf the data cannot be json-encoded.
- async write_str(line: str) None¶
Encode, terminate, and write a str.
Encode the str as
self.encodingwith strict error handling, and appendself.terminator.- Parameters:
- line
str The line of data to be written.
- line
- Raises:
ConnectionErrorIf the connection is lost before, or while, reading.
UnicodeErrorIf encoding fails.
- async writelines(lines: Iterable) None¶
Write an iterable of bytes and call
drain.- Parameters:
- lines
collections.abc.Iterable[bytes] The data to write, as an iterable collection of
bytes.
- lines
- Raises:
ConnectionErrorIf
self.connectedfalse before writing begins.