{
  "type": "object",
  "additionalProperties": false,
  "required": [
    "revision",
    "selected_source",
    "double_click",
    "lanes"
  ],
  "properties": {
    "revision": {
      "type": "integer",
      "minimum": 0,
      "maximum": 2147483647
    },
    "selected_source": {
      "enum": [
        "original",
        "user"
      ]
    },
    "double_click": {
      "enum": [
        "none",
        "switch_display"
      ]
    },
    "lanes": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "items": {
        "$ref": "#/$defs/lane"
      },
      "allOf": [
        {
          "contains": {
            "properties": {
              "slot": {
                "const": 0
              }
            },
            "required": [
              "slot"
            ]
          },
          "minContains": 1,
          "maxContains": 1
        },
        {
          "contains": {
            "properties": {
              "slot": {
                "const": 1
              }
            },
            "required": [
              "slot"
            ]
          },
          "minContains": 1,
          "maxContains": 1
        }
      ]
    }
  },
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://phicomm-io.github.io/iotcloud-community-protocols/contracts/schemas/m1-display-source-set-v2.schema.json",
  "title": "M1 display_source.set complete dual-lane configuration",
  "$defs": {
    "lane": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "slot",
        "source",
        "device_id",
        "metric",
        "unit",
        "decimals",
        "scale",
        "ttl_seconds",
        "color"
      ],
      "properties": {
        "slot": {
          "type": "integer",
          "minimum": 0,
          "maximum": 1
        },
        "source": {
          "enum": [
            "none",
            "local",
            "mqtt",
            "api"
          ]
        },
        "device_id": {
          "type": "string"
        },
        "metric": {
          "enum": [
            "temperature",
            "humidity",
            "pm25",
            "formaldehyde",
            "co2",
            "pressure",
            "power",
            "energy",
            "tvoc",
            "pm10",
            "custom",
            "fan_speed_percent"
          ]
        },
        "unit": {
          "enum": [
            "",
            "°C",
            "%",
            "µg/m³",
            "mg/m³",
            "ppm",
            "Pa",
            "W",
            "kWh",
            "ppb"
          ]
        },
        "decimals": {
          "type": "integer",
          "minimum": 0,
          "maximum": 2
        },
        "scale": {
          "type": "integer",
          "enum": [
            1,
            1000
          ]
        },
        "ttl_seconds": {
          "type": "integer",
          "minimum": 1,
          "maximum": 60
        },
        "color": {
          "oneOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "mode",
                "value"
              ],
              "properties": {
                "mode": {
                  "const": "fixed"
                },
                "value": {
                  "enum": [
                    "off",
                    "red",
                    "green",
                    "yellow"
                  ]
                }
              }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "mode",
                "low",
                "high"
              ],
              "properties": {
                "mode": {
                  "const": "thresholds"
                },
                "low": {
                  "type": "number"
                },
                "high": {
                  "type": "number"
                }
              },
              "x-orderedFields": [
                "low",
                "high"
              ]
            }
          ]
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "source": {
                "const": "mqtt"
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "device_id": {
                "pattern": "^[0-9a-f]{12}$",
                "minLength": 12,
                "maxLength": 12
              },
              "metric": {
                "enum": [
                  "temperature",
                  "humidity",
                  "pm25",
                  "formaldehyde",
                  "co2",
                  "pressure",
                  "power",
                  "energy",
                  "tvoc",
                  "pm10",
                  "fan_speed_percent"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "source": {
                "enum": [
                  "none",
                  "local",
                  "api"
                ]
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "device_id": {
                "const": ""
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "source": {
                "const": "none"
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "metric": {
                "const": "custom"
              },
              "unit": {
                "const": ""
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "source": {
                "const": "local"
              }
            },
            "required": [
              "source"
            ]
          },
          "then": {
            "properties": {
              "metric": {
                "enum": [
                  "temperature",
                  "humidity",
                  "pm25",
                  "formaldehyde",
                  "co2",
                  "pressure"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "custom"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "source": {
                "enum": [
                  "none",
                  "api"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "temperature"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "°C"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "humidity"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "pm25"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "µg/m³"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "formaldehyde"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "mg/m³"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "co2"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "ppm"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "pressure"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "Pa"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "power"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "W"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "energy"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "kWh"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "tvoc"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "ppb"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "pm10"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "µg/m³"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "metric": {
                "const": "fan_speed_percent"
              }
            },
            "required": [
              "metric"
            ]
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        }
      ]
    }
  },
  "x-defaults": {
    "selected_source": "original",
    "double_click": "none",
    "lane": {
      "source": "none",
      "device_id": "",
      "metric": "custom",
      "unit": "",
      "decimals": 0,
      "scale": 1,
      "ttl_seconds": 20,
      "color": {
        "mode": "fixed",
        "value": "off"
      }
    }
  }
}
