server – ZMQ based server

ZMQ based server to stream content and TCS-like event generators.

class tcs_lib.server.ZMQServer(url, context=None)[source]

Bases: object

Create a ZeroMQ server that publishes content on the give ulr.

Sending events is performed by the send_event() method using zmq.Socket.send_multipart().

send_event() and start() allow to send generic events.

send_tcs_event() is designed to send events that reflect TCS expectations. If the tcs_event passed to the method does not contain the __wire_time key, set it to time.time().

Parameters:
url : string

url and port to bind the socket to

context : zmq.Context, optional

context to use when creating the sockets. If not given, uses the global instance returned by zmq.Context.instance()

send_event(event)[source]

Send the event via the socket.

Parameters:
event : list of string

event to send using zmq.Socket.send_multipart(); the events are converted to byte string using string_to_bytes().

send_tcs_event(tcs_topic, tcs_event)[source]

Version of send_event() specialized to send TCS-like events.

Set __wire_time in the tcs_event to time.time().

If event is a string, decode it into a dictionary using json.loads().

Parameters:
tcs_topic : string

topic of the event

tcs_event : dict or string

event to send

start(events)[source]

Start serving events via the socket

Parameters:
events : generator yielding lists of strings or bytes

each event retrieved in a loop and sent using send_event(). If one event is None, it is not sent.

close()[source]

Close the socket

class tcs_lib.server.TCSDBReplay(db_name, sort_by='none', speedup=1.0, topics=None, convert_number=False, convert_bool=False)[source]

Bases: object

Open a TCS sqlite3 database, query it and return one entry at a time when looping or using the next() builtin function.

Parameters:
db_name : string

file containing the database

sort_by : string, optional

whether to sort or not the event_ids. Accepted values: 'none', '__data_time', '__wire_time'

speedup : float, optional

speedup to use to replay the database. A value larger than 1 fast-forwards the replay, a smaller value slows the replay down

topics : list, optional

list of topics to return when iterating. If None or [], all topics are returned.

convert_number, convert_bool : bool, optional

try to convert database entries to number (int or float) and to boolean, according to the value of the data_type column. The conversion is fail-safe

Attributes:
event_ids : iterator

results of the query for the event_id. The attribute is filled by the _reset_iter()

start_wire_time, start_iter_time : float

wire time and current time when calling __next__() the first time. Call _reset_iter() to reset them before starting a new iteration

_reset_iter()[source]

Reset the status and allow restarting the iterations.

  • Create and execute the query to retrieve the event_id and save it in event_ids. The query is done on the event table, if no ordering 'none' or the '__data_time' ordering is required, or on the attribute table, if the '__wire_time' ordering is required.
  • Unset the start_wire_time and start_iter_time

This method is called when initializing the class. It can be called to reinitialize the iterator.

Raises:
tcs_lib.DBOrderingError

if the ordering is not known

__iter__()[source]

Return the instance for use as iterator

__next__()[source]

Get the next element of event_id, build the TCS object and return it.

Returns:
list of strings or ``None``

if the topic is accepted returns [topic, json(event)], otherwise returns None

_event_dict(event_id)[source]

Create dictionary representing the event with id event_id

Parameters:
event_id : string

id of the event

Returns:
result : dictionary

event information

_convert_to_type(value, type_)[source]

Try to convert value to the given type, if the conversion is required

Parameters:
value : string

value to convert

type_ : string

type of value. Known types: “number”, “boolean”, “string”. Any unknown type is treated as a string

Returns:
value : int, float, bool or string

converted value

_convert_to_number(value)[source]

Try to convert the input value from string to int or float, in that order. If it fails, returns value unchanged

_convert_to_bool(value)[source]

Try to convert the input value from "true"/"false" to True/False. If it fails, returns value unchanged

_update_times(event_dict)[source]

Update the '__data_time' and '__wire_time' as an offset from start_iter_time according to the required speedup.

Parameters:
event_dict : dictionary

event information

Returns:
event_dict : dictionary

updated input

class tcs_lib.server.TCSMockEvent(sleep=1)[source]

Bases: object

Iterator class that always return a mock event 'lrs2.hardware.status'

Parameters:
sleep : float, optional

sleep sleep seconds between events

__iter__()[source]

Return the instance for use as iterator

__next__()[source]

Return a mock event after sleeping for one second. It never raises a StopIteration exception.

Returns:
list of strings

topic and mock event as [topic, json(event)]