All docsStellar ControlMission control · by Stellar Systems v1.1.0

SDKs and Integration

Python SDK

The stellar-control Python package: drivers, transports, gateways, output connectors, pass feeders, and the client of the API that runs procedures.

The Python SDK (package stellar-control, module stellar_control) implements the contracts of Stellar Control for drivers, transports, gateways and output connectors over NATS, and for pass feeders over the HTTP API. A component only declares what it does, with decorators; the SDK handles registration with the reconciler, key pair and JWT renewal, heartbeats, the bindings it receives, JetStream consumers, telecommand events (ACK 1 and ACK 2), deadlines, fragments, control verbs and reconnection.

The same package holds the client of the API: it launches runs of procedures, follows their events, answers their questions and controls them, from a script, a test bench or a notebook.

Install#

The package needs Python 3.11 or later; its dependencies are nats-py (with nkeys), httpx and websockets.

The package is available on request (contact@stellar-systems.eu) until its publication on PyPI. Once published, it installs as any package:

Shell
pip install stellar-control          # or: uv add stellar-control

Public API#

Everything is imported from the package root:

Python
from stellar_control import (
    Driver, Transport, Gateway, Connector, PassFeeder,          # components
    SemanticTc, RawSample, FileChunk, LinkContext, Link,        # messages
    Downlink, Message, Measure, Pass, Replaced,                 # helpers
    Bindings, BoundLink, Health, TcState, Timestamp, now,
    Options, RegistrationRejected, PassRefused,
    Client, Run, RunEvent, RunResult, RunStatus, Answer, Question, ApiError,   # API client
)

Components#

ClassConstructorDeclarations
DriverDriver(software, version, *, catalogue, telecommands=(), measures=(), codec=None, output=None, params_schema=None, decode_frame_errors=False, instance=None)@driver.encode, @driver.decode, @driver.echo, @driver.chunks, @driver.decode_file, @driver.decode_tc
TransportTransport(software, version, *, input, output, params_schema=None, instance=None)@transport.wrap, @transport.unwrap
GatewayGateway(software, version, *, tags=(), uplink=None, downlink=None, params_schema=None, instance=None)@gateway.send, @gateway.receive, @gateway.link_available
ConnectorConnector(name, *, software=None, version="0.1.0")@connector.on(kind)
PassFeederPassFeeder(url, identity=None, *, source, token=None, timeout=10.0)methods upsert, replace_window, passes

instance is the default instance name, <software>-1 otherwise (the connector name for a connector); STELLAR_INSTANCE overrides it.

Declarations#

DecoratorSignatureNotes
@driver.encode(tc: SemanticTc) -> bytes or str, or (tc, context: LinkContext)An exception gives ENCODE_FAILED, its message as the reason
@driver.decode(frame: bytes) -> Iterable[RawSample], or (frame, context)A frame that is not telemetry yields nothing
@driver.echo(tc: SemanticTc, frame: bytes) -> bool or NoneTrue conforming echo, False non-conforming, None not its echo
@driver.chunks(frame: bytes) -> Iterable[FileChunk]File chunks carried by a frame
@driver.decode_file(file_type: str, content: bytes) -> Iterable[RawSample] or NoneNone for a type the driver does not decode; samples carry onboard_time
@driver.decode_tc(frame: bytes, context) -> dict or NoneOptional: a telecommand of another equipment observed on a monitor link, {component, instance?, telecommand, args}; published for expect observed
@transport.wrap(unit: bytes, context: LinkContext) -> bytes or Iterable[bytes]Frames in order; an exception gives SEND_FAILED
@transport.unwrap(frame: bytes, context: LinkContext) -> Iterable[bytes] or NoneUnits the frame completes; an exception drops the frame
@gateway.send(target: str, frame: bytes) -> None, or (target, frame, context: LinkContext)Returning means SENT; an exception SEND_FAILED. With a third parameter, the context of the frame: environment, mode of the target and its parameters
@gateway.send_at(target, frame, at: Timestamp, context) -> Timestamp \| NoneOptional: emit a frame at a given time (Stellar-Send-At), returning when it left on the wire; declare Link(..., features=("timed-send",))
@gateway.configure(context: LinkContext) -> NoneOptional: called when a link is bound to the gateway, and again each time its binding changes, such as a new mode of its target; runs in the background
@gateway.receiveasync (downlink: Downlink) -> NoneReceives until the end; if it fails, the gateway is reported degraded
@gateway.link_available() -> boolLink state of the heartbeats; always available without it
@connector.on(kind)(message: Message) -> Nonemeasures, tm_raw, tc_events, run_events, alarms, files, streams; an exception has the message delivered again later
@component.rpc(verb)(request: dict) -> dictOwn control verb on stellar.ctl.rpc.<kind>.<instance>.<verb>; an exception replies {"error": …}

Decorated functions may be plain or async.

Messages#

ClassFields and helpers
SemanticTcid, target, component, telecommand, instance, link, args, environment; tc["arg"], tc.get("arg", default), tc.bytes_arg("arg") (hexadecimal to bytes), tc.name (component.telecommand)
RawSampleRawSample(component, measure, value, instance=None, onboard_time=None); bytes values are sent in hexadecimal
FileChunkFileChunk(file_id, generation, offset, data)
LinkContexttarget, link, environment, params, mode, parameters; context.param(name, default=None) reads a parameter of the link (params of the topology), context.parameter(name, default=None) a parameter of the target in its mode (rf.tx_baudrate, thermal[PAYLOAD].heater_setpoint; an instance without a value of its own takes the value given for every instance, thermal.heater_setpoint)
LinkLink(type, rate_bps, features=()): a gateway link in one direction, Link("rf", 64_000); Link("can", 1_000_000, features=("fd",)) carries CAN FD frames
Downlinkawait publish(target, frame, *, ground_time=None, deferred=False), await publish_segment(target, stream, seq, segment, *, ground_time=None), await publish_shared(frame, accepts, *, ground_time=None, deferred=False) (to the bound targets whose link accepts it, links()), await publish_frame_error(target, frame, *, reason=None, ground_time=None), set_clock(ClockState(source, state, offset_ns=None)); the ground time is now by default
Messagekind, subject, tokens, id (stable), stream_sequence, published, headers, data; json(), target, measure()
Measuretarget, component, instance, name, value, raw, time, ground_time, link, delivery
Bindings, BoundLinkThe links served, as last delivered (component.bindings); BoundLink.context(environment) resolves the parameters
HealthHEALTHY, DEGRADED
TcStatePENDING, REJECTED, ENCODED, ENCODE_FAILED, SENT, SEND_FAILED, VERIFIED, VERIFY_FAILED, VERIFY_TIMEOUT, COMPLETE, and ECHO (not a state)
Timestamp, now(), parse_time, format_time, TimeLikeTimestamp: an instant to the nanosecond (nanos since 1970 UTC), written RFC 3339 in UTC with up to 9 decimals; ordered, +/- a timedelta, nanos_since, from_datetime, to_datetime (cut to the microsecond). Every time the SDK reads is a Timestamp; wherever it takes one, a datetime also goes (TimeLike, naive meaning UTC)
Pass, Replaced, PassRefusedSee Writing a Pass Feeder

Every component#

MemberRole
run(**options)Runs until Ctrl-C or SIGTERM, settings from the environment and options; logs at STELLAR_LOG (INFO)
async with running(options=None)Runs in the background while in the block; returns once registered; registration errors are raised on entering
await serve(options=None)Runs until cancelled
set_health(health, reason=None)Health of the next heartbeats; degraded needs a reason
bindingsThe links served, as last delivered
instanceThe instance name

RegistrationRejected is raised when the reconciler refuses the registration: it is final.

Examples#

Python
from stellar_control import Driver, RawSample

driver = Driver(
    "scpi-psu", "1.0.0",
    catalogue="lab-psu@^1.0",
    telecommands=["psu.set_voltage", "psu.output_on", "psu.output_off"],
    measures=["psu.voltage", "psu.current", "psu.output_enabled"],
)

@driver.encode
def encode(tc):
    return f"VOLT {tc['voltage']}"

@driver.decode
def decode(frame):
    for field in frame.decode().split(";"):
        key, value = field.split()
        if key == "VOLT":
            yield RawSample("psu", "voltage", float(value))

driver.run()

The reference components that ship with the SDK carry a SCPI power supply into Stellar Control without hardware: the driver scpi-psu, the TCP gateway lab-eth (polling, link state, reconnection, a control verb), the connector csv-export (measures and telecommand events to CSV files, idempotently), a pass feeder, and a fake SCPI power supply on TCP in place of the hardware.

The driver of the HSC-100 camera in the Payload integration use case, drivers/hsc_100, is a complete driver on this SDK: made with stellar generate driver, then completed from the ICD of the camera (CSP packets in driver.py and csp.py), and tested frame by frame against the ICD with pytest (test_driver.py).

A gateway that follows the mode of its target#

A gateway that must adapt the medium to the mode of its target (a data rate, a modulation) reads the parameters of the target from the context. @gateway.configure applies them when the link is bound and each time the binding changes, for instance after a configure of a procedure or stellar mode; the send can also take the context of each frame:

Python
from stellar_control import Gateway, Link, LinkContext

gateway = Gateway("uhf-gateway", "1.0.0", tags=["uhf"], uplink=Link("rf", 9600))

@gateway.configure
async def configure(context: LinkContext) -> None:
    await modem.set_rate(context.parameter("rf.tx_baudrate"))

@gateway.send
async def send(target: str, frame: bytes, context: LinkContext) -> None:
    await modem.transmit(frame, power=context.parameter("rf.tx_power"))

See Modes and Parameters and Writing a Gateway.

API client#

Client talks to the HTTP API of a cell. Stellar Control executes a run (leases, confirmations, log, validated versions); the client only launches it, follows its events, answers its questions and decisions, and controls it. Everything is async.

Python
from stellar_control import Answer, Client

async with Client("http://control:8080", "alice") as control:        # or token="…"
    result = await control.execute(
        "TVAC cycle",
        environment="LAB",
        targets={"chamber": "chamber-1", "psu": "psu-1", "dut": "cam-em-1"},
        inputs={"hot": "50 degC", "cold": "-30 degC"},
        on_ask=lambda question: True,       # confirms every question
    )
    assert result.ok, result.reason

Client#

Python
Client(url, identity=None, *, token=None, timeout=10.0, reconnections=5, transport=None)
ParameterMeaning
urlAPI of Stellar Control, http://control:8080 or https://…; WebSockets use ws:// or wss:// accordingly
identityOperator declared with X-Stellar-User, where the environment accepts declared identities; ignored when token is given
tokenOIDC token, sent as Authorization: Bearer
timeoutTimeout of the HTTP requests and of the opening of a WebSocket, in seconds
reconnectionsAttempts to take up the events of a run after a lost connection
transportAn httpx transport, for tests

Use it as an async context (async with Client(...) as control), or call await control.close().

MethodReturnsDoes
await launch(procedure, *, environment, targets, inputs=None, library=None)RunPOST /v1/runs; returns once Stellar Control accepted the run. inputs are written as in a run request: "50 degC", "PAYLOAD", True, 3. library when several libraries have a procedure of that name
await execute(procedure, *, environment, targets, inputs=None, library=None, on_ask=None, on_event=None)RunResultlaunch, then Run.wait
run(id)RunA run already launched, by its identifier: to follow, answer or control it
await upload(content: bytes)strPOST /v1/uploads: stores a content and returns its hash, the value of a file input

Run#

A run launched on Stellar Control: id, ir (hash of the resolved run) and warnings (for instance a run that may outlast the pass in progress), as launch returned them.

MethodDoes
async for event in run.events(after=0)The events of the log from the start (those after position after), then as they come, up to Finished. A lost connection is taken up again, with a growing delay (0.4 s up to 5 s) and at most reconnections attempts in a row, without repeating an event; then ConnectionError
await run.wait(on_ask=None, on_event=None)Follows the run to its end and returns a RunResult. Each event goes to on_event; each question or decision to on_ask, whose reply is sent as the answer
await run.answer(path, accepted=True, value=None)Answers the question or decision of step path: value is a typed answer, or a decision ("replay", "skip", "abort")
await run.suspend()Suspends at the end of the current instruction; the leases are released
await run.resume(choice="replay")Resumes: replay the step where it stopped, skip it, or abort
await run.abort()Stops the run for good
await run.status()RunStatus: run, procedure, suspended, step, pending (a Question), finished, reason
await run.report()The HTML report of the run

RunResult holds run, ok, reason and every events of the log; result.failed_steps() lists the StepFinished attempts that failed.

Questions and answers#

on_ask receives a Question: run, path (the step asking), kind ("ask" or "decision"), prompt (the question, or why a decision is needed) and role (the role whose confirmation is awaited, for a hazardous telecommand). It may be plain or async, and returns:

ReplySent as
True / FalseA confirmation, or a refusal (the step fails)
Answer(accepted=True, value=…)A typed answer (ask … as <type> into <name>)
"replay", "skip", "abort"A decision
NoneNothing: someone else answers, from the web console, the CLI or another client

Who may answer is decided by Stellar Control, as for any answer: see Questions, Decisions and Control and Hazardous Confirmations.

Events#

run.events() and on_event give typed events, from stellar_control.client. Every event has run, seq (position in the log, from 1), at, event (its kind) and data (the event as the API sent it).

ClassKindFields
Startedstartedprocedure, library, environment, targets, ir, by, inputs
ProcedureStartedprocedure_startedpath
ProcedureFinishedprocedure_finishedpath, ok
StepStartedstep_startedpath, attempt, inputs
TelecommandSenttelecommand_sentpath, tc, target, telecommand (component[instance].telecommand), changes_state, retry_of
TelecommandFinishedtelecommand_finishedpath, tc, state, detail, acks
Checkedcheckedpath, statement, condition, ok, reason, samples
Loggedloggedpath, message, samples
Askedaskedpath, prompt, role
Answeredansweredpath, accepted, by, value, role
AnswerRefusedanswer_refusedpath, by, reason
TakenOvertaken_overby
Suspendedsuspendedpath, by
Continuedcontinuedpath, by, choice
DecisionRequireddecision_requiredpath, reason
StepFinishedstep_finishedpath, ok, attempt, reason
ModeChangedmode_changedpath, target, mode
Finishedfinishedok, reason

ModeChanged is published when a configure sets the mode of a target:

Python
from stellar_control.client import ModeChanged

if isinstance(event, ModeChanged):
    print(f"{event.target} is now in {event.mode}")

An event of a kind the SDK has no class for yet comes as a plain RunEvent, its fields in data (event.event gives its kind).

See Running Procedures for the meaning of each event.

Errors#

A request the API refuses raises ApiError: status (HTTP), errors (the [{code, message, help}] of the error body) and codes:

Python
from stellar_control import ApiError

try:
    run = await control.launch("Transmitter test", environment="FM", targets={"sat": "sat-fm-1"})
except ApiError as refused:
    if "run::must-be-scheduled" in refused.codes:
        ...

run.events() raises ApiError too when the WebSocket is refused (an unknown run).

A command-line runner#

A runner of any procedure prints its events as they come and answers at the terminal, in a few lines; a complete one, with its command line, ships with the SDK. Its core:

Python
from stellar_control.client import Checked, Finished, StepStarted, TelecommandSent

def show(event: RunEvent) -> None:
    match event:
        case StepStarted(path=path, attempt=attempt):
            print(f"▶ {path}" + (f" (attempt {attempt})" if attempt > 1 else ""))
        case TelecommandSent(telecommand=telecommand, target=target):
            print(f"  → {telecommand} to {target}")
        case Checked(condition=condition, ok=ok):
            print(f"  {'✓' if ok else '✗'} {condition}")
        case Finished(ok=ok, reason=reason):
            print("run succeeded" if ok else f"run failed: {reason}")

async with Client(args.api, args.identity) as control:
    run = await control.launch(args.procedure, environment=args.environment,
                           targets=targets, inputs=inputs)
    result = await run.wait(on_ask=ask, on_event=show)

Typed functions per procedure#

stellar generate client writes, for a library, one pair of functions per procedure with its roles, inputs and environments typed: <procedure>(client, …) follows the run to its end, launch_<procedure>(client, …) returns the Run once accepted.

Python
from hsc_100_ops import first_light          # stellar generate client hsc-100-ops -o hsc_100_ops

async with Client("https://<name>.control.stellar-systems.eu", token=token) as control:
    result = await first_light(
        control, cam="cam-sim-1",
        environment="SIM",                  # Literal["SIM", "BENCH"]: where the procedure may run
        on_ask=lambda question: True,
    )

See Code Generators. stellar generate driver writes the typed skeleton of a driver in the same way: see Writing a Driver.

Running#

component.run() reads its settings from the environment (Options.from_env()):

VariableDefault
STELLAR_NATS_URLnats://localhost:4222NATS server
STELLAR_INSTANCE<software>-1Instance name, unique per kind
STELLAR_NATS_CREDENTIALSnoneBootstrap credentials, limited to registration
STELLAR_NATS_CAnoneCA certificates (PEM) the server is checked against: TLS only
STELLAR_NATS_CERT, STELLAR_NATS_KEYnoneClient certificate and its key, for mTLS
STELLAR_LOGINFOLog level of run()

Within an application, build Options yourself and use the async context:

Python
from stellar_control import Options

async with driver.running(Options(nats_url="nats://control:4222", instance="scpi-psu-2")):
    ...

Options also has heartbeat_period (1 s), register_timeout (2 s) and metrics_period (5 s, throughput reports of a gateway). Without CA nor client certificate, a tls:// URL checks the server against the system roots.

Protocol helpers#

stellar_control.csp (CSP 1 and CSP 2 packets, CRC32), stellar_control.can (CAN frames on NATS) and stellar_control.cfp (CSP over CAN, the CFP of libcsp): see Writing a Transport.

What the Python SDK does not cover#

  • the CFDP entity of drivers, and COP-1 in transports: they are in the Rust SDK only;
  • a Prometheus endpoint.

A Python gateway still carries the frames of no telecommand (CFDP PDUs of a Rust driver) like the others, without event.

↑↓ to moveEnter to open