-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
939 lines (778 loc) · 29.2 KB
/
index.html
File metadata and controls
939 lines (778 loc) · 29.2 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<style type="text/css">
body {
font-family: Helvetica, arial, sans-serif;
font-size: 14px;
line-height: 1.6;
padding-top: 10px;
padding-bottom: 10px;
background-color: white;
padding: 30px; }
body > *:first-child {
margin-top: 0 !important; }
body > *:last-child {
margin-bottom: 0 !important; }
a {
color: #4183C4; }
a.absent {
color: #cc0000; }
a.anchor {
display: block;
padding-left: 30px;
margin-left: -30px;
cursor: pointer;
position: absolute;
top: 0;
left: 0;
bottom: 0; }
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
cursor: text;
position: relative; }
h1:hover a.anchor, h2:hover a.anchor, h3:hover a.anchor, h4:hover a.anchor, h5:hover a.anchor, h6:hover a.anchor {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA09pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoMTMuMCAyMDEyMDMwNS5tLjQxNSAyMDEyLzAzLzA1OjIxOjAwOjAwKSAgKE1hY2ludG9zaCkiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OUM2NjlDQjI4ODBGMTFFMTg1ODlEODNERDJBRjUwQTQiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OUM2NjlDQjM4ODBGMTFFMTg1ODlEODNERDJBRjUwQTQiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo5QzY2OUNCMDg4MEYxMUUxODU4OUQ4M0REMkFGNTBBNCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo5QzY2OUNCMTg4MEYxMUUxODU4OUQ4M0REMkFGNTBBNCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PsQhXeAAAABfSURBVHjaYvz//z8DJYCRUgMYQAbAMBQIAvEqkBQWXI6sHqwHiwG70TTBxGaiWwjCTGgOUgJiF1J8wMRAIUA34B4Q76HUBelAfJYSA0CuMIEaRP8wGIkGMA54bgQIMACAmkXJi0hKJQAAAABJRU5ErkJggg==) no-repeat 10px center;
text-decoration: none; }
h1 tt, h1 code {
font-size: inherit; }
h2 tt, h2 code {
font-size: inherit; }
h3 tt, h3 code {
font-size: inherit; }
h4 tt, h4 code {
font-size: inherit; }
h5 tt, h5 code {
font-size: inherit; }
h6 tt, h6 code {
font-size: inherit; }
h1 {
font-size: 28px;
color: black; }
h2 {
font-size: 24px;
border-bottom: 1px solid #cccccc;
color: black; }
h3 {
font-size: 18px; }
h4 {
font-size: 16px; }
h5 {
font-size: 14px; }
h6 {
color: #777777;
font-size: 14px; }
p, blockquote, ul, ol, dl, li, table, pre {
margin: 15px 0; }
hr {
background: transparent url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAECAYAAACtBE5DAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OENDRjNBN0E2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OENDRjNBN0I2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo4Q0NGM0E3ODY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo4Q0NGM0E3OTY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PqqezsUAAAAfSURBVHjaYmRABcYwBiM2QSA4y4hNEKYDQxAEAAIMAHNGAzhkPOlYAAAAAElFTkSuQmCC) repeat-x 0 0;
border: 0 none;
color: #cccccc;
height: 4px;
padding: 0;
}
body > h2:first-child {
margin-top: 0;
padding-top: 0; }
body > h1:first-child {
margin-top: 0;
padding-top: 0; }
body > h1:first-child + h2 {
margin-top: 0;
padding-top: 0; }
body > h3:first-child, body > h4:first-child, body > h5:first-child, body > h6:first-child {
margin-top: 0;
padding-top: 0; }
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0; }
h1 p, h2 p, h3 p, h4 p, h5 p, h6 p {
margin-top: 0; }
li p.first {
display: inline-block; }
li {
margin: 0; }
ul, ol {
padding-left: 30px; }
ul :first-child, ol :first-child {
margin-top: 0; }
dl {
padding: 0; }
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px; }
dl dt:first-child {
padding: 0; }
dl dt > :first-child {
margin-top: 0; }
dl dt > :last-child {
margin-bottom: 0; }
dl dd {
margin: 0 0 15px;
padding: 0 15px; }
dl dd > :first-child {
margin-top: 0; }
dl dd > :last-child {
margin-bottom: 0; }
blockquote {
border-left: 4px solid #dddddd;
padding: 0 15px;
color: #777777; }
blockquote > :first-child {
margin-top: 0; }
blockquote > :last-child {
margin-bottom: 0; }
table {
padding: 0;border-collapse: collapse; }
table tr {
border-top: 1px solid #cccccc;
background-color: white;
margin: 0;
padding: 0; }
table tr:nth-child(2n) {
background-color: #f8f8f8; }
table tr th {
font-weight: bold;
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
table tr td {
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
table tr th :first-child, table tr td :first-child {
margin-top: 0; }
table tr th :last-child, table tr td :last-child {
margin-bottom: 0; }
img {
max-width: 100%; }
span.frame {
display: block;
overflow: hidden; }
span.frame > span {
border: 1px solid #dddddd;
display: block;
float: left;
overflow: hidden;
margin: 13px 0 0;
padding: 7px;
width: auto; }
span.frame span img {
display: block;
float: left; }
span.frame span span {
clear: both;
color: #333333;
display: block;
padding: 5px 0 0; }
span.align-center {
display: block;
overflow: hidden;
clear: both; }
span.align-center > span {
display: block;
overflow: hidden;
margin: 13px auto 0;
text-align: center; }
span.align-center span img {
margin: 0 auto;
text-align: center; }
span.align-right {
display: block;
overflow: hidden;
clear: both; }
span.align-right > span {
display: block;
overflow: hidden;
margin: 13px 0 0;
text-align: right; }
span.align-right span img {
margin: 0;
text-align: right; }
span.float-left {
display: block;
margin-right: 13px;
overflow: hidden;
float: left; }
span.float-left span {
margin: 13px 0 0; }
span.float-right {
display: block;
margin-left: 13px;
overflow: hidden;
float: right; }
span.float-right > span {
display: block;
overflow: hidden;
margin: 13px auto 0;
text-align: right; }
code, tt {
margin: 0 2px;
padding: 0 5px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px; }
pre code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent; }
.highlight pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px; }
pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px; }
pre code, pre tt {
background-color: transparent;
border: none; }
sup {
font-size: 0.83em;
vertical-align: super;
line-height: 0;
}
kbd {
display: inline-block;
padding: 3px 5px;
font-size: 11px;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: solid 1px #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb
}
* {
-webkit-print-color-adjust: exact;
}
@media screen and (min-width: 914px) {
body {
width: 854px;
margin:0 auto;
}
}
@media print {
table, pre {
page-break-inside: avoid;
}
pre {
word-wrap: break-word;
}
}
</style>
</head>
<body>
<h1 id="toc_0">Projet NSY102 - Composite WEB OF THING- jmDNS</h1>
<h4 id="toc_1"><em>28/04/2021</em></h4>
<table>
<thead>
<tr>
<th style="text-align: left">Nom</th>
<th style="text-align: center">Prénom</th>
<th style="text-align: right">Matricule</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left">VALENTIN</td>
<td style="text-align: center">Fabien</td>
<td style="text-align: right">0g5drtfg8y8</td>
</tr>
</tbody>
</table>
<h2 id="toc_2">Introduction</h2>
<h3 id="toc_3">En m'inspirant de votre idée proposée sur le forum j'ai développé une mini application permettant d'imaginer une maison domotisée avec des pièces contenant des services d'objets connectés WebOfThing et de les détecter via jmDNS.</h3>
<h3 id="toc_4">Exécuter le jeu d'essai du projet :</h3>
<ul>
<li><p>Lancer la classe jmDNS/MainDiscoverServices</p></li>
<li><p>Lancer la classe MainWoT</p></li>
</ul>
<p><em>Info DNS "webthing" dans le terminal :</em></p>
<div><pre><code class="language-none">dns-sd -B _webthing</code></pre></div>
<h2 id="toc_5">Questions</h2>
<h3 id="toc_6">Q1) Création grâce au patron composite d'une maison avec des pièces (cuisine, salle de bain..) avec des objets connectés WebOfThing dans chaques pièces. (1 objet = 1 serveur API WebOfThing)</h3>
<p>L'ensemble des classes constituantes du patron composite des objets WoT se trouvent dans le package CompositeWot</p>
<h4 id="toc_7">Patron Composite :</h4>
<p><strong>Classe composite :</strong></p>
<div><pre><code class="language-none">public abstract class Composite extends Composant implements Iterable<Composant> {
private List<Composant> list;
public Composite(String title) {
super(title);
this.list = new ArrayList<>();
}
public Composite ajouter(Composant composant) {
composant.setPieceMere(this.getNom());
list.add(composant);
return this;
}
public int nombreDeIoT() {
int nombre = 0;
for (Composant c : this) {
nombre += c.nombreDeIoT();
}
return nombre;
}
@Override
public Iterator<Composant> iterator() {
return list.iterator();
}
}</code></pre></div>
<p><strong>Classe Liste de composants :</strong></p>
<div><pre><code class="language-none">public class ListeDeComposants extends Composite {
public ListeDeComposants(String nom) {
super(nom);
}
@Override
public WebThingServer getServer() {
throw new UnsupportedOperationException();
}
@Override
public <T> T accepter(Visiteur<T> visiteur) {
return visiteur.visite(this);
}
}</code></pre></div>
<p><strong>Classe Composant :</strong></p>
<div><pre><code class="language-none">public abstract class Composant {
private String title;
private String piece;
public Composant(String title) {
this.title = title;
}
public String getNom() {
return this.title;
}
public abstract WebThingServer getServer();
public abstract int nombreDeIoT();
public abstract <T> T accepter(Visiteur<T> visiteur);
protected void setPieceMere(String nom) {
this.piece = nom;
}
public String getPiece() {
return piece;
}
}</code></pre></div>
<p><strong>Une feuille composant WoT :</strong></p>
<p>A chaque composant est associé sa pièce maitresse (<em>ex : ampoule est dans le salon, interrupteur est dans le salle de bain...</em>)</p>
<div><pre><code class="language-none">public class Ampoule extends Composant {
private WebThingServer server;
private Thing thing;
private String id;
public Ampoule(String title, int port) throws IOException {
super(title);
id = UUID.randomUUID().toString();
thing = new Thing(id,
title,
new JSONArray(Arrays.asList(
title)),
"Une ampoule connectée");
server = new WebThingServer(new WebThingServer.SingleThing(thing), port);
}
protected void setPieceMere(String nom) {
super.setPieceMere(nom);
JSONObject onDescription = new JSONObject();
onDescription.put("@type", "Piece");
onDescription.put("nom", nom);
onDescription.put("description", "La pièce dans laquelle l'objet se trouve");
thing.addProperty(new Property(thing,
"piece",
new Value(nom),
onDescription));
}
@Override
public WebThingServer getServer() {
return server;
}
@Override
public int nombreDeIoT() {
return 1;
}
@Override
public <T> T accepter(Visiteur<T> visiteur) {
return visiteur.visite(this);
}
}</code></pre></div>
<h3 id="toc_8">Q2) Création d'un visiteur permettant de créer et démarrer/éteindre les services d'objets connectés</h3>
<p>La patron composite va nous permettre de "visiter" chaque composant mais aussi plusieurs composants.
C'est pourquoi, grâce aux deux visiteurs VisiteurOn et VisiteurOff, nous allons pouvoir démarrer ou éteindre le serveur
d'un seul composant WoT mais aussi d'un sous ensemble de composant WoT appartenant à une pièce (<em>salon, cuisine, salle de bain...)</em></p>
<p><strong>Visiteur de démarrage :</strong></p>
<div><pre><code class="language-none">public class VisiteurOn extends Visiteur<Boolean> {
@Override
public Boolean visite(Composant n) {
try {
System.out.println("Starting WoT (pièce " + n.getPiece() + ") :" + n.getNom());
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
n.getServer().stop();
}
});
Thread.sleep(5000);
n.getServer().start(false);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public Boolean visite(ListeDeComposants n) {
boolean result = true;
for (Composant c : n) {
result &= c.accepter(this);
}
return result;
}
}</code></pre></div>
<p><strong>Visiteur pour éteindre :</strong></p>
<div><pre><code class="language-none">public class VisiteurOff extends Visiteur<Boolean> {
@Override
public Boolean visite(Composant n) {
try {
System.out.println("Stopping WoT :" + n.getNom());
n.getServer().stop();
} catch (Exception e) {
return false;
}
return true;
}
@Override
public Boolean visite(ListeDeComposants n) {
boolean result = true;
for (Composant c : n) {
result &= c.accepter(this);
}
return result;
}
}</code></pre></div>
<h3 id="toc_9">Q3) Le jeu d'essai MainWot</h3>
<p>Ci-dessous un petit jeu d'essai pour simuler une maison avec des pièces constituées de composants WoT :</p>
<table>
<thead>
<tr>
<th style="text-align: left">Pièce</th>
<th style="text-align: center">Objets</th>
<th style="text-align: center">Port</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left">Maison</td>
<td style="text-align: center">Salon</td>
<td style="text-align: center">_</td>
</tr>
<tr>
<td style="text-align: left"></td>
<td style="text-align: center">Cuisine</td>
<td style="text-align: center">_</td>
</tr>
<tr>
<td style="text-align: left"></td>
<td style="text-align: center">Salle de bain</td>
<td style="text-align: center">_</td>
</tr>
<tr>
<td style="text-align: left">Salon</td>
<td style="text-align: center">Ampoule</td>
<td style="text-align: center">8060</td>
</tr>
<tr>
<td style="text-align: left"></td>
<td style="text-align: center">Capteur Temperature</td>
<td style="text-align: center">8070</td>
</tr>
<tr>
<td style="text-align: left"></td>
<td style="text-align: center">Interrupteur</td>
<td style="text-align: center">8080</td>
</tr>
<tr>
<td style="text-align: left">Cuisine</td>
<td style="text-align: center">Ampoule</td>
<td style="text-align: center">8061</td>
</tr>
<tr>
<td style="text-align: left"></td>
<td style="text-align: center">Interrupteur</td>
<td style="text-align: center">8081</td>
</tr>
<tr>
<td style="text-align: left">Salle de bain</td>
<td style="text-align: center">Capteur Temperature</td>
<td style="text-align: center">8071</td>
</tr>
</tbody>
</table>
<div><pre><code class="language-none">public class MainWoT {
public static void main(String[] args) {
//Les pièces de la maison :
ListeDeComposants maison = new ListeDeComposants("Maison");
ListeDeComposants salon = new ListeDeComposants("Salon");
ListeDeComposants cuisine = new ListeDeComposants("Cuisine");
ListeDeComposants sdb = new ListeDeComposants("Salle de bain");
try {
//3 objets dans le salon...
Composant iotSalon1 = new Ampoule("Ampoule", 8060);
Composant iotSalon2 = new CapteurTemperature("Capteur Temperature", 8070);
Composant iotSalon3 = new Interrupteur("Interrupteur", 8080);
salon.ajouter(iotSalon1).ajouter(iotSalon2).ajouter(iotSalon3);
maison.ajouter(salon);
//visiteurs string+start
Visiteur<String> visiteurStr = new VisiteurString();
System.out.println(visiteurStr.visite(maison));
Visiteur<Boolean> visiteurOn = new VisiteurOn();
boolean startStatus = visiteurOn.visite(maison); //on démarre tous les objets de la maison...
System.out.println("Starting WoT component of house : " + startStatus);
Thread.sleep(15000);
Visiteur<Boolean> visiteurOff = new VisiteurOff();
boolean stopStatus = visiteurOff.visite(maison); //on éteint tous les objets de la maison...
System.out.println("Stopping all WoT component of house : " + stopStatus);
Thread.sleep(15000);
//2 objets dans la cuisine...
Composant iotCuisine1 = new Ampoule("Ampoule", 8061);
Composant iotCuisine2 = new CapteurTemperature("Interrupteur", 8081);
cuisine.ajouter(iotCuisine1).ajouter(iotCuisine2);
maison.ajouter(cuisine);
//visiteurs string+start
System.out.println(visiteurStr.visite(maison));
startStatus = visiteurOn.visite(maison); //on démarre tous les objets de la maison...(salon+cuisine donc..)
System.out.println("Starting WoT component of house : " + startStatus);
Thread.sleep(15000);
//1 objet dans la salle de bain...
Composant iotSdb2 = new CapteurTemperature("Capteur Temperature", 8071);
sdb.ajouter(iotSdb2);
//visiteurs string+start
System.out.println(visiteurStr.visite(sdb));
startStatus = visiteurOn.visite(sdb); //on démarre tous les objets de la salle de bain uniquement...
System.out.println("Starting WoT component of bathroom only : " + startStatus);
} catch (IOException e) {
System.out.println("Erreur lors du deploiement d'un WoT");
} catch (InterruptedException e) {
System.out.println("Thead.sleep InterruptedException");
}
}
}</code></pre></div>
<h3 id="toc_10">Q4) Découverte des services DNS WebOfThing via jmDNS. (éteints et démarrés par les visiteurs respectifs).</h3>
<p>La classe suivante permet de découvrir les services mDNS grâce à la librairie jmDNS.</p>
<p>Plus spécifiquement ici, ce sont les services <strong>_webthing</strong> qui nous interesses.</p>
<p>Nous allons installer deux listenners dans l'attente de la découverte de services <strong>_webthing</strong> :</p>
<ul>
<li><p>StringListener : Se contente d'afficher un retour console.</p></li>
<li><p>IHMListener : Voir Q5</p></li>
</ul>
<p><strong>La classe MainDiscoverServices:</strong></p>
<div><pre><code class="language-none">public class MainDiscoverServices {
private static final String serviceDNS = "_webthing._tcp.local.";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Logger logger = Logger.getLogger(JmDNS.class.getName());
ConsoleHandler handler = new ConsoleHandler();
logger.addHandler(handler);
logger.setLevel(Level.FINER);
handler.setLevel(Level.FINER);
try {
JmDNS jmdns = null;
InetAddress addr = InetAddress.getLocalHost();
String hostname = InetAddress.getByName(addr.getHostName()).toString();
try {
System.out.println("Listening on " + hostname);
jmdns = JmDNS.create(addr, hostname); // throws IOException
} catch (IOException e) {
e.printStackTrace(); // handle this if you want
}
StringListener strListener = new StringListener();
IHMListener ihmListener = new IHMListener(jmdns);
//jmdns.addServiceListener(serviceDNS, strListener);
jmdns.addServiceListener(serviceDNS, ihmListener);
JmDNS finalJmdns = jmdns;
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
finalJmdns.unregisterAllServices();
finalJmdns.removeServiceListener(serviceDNS, strListener);
finalJmdns.removeServiceListener(serviceDNS, ihmListener);
finalJmdns.close();
} catch (Exception e) {
}
}
});
System.out.println("Apputer sur la touche q et Entrer, pour terminer le programme" + jmdns.getInetAddress());
int b;
while ((b = System.in.read()) != -1 && (char) b != 'q') {
/* en attente utilisateur */
}
jmdns.unregisterAllServices();
jmdns.removeServiceListener(serviceDNS, strListener);
jmdns.removeServiceListener(serviceDNS, ihmListener);
jmdns.close();
System.out.println("Terminé");
} catch (IOException e) {
e.printStackTrace();
}
}
}</code></pre></div>
<h3 id="toc_11">Q5) IHM pour visualiser le démarrage/arrêt des objets WoT via jmDNS</h3>
<p>Ce listener permet d'ecouter les evenements jmDNS à travers une IHM.</p>
<p>Dans cette IHM j'ai ajouté un bouton permettant de faire une requête sur un service WoT.
Le but de cette requête est de faire une demande concernant la pièce dans laquelle se trouve l'objet connecté.
Cette information est disponible dans le JSON du service jmDNS.</p>
<p>Le bouton va donc déclencher l'appel au Thread <strong>ThreadURLRequest</strong>.
Ce thread est chargé d'executer la requete HTTP ainsi que de parser le JSON de retour de cette requête.</p>
<div><pre><code class="language-none">public class IHMListener implements ServiceListener {
//JMdns
private JmDNS jmdns;
//UI
private JFrame frame;
private final DefaultTableModel dm;
//Json data from WoT object...
private Vector<Vector<Object>> data;
public IHMListener(JmDNS jmdns) {
this.data = new Vector<>();
this.jmdns = jmdns;
frame = new JFrame("Ma maison connectée");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 300);
Vector<Object> columnNames = new Vector<>();
columnNames.add("Objet WoT");
columnNames.add("API URL");
columnNames.add("Pièce");
dm = new DefaultTableModel(data, columnNames);
JTable table = new JTable(dm);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(36, 37, 407, 79);
Action request = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JTable table = (JTable) e.getSource();
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
Vector<String> data = dtm.getDataVector().get(table.getSelectedRow());
Thread threadURLRequest = new Thread(new ThreadURLRequest(data.get(1), data.get(0)));
threadURLRequest.start();
}
};
ButtonColumn buttonColumn = new ButtonColumn(table, request, 2);
buttonColumn.setMnemonic(KeyEvent.VK_D);
frame.add(table);
frame.setVisible(true);
}
@Override
public void serviceAdded(ServiceEvent event) {
ServiceInfo info = jmdns.getServiceInfo(event.getType(), event.getName());
if (info != null) {
Vector<Object> aData = new Vector<Object>();
String url =
String.format("http://%s:%s",
info.getHostAddresses()[0],
info.getPort()
);
aData.add(event.getName());
aData.add(url);
aData.add("Pièce ? (Requête HTTP)");
data.add(aData);
dm.fireTableDataChanged();
}
}
@Override
public void serviceRemoved(ServiceEvent event) {
try {
Vector<Vector> foundRow = new Vector<>();
for (Vector<Object> row : data) {
if (row.get(0).equals(event.getName())) {
foundRow.add(row);
}
}
data.removeAll(foundRow);
dm.fireTableDataChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void serviceResolved(ServiceEvent event) { }
/**
* Ce thread permet de faire une requete HTTP pour obtenir le nom de la pièce
*/
public class ThreadURLRequest implements Runnable {
private String url;
private String name;
public ThreadURLRequest(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
JSONObject json = getJSONFromUrl(url + "/properties/piece");
String piece = new String("Requete échouée");
if (json != null) {
piece = json.getString("piece");
System.out.println("[IHMListener]Requete http de la pièce...(url:" + url + ", nom :" + name + ")" + piece);
}
Vector<Vector> foundRow = new Vector<>();
for (Vector<Object> row : data) {
if (row.get(0).equals(name)) {
foundRow.add(row);
}
}
data.removeAll(foundRow);
Vector<Object> aData = new Vector<Object>();
aData.add(name);
aData.add(url);
aData.add(piece);
data.add(aData);
dm.fireTableDataChanged();
}
/**
* URL to Json Object
*
* @param urlStr
* @return
*/
public JSONObject getJSONFromUrl(String urlStr) {
try {
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"));
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());/**/
}
JSONObject jsonObject = new JSONObject(
"" + response.toString() + ""
);
return jsonObject;
} catch (Exception e) {
System.out.println(e.toString());
}
return null;
}
}
}</code></pre></div>
</body>
</html>