Skip to main content

API for interfacing with the core acquisition process via platform and language agnostic message queues.

Project description

Autotrainer API: Python Integration

The python auto-trainer-api module is intended to provide an efficient means to emit information that is needed for local or remote management of applications running locally on the device and to receive commands from those sources.

The exposed API is intended to be agnostic to the underlying transport layer. The current implementation uses ZeroMQ. The reasons for this decision include:

  • Relatively low overhead for the acquisition application
  • Does not require either side to manage connections and know when the other side is available or changes availability
  • Does not require an additional process or service to maintain a persistent message queue (e.g., RabbitMQ)
    • Persistent data is managed elsewhere

Client Integration

There are two points of integration available for clients to support the remote interface. The first allows publishing "events" for state and property changes that occur in the client. The second is a "command" interface for the client to receive command requests from remote sources.

Both interfaces are provided through an instance of the RpcService protocol. An instance can be obtained via create_api_service(...) which constructs the specific concrete implementation. After creation, the service must be explicitly started (start(...)) and can be stopped (stop(...)). Once stopped, an instance can not be restarted. If a connection should be reestablished after stopping an instance, a new instance should be created and started.

Events

Events are supported through the send_dict(topic: ApiTopic, message: dict) method on the RpcService instance.

The first argument is the appropriate ApiTopic value for the event. The most common for clients to support are

  • ApiTopic.EVENT - the topic for most events representing general activity in the about or high-level state changes
  • ApiTopic.EMERGENCY - a dedicated topic for emergency-related events such as emergency-stop/resume, alarm changes, etc. [1]
  • ApiTopic.PROPERTY_CHANGE - lower-level property changes on specific objects of interest
  • ApiTopic.COMMAND_RESULT - command responses for non-immediate commands (see Commands section)

The second argument is a dictionary whose contents depend on the topic. All elements must serializable to JSON.

ApiTopic.EVENT

The dictionary contains the following entries:

  • kind - an ApiEventKind value
  • when - a wall-clock value of time
  • index - monotonically increasing timestamp w/units if nanoseconds
  • context - an object whose contents depend on the ApiEventKind; may be None for some event kinds

ApiTopic.EMERGENCY

Still under development.

ApiTopic.PROPERTY_CHANGE

Still under development.

ApiTopic.COMMAND_RESULT

An instance of ApiCommandRequestResponse.

[1] Currently these are supported as special cases of ApiTopic.EVENT. At some point these should be transitioned to the dedicated topic.

Commands

Commands from external sources are supported by registering a CommandRequestDelegate with the RpcService instance. The delegate receives an instance of ApiCommandRequest and must return an instance of ApiCommandRequestResponse.

The primary property of the ApiCommandRequest is command which is an ApiCommand value. Depending on the command, there may also be a dictionary in the data property with arguments or other information relevant to the command. The nonce property can be ignored if the command is handled synchronously. For commands that send an ApiTopic.COMMAND_RESULT event after completion (see below), the nonce must be stored to associate with that event (along with the command value).

The returned ApiCommandRequestResponse object contains one require field

  • result - a value of ApiCommandReqeustResult

There are also three optional fields

  • data - an optional dictionary with results from the command beyond success/failure (often will be None)
  • error_code an integer error code value if the command is not successful or can't be initiated
  • error_message an integer error code value if the command is not successful or can't be initiated

The expected contents of the data property are defined by the command, but is typically None.

The error_code property should be a non-zero value if there is an error code to report.

There are two fields on the ApiCommandRequestResponse object that are ignored as part of the returned object from the command delegate: command and nonce. See Asynchronous Commands for when these fields are required.

Asynchronous Commands

The command delegate is expected to return "immediately" (low millisecond type of time frame). If the command is not deterministically fast, it is expected to immediately return an ApiCommandRequestResponse with a result value of ApiCommandReqeustResult.PENDING_WITH_NOTIFICATION.

Once the action associated with the command is complete, the client should send an Event (previous section), with a topic of ApiTopic.COMMAND_RESULT. The message argument of the send_dict method should be another instance of ApiCommandRequestResponse. The command and nonce properties of the response object should be set to the values received in the ApiCommandRequest (the client is responsible for storing these values until needed). Note that those two properties are ignored for synchronous command handling, but required for asynchronous responses.

Publishing

python -m build

python -m twine upload dist/* (requires PyPi API token)

Installation

The package is published to the PyPi package index and can be installed with standard pip commands.

pip install auto-trainer-api

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_trainer_api-0.9.6.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_trainer_api-0.9.6-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file auto_trainer_api-0.9.6.tar.gz.

File metadata

  • Download URL: auto_trainer_api-0.9.6.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.0

File hashes

Hashes for auto_trainer_api-0.9.6.tar.gz
Algorithm Hash digest
SHA256 171bd45b02902cc0ab2250a704cb1ea8851a8a06a860c1bbe92f24a5a0598484
MD5 e1d0ad5b92b748b1f244cafd6236d639
BLAKE2b-256 45bf9693d36cb70f87371bacd129570195ed3ca4e30790d32a5ed711b2b3bc9e

See more details on using hashes here.

File details

Details for the file auto_trainer_api-0.9.6-py3-none-any.whl.

File metadata

File hashes

Hashes for auto_trainer_api-0.9.6-py3-none-any.whl
Algorithm Hash digest
SHA256 962f2a9a52aefdace9a511add32b9b7f121c943ff5ec413f912339b20a0a6ee1
MD5 19751b5942eaabe18a38004d019860e6
BLAKE2b-256 3c837db38754dcad6ceae818bcbc8eba3f9d60682cac055cb9bc3ff57d7ae3a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page