{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "title": "Alarm Configuration",
    "description": "Lists alarms",
    "limited": false,
    "strictProps": false,
    "definitions": {
        "emailRecipient": {
            "title": "E-mail recipient",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "title": "Type",
                    "enum": [
                        "email"
                    ],
                    "default": "email",
                    "options": {
                        "hidden": true
                    },
                    "propertyOrder": 1
                },
                "to": {
                    "type": "string",
                    "title": "E-mail address",
                    "minLength": 1,
                    "propertyOrder": 2
                },
                "subject": {
                    "type": "string",
                    "title": "Subject",
                    "description": "{} will be replaced with alarm message text",
                    "propertyOrder": 3
                }
            },
            "required": [
                "type",
                "to"
            ]
        },
        "smsRecipient": {
            "title": "SMS recipient",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "title": "Type",
                    "enum": [
                        "sms"
                    ],
                    "default": "sms",
                    "options": {
                        "hidden": true
                    },
                    "propertyOrder": 1
                },
                "to": {
                    "type": "string",
                    "title": "Phone number",
                    "minLength": 1,
                    "propertyOrder": 2
                },
                "command": {
                    "type": "string",
                    "title": "Command",
                    "propertyOrder": 3
                }
            },
            "required": [
                "type",
                "to"
            ]
        },
        "alarmBase": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "title": "Alarm name",
                    "minLength": 1,
                    "propertyOrder": 1
                },
                "cell": {
                    "type": "string",
                    "title": "Cell",
                    "description": "Use the following format: device/control",
                    "pattern": "^[^/+#]+/[^/+#]+$",
                    "minLength": 3,
                    "propertyOrder": 2
                },
                "alarmMessage": {
                    "type": "string",
                    "title": "Alarm activation message",
                    "description": "{} will be replaced by target cell value",
                    "propertyOrder": 3
                },
                "noAlarmMessage": {
                    "type": "string",
                    "title": "Alarm deactivation message",
                    "description": "{} will be replaced by target cell value",
                    "propertyOrder": 4
                },
                "interval": {
                    "type": "integer",
                    "title": "Alarm interval in seconds",
                    "description": "If specified, alarm messages will be repeated while the alarm is active",
                    "propertyOrder": 5
                },
                "maxCount": {
                    "type": "integer",
                    "title": "Maximum number of messages",
                    "description": "Maximum number of messages to send while the alarm is active",
                    "propertyOrder": 6
                },
                "alarmDelayMs": {
                    "type": "integer",
                    "title": "Alarm Activation Delay",
                    "description": "If set, the alarm condition must be violated for the specified time interval (in ms) for the alarm to be triggered",
                    "minimum": 0,
                    "default": 0,
                    "propertyOrder": 7
                },
                "noAlarmDelayMs": {
                    "type": "integer",
                    "title": "Alarm Deactivation Delay",
                    "description": "If set, the alarm condition must be met for the specified time interval (in ms) for the alarm to be cleared",
                    "minimum": 0,
                    "default": 0,
                    "propertyOrder": 8
                }
            }
        },
        "minValue": {
            "type": "number",
            "title": "Minimum value",
            "description": "Alarm activates when cell value is less than the minimum value"
        },
        "maxValue": {
            "type": "number",
            "title": "Maximum value",
            "description": "Alarm activates when cell value is greater than the maximum value"
        },
        "expectedValueAlarm": {
            "title": "Expected Value Alarm",
            "defaultProperties": [
                "name",
                "cell",
                "alarmMessage",
                "noAlarmMessage",
                "alarmDelayMs",
                "expectedValue"
            ],
            "allOf": [
                {
                    "$ref": "#/definitions/alarmBase"
                },
                {
                    "properties": {
                        "expectedValue": {
                            "title": "Expected value",
                            "description": "Alarm activates when cell value differs from the expected value"
                        }
                    },
                    "required": [
                        "name",
                        "cell",
                        "expectedValue"
                    ]
                }
            ]
        },
        "minValueAlarm": {
            "title": "Minimum Value Alarm",
            "defaultProperties": [
                "name",
                "cell",
                "alarmMessage",
                "noAlarmMessage",
                "alarmDelayMs",
                "minValue"
            ],
            "allOf": [
                {
                    "$ref": "#/definitions/alarmBase"
                },
                {
                    "properties": {
                        "minValue": {
                            "$ref": "#/definitions/minValue",
                            "propertyOrder": 10
                        }
                    },
                    "required": [
                        "name",
                        "cell",
                        "minValue"
                    ]
                },
                {
                    "not": {
                        "required": [
                            "maxValue"
                        ]
                    }
                }
            ]
        },
        "maxValueAlarm": {
            "title": "Maximum Value Alarm",
            "defaultProperties": [
                "name",
                "cell",
                "alarmMessage",
                "noAlarmMessage",
                "alarmDelayMs",
                "maxValue"
            ],
            "allOf": [
                {
                    "$ref": "#/definitions/alarmBase"
                },
                {
                    "properties": {
                        "maxValue": {
                            "$ref": "#/definitions/maxValue",
                            "propertyOrder": 10
                        }
                    },
                    "required": [
                        "name",
                        "cell",
                        "maxValue"
                    ]
                },
                {
                    "not": {
                        "required": [
                            "minValue"
                        ]
                    }
                }
            ]
        },
        "minMaxValueAlarm": {
            "title": "Minimum&Maximum Value Alarm",
            "defaultProperties": [
                "name",
                "cell",
                "alarmMessage",
                "noAlarmMessage",
                "minValue",
                "maxValue"
            ],
            "allOf": [
                {
                    "$ref": "#/definitions/alarmBase"
                },
                {
                    "properties": {
                        "minValue": {
                            "$ref": "#/definitions/minValue",
                            "propertyOrder": 10
                        },
                        "maxValue": {
                            "$ref": "#/definitions/maxValue",
                            "propertyOrder": 11
                        }
                    },
                    "required": [
                        "name",
                        "cell",
                        "minValue",
                        "maxValue"
                    ]
                }
            ]
        },
        "recipient": {
            "options": {
                "disable_collapse": true
            },
            "title": "Recipient",
            "oneOf": [
                {
                    "$ref": "#/definitions/emailRecipient"
                },
                {
                    "$ref": "#/definitions/smsRecipient"
                }
            ]
        },
        "alarm": {
            "headerTemplate": "Alarm{{: |self.name}}",
            "oneOf": [
                {
                    "$ref": "#/definitions/expectedValueAlarm"
                },
                {
                    "$ref": "#/definitions/minValueAlarm"
                },
                {
                    "$ref": "#/definitions/maxValueAlarm"
                },
                {
                    "$ref": "#/definitions/minMaxValueAlarm"
                }
            ],
            "options": {
                "disable_collapse": true,
                "remove_empty_properties": true
            }
        }
    },
    "properties": {
        "deviceName": {
            "type": "string",
            "title": "Alarm device name",
            "description": "Device name to be used in MQTT topics for logging & alarm cells",
            "pattern": "^[^\\s/]+$",
            "minLength": 1,
            "propertyOrder": 1
        },
        "deviceTitle": {
            "type": "string",
            "title": "Alarm device title",
            "propertyOrder": 2
        },
        "recipients": {
            "type": "array",
            "title": "Recipients",
            "items": {
                "$ref": "#/definitions/recipient"
            },
            "propertyOrder": 3,
            "options": {
                "disable_array_reorder": true
            }
        },
        "alarms": {
            "type": "array",
            "title": "Alarms",
            "items": {
                "$ref": "#/definitions/alarm"
            },
            "propertyOrder": 4,
            "options": {
                "disable_collapse": true,
                "disable_array_reorder": true
            }
        }
    },
    "required": [
        "deviceName",
        "recipients",
        "alarms"
    ],
    "configFile": {
        "path": "/etc/wb-rules/alarms.conf",
        "service": "wb-rules"
    }
}