/
JsonServlet.shtml
1064 lines (700 loc) · 22.6 KB
/
JsonServlet.shtml
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15.17), see www.w3.org">
<!-- Copyright 2015 -->
<!-- $Id$ -->
<title>JMRI JSON Protocol</title><!-- Style -->
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii">
<link rel="stylesheet" type="text/css" href="/css/default.css"
media="screen">
<link rel="stylesheet" type="text/css" href="/css/print.css"
media="print">
<link rel="icon" href="/images/jmri.ico" type="image/png">
<link rel="home" title="Home" href="/">
<style type="text/css">
table#jsonResponses {
border-collapse:collapse;
}
table#jsonResponses td {
border: 1px solid grey;
width: 33%;
}
table.data {
margin: initial;
margin-left: 2em;
}
code {
white-space: normal;
}
</style><!-- /Style -->
</head>
<body>
<!--#include virtual="/Header" -->
<div id="mBody">
<!--#include virtual="Sidebar" -->
<div id="mainContent">
<h1>JMRI JSON Protocol</h1>
<p>The JMRI JSON Protocol provides access for a number of
JMRI-defined entities, and is accessible through multiple
methods:</p>
<ul>
<li><strong>The JMRI JSON servlet</strong>, providing both
HTTP and WebSocket access</li>
<li><strong>The JMRI JSON server</strong>, providing a
standard network socket for streamed access</li>
<li><strong>jquery.jmri</strong>, A jQuery-based JavaScript
library that chooses the best method to communicate with a
JMRI server using the JMRI JSON protocol</li>
</ul>
<h2>JMRI JSON Servlet</h2>
<p>The <b>JMRI JSON Servlet</b> provides web access to lists
and values for numerous JMRI-defined entities, in the modern
<a href="https://en.wikipedia.org/wiki/JSON">JSON format</a>.
It also provides a <a href=
"https://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
interface for making changes to the state of those entities,
and listening for external changes.</p>
<h2>Setting up the JSON Servlet</h2>
<p>The JMRI JSON Servlet is a feature that is part of the Web
Server. In order to use this service, you'll need to do the
following:</p>
<ul>
<li>Ensure the web server's port is unique:</li>
<li style="list-style: none">
<ul>
<li>Click <b>Edit</b> and then <b>Preferences</b> to
open the Preferences dialog</li>
<li>Click <b>Web Server</b> on the left side</li>
<li>Ensure the port number is unique (for example
12080). Other ports are used by WiThrottle, JSON
Server, etc.</li>
<li>Click Save</li>
</ul>
</li>
<li>Click <b>Actions</b> and <b>Start Web Server</b> to
ensure the web server is running</li>
<li>If you want the web server to run each time you start
JMRI:</li>
<li style="list-style: none">
<ul>
<li>Open the <b>Preferences</b> dialog</li>
<li>Select <b>Start Up</b></li>
<li>Click <b>Add ▾</b></li>
<li>Select <b>Perform action...</b></li>
<li>Select <b>Start Web Server</b></li>
<li>Click <b>OK</b></li>
</ul>
</li>
</ul>
<h2>Using HTTP JSON</h2>
<p>The <b>HTTP</b> feature of the servlet responds to the
following URL patterns:</p>
<ul>
<li>/json/<i><listName></i> -- used with HTTP GET to
retrieve a list from JMRI, or with PUT to add an item to a
list</li>
<li>/json/<<i>typeName</i>>/<i><itemName></i>
-- used with HTTP GET to retrieve a single item from a
list, or with POST to update an existing item</li>
</ul>
<p>There are a number of lists, and not all operations are
supported on these lists and/or items within each list. Check
the sections below for the operations that are currently
supported.</p>
<h3>List Names</h3>
<p>Currently the servlet supports the following list names
(not all of these are actually lists) used for GET operations
to return lists.</p>
<table class="data">
<tr>
<th>Name</th>
<th>GET</th>
<th>PUT</th>
<th>POST</th>
<th>Returns:</th>
</tr>
<tr>
<td>blocks</td>
<td>*</td>
<td></td>
<td></td>
<td>block array</td>
</tr>
<tr>
<td>cars</td>
<td>*</td>
<td></td>
<td></td>
<td>car array</td>
</tr>
<tr><td>configProfiles</td><td>*</td><td></td><td></td><td>configProfile array</td></tr>
<tr>
<td>consists</td>
<td>*</td>
<td></td>
<td></td>
<td>consist array</td>
</tr>
<tr>
<td>engines</td>
<td>*</td>
<td></td>
<td></td>
<td>engine array</td>
</tr>
<tr>
<td>hello</td>
<td>*</td>
<td></td>
<td></td>
<td>hello message and power message</td>
</tr>
<tr>
<td>layoutBlocks</td>
<td>*</td>
<td></td>
<td></td>
<td>layoutBlock array</td>
</tr>
<tr>
<td>lights</td>
<td>*</td>
<td></td>
<td></td>
<td>light array</td>
</tr>
<tr>
<td>locations</td>
<td>*</td>
<td></td>
<td></td>
<td>location array</td>
</tr>
<tr>
<td>memories</td>
<td>*</td>
<td></td>
<td></td>
<td>memory array</td>
</tr>
<tr>
<td>metadata</td>
<td>*</td>
<td></td>
<td></td>
<td>metadata array</td>
</tr>
<tr>
<td>networkServices</td>
<td>*</td>
<td></td>
<td></td>
<td>networkService array</td>
</tr>
<tr>
<td>node</td>
<td>*</td>
<td></td>
<td></td>
<td>node message</td>
</tr>
<tr>
<td>panels</td>
<td>*</td>
<td></td>
<td></td>
<td>panel array</td>
</tr>
<tr>
<td>power</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>n/a</td>
</tr>
<tr>
<td>railroad</td>
<td>*</td>
<td></td>
<td></td>
<td>railroad message</td>
</tr>
<tr>
<td>reporters</td>
<td>*</td>
<td></td>
<td></td>
<td>reporter array</td>
</tr>
<tr>
<td>roster</td>
<td>*</td>
<td></td>
<td></td>
<td>rosterEntry array</td>
</tr>
<tr>
<td>rosterGroups</td>
<td>*</td>
<td></td>
<td></td>
<td>rosterGroup array</td>
</tr>
<tr>
<td>routes</td>
<td>*</td>
<td></td>
<td></td>
<td>route array</td>
</tr>
<tr>
<td>signalHeads</td>
<td>*</td>
<td></td>
<td></td>
<td>signalHead array</td>
</tr>
<tr>
<td>signalMasts</td>
<td>*</td>
<td></td>
<td></td>
<td>signalMast array</td>
</tr>
<tr>
<td>sensors</td>
<td>*</td>
<td></td>
<td></td>
<td>sensor array</td>
</tr>
<tr>
<td>systemConnections</td>
<td>*</td>
<td></td>
<td></td>
<td>systemConnection array</td>
</tr>
<tr>
<td>time</td>
<td>*</td>
<td></td>
<td></td>
<td>n/a</td>
</tr>
<tr>
<td>trains</td>
<td>*</td>
<td></td>
<td></td>
<td>train array</td>
</tr>
<tr>
<td>turnouts</td>
<td>*</td>
<td></td>
<td></td>
<td>turnout array</td>
</tr>
</table>
<p>Note that these names are case sensitive, so you must
supply them as shown above. As an example, let's say you
issued the following GET command:</p>
<pre>
myjmri:12080/json/power
</pre>
<p>The would return JSON that looks something like this:</p>
<pre>
{"type":"power","data":{"state":2}}
</pre>
<p>The returned data consists of two parts:</p>
<ul>
<li><b>Type:</b> The type of item returned, which in this
case is "power"</li>
<li><b>Data:</b> Data that is specific to the type of
object returned. For example, in this case the state of
track power</li>
</ul>
<p>In order to understand all of the details on the data for
different types, the best solution is to look at the source
code. There are two files you can start with that will help a
lot:</p>
<ul>
<li><a href=
"https://github.com/JMRI/JMRI/blob/master/java/src/jmri/web/servlet/json/JsonServlet.java">
JsonServlet.java</a> contains the code that handles the
HTTP and Websocket requests</li>
<li><a href=
"https://github.com/JMRI/JMRI/blob/04a935d0c410174fbfa0cc6a2c34a818940b6b37/java/src/jmri/jmris/json/JsonUtil.java">
JsonUtil.java</a> that is used by JsonServlet to return the
data for different lists</li>
</ul>
<p>Continuing with the power example above, the JsonServlet
code calls JsonUtil.getPower to get the reply. Looking this
up in JsonUtil, the result is set to one of three values:
OFF, ON, or UNKNOWN. These values, in turn, are defined as
static imports. These names are too generic to be able to
find easily with Github's search box for searching the
repository. However, as UNKNOWN is defined like this:</p>
<pre>
import static jmri.jmris.json.JSON.UNKNOWN;
</pre>
<p>it makes sense to browse the source tree for for a file
called JSON.java under jmri/jmris/json. Sure enough, the file
exists and contains definitions for these three values:</p>
<pre>
public static final int UNKNOWN = 0x00;
public static final int ON = 0x02;
public static final int OFF = 0x04;
</pre>
<p>Therefore, the JSON returned above for power indicates
that the power is currently on (state is set to 2).</p>
<p>As you can see, given the number of lists and types, it's
best to do some searching through the source for all of the
details, as well as experimenting with a tool like Postman in
Google's Chrome browser.</p>
<h3>Type Names</h3>
<p>Here are the different type names supported for the second
pattern, along with which HTTP operations are supported:</p>
<table class="data">
<tr>
<th>Name</th>
<th>GET</th>
<th>PUT</th>
<th>POST</th>
<th>DELETE</th>
<th>Module</th>
</tr>
<tr>
<td>block</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>car</td>
<td>*</td>
<td></td>
<td></td>
<td></td>
<td>OperationsPro</td>
</tr>
<tr>
<td>consist</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>DecoderPro</td>
</tr>
<tr>
<td>engine</td>
<td>*</td>
<td></td>
<td></td>
<td></td>
<td>OperationsPro</td>
</tr>
<tr>
<td>layoutBlock</td>
<td>*</td>
<td></td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>light</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>location</td>
<td>*</td>
<td></td>
<td></td>
<td></td>
<td>OperationsPro</td>
</tr>
<tr>
<td>memory</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>metadata</td>
<td>*</td>
<td></td>
<td></td>
<td></td>
<td>DecoderPro</td>
</tr>
<tr>
<td>reporter</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>roster</td>
<td>*</td>
<td></td>
<td></td>
<td></td>
<td>DecoderPro</td>
</tr>
<tr>
<td>rosterEntry</td>
<td>*</td>
<td></td>
<td></td>
<td></td>
<td>DecoderPro</td>
</tr>
<tr>
<td>rosterGroup</td>
<td>*</td>
<td></td>
<td></td>
<td></td>
<td>DecoderPro</td>
</tr>
<tr>
<td>route</td>
<td>*</td>
<td></td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>sensor</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>signalHead</td>
<td>*</td>
<td></td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>signalMast</td>
<td>*</td>
<td></td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
<tr>
<td>train</td>
<td>*</td>
<td></td>
<td>*</td>
<td></td>
<td>OperationsPro</td>
</tr>
<tr>
<td>turnout</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
<td>PanelPro</td>
</tr>
</table>
<h2>Using HTTP JSON POST/PUT/DELETE</h2>
<p>You can also insert, update, or delete items in the lists
above using the HTTP POST, PUT, and DELETE methods. Not all
combinations are supported.</p>
<p>You can use these type names to set values. Continuing
with the power example above, you can turn the power on by
posting to a URL like this:</p>
<pre>
myjmri:12080/json/power
</pre>
<p>With this as the body</p>
<pre>
{"state":2}
</pre>
<h2>WebSocket</h2>
<p>The <b>WebSocket</b> feature of this servlet currently
handles JSON strings in four different forms:</p>
<ul>
<li>individual item state requests in the form:
<code>{"type":"turnout","data":{"name":"LT14"}}</code> that
are passed to type-specific handlers. These requests will
initiate "listeners", which will send updated responses
every time the item's state changes.
<ul>
<li>an item's state can be set by adding a
<strong>state</strong> node to the <em>data</em> node
in an HTTP POST request:
<code>{"type":"turnout","data":{"name":"LT14","state":4}}</code></li>
<li>individual types can be created if the request is
an HTTP PUT request. Note that not all types support
this.</li>
</ul>
</li>
<li>list requests in the form:
<code>{"list":"trains"}</code> or
<code>{"type":"list","list":"trains"}</code> that return an
array of items, or an empty array (<code>[]</code>). This request
initiates a "listener" which will resend
the entire list when items are added or removed, as well as
set individual listeners for each item (see above).</li>
<li>a heartbeat in the form <code>*</code> or
<code>{"type":"ping"}</code>. The <code>*</code> heartbeat
gets no response, while the JSON heartbeat causes a
<code>{"type":"pong"}</code> response.</li>
<li>a signoff in the form: <code>{"type":"goodbye"}</code>
to which an identical response is sent before the
connection gets closed.</li>
</ul>
<p>The <b>WebSocket</b> feature of the servlet can be seen by
browsing to the <b>/json/</b> url, where a <b>JSON
console</b> is providing for testing.<br>
Example commands and responses:</p>
<table id="jsonResponses">
<tr>
<th>command</th>
<th>response</th>
<th>notes</th>
</tr>
<tr>
<td>
<code>{"type":"sensor","data":{"name":"IS2"}}</code></td>
<td>
<code>{"type":"sensor","data":{"name":"IS2","state":4}}</code></td>
<td>request current state of sensor IS2</td>
</tr>
<tr>
<td>
<code>{"type":"sensor","data":{"name":"IS2","state":4}}</code></td>
<td>
<code>{"type":"sensor","data":{"name":"IS2","state":4}}</code></td>
<td>set sensor IS2 to InActive (4)</td>
</tr>
<tr>
<td>
<code>{"type":"turnout","data":{"name":"IT99","state":0}}</code></td>
<td>
<code>{"type":"turnout","data":{"name":"IT99","state":4}}</code></td>
<td>sending state=0 is also treated as request for
current state</td>
</tr>
<tr>
<td><code>{"type":"power","data":{}}</code></td>
<td><code>{"type":"power","data":{"state":2}}</code></td>
<td>power does not need a name</td>
</tr>
<tr>
<td>
<code>{"type":"memory","data":{"name":"IMCURRENTTIME"}}</code></td>
<td>
<code>{"type":"memory","data":{"name":"IMCURRENTTIME","userName":null,"comment":null,"value":"2:53
PM"}}</code></td>
<td>get (fast)clock time, resent each minute</td>
</tr>
<tr>
<td><code>{"type":"ping"}</code></td>
<td><code>{"type":"pong"}</code></td>
<td>ping request and response</td>
</tr>
<tr>
<td><code>{"list":"panels"}</code></td>
<td>
<code>[{"type":"panel","data":{"name":"ControlPanel/R&R","URL":"/panel/ControlPanel/R&R?format=xml","userName":"R&R","type":"Control
Panel"}},{"type":"panel","data":{"name":"Layout/Waccamaw%20Coast%20Line","URL":"/panel/Layout/Waccamaw%20Coast%20Line?format=xml","userName":"Waccamaw
Coast Line","type":"Layout"}}]</code></td>
<td>request a list of panels, respond with array, example
shows two panels</td>
</tr>
<tr>
<td>
<code>{"type":"throttle","data":{"throttle":"CSX754","address":754}}</code></td>
<td>
<code>{"type":"throttle","data":{"address":754,"speed":0.0,"forward":true,
"F0":false,"F1":false,"F2":false,"F3":false,"F4":false,"F5":false,"F6":false,"F7":false,
"F8":false,"F9":false,"F10":false,"F11":false,"F12":false,"F13":false,"F14":false,
"F15":false,"F16":false,"F17":false,"F18":false,"F19":false,"F20":false,"F21":false,
"F22":false,"F23":false,"F24":false,"F25":false,"F26":false,"F27":false,"F28":false,
"throttle":"CSX754"}}</code></td>
<td>request throttle for address 754, refer to it as
"CSX754"</td>
</tr>
<tr>
<td>
<code>{"type":"throttle","data":{"throttle":"CSX754","speed":0.25}}</code></td>
<td>
<code>{"type":"throttle","data":{"speed":0.25,"throttle":"CSX754"}}</code></td>
<td>request speed of throttle "CSX754" to 25%</td>
</tr>
<tr>
<td>