Robot Dokümantasyonu/Rehberler

Dolaşımı Başlat

Bu rehberde curl kullanan bir bash betiğiyle robotta dolaşım (devriye) başlatmayı göreceksiniz.

Gereksinimler:

  • Robota erişimi olan bir API anahtarı ve robotun ID'si (bkz. Başlangıç)
  • Bilgisayarınızda curl ve jq kurulu olmalı
  • En az bir tanımlı dolaşım ya da ayarlanmış bir varsayılan dolaşım rotası
#!/usr/bin/env bash
set -euo pipefail

# Configuration
ROBOT_ID="${ROBOT_ID:?Set ROBOT_ID to the robot ID}"
API_KEY="${API_KEY:?Set API_KEY to your API key}"
ROBOT_URL="${ROBOT_URL:-https://robots.saharobotik.com/api/v1/${ROBOT_ID}/robot}"
CRUISE_ROUTE="${CRUISE_ROUTE:-}"   # Optional, leave empty to use default or first available
ROUNDS="${ROUNDS:-2}"               # Number of rounds

command -v jq >/dev/null 2>&1 || { echo "jq is required (sudo apt install jq)"; exit 1; }

echo "============================================================"
echo "Start Cruise"
echo "============================================================"

echo "[1/3] Listing existing cruises..."
cruises_resp=$(curl -sS -w '\n%{http_code}' -H "x-api-key: ${API_KEY}" "${ROBOT_URL}/cruises")
cruises_status=$(echo "$cruises_resp" | tail -n 1)
cruises_json=$(echo "$cruises_resp" | sed '$d')
if [ "$cruises_status" = "200" ]; then
  cruises_count=$(echo "$cruises_json" | jq 'length')
else
  echo "⚠ Could not list cruises (HTTP ${cruises_status}): $(echo "$cruises_json" | jq -r '.message // .' 2>/dev/null || echo "$cruises_json")"
  cruises_json="[]"
  cruises_count=0
fi

if [ "$cruises_count" -gt 0 ]; then
  echo "✓ Found ${cruises_count} cruises:"; echo
  echo "$cruises_json" | jq -r 'to_entries[] | "\(.key+1). \(.value.name)\n   Site: \(.value.site_floor.site), Floor: \(.value.site_floor.floor)\n   Number of waypoints: \(.value.waypoints | length)\n"'
else
  echo "⚠ No cruises found. You should create a cruise first or ensure a default route exists."
fi

echo
echo "[2/3] Checking default cruise route..."
default_route_resp=$(curl -sS -H "x-api-key: ${API_KEY}" "${ROBOT_URL}/config/default-route" || true)
default_route=$(echo "$default_route_resp" | jq -r '.route // empty' || true)
if [ -n "$default_route" ]; then
  echo "✓ Default route: $default_route"
else
  echo "⚠ Default route not found"
fi

# Decide which route to use
route_to_use="$CRUISE_ROUTE"
if [ -z "$route_to_use" ]; then
  if [ "$cruises_count" -gt 0 ]; then
    route_to_use=$(echo "$cruises_json" | jq -r '.[0].name // empty')
    echo "Using first available cruise: $route_to_use"
  else
    route_to_use=""  # leave empty to rely on default route
  fi
fi

if [ -z "$route_to_use" ] && [ -z "$default_route" ]; then
  echo "✗ No cruise available and no default route configured. Aborting."
  exit 1
fi

echo
echo "[3/3] Starting cruise..."
start_body=$(jq -n \
  --arg cmd "CMD_START" \
  --arg route "$route_to_use" \
  --arg rounds "$ROUNDS" \
  '{cruise_cmd:$cmd, cruise_route:$route, number_of_rounds: ($rounds|tonumber)}')

start_resp=$(curl -sS -X POST "${ROBOT_URL}/cruises/control" \
  -H "x-api-key: ${API_KEY}" \
  -H 'Content-Type: application/json' \
  -d "$start_body")

ok=$(echo "$start_resp" | jq -r '.success // empty')
msg=$(echo "$start_resp" | jq -r '.message // empty')
if [ "$ok" = "true" ]; then
  using_label="$([ -n "$route_to_use" ] && echo "$route_to_use" || echo "default route")"
  echo "✓ $msg"
  echo
  echo "Cruise started: ${using_label}"
  echo "Robot started patrolling! 🚀"
else
  echo "✗ Error: ${msg}"
fi

echo
echo "============================================================"
echo "You can stop the cruise using the CMD_STOP command."
echo "============================================================"

Notlar:

  • ROBOT_ID ve API_KEY zorunludur. ROBOT_URL, CRUISE_ROUTE ve ROUNDS değerlerini ortam değişkenleriyle değiştirebilirsiniz.
  • CRUISE_ROUTE boşsa betik listedeki ilk dolaşımı kullanır; hiç dolaşım yoksa varsayılan rotayı kullanır.