{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://qwatt.org/protocol/interval.schema.json",
  "x-schema-version": "1.1.0",
  "title": "QWATT normalized interval record",
  "description": "What every ingestion adapter (Modbus RTU/TCP, HTTP/JSON push, CSV replay) emits, and the only shape the gate engine accepts. A meter is supported by writing a register map that produces this — never by editing the engine. Every numeric field is an integer (milliwatt-hours, milliwatts, seconds) so the canonical JSON hashes identically in every language; see docs/ARCHITECTURE-FREEZE.md §1. v1.1.0 replaced the floating-point `wh`/`w_mean` of v1.0.0 with integer `mwh`/`mw_mean`.",
  "type": "object",
  "required": ["spec_version", "schema_version", "site_id", "node_id", "seq", "ts_start", "ts_end", "dt_s", "meters", "source"],
  "additionalProperties": false,
  "properties": {
    "spec_version": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$", "description": "gates.spec.json version the node was configured with." },
    "schema_version": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$", "description": "This schema's version (x-schema-version)." },
    "site_id": { "type": "string", "minLength": 1, "description": "Registered site identifier (hash or slug)." },
    "node_id": { "type": "string", "minLength": 1, "description": "Node identity — the public key of the node's signing key once nodes sign." },
    "seq": { "type": "integer", "minimum": 0, "description": "Strictly increasing per node. Replay protection." },
    "ts_start": { "type": "string", "format": "date-time", "description": "Interval start in canonical UTC, exactly as Date#toISOString writes it (YYYY-MM-DDTHH:mm:ss.sssZ), a multiple of dt_s from the epoch. One spelling per instant: the assembler refuses any other (ingest.off_grid); the HTTP gateway rewrites any other RFC 3339 spelling of the same instant to this form and keeps the request hash in source.raw_sha256." },
    "ts_end": { "type": "string", "format": "date-time", "description": "Interval end in the same canonical UTC form, exactly ts_start + dt_s." },
    "dt_s": { "type": "integer", "minimum": 1, "description": "Interval length in seconds. Must equal ts_end − ts_start within 1 s." },
    "clock_skew_s": { "type": "integer", "description": "Receive time − ts_end, whole seconds, stamped by the gateway. Every sample path (Modbus, CSV, HTTP /samples, the simulator through CSV) gets it from the IntervalBuilder when it closes the interval: from a real clock, or in a replay from the replay clock (the latest sample timestamp pushed). Records posted whole to /intervals are stamped by the HTTP gateway. Negative when an interval is closed before its end (shutdown, end of file); G0 judges its magnitude. Absent at the node." },
    "meters": {
      "type": "object",
      "required": ["a"],
      "additionalProperties": false,
      "properties": {
        "a": { "$ref": "#/$defs/meter" },
        "b": { "oneOf": [ { "$ref": "#/$defs/meter" }, { "type": "null" } ] }
      },
      "description": "Two independent instruments. In symmetric profiles both measure the same DC quantity. In ac_dc_ratio profiles `a` is DC before the inverter and `b` is AC after it."
    },
    "source": {
      "type": "object",
      "required": ["adapter"],
      "additionalProperties": false,
      "properties": {
        "adapter": { "type": "string", "enum": ["modbus-rtu", "modbus-tcp", "http-json", "csv", "replay", "simulator", "demo"] },
        "register_map": { "type": "string", "description": "Name of the register-map config that produced this record, e.g. `pzem-017@1`." },
        "raw_sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$", "description": "Hash of the raw adapter payload, kept so the normalization itself is auditable." }
      }
    }
  },
  "$defs": {
    "meter": {
      "type": "object",
      "required": ["id", "class", "mwh"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1, "description": "Instrument identity, e.g. `pzem-017:0x01`." },
        "class": { "type": "string", "enum": ["dc", "ac"] },
        "mwh": { "type": "integer", "minimum": 0, "description": "Energy over the interval, milliwatt-hours." },
        "mw_mean": { "type": "integer", "minimum": 0, "description": "Mean power over the interval, milliwatts. Optional; enables the energy/power consistency check." },
        "samples": { "type": "integer", "minimum": 0, "description": "Number of raw samples integrated into mwh." }
      }
    }
  }
}
