{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://synacl.com/protocol/v1/schemas/device-status.json",
  "title": "Device reachability",
  "description": "Whether one device is reachable. A gateway publishes it on `<prefix>/devices/{deviceId}/status` for each device it polls; a directly connected device publishes it on `tenants/{tenantId}/devices/{deviceId}/status` (and should set it as its MQTT last will with `reachable:false`). Publish it retained. A gateway's child device is marked offline 90 s after it was last seen, so a gateway should republish `reachable:true` (or data) at least every ~30 s for each healthy device.",
  "type": "object",
  "required": [
    "ts",
    "reachable"
  ],
  "properties": {
    "ts": {
      "description": "Epoch milliseconds.",
      "type": "integer",
      "minimum": 0
    },
    "reachable": {
      "type": "boolean"
    },
    "reason": {
      "description": "Why the device is unreachable, e.g. `timeout`, `crc`, `lwt`.",
      "type": "string",
      "maxLength": 128
    },
    "bus": {
      "description": "Bus scorecard for this device (gateway firmware 1.5.7 and later, Modbus devices): transaction counters SINCE THE GATEWAY BOOTED (never reset per pass) and a latency histogram measured from the first byte sent to the last byte received. Published with every per-device status — the topic is retained, so the last snapshot survives a backend restart. The backend keeps the latest snapshot for an hour and serves it as GET /devices/:id/bus-health.",
      "type": "object",
      "required": [
        "ok",
        "to",
        "crc",
        "exc",
        "rt",
        "h"
      ],
      "properties": {
        "ok": {
          "description": "Successful transactions.",
          "type": "integer",
          "minimum": 0
        },
        "to": {
          "description": "Timeouts — no reply within the response window.",
          "type": "integer",
          "minimum": 0
        },
        "crc": {
          "description": "Replies that failed the CRC check.",
          "type": "integer",
          "minimum": 0
        },
        "exc": {
          "description": "Modbus exception replies (illegal function/address/value…).",
          "type": "integer",
          "minimum": 0
        },
        "rt": {
          "description": "Retries issued after a failed attempt.",
          "type": "integer",
          "minimum": 0
        },
        "le": {
          "description": "The most recent exception code (1..255). Omitted while no exception has been seen since boot.",
          "type": "integer",
          "minimum": 0,
          "maximum": 255
        },
        "h": {
          "description": "Latency histogram: EXACTLY eight log2 buckets counting transactions that took ≤1, ≤2, ≤4, ≤8, ≤16, ≤32, ≤64 ms, and an open-ended eighth bucket for everything above 64 ms.",
          "type": "array",
          "items": {
            "type": "integer",
            "minimum": 0
          },
          "minItems": 8,
          "maxItems": 8
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
