{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"$id": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.0.5.schema.json",
	"$defs": {
		
		"rx.JSONSchema": {
			"description": "Shortcut for subschema used for received 'JSON' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.rx.0.5.schema.json#JSONSchema"
		},
		"rx.CategoryError": {
			"description": "Shortcut for subschema used for received error events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.rx.0.5.schema.json#CategoryError"
		},
		"rx.SystemSchema": {
			"description": "Shortcut for subschema used for received 'system' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.rx.0.5.schema.json#SystemSchema"
		},
		"rx.HIDSchema": {
			"description": "Shortcut for subschema used for received 'HID' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.rx.0.5.schema.json#HIDSchema"
		},
		"rx.CANSchema": {
			"description": "Shortcut for subschema used for received 'CAN' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.rx.0.5.schema.json#CANSchema"
		},
		"rx.UserProfilesSchema": {
			"description": "Shortcut for subschema used for received 'user profiles' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.rx.0.5.schema.json#UserProfilesSchema"
		},
		
		"tx.SystemSchema": {
			"description": "Shortcut for subschema used for transmitted 'system' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.tx.0.5.schema.json#SystemSchema"
		},
		"tx.HIDSchema": {
			"description": "Shortcut for subschema used for transmitted 'HID' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.tx.0.5.schema.json#HIDSchema"
		},
		"tx.CANSchema": {
			"description": "Shortcut for subschema used for transmitted 'CAN' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.tx.0.5.schema.json#CANSchema"
		},
		"tx.UserProfilesSchema": {
			"description": "Shortcut for subschema used for transmitted 'user profiles' events.",
			"$ref": "https://products.digiteqautomotive.com/schemas/screenlinq/COM.protocol.tx.0.5.schema.json#UserProfilesSchema"
		}
		
	},
	"title": "COM protocol events",
	"description": "Specification of events that are used to communicate with ScreenLinq via virtual COM port.",
	"type": "object",
	"properties": {
		"category" : {
			"description": "Category of the event.",
			"enum": ["JSON", "system", "HID", "CAN", "user profiles"]
		},
		"case": {
			"description": "Case of the event.",
			"enum": ["get", "set", "state", "update", "error"]
		},
		"data": {
			"description": "Array of the actual data objects.",
			"type": ["object", "array"],
			"items": {
				"description": "Data objects acording to the appropriate subschema.",
				"type": "object"
			}
		}
	},
	"required": ["category", "case", "sort"],
	"if": {
		"$comment": "Sort type according to case.",
		"properties": {
			"case": {
				"const": "error"
			}
		}
	},
	"then": {
		"properties": {
			"sort": {
				"description": "Error information.",
				"type": "object",
				"properties": {
					"number": {
						"description": "Error code. Actual value depends on 'category' property value.",
						"type": "integer"
					},
					"message": {
						"description": "Error description. Actual value depends on 'category' property value.",
						"type": "string"
					}
				}
			}
		},
		"$comment": "'data' subschemas according to the 'case' value:",
		"if": {
			"properties": {
				"category": {
					"const": "JSON"
				}
			}
		},
		"then": {
			"$ref": "#/$defs/rx.JSONSchema"
		},
		"else": {
			"properties": {
				"data": {
					"type": "array",
					"items": {
						"type": "object"
					}
				}
			},
			"$ref": "#/$defs/rx.CategoryError"
		}
	},
	"else": {
		"properties": {
			"sort": {
				"description": "Type of the event. Actual value depends on 'category' property value.",
				"type": "string"
			},
			"data": {
				"description": "Data of the event - array of objects. Actual value depends on 'category' property value.",
				"type": "array",
				"items": {
					"type": "object"
				}
			}
		},
		"allOf": [
			{
				"$comment": "General relations:",
				"if": {
					"properties": {
						"case": {
							"description": "tx direction",
							"enum": ["get", "set"]
						}
					}
				},
				"then": {
					"description": "tx json cannot have other properties than specified.",
					"properties": {
						"category": true,
						"case": true,
						"sort": true,
						"data": true
					},
					"additionalProperties": false
				}
			},
			{
				"$comment": "Allowed 'sort' values according to the 'category' value:",
				"if": {
					"properties": {
						"category": {
							"const": "JSON"
						}
					}
				},
				"then": {
					"properties": {
						"case": {
							"enum": ["error"]
						},
						"data": {
							"type": "object"
						}
					},
					"$comment": "'data' subschemas according to the 'case' value:",
					"allOf": [
						{
							"$ref": "#/$defs/rx.JSONSchema"
						}
					]
				},
				"else": {
					"properties": {
						"data": {
							"type": "array"
						}
					}
				}
			},
			{
				"if": {
					"properties": {
						"category": {
							"const": "system"
						}
					}
				},
				"then": {
					"properties": {
						"sort": {
							"enum": ["information"]
						}
					},
					"$comment": "'data' subschemas according to the 'case' value:",
					"allOf": [
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["get"]
									}
								}
							},
							"then" :{
								"$ref": "#/$defs/tx.SystemSchema"
							}
						},
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["state", "update"]
									}
								}
							},
							"then" :{
								"$ref": "#/$defs/rx.SystemSchema"
							}
						}
					]
				}
			},
			{
				"if": {
					"properties": {
						"category": {
							"const": "HID"
						}
					}
				},
				"then": {
					"properties": {
						"sort" : {
							"enum": ["input panel ready", "feature list", "touch", "event", "key", "slider"]
						}
					},
					"$comment": "'data' subschemas according to the 'case' value:",
					"allOf": [
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["get"]
									}
								}
							},
							"then": {
								"$ref": "#/$defs/tx.HIDSchema"
							}
						},
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["state", "update"]
									}
								}
							},
							"then": {
								"$ref": "#/$defs/rx.HIDSchema"
							}
						},
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["set"]
									}
								}
							},
							"then": false
						}
					]
				}
			},
			{
				"if": {
					"properties": {
						"category": {
							"const": "CAN"
						}
					}
				},
				"then": {
					"properties": {
						"sort": {
							"enum": ["signal"]
						}
					},
					"$comment": "'data' subschemas according to the 'case' value:",
					"allOf": [
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["get", "set"]
									}
								}
							},
							"then" :{
								"$ref": "#/$defs/tx.CANSchema"
							}
						},
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["state", "update"]
									}
								}
							},
							"then" :{
								"$ref": "#/$defs/rx.CANSchema"
							}
						}
					]
				}
			},
			{
				"if": {
					"properties": {
						"category": {
							"const": "user profiles"
						}
					}
				},
				"then": {
					"properties": {
						"sort": {
							"enum": ["reset", "upload", "download"]
						}
					},
					"$comment": "'data' subschemas according to the 'case' value:",
					"allOf": [
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["get", "set"]
									}
								}
							},
							"then" :{
								"$ref": "#/$defs/tx.UserProfilesSchema"
							}
						},
						{
							"if": {
								"properties": {
									"case": {
										"enum": ["state"]
									}
								}
							},
							"then" :{
								"$ref": "#/$defs/rx.UserProfilesSchema"
							}
						}
					]
				}
			}
		]
	}
}
