forked from WebThingsIO/api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
588 lines (561 loc) · 24.3 KB
/
index.html
File metadata and controls
588 lines (561 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Lab Things API</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class="remove" defer></script>
<script class='remove'>
var respecConfig = {
specStatus: "base",
editors: [
{
name: "Joel Collins",
company: "University of Bath",
companyURL: "https://www.bath.ac.uk/",
},
{
name: "Ben Francis",
company: "Mozilla Corporation",
companyURL: "https://mozilla.com/",
w3cid: 51527
}],
processVersion: 2018,
edDraftURI: "https://spec.labthings.org/",
shortName: "WoT",
localBiblio: {
"web-linking": {
title: "Web Linking",
href: "https://tools.ietf.org/html/rfc5988",
publisher: "IETF",
}
}
};
</script>
<style>
table {
width: 100%;
border: solid 1px #005A9C;
border-collapse: collapse;
}
th {
background-color: #005A9C;
color: white;
}
td,
th {
border: solid 1px #005A9C;
padding: 4px;
}
</style>
</head>
<body>
<p class="copyright">Copyright © 2017 Mozilla</p>
<section id='abstract'>
<p>This document describes a common data model and API for the LabThings devices. The <a
href="#thing-description">Thing Description</a> 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 <a href="#context-member">semantic annotations</a>. The <a
href="#rest-api">REST API</a> and <a href="#websocket-api">WebSocket
API</a> allow a web client to access the properties of devices, request the execution of actions and subscribe
to events representing a change in state.</p>
</section>
<section id='sotd'>
<p>This document includes a plain JSON serialisation of a <a
href="https://w3c.github.io/wot-thing-description/">Thing Description</a> and a concrete HTTP and WebSockets <a
href="https://w3c.github.io/wot-binding-templates/">protocol binding</a> for the Web of Things. This
implementation is based on <a href="https://iot.mozilla.org/wot/">Mozilla's Web Thing API</a>, which defines
simple concrete serialisation and protocol binding complementing the current <a
href="https://www.w3.org/WoT/WG/">W3C Web of Things Working Group</a>'s work on an abstract data model and API
for the Web of Things</p>
<p>This implementation is intended to act as an extension to <a href="https://iot.mozilla.org/wot/">Mozilla's Web
Thing API</a>, with additional provisions for laboratory use based on the project goals of <a
href="https://labthings.org">LabThings</a>. Ideally, this document should describe a superset of the Mozilla
implementation.</p>
<p>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 <a
href="https://iot.mozilla.org/">Project Things</a>. We encourage developers to continue to implement this API to
build web things compatible with Mozilla's <a href="https://iot.mozilla.org/gateway/">Things Gateway</a>, and
provide feedback to help us further improve the specification.
</p>
</section>
<section>
<h2>Introduction</h2>
<p>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.</p>
<p>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.
</p>
<p>In this document we outline a common data model and API for networked laboratory instruments based on <a
href="https://iot.mozilla.org/wot/">Mozilla's Web Thing API</a>.</p>
</section>
<section>
<h2>Thing Description</h2>
<p>LabThings devices must use a standard W3C Web of Things Thing Description as per [[WOT-THING-DESCRIPTION]].</p>
<p>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.</p>
<pre class="example" title="A simple Thing Description" data-include="include/simple-td.json"></pre>
<pre class="example" title="A more complex Thing Description with semantic annotations"
data-include="include/complex-td.json"></pre>
<section>
<h3><code>@context</code> member</h3>
<p>
The <code>@context</code> 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 <code>@type</code> annotations elsewhere in the Thing Description.
</p>
<div class="note">Example experimental schema repositories can be found at <a href="http://iotschema.org/">http://iotschema.org/</a> and <a href="https://iot.mozilla.org/schemas">https://iot.mozilla.org/schemas</a>.</div>
<div class="note">The Lab Things project will eventually introduce a laboratory-focused schema repository. This specification document will be updated accordingly.</div>
<pre class="example" title="Example @context member">"@context": ["https://www.w3.org/2019/wot/td/v1", "https://iot.mozilla.org/schemas"]</pre>
</section>
</section>
<section>
<h2>REST API</h2>
<p>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.</p>
<p>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.</p>
<p>This specification does <strong>not</strong> define a fixed URL structure.</p>
<section>
<h3><code>Thing</code> resource</h3>
<p>A thing Resource provides a <a href="#thing-description">Thing Description</a> for a device. A Thing
Resource is considered the root resource of a Thing.</p>
<p>The URL of a Thing Resource may be enumerated by a gateway or directory, or may be discovered by some other
means.</p>
<p>A Thing description is usually read only. An HTTP GET request can be used to retrieve a Thing Description for a
Thing.</p>
<p><strong>Example: Get a description of a Thing</strong></p>
<div class="example">
<div class="example-title marker">
<span>Request</span>
</div>
<pre>GET http://mythingserver.com/things/pi
Accept: application/json</pre>
</div>
<div class="example">
<div class="example-title marker">
<span>Response</span>
</div>
<pre>200 OK
<div data-include="include/simple-td.json"></div>
</pre>
</div>
</section>
<section>
<!--Property resource-->
<h3><code>Property</code> resource</h3>
<p>A 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.</p>
<p>The URL of a Property resource must be defined by the <code>forms</code> section of a Property object in a Thing
Description.</p>
<p>If a Property object does not define a primitive <code>type</code> 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).
<p><strong>Example: Get a property</strong></p>
<div class="example">
<div class="example-title marker">
Request
</div>
<pre>GET http://mythingserver.com/things/pi/properties/temperature
Accept: application/json</pre>
</div>
<div class="example">
<div class="example-title marker">
Response
</div>
<pre>200 OK
21
</pre>
</div>
<p><strong>Example: Set a property</strong></p>
<div class="example">
<div class="example-title marker">
Request
</div>
<pre>PUT http://mythingserver.com/things/pi/properties/led
true
Accept: application/json</pre>
</div>
<div class="example">
<div class="example-title marker">
Response
</div>
<pre>200 OK
true
</pre>
</div>
</section>
<section>
<!--Properties resource-->
<h3><code>Properties</code> resource</h3>
<p>A properties resource represents all the properties of a device. The value of all properties can be retrieved
with an HTTP GET request.</p>
<p>The URL of a Properties resource can be defined by a <code>properties</code> link relation in the <code>links</code> member of a Thing Description.</p>
<p><strong>Example: Get all properties</strong></p>
<div class="example">
<div class="example-title marker">
Request
</div>
<pre>GET http://mythingserver.com/things/pi/properties
Accept: application/json</pre>
</div>
<div class="example">
<div class="example-title marker">
Response
</div>
<pre>200 OK
{
"temperature": 21,
"humidity": 50,
"led": true
}</pre>
</div>
</section>
<section>
<!--Action resource-->
<h3><code>Action</code> resource</h3>
<p>An 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.
</p>
<p>The URL of an Action resource must be defined by the <code>links</code> section of an Action object in a Thing Description.</p>
<p>If a client tries to request an action of another type via this resource, the server should respond with a
<code>400 Bad Request</code> response.</p>
<p>If an action request is successful, the server should respond with a <code>201 Created</code>
response, containing an <a href="#actionrequest-resource">ActionRequest resource</a> in the response body.</p>
<p><strong>Action Request</strong></p>
<pre class="example" title="Request an action">
POST https://mythingserver.com/actions/fade
Accept: application/json
{
"level": 50,
"duration": 2000
}
</pre>
<pre class="example" title="Response to creating an action request">201 Created
<div data-include="include/action_resource/actionRequest-resource.json"></div>
</pre>
<p><strong>Action Queue</strong></p>
<pre class="example" title="Get a list of all action requests for a given action">
GET /actions/fade
Accept: application/json
</pre>
<pre class="example" title="Action list response">200 OK
<div data-include="include/action_resource/action-queue-resource.json"></div>
</pre>
</section>
<section>
<!--Action Request resource-->
<h3><code>ActionRequest</code> resource</h3>
<p>An 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.</p>
<p>The URL of an Action request resource can be defined by the <code>href</code> member of an action request
object in an action queue.</p>
<p>An ActionRequest object should conform to the following JSON schema:</p>
<pre title="ActionResource schema" data-include="include/action_resource/schema.json"></pre>
<p><strong>Action Request Status</strong></p>
<pre class="example" title="Get the status of an action request">
GET /actions/fade/123e4567-e89b-12d3-a456-426655
Accept: application/json
</pre>
<pre class="example" title="Action request status response">200 OK
<div data-include="include/action_resource/actionRequest-resource.json"></div>
</pre>
<p><strong>Cancel an Action Request</strong></p>
<pre class="example" title="Cancel an action request response">
DELETE /actions/fade/123e4567-e89b-12d3-a456-426655
</pre>
<pre class="example" title="Action cancellation response">
204 No Content
</pre>
</section>
<section>
<!--Actions resource-->
<h3><code>Actions</code> resource</h3>
<p>An 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.</p>
<p>The URL of an actions resource can be defined by an <code>actions</code> link relation in the <code>links</code> member of a Thing Description.</p>
<p>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 <code>400 Bad Request</code> response.</p>
<p><strong>Action Request</strong></p>
<pre class="example" title="Request an action">
POST https://mythingserver.com/actions/
Accept: application/json
{
"fade": {
"input": {
"level": 50,
"duration": 2000
}
}
}
</pre>
<pre class="example" title="Response to creating an action request">201 Created
<div data-include="include/action_resource/actionRequest-resource.json"></div>
</pre>
<p><strong>Actions Queue</strong></p>
<pre class="example" title="Get a list of all action requests">
GET /actions
Accept: application/json
</pre>
<pre class="example" title="Action list response">200 OK
<div data-include="include/action_resource/actions-queue-resource.json"></div>
</pre>
</section>
<section>
<!--Event resource-->
<h3><code>Event</code> resource</h3>
<p>An event resource provides a log of events recently emitted by a device for a particular event type. An event
resource is usually read only.</p>
<p>The URL of an Event resource must be defined by the <code>forms</code> section of an Event object in a Thing Description.</p>
<p><strong>Event Log</strong></p>
<pre class="example" title="Event Request">
GET /events/overheated
Accept: application/json
</pre>
<pre class="example" title="Event Response">
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"
}
]
</pre>
</section>
<section>
<!--Events resource-->
<h3><code>Events</code> resource</h3>
<p>An events resource provides a log of all events recently emitted by a device. An events resource is usually
read only.</p>
<p>The URL of an Events resource can be defined by an <code>events</code> link relation in the <code>links</code> member of a Thing Description.</p>
<p><strong>Event Log</strong></p>
<pre class="example" title="Events Request">
GET /events
Accept: application/json
</pre>
<pre class="example" title="Events Response">
200 OK
[
{
"event": "overheated",
"data": 102,
"timestamp": "2017-01-25T15:01:35+00:00"
},
{
"event": "reboot",
"timestamp": "2017-01-24T13:02:45+00:00"
}
]
</pre>
</section>
<section>
<h3>Alternative Protocol Bindings</h3>
<p>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.</p>
</section>
<section>
<h3>Alternative Encodings</h3>
<p>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.</p>
</section>
</section>
<section>
<h2>WebSocket API</h2>
<p>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.</p>
<section>
<h3>Protocol Handshake</h3>
<p>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.</p>
<div class="example">
<div class="example-title marker">
Request
</div>
<pre>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</pre>
</div>
<div class="example">
<div class="example-title marker">
Response
</div>
<pre>HTTP 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: webthing</pre>
</div>
<p>A WebSocket can be opened from a web page using the JavaScript <a
href="https://www.w3.org/TR/websockets/">WebSocket API</a> which will take care of the handshake detailed
above and allow messages to be sent and received.</p>
<div class="example">
<div class="example-title marker">
Example
</div>
<pre>const socket = new WebSocket('wss://mywebthingserver/things/robot', 'webthing');</pre>
</div>
</section>
<section>
<h3><code>setProperty</code> message</h3>
<p>The <code>setProperty</code> 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 <code>PUT</code> 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.</p>
<div class="example">
<div class="example-title marker">
Example
</div>
<pre>{
"messageType": "setProperty",
"data": {
"leftMotor": 100
}
}</pre>
</div>
</section>
<section>
<h3><code>requestAction</code> message</h3>
<p>The <code>requestAction</code> 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 <code>POST</code> 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.
</p>
<div class="example">
<div class="example-title marker">
Example
</div>
<pre>{
"messageType": "requestAction",
"data": {
"goForward": {
"steps": 100
},
}
}</pre>
</div>
</section>
<section>
<h3><code>addEventSubscription</code> message</h3>
<p>The <code>addEventSubscription</code> 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 <code>events</code> member of a Web Thing description.
This is similar to adding an event listener in JavaScript, but events are received as an <code>event</code>
message over the Web Thing WebSocket API.</p>
<div class="example">
<div class="example-title marker">
Example
</div>
<pre>{
"messageType": "addEventSubscription",
"data": {
"motion": {}
}
}</pre>
</div>
</section>
<section>
<h3><code>propertyStatus</code> message</h3>
<p>The <code>propertyStatus</code> 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 <code>GET</code>
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.
<div class="example">
<div class="example-title marker">
Example
</div>
<pre>{
"messageType": "propertyStatus",
"data": {
"led": true
}
}</pre>
</div>
</section>
<section>
<h3><code>actionStatus</code> message</h3>
<p>The <code>actionStatus</code> 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.
<div class="example">
<div class="example-title marker">
Example
</div>
<pre>{
"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": {},
}
}
}</pre>
</div>
</section>
<section>
<h3><code>event</code> message</h3>
<p>The <code>event</code> 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.
<div class="example">
<div class="example-title marker">
Example
</div>
<pre>{
"messageType": "event",
"data": {
"motion": {
"timestamp": "2017-01-24T13:02:45+00:00"
}
}
}</pre>
</div>
</section>
</section>
<section>
<h2>Thing Discovery</h2>
<p>
Web Things can make themselves discoverable to clients via various methods.
</p>
<section>
<h3>mDNS/DNS-SD</h3>
<p>
A Web Thing can make itself discoverable on a local network by advertising itself via DNS-SD, with a service type of <code>labthing</code> over TCP, i.e. <code>_labthing._tcp</code>. The service should define a TXT record named
<code>path</code>, with its value denoting the URI of the thing description on the Web Thing's web server, e.g. <code>path=/api/</code>. Additionally, a TXT record named <code>tls</code> with a value of <code>1</code> should be included if the Web Thing supports connections via HTTPS.
</p>
</section>
</section>
<section>
<h2>Further Information</h2>
<p><a href="https://docs.google.com/document/d/1H3coHbb3Bwd02_NJi4KEBONByUkq92_HsTk1IpfmACY/edit?usp=sharing">Web of
Things Integration Patterns</a><br />
Advice on different design patterns for integrating connected devices with the Web of Things.</p>
</section>
</body>
</html>