Skip to main content

Examples of using the API

Examples

Examples of using the web API endpoints to control a pioreactor called worker02, part of a cluster controlled by the leader leader, in an experiment Demo experiment 6.

#!/usr/bin/env bash
set -euo pipefail

LEADER_URL="${LEADER_URL:-http://leader.local}"
UNIT="${UNIT:-worker02}"
EXPERIMENT="${EXPERIMENT:-Demo experiment 6}"

# URL-encode the experiment (space-safe) using curl only.
EXPERIMENT_ENC=$(curl -sS --get --data-urlencode "v=${EXPERIMENT}" "" | sed -e 's/^.*v=//' )

json_post() {
local path="$1"
local body="$2"
curl -sS -X POST -H "Content-Type: application/json" \
--data "$body" \
"${LEADER_URL}${path}"
}

# RUN STIRRING
json_post "/api/workers/${UNIT}/jobs/run/job_name/stirring/experiments/${EXPERIMENT_ENC}" \
'{"options": {"target_rpm": "600"}}'

# UPDATE STIRRING RPM
json_post "/api/workers/${UNIT}/jobs/update/job_name/stirring/experiments/${EXPERIMENT_ENC}" \
'{"settings": {"target_rpm": "500"}}'

# STOP STIRRING
curl -sS -X POST "${LEADER_URL}/api/workers/${UNIT}/jobs/stop/job_name/stirring/experiments/${EXPERIMENT_ENC}"

# CHANGE LEDS
json_post "/api/workers/${UNIT}/jobs/run/job_name/led_intensity/experiments/${EXPERIMENT_ENC}" \
'{"options": {"A": 60}}'

# START THERMOSTAT AT 35C
json_post "/api/workers/${UNIT}/jobs/run/job_name/temperature_automation/experiments/${EXPERIMENT_ENC}" \
'{"options": {"automation_name": "thermostat", "target_temperature": 35}}'

# CHANGE TARGET TEMP TO 32C
json_post "/api/workers/${UNIT}/jobs/update/job_name/temperature_automation/experiments/${EXPERIMENT_ENC}" \
'{"settings": {"target_temperature": 32}}'

# STOP THERMOSTAT
curl -sS -X POST "${LEADER_URL}/api/workers/${UNIT}/jobs/stop/job_name/temperature_automation/experiments/${EXPERIMENT_ENC}"

# GET RUNNING JOBS (returns task id, then poll /unit_api/task_results/...)
running_jobs_task=$(curl -sS "${LEADER_URL}/api/workers/${UNIT}/jobs/running")
result_url_path=$(echo "$running_jobs_task" | jq -r '.result_url_path')

# POLL TASK RESULT UNTIL COMPLETE
for _ in $(seq 1 60); do
result=$(curl -sS "${LEADER_URL}${result_url_path}")
status=$(echo "$result" | jq -r '.status')
if [ "$status" = "complete" ]; then
echo "$result"
break
elif [ "$status" = "failed" ]; then
echo "$result" >&2
break
fi
sleep 0.5
done

# TIP: use UNIT="$broadcast" to target all active workers.