# blueforsoy/control_software_gen2_api_examples architecture

> Architecture diagram of the GitHub repository blueforsoy/control_software_gen2_api_examples, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/blueforsoy/control_software_gen2_api_examples
- Repository: https://github.com/blueforsoy/control_software_gen2_api_examples

## Overview

#### Purpose and execution model

This is a Python 3.10+ example repository for integrating with Bluefors Control Software 2 (CS2). It is not a single deployable application: scripts are intended to be run or adapted by users to monitor CS2 state and issue device commands through CS2 APIs.

Primary example families are:
- HTTP command/API examples: `api_examples/`
- WebSocket monitoring: `websocket_example/monitoring.py`
- Automated cooldown/warm-up orchestration: `cooldown_scripts/`

#### CS2 API boundary

All functional scripts act as clients of a CS2 installation, typically addressed through its HTTPS API and documented by the instance’s Swagger UI. CS2 is the system of record and device-control authority; this repository supplies client-side polling, command sequencing, and monitoring examples.

Transport security is a material boundary: HTTPS clients must trust the CS2 certificate, either through the host trust store or an explicitly supplied certificate path. WebSocket connections likewise carry authenticated CS2 traffic.

Paths: `api_examples/README.md`, `websocket_example/README.md`, `cooldown_scripts/configuration.py`

#### Authentication and token lifecycle

CS2 authentication is delegated to Keycloak. The shared authentication layer obtains access tokens, refreshes them when needed, and attaches them to subsequent HTTP or WebSocket requests.

There are two authentication implementations: a reusable package-level Keycloak client and a cooldown-specific adapter used by the orchestration scripts.

Paths: `auth/keycloak.py`, `auth/__init__.py`, `cooldown_scripts/keycloak_auth.py`

#### HTTP API command examples

The HTTP examples establish the basic control-plane pattern: authenticate, call CS2 REST endpoints over HTTPS, inspect returned values, and submit device commands. They are a starting point for bespoke integrations rather than a generalized SDK, so callers own production concerns such as retries and robust error handling.

Paths: `api_examples/`, `api_examples/README.md`

#### WebSocket monitoring pipeline

The monitoring example represents the event/streaming path. It authenticates to CS2, opens a WebSocket connection, and consumes monitoring values as they arrive, avoiding a polling-only integration model. Downstream user code can use this pattern for live dashboards, alarms, or process supervision.

Paths: `websocket_example/monitoring.py`, `websocket_example/__init__.py`

#### Cooldown and warm-up orchestration

`cooldown_scripts/` is the repository’s highest-level workflow layer. It encodes staged cryogenic-system procedures: condensing, pulse-tube cooling, pump/vacuum control, full PPC handling, and warm-system operations. These scripts coordinate multiple CS2 commands and observed conditions rather than exposing isolated endpoint calls.

Paths: `cooldown_scripts/cooldown.py`, `cooldown_scripts/warm_system.py`, `cooldown_scripts/condensing.py`

#### Procedure stages and subsystem boundaries

The orchestration is decomposed by physical subsystem and operational stage:
- configuration and shared operational parameters;
- common API/process functions;
- pulse-tube cooling;
- vacuum-can pump control;
- PPC full procedure;
- condensing and overall cooldown sequencing.

This separation allows an engineer to reuse stage-specific operations while `cooldown.py` composes broader procedures.

Paths: `cooldown_scripts/configuration.py`, `cooldown_scripts/functions.py`, `cooldown_scripts/pt_cooling.py`, `cooldown_scripts/pump_vc_full.py`, `cooldown_scripts/ppc_full.py`

#### Operational constraints

Examples intentionally omit much production hardening, including comprehensive error handling and retry behavior. Any production automation built from the cooldown scripts should add command idempotency, state validation, timeout/retry policies, token-refresh failure handling, audit logging, and safe abort/interlock behavior around CS2 device-control calls.

Main entry points: [`cooldown_scripts/functions.py`](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/functions.py) (Common API functions), [`cooldown_scripts/cooldown.py`](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/cooldown.py) (Cooldown sequencer), [`auth/keycloak.py`](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/auth/keycloak.py) (Shared Keycloak client), [`websocket_example/monitoring.py`](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/websocket_example/monitoring.py) (WebSocket monitor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client examples"]
  node_http_examples["HTTP API examples<br/>Python examples"]
  node_websocket_monitor["WebSocket monitor<br/>Python monitor<br/>[monitoring.py]"]
end

subgraph group_auth["Authentication"]
  node_shared_keycloak["Shared Keycloak client<br/>auth package<br/>[keycloak.py]"]
  node_cooldown_keycloak["Cooldown auth adapter<br/>[keycloak_auth.py]"]
end

subgraph group_orchestration["Cooldown orchestration"]
  node_configuration["Operational configuration<br/>procedure config<br/>[configuration.py]"]
  node_functions["Common API functions<br/>procedure library<br/>[functions.py]"]
  node_pt_cooling["Pulse-tube cooling<br/>procedure stage<br/>[pt_cooling.py]"]
  node_pump_vc["Vacuum-can pumping<br/>procedure stage<br/>[pump_vc_full.py]"]
  node_ppc["Full PPC procedure<br/>procedure stage<br/>[ppc_full.py]"]
  node_condensing["Condensing stage<br/>procedure stage<br/>[condensing.py]"]
  node_cooldown["Cooldown sequencer<br/>workflow script<br/>[cooldown.py]"]
  node_warm_system["Warm-system procedure<br/>workflow script<br/>[warm_system.py]"]
end

subgraph group_external["CS2 services"]
  node_keycloak{{"Keycloak<br/>identity provider"}}
  node_cs2{{"CS2 installation<br/>device-control system"}}
end

node_http_examples -->|"obtains tokens"| node_shared_keycloak
node_http_examples -->|"HTTPS REST commands"| node_cs2
node_websocket_monitor -->|"obtains tokens"| node_shared_keycloak
node_websocket_monitor -->|"authenticated WebSocket"| node_cs2
node_shared_keycloak -->|"authenticate / refresh"| node_keycloak
node_cooldown_keycloak -->|"authenticate / refresh"| node_keycloak
node_functions -->|"uses auth"| node_cooldown_keycloak
node_functions -->|"HTTPS device calls"| node_cs2
node_pt_cooling -->|"uses common operations"| node_functions
node_pump_vc -->|"uses common operations"| node_functions
node_ppc -->|"uses common operations"| node_functions
node_condensing -->|"uses common operations"| node_functions
node_cooldown -->|"reads parameters"| node_configuration
node_cooldown -->|"coordinates"| node_condensing
node_cooldown -->|"coordinates"| node_pt_cooling
node_cooldown -->|"coordinates"| node_pump_vc
node_cooldown -->|"coordinates"| node_ppc
node_warm_system -->|"reads parameters"| node_configuration
node_warm_system -->|"issues operations"| node_functions

click node_websocket_monitor "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/websocket_example/monitoring.py"
click node_shared_keycloak "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/auth/keycloak.py"
click node_cooldown_keycloak "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/keycloak_auth.py"
click node_configuration "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/configuration.py"
click node_functions "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/functions.py"
click node_pt_cooling "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/pt_cooling.py"
click node_pump_vc "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/pump_vc_full.py"
click node_ppc "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/ppc_full.py"
click node_condensing "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/condensing.py"
click node_cooldown "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/cooldown.py"
click node_warm_system "https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/warm_system.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_http_examples,node_websocket_monitor toneBlue
class node_shared_keycloak,node_cooldown_keycloak toneAmber
class node_configuration,node_functions,node_pt_cooling,node_pump_vc,node_ppc,node_condensing,node_cooldown,node_warm_system toneMint
class node_keycloak,node_cs2 toneRose
```

## Components

### Client examples

- **HTTP API examples**: Python examples · Authenticate, inspect state, and submit REST commands.
- [WebSocket monitor](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/websocket_example/monitoring.py): Python monitor · Consumes live CS2 monitoring values. · `websocket_example/monitoring.py`

### Authentication

- [Shared Keycloak client](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/auth/keycloak.py): auth package · Manages reusable access-token acquisition and refresh. · `auth/keycloak.py`
- [Cooldown auth adapter](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/keycloak_auth.py): auth adapter · Authentication implementation for procedure scripts. · `cooldown_scripts/keycloak_auth.py`

### Cooldown orchestration

- [Operational configuration](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/configuration.py): procedure config · `cooldown_scripts/configuration.py`
- [Common API functions](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/functions.py): procedure library · `cooldown_scripts/functions.py`
- [Pulse-tube cooling](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/pt_cooling.py): procedure stage · `cooldown_scripts/pt_cooling.py`
- [Vacuum-can pumping](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/pump_vc_full.py): procedure stage · `cooldown_scripts/pump_vc_full.py`
- [Full PPC procedure](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/ppc_full.py): procedure stage · `cooldown_scripts/ppc_full.py`
- [Condensing stage](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/condensing.py): procedure stage · `cooldown_scripts/condensing.py`
- [Cooldown sequencer](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/cooldown.py): workflow script · Composes staged cryogenic cooldown operations. · `cooldown_scripts/cooldown.py`
- [Warm-system procedure](https://github.com/blueforsoy/control_software_gen2_api_examples/blob/main/cooldown_scripts/warm_system.py): workflow script · `cooldown_scripts/warm_system.py`

### CS2 services

- **Keycloak**: identity provider · Delegated CS2 authentication authority.
- **CS2 installation**: device-control system · System of record and device-control authority, reached through authenticated HTTPS and WebSocket APIs.

## Connections

- HTTP API examples → Shared Keycloak client: obtains tokens
- HTTP API examples → CS2 installation: HTTPS REST commands · Clients must trust the CS2 certificate.
- WebSocket monitor → Shared Keycloak client: obtains tokens
- WebSocket monitor → CS2 installation: authenticated WebSocket
- Shared Keycloak client → Keycloak: authenticate / refresh
- Cooldown auth adapter → Keycloak: authenticate / refresh
- Common API functions → Cooldown auth adapter: uses auth
- Common API functions → CS2 installation: HTTPS device calls
- Pulse-tube cooling → Common API functions: uses common operations
- Vacuum-can pumping → Common API functions: uses common operations
- Full PPC procedure → Common API functions: uses common operations
- Condensing stage → Common API functions: uses common operations
- Cooldown sequencer → Operational configuration: reads parameters
- Cooldown sequencer → Condensing stage: coordinates
- Cooldown sequencer → Pulse-tube cooling: coordinates
- Cooldown sequencer → Vacuum-can pumping: coordinates
- Cooldown sequencer → Full PPC procedure: coordinates
- Warm-system procedure → Operational configuration: reads parameters
- Warm-system procedure → Common API functions: issues operations
