Skip to content

Commit

Permalink
Adding a JSON schema for TDs based on the reference example from
Browse files Browse the repository at this point in the history
  • Loading branch information
mlagally committed Jan 24, 2018
1 parent db6e80c commit 62bd37b
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Prague Plugfest/PragueAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Source: https://github.com/w3c/wot-scripting-api/issues/82
*/

interface WoT {
Observable<ConsumedThing> discover(optional ThingFilter filter);
Promise<ThingDescription> fetchTD(USVString url);
ConsumedThing consume(ThingDescription td); // with fetch split out, we could make it synchrounous
ExposedThing expose(ThingTemplate init); // I think it was only symmetry to make it return a promise
};

typedef USVString ThingDescription;

// A full ThingTemplate aligned with ThingDescription requires pure JSON serialization of TD!
dictionary ThingTemplate {
DOMString name;
sequence<SemanticType>? semanticTypes = []; // additional @types to "Thing" -- use case?
sequence<SemanticMetadata>? metadata = []; // metadata fields in TD root (same level as 'name')
}

dictionary SemanticType {
DOMString name;
USVString context;
DOMString prefix;
};

dictionary SemanticMetadata {
SemanticType type;
any value;
};

interface ConsumedThing {
readonly attribute DOMString name;
readonly attribute ThingDescription td;
Promise<any> readProperty(DOMString name); // aligned with 'writable'
Promise<void> writeProperty(DOMString name, any value); // aligned with 'writable'
Promise<any> invokeAction(DOMString name, any parameters);
Observable onEvent(DOMString name); // subscribe to events
Observable onPropertyChange(DOMString name); // subscribe to property change
Observable onTDChange(); // subscribe to Thing Description change
};

interface ExposedThing: ConsumedThing {
Promise<void> start(); // useful to make async or counter-productive?
Promise<void> stop();
Promise<void> register(USVString url); // convenience for Thing Directory
Promise<void> unregister(USVString url); // convenience for Thing Directory
Promise<void> emitEvent(DOMString eventName, any payload);

void addProperty(ThingPropertyInit property); // throws on error
void removeProperty(DOMString name); // throws on error
void addAction(ThingActionInit action); // throws on error
void removeAction(DOMString name); // throws on error
void addEvent(ThingEventInit event); // throws on error
void removeEvent(DOMString name); // throws on error
};

typedef DOMString ValueType; // Linked Data JSON Schema

dictionary ThingPropertyInit {
DOMString name;
ValueType type;
any initValue;
boolean? writable = false;
boolean? observable = false;
sequence<SemanticType>? semanticTypes = [];
sequence<SemanticMetadata>? metadata = []; // metadata fields in TD root (same level as 'name')
callback? onRead = Promise<any> (any oldValue) = null; // programmatic value creation (Function does not work for initValue); Promise for forwarding
callback? onWrite = Promise<void> (any oldValue, any newValue) = null; // for applying state changes; Promise for forwarding
};

dictionary ThingActionInit {
DOMString name;
ValueType inputType;
ValueType outputType;
Function action;
sequence<SemanticType>? semanticTypes = [];
sequence<SemanticMetadata>? metadata = []; // metadata fields in TD root (same level as 'name')
};

dictionary ThingEventInit {
DOMString name;
ValueType type; // Linked Data JSON Schema
sequence<SemanticType>? semanticTypes = [];
sequence<SemanticMetadata>? metadata = []; // metadata fields in TD root (same level as 'name')
};
81 changes: 81 additions & 0 deletions Prague Plugfest/PragueTD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"@context": [ "http://w3c.github.io/wot/w3c-wot-td-context.jsonld" ],
"@type": [ "Thing" ],
"name": "MyLampThing",
"interaction": [
{
"@type": [ "Property" ],
"name": "status",
"outputData": { "type": "boolean" },
"writable": true,
"observable": true,
"form": [
{
"rel": "readProperty",
"href": "coaps://mylamp.example.com:5683/status",
"mediaType": "application/ocf+cbor",
"coap:methodCode": 1,
"coap:options": [
{
"coap:optionNumber": 2049,
"coap:optionValue": "1.1.0"
}
]
}, {
"rel": "writeProperty",
"href": "coaps://mylamp.example.com:5683/status",
"mediaType": "application/json",
"coap:methodCode": 3
}, {
"rel": "observeProperty",
"href": "coaps://mylamp.example.com:5683/status",
"mediaType": "application/json",
"coap:methodCode": 1,
"coap:options": [
{
"coap:optionNumber": 6,
"coap:optionValue": 0
}
]
}
]
}, {
"@type": [ "Action" ],
"name": "toggle",
"form": [
{
"rel": "invokeAction",
"href": "https://mylamp.example.com:443/toggle",
"http:methodName": "POST",
"http:MessageHeader": [
{
"http:fieldName": "Pragma",
"http:fieldValue": "no-cache"
}
]
}
]
}, {
"@type": [ "Event" ],
"name": "overheating",
"outputData": { "type": "string" },
"form": [
{
"rel": "subscribeEvent",
"href": "mqtt://broker.example.com:8883/MyLampThing/events/overheating",
"mediaType": "application/json",
"mqtt:messageType": 3
}
]
}
],
"link": [
{
"rel": "contains",
"href": "https://mylamp.example.com:443/smart-meter/td"
}, {
"rel": "locatedAt",
"href": "https://myroom.example.com:443/"
}
]
}
104 changes: 104 additions & 0 deletions Prague Plugfest/TD-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"title": "WoT TD Schema for Prague Plug Fest",
"description": "JSON Schema representation of the TD serialisation format.",
"$schema ": "http://json-schema.org/draft-06/schema#",
"id": "http://w3c.org/wot/schemas/w3c-wot-td.json",

"type": "object",

"properties": {
"@type": { "$ref": "#/definitions/type_declaration"},
"@context": { "type": "array",
"items": {"$ref": "#/definitions/jsonld_url"}},
"name": { "type": "string" },
"interaction": { "type": "array",
"items": { "$ref": "#/definitions/interaction" }},
"link": { "type": "array",
"items": { "$ref": "#/definitions/link" }}
},
"required": ["@type", "name", "interaction", "link"],
"additionalProperties": false,


"definitions": {
"type_declaration": { "type": "array",
"items": { "type" : "string" }},

"form_declaration": { "type": "array",
"items": { "$ref": "#/definitions/form_element"}},

"form_element": { "type": "object",
"properties" : {
"href": { "type": "string" },
"rel": { "type": "string" },
"mediaType": { "type": "string" }
},
"required": ["href", "rel", "mediaType"],
"additionalProperties": true
},

"interaction":{ "anyOf": [
{ "$ref": "#/definitions/property_element" },
{ "$ref": "#/definitions/action_element" },
{ "$ref": "#/definitions/event_element" } ] },

"property_element":{ "type": "object",
"@type": { "type" : "array",
"items": {
"type": "string",
"enum": [ "Property" ]
}
},
"name": "string",
"outputData": { "$ref": "#/definitions/output_data" },
"writable": { "type": "boolean" },
"observable": { "type": "boolean" },
"form": { "$ref": "#/definitions/form_declaration" }
},

"action_element":{ "type": "object",
"@type": { "type" : "array",
"items": {
"type": "string",
"enum": [ "Action" ]
}
},
"name": "string",
"form": { "$ref": "#/definitions/form_declaration" }
},

"event_element":{ "type": "object",
"@type": { "type" : "array",
"items": {
"type": "string",
"enum": [ "Event" ]
}
},
"name": "string",
"outputData": { "$ref": "#/definitions/output_data" },
"writable": { "type": "boolean" },
"observable": { "type": "boolean" },
"form": { "$ref": "#/definitions/form_declaration" }
},

"link": {
"type": "object",
"properties": {
"href": { "$ref": "#/definitions/url" },
"rel": { "type": "string" }
},
"required": ["href", "rel"]
},
"url": {
"type": "string",
"format": "uri",
"pattern": "(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(([^#]*))?(#(.*))?"
},
"jsonld_url": {
"type": "string",
"format": "uri",
"pattern": "http://[^/?#]*"
}

}
}

0 comments on commit 62bd37b

Please sign in to comment.