{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.therealer.com/docs/api/schemas/device-bootstrap-response.schema.json",
  "title": "Realer device bootstrap response",
  "type": "object",
  "required": ["code", "device", "runtime", "catalog"],
  "properties": {
    "code": {
      "type": "integer",
      "enum": [2000]
    },
    "device": {
      "type": "object",
      "required": ["id", "model_api_version", "supported_model_api_version"],
      "properties": {
        "id": { "type": "string", "pattern": "^cdv_[0-9A-Za-z]{26}$" },
        "model_api_version": { "type": "string" },
        "supported_model_api_version": { "type": "boolean" }
      },
      "additionalProperties": true
    },
    "runtime": {
      "type": "object",
      "required": ["version", "api_version", "generated_at", "available_protocols", "endpoints", "oauth", "backoff", "catalog_revision", "runtime_revision"],
      "properties": {
        "version": { "type": "string", "const": "iot.v1.device_bootstrap" },
        "api_version": { "type": "string", "const": "iot.v1" },
        "generated_at": { "type": "string", "format": "date-time" },
        "available_protocols": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["https", "mqtt"]
          },
          "minItems": 1
        },
        "endpoints": {
          "type": "object",
          "required": ["bootstrap"],
          "properties": {
            "bootstrap": { "$ref": "#/$defs/endpoint" },
            "mqtt_catalog": { "$ref": "#/$defs/endpoint" }
          },
          "additionalProperties": true
        },
        "oauth": {
          "type": "object",
          "required": ["token_type", "catalog_scope"],
          "properties": {
            "token_type": { "type": "string" },
            "catalog_scope": { "type": "string" }
          },
          "additionalProperties": true
        },
        "backoff": {
          "type": "object",
          "required": ["retry_after_header", "retryable_http_statuses", "default_retry_after_seconds"],
          "properties": {
            "retry_after_header": { "type": "string" },
            "retryable_http_statuses": {
              "type": "array",
              "items": { "type": "integer" },
              "minItems": 1
            },
            "default_retry_after_seconds": { "type": "integer", "minimum": 1 }
          },
          "additionalProperties": true
        },
        "catalog_revision": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
        "runtime_revision": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" }
      },
      "additionalProperties": true
    },
    "catalog": {
      "type": "object",
      "required": ["commands", "sensors"],
      "properties": {
        "commands": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id"],
            "properties": {
              "id": { "type": "string", "pattern": "^cmd_[0-9A-Za-z]{26}$" },
              "value": { "type": "string" },
              "value_type": { "type": "string" },
              "kind": { "type": "string" },
              "pin_port": { "type": "string" },
              "error": { "type": ["string", "object"] }
            },
            "additionalProperties": true
          }
        },
        "sensors": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id"],
            "properties": {
              "id": { "type": "string", "pattern": "^sen_[0-9A-Za-z]{26}$" },
              "kind": { "type": "string" },
              "qc": { "type": "string" },
              "mu": { "type": "string" },
              "pin_port": { "type": "string" },
              "expected_update_interval_seconds": { "type": ["integer", "null"], "minimum": 1 },
              "minimum_supported_update_interval_seconds": { "type": ["integer", "null"], "minimum": 1 },
              "error": { "type": ["string", "object"] }
            },
            "additionalProperties": true
          }
        }
      },
      "additionalProperties": true
    },
    "https": {
      "type": "object",
      "required": ["protocol", "profile", "endpoints", "desired_states", "feed_data", "qos"],
      "properties": {
        "protocol": { "type": "string", "enum": ["https"] },
        "profile": { "type": "string" },
        "endpoints": {
          "type": "object",
          "properties": {
            "bootstrap": { "$ref": "#/$defs/endpoint" },
            "commands": { "$ref": "#/$defs/endpoint" },
            "sensors": { "$ref": "#/$defs/endpoint" }
          },
          "additionalProperties": true
        },
        "desired_states": {
          "type": "object",
          "required": ["endpoint", "http_method", "required_scope", "poll_after_seconds"],
          "properties": {
            "endpoint": { "type": "string" },
            "http_method": { "type": "string" },
            "required_scope": { "type": "string" },
            "poll_after_seconds": { "type": "integer", "minimum": 1 }
          },
          "additionalProperties": true
        },
        "feed_data": {
          "type": "object",
          "properties": {
            "single_endpoint": { "$ref": "#/$defs/endpoint" },
            "batch_endpoint": { "$ref": "#/$defs/endpoint" },
            "content_type": { "type": "string" },
            "batch_items_key": { "type": "string" }
          },
          "additionalProperties": true
        },
        "qos": {
          "type": "object",
          "required": ["minimum_request_interval_seconds"],
          "properties": {
            "minimum_request_interval_seconds": { "type": "integer", "minimum": 1 },
            "max_request_body_bytes": { "type": ["integer", "null"], "minimum": 1 }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    },
    "mqtt": {
      "type": "object",
      "additionalProperties": true
    },
    "mqtt_routes": {
      "type": "array",
      "items": { "type": "object" }
    }
  },
  "$defs": {
    "endpoint": {
      "type": "object",
      "required": ["endpoint", "http_method", "required_scope"],
      "properties": {
        "endpoint": { "type": "string" },
        "http_method": { "type": "string" },
        "required_scope": { "type": "string" }
      },
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}
