Copyright © 2017 Mozilla
This document describes a common data model and API for the LabThings devices. The Thing Description provides a vocabulary for describing physical devices connected to the World Wide Web in a machine readable format with a default JSON encoding. Common device capabilities can be specified using optional semantic annotations. The REST API and WebSocket API allow a web client to access the properties of devices, request the execution of actions and subscribe to events representing a change in state.
This document includes a plain JSON serialisation of a Thing Description and a concrete HTTP and WebSockets protocol binding for the Web of Things. This implementation is based on Mozilla's Web Thing API, which defines simple concrete serialisation and protocol binding complementing the current W3C Web of Things Working Group's work on an abstract data model and API for the Web of Things
This implementation is intended to act as an extension to Mozilla's Web Thing API, with additional provisions for laboratory use based on the project goals of LabThings. Ideally, this document should describe a superset of the Mozilla implementation.
While the Web of Things standardisation process continues, this document will continue to be maintained to reflect the current API implemented by Mozilla's open source Web of Things implementation through Project Things. We encourage developers to continue to implement this API to build web things compatible with Mozilla's Things Gateway, and provide feedback to help us further improve the specification.
Instrumentation control poses a unique set of programming challenges related to resource locking, asynchronous execution, experiment planning, and data management. LabThings aims to provide a framework for solving these problems in an open, interoperable way using existing standards and technologies. Good solutions to many of these problems exist outside of research laboratory development. By utilizing web technologies and standards, labs can begin to move away from often prohibitively expensive, proprietary solutions, and modernize lab automation in a sustainable and accessible fashion.
Whilst web technologies are already in widespread use on the Internet of Things, this is currently done with mostly proprietary data formats and APIs which require per-vendor integrations to make devices interoperable. In order to promote ad-hoc interoperability on the Internet of Things a shared vocabulary and common API is needed.
In this document we outline a common data model and API for networked laboratory instruments based on Mozilla's Web Thing API.
LabThings devices must use a standard W3C Web of Things Thing Description as per [[WOT-THING-DESCRIPTION]].
The Thing Description provides a vocabulary for describing physical devices connected to the World Wide Web in a machine readable format with a default JSON encoding.
@context
member
The @context
member is an optional annotation which can be used to provide a URI for a schema repository which defines standard schemas for common "types" of device capabilities. These types can then be referred to
using @type
annotations elsewhere in the Thing Description.
"@context": ["https://www.w3.org/2019/wot/td/v1", "https://iot.mozilla.org/schemas"]
The Web Thing API provides a web services based programming interface with a RESTful design for applications to access the properties of devices, request the execution of actions and access a list of events representing a change in state. A default HTTP [[HTTP11]] protocol binding is defined here.
The Web Thing REST API consists of a number of different types of resources which represent different aspects of a device and can be independently referenced by a URL. The specific URLs and URL structure of resources are defined by the Thing Description.
This specification does not define a fixed URL structure.
Thing
resourceA thing Resource provides a Thing Description for a device. A Thing Resource is considered the root resource of a Thing.
The URL of a Thing Resource may be enumerated by a gateway or directory, or may be discovered by some other means.
A Thing description is usually read only. An HTTP GET request can be used to retrieve a Thing Description for a Thing.
Example: Get a description of a Thing
GET http://mythingserver.com/things/pi Accept: application/json
200 OK
Property
resourceA property resource represents a single property of a device. Some property resources may be read only and some may be writable. The value of a property can be retrieved with an HTTP GET request and updated with an HTTP PUT request.
The URL of a Property resource must be defined by the forms
section of a Property object in a Thing
Description.
If a Property object does not define a primitive type
it may not have a JSON serialisation and may
instead return a binary file or stream in response to an HTTP GET request (e.g. an image file or video stream).
Example: Get a property
GET http://mythingserver.com/things/pi/properties/temperature Accept: application/json
200 OK 21
Example: Set a property
PUT http://mythingserver.com/things/pi/properties/led true Accept: application/json
200 OK true
Properties
resourceA properties resource represents all the properties of a device. The value of all properties can be retrieved with an HTTP GET request.
The URL of a Properties resource can be defined by a properties
link relation in the links
member of a Thing Description.
Example: Get all properties
GET http://mythingserver.com/things/pi/properties Accept: application/json
200 OK { "temperature": 21, "humidity": 50, "led": true }
Action
resourceAn action resource represents a queue of actions of a single action type. A new action is created in the queue with an HTTP POST request and a list of action requests in the queue can be requested with an HTTP GET request.
The URL of an Action resource must be defined by the links
section of an Action object in a Thing Description.
If a client tries to request an action of another type via this resource, the server should respond with a
400 Bad Request
response.
If an action request is successful, the server should respond with a 201 Created
response, containing an ActionRequest resource in the response body.
Action Request
POST https://mythingserver.com/actions/fade Accept: application/json { "level": 50, "duration": 2000 }
201 Created
Action Queue
GET /actions/fade Accept: application/json
200 OK
ActionRequest
resourceAn action request resource represents an individual action request for a given action. The current status of an action request can be retrieved with an HTTP GET request, updated with an HTTP PUT request and deleted with an HTTP DELETE request.
The URL of an Action request resource can be defined by the href
member of an action request
object in an action queue.
An ActionRequest object should conform to the following JSON schema:
Action Request Status
GET /actions/fade/123e4567-e89b-12d3-a456-426655 Accept: application/json
200 OK
Cancel an Action Request
DELETE /actions/fade/123e4567-e89b-12d3-a456-426655
204 No Content
Actions
resourceAn actions resource represents a queue of actions to be executed on a device. A new action is created in the queue with an HTTP POST request and a list of action requests in the queue can be requested with an HTTP GET request.
The URL of an actions resource can be defined by an actions
link relation in the links
member of a Thing Description.
Any action supported by the thing can be requested via this top level action queue. If an unsupported action
type is requested, the server should respond with a 400 Bad Request
response.
Action Request
POST https://mythingserver.com/actions/ Accept: application/json { "fade": { "input": { "level": 50, "duration": 2000 } } }
201 Created
Actions Queue
GET /actions Accept: application/json
200 OK
Event
resourceAn event resource provides a log of events recently emitted by a device for a particular event type. An event resource is usually read only.
The URL of an Event resource must be defined by the forms
section of an Event object in a Thing Description.
Event Log
GET /events/overheated Accept: application/json
200 OK [ { "event": "overheated", "data": 102, "timestamp": "2017-01-25T15:01:35+00:00" }, { "event": "overheated", "data": 101, "timestamp": "2017-01-24T13:02:45+00:00" } ]
Events
resourceAn events resource provides a log of all events recently emitted by a device. An events resource is usually read only.
The URL of an Events resource can be defined by an events
link relation in the links
member of a Thing Description.
Event Log
GET /events Accept: application/json
200 OK [ { "event": "overheated", "data": 102, "timestamp": "2017-01-25T15:01:35+00:00" }, { "event": "reboot", "timestamp": "2017-01-24T13:02:45+00:00" } ]
The default protocol binding for the Web Thing REST API is HTTP(S). Bindings to alternative application protocols (e.g. CoAP) may be used, but these bindings are beyond the scope of this specification. A Web Thing API protocol binding may also be layered on top of a non-Internet application protocol by use of a gateway.
The default content encoding for the Web Thing HTTP(S) REST API is JSON. Alternative content encodings (e.g. CBOR) may be used, but these bindings are beyond the scope of this specification.
The Web Thing WebSocket API complements the REST API to provide a realtime mechanism to make multiple requests and be notified of events as soon as they happen, by keeping a WebSocket [[WEBSOCKETS-PROTOCOL]] open on the Web Thing. The "webthing" WebSocket subprotocol defined here has a simple set of message types and a JSON response format consistent with the Web Thing REST API.
To open a WebSocket on a Thing, an HTTP GET request is upgraded to a WebSocket using a standard WebSocket protocol handshake [[WEBSOCKETS-PROTOCOL]] and the "webthing" subprotocol. The WebSocket URL for a Web Thing is specified in the links member of the Web Thing Description.
GET wss://mythingserver.com/things/robot Host: mythingserver.com Origin: https://mythingserver.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: webthing Sec-WebSocket-Version: 13
HTTP 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: webthing
A WebSocket can be opened from a web page using the JavaScript WebSocket API which will take care of the handshake detailed above and allow messages to be sent and received.
const socket = new WebSocket('wss://mywebthingserver/things/robot', 'webthing');
setProperty
messageThe setProperty
message type is sent from a web client to a Web Thing in order to set the value of
one or more of its properties. This is equivalent to a PUT
request on a Property resource URL using
the REST API, but with the WebSocket API a property value can be changed multiple times in quick succession over
an open socket and multiple properties can be set at the same time.
{ "messageType": "setProperty", "data": { "leftMotor": 100 } }
requestAction
messageThe requestAction
message type is sent from a web client to a Web Thing to request an action be
carried out on a Web Thing. This is equivalent to a POST
request on an Actions resource URL using
the REST API, but multiple actions can be requested at the same time or in quick succession over an open socket.
{ "messageType": "requestAction", "data": { "goForward": { "steps": 100 }, } }
addEventSubscription
messageThe addEventSubscription
message type is sent from a web client to a Web Thing to allow a client
to subscribe to a particular event type, defined by the events
member of a Web Thing description.
This is similar to adding an event listener in JavaScript, but events are received as an event
message over the Web Thing WebSocket API.
{ "messageType": "addEventSubscription", "data": { "motion": {} } }
propertyStatus
messageThe propertyStatus
message type is sent from a Web Thing to a web client whenever a property of a
Web Thing changes. The payload data of this message is in the same format as a response to a GET
request on Property resource using the REST API, but the message is pushed to the client whenever a property
changes and can include multiple properties at the same time.
{ "messageType": "propertyStatus", "data": { "led": true } }
actionStatus
messageThe actionStatus
message type is sent from a Web Thing to a web client when the status of a
requested action changes. The payload data is consistent with the format of an Action resource in the REST API,
but messages are pushed to the client as soon as the status of an action changes.
{ "messageType": "actionStatus", "data": { "grab": { "action": "goForward", "id": "123e4567-e89b-12d3-a456-426655", "href": "/actions/go-forward/123e4567-e89b-12d3-a456-426655", "timeRequested": "2017-01-24T11:02:45+00:00", "timeCompleted": "2017-01-24T11:02:46+00:00", "status": "completed", "input": { "steps": 100 }, "output": {}, } } }
event
messageThe event
message type is sent from a Web Thing to a web client when an event occurs on the Web
Thing. The payload data is consistent with the format of an Event resource in the REST API but messages are
pushed to the client as soon as an event occurs.
{ "messageType": "event", "data": { "motion": { "timestamp": "2017-01-24T13:02:45+00:00" } } }
Web Things can make themselves discoverable to clients via various methods.
A Web Thing can make itself discoverable on a local network by advertising itself via DNS-SD, with a service type of labthing
over TCP, i.e. _labthing._tcp
. The service should define a TXT record named
path
, with its value denoting the URI of the thing description on the Web Thing's web server, e.g. path=/api/
. Additionally, a TXT record named tls
with a value of 1
should be included if the Web Thing supports connections via HTTPS.
Web of
Things Integration Patterns
Advice on different design patterns for integrating connected devices with the Web of Things.