{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://interchange.stellarview.net/schemas/asset.schema.json",
  "title": "An asset record: every maintained thing, a sensor included, with how each field was got",
  "allOf": [
    {
      "$ref": "common.schema.json#/$defs/provenance"
    }
  ],
  "type": "object",
  "required": [
    "kind",
    "asset_id",
    "asset_class",
    "name",
    "codes",
    "location",
    "integration",
    "source",
    "read_at",
    "validated_at"
  ],
  "unevaluatedProperties": false,
  "$defs": {
    "basis": {
      "enum": [
        "loaded",
        "read",
        "generated",
        "guessed"
      ]
    },
    "value": {
      "description": "A value, or the honest word for its absence.",
      "anyOf": [
        {
          "type": [
            "string",
            "number"
          ]
        },
        {
          "enum": [
            "not public",
            "not yet captured",
            "not known"
          ]
        }
      ]
    },
    "snapshot": {
      "type": "object",
      "description": "The target asset's facts the events need, copied from the asset system. Refreshed on a schedule and when the asset changes; never fetched per reading.",
      "required": [
        "asset_id",
        "name",
        "asset_class",
        "codes",
        "location",
        "criticality",
        "maintenance_owner",
        "snapshot_at",
        "from"
      ],
      "additionalProperties": false,
      "properties": {
        "asset_id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "asset_class": {
          "$ref": "common.schema.json#/$defs/asset_class_id"
        },
        "codes": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "code",
              "value"
            ],
            "additionalProperties": false,
            "properties": {
              "code": {
                "type": "string"
              },
              "value": {
                "type": "string"
              }
            }
          }
        },
        "location": {
          "type": "object",
          "additionalProperties": {
            "type": [
              "string",
              "number",
              "array"
            ]
          }
        },
        "criticality": {
          "enum": [
            "low",
            "medium",
            "high",
            "critical",
            "not known"
          ]
        },
        "maintenance_owner": {
          "type": "string"
        },
        "snapshot_at": {
          "$ref": "common.schema.json#/$defs/time"
        },
        "from": {
          "type": "string",
          "description": "The asset system the snapshot was read from."
        }
      }
    }
  },
  "properties": {
    "kind": {
      "const": "asset"
    },
    "asset_id": {
      "type": "string",
      "minLength": 1
    },
    "asset_class": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*$",
      "description": "A catalogue asset class; a sensor's is its sensor type (sensor.plc_photo_eye)."
    },
    "name": {
      "type": "string"
    },
    "codes": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "code",
          "value",
          "owner"
        ],
        "additionalProperties": false,
        "properties": {
          "code": {
            "type": "string"
          },
          "value": {
            "$ref": "#/$defs/value"
          },
          "owner": {
            "type": "string"
          },
          "source": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      }
    },
    "location": {
      "type": "object",
      "required": [
        "site"
      ],
      "additionalProperties": false,
      "properties": {
        "site": {
          "type": "string"
        },
        "building": {
          "type": "string"
        },
        "level": {
          "type": "string"
        },
        "section": {
          "type": "string"
        },
        "room": {
          "type": "string"
        },
        "position": {
          "type": "array",
          "items": {
            "type": "number"
          },
          "minItems": 3,
          "maxItems": 3
        },
        "tolerance_m": {
          "type": "number",
          "minimum": 0
        }
      }
    },
    "purpose": {
      "type": "string"
    },
    "vendor": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/value"
      }
    },
    "integration": {
      "type": "object",
      "required": [
        "monitoring",
        "interface",
        "adapter"
      ],
      "additionalProperties": false,
      "properties": {
        "monitoring": {
          "enum": [
            "none",
            "local only",
            "networked",
            "not known"
          ]
        },
        "interface": {
          "type": "string"
        },
        "adapter": {
          "type": "string"
        },
        "adapter_state": {
          "enum": [
            "exists",
            "designed",
            "none"
          ]
        },
        "event_types": {
          "type": "array",
          "items": {
            "$ref": "common.schema.json#/$defs/event_type_id"
          }
        },
        "gap": {
          "type": "string"
        }
      }
    },
    "role_links": {
      "type": "array",
      "description": "Roles linked to this asset; alert rules route to them.",
      "items": {
        "type": "string"
      }
    },
    "field_basis": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/basis"
      }
    },
    "source": true,
    "read_at": true,
    "validated_at": true,
    "criticality": {
      "enum": [
        "low",
        "medium",
        "high",
        "critical",
        "not known"
      ],
      "description": "How much depends on it. Not known until the owner rates it."
    },
    "sensor": {
      "type": "object",
      "description": "Set when the asset is a sensor. A sensor is an asset: installed, calibrated, maintained, replaced. It observes its target asset.",
      "required": [
        "sensor_type",
        "target_asset",
        "target_snapshot",
        "binding",
        "retention",
        "state"
      ],
      "additionalProperties": false,
      "properties": {
        "sensor_type": {
          "type": "string",
          "pattern": "^sensor\\.[a-z][a-z0-9_]*$"
        },
        "target_asset": {
          "type": "string",
          "minLength": 1,
          "description": "The asset tag of what the sensor observes (the carousel). Events name it as their asset."
        },
        "target_snapshot": {
          "$ref": "#/$defs/snapshot"
        },
        "binding": {
          "type": "object",
          "required": [
            "source",
            "device",
            "basis"
          ],
          "additionalProperties": false,
          "properties": {
            "source": {
              "type": "string"
            },
            "device": {
              "type": "string",
              "description": "The device or point name as the source sends it."
            },
            "basis": {
              "enum": [
                "loaded",
                "read",
                "generated",
                "guessed"
              ]
            },
            "basis_note": {
              "type": "string"
            }
          }
        },
        "point_name": {
          "type": "string"
        },
        "retention": {
          "type": "object",
          "required": [
            "mode",
            "basis"
          ],
          "additionalProperties": false,
          "properties": {
            "mode": {
              "enum": [
                "full",
                "summary"
              ],
              "description": "full: every reading kept, from day one (trend points). summary: one summary per interval."
            },
            "summary_interval_s": {
              "type": "integer",
              "minimum": 60
            },
            "basis": {
              "type": "string"
            }
          },
          "if": {
            "properties": {
              "mode": {
                "const": "summary"
              }
            }
          },
          "then": {
            "required": [
              "summary_interval_s"
            ]
          }
        },
        "state": {
          "enum": [
            "reported",
            "needs sensor",
            "to confirm"
          ],
          "description": "The pass 3 plan's state: the asset reports it today, a sensor is needed, or to confirm."
        },
        "plan_ref": {
          "type": "string"
        },
        "calibration_due": {
          "type": "string",
          "format": "date"
        }
      }
    }
  }
}