This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
ThingManagerImpl.java
1364 lines (1208 loc) · 60.2 KB
/
ThingManagerImpl.java
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
/**
* Copyright (c) 2014,2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.core.thing.internal;
import java.net.URI;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.ConfigDescriptionRegistry;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator;
import org.eclipse.smarthome.core.common.SafeCaller;
import org.eclipse.smarthome.core.common.ThreadPoolManager;
import org.eclipse.smarthome.core.common.registry.Identifiable;
import org.eclipse.smarthome.core.common.registry.ManagedProvider;
import org.eclipse.smarthome.core.common.registry.Provider;
import org.eclipse.smarthome.core.events.EventPublisher;
import org.eclipse.smarthome.core.service.ReadyMarker;
import org.eclipse.smarthome.core.service.ReadyMarkerFilter;
import org.eclipse.smarthome.core.service.ReadyService;
import org.eclipse.smarthome.core.storage.Storage;
import org.eclipse.smarthome.core.storage.StorageService;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelGroupUID;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingManager;
import org.eclipse.smarthome.core.thing.ThingRegistry;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.ThingStatusInfo;
import org.eclipse.smarthome.core.thing.ThingTypeMigrationService;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.UID;
import org.eclipse.smarthome.core.thing.binding.BridgeHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.thing.binding.builder.ThingStatusInfoBuilder;
import org.eclipse.smarthome.core.thing.events.ThingEventFactory;
import org.eclipse.smarthome.core.thing.i18n.ThingStatusInfoI18nLocalizationService;
import org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry;
import org.eclipse.smarthome.core.thing.type.ChannelDefinition;
import org.eclipse.smarthome.core.thing.type.ChannelGroupType;
import org.eclipse.smarthome.core.thing.type.ChannelGroupTypeRegistry;
import org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID;
import org.eclipse.smarthome.core.thing.type.ChannelType;
import org.eclipse.smarthome.core.thing.type.ChannelTypeRegistry;
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID;
import org.eclipse.smarthome.core.thing.type.ThingType;
import org.eclipse.smarthome.core.thing.type.ThingTypeRegistry;
import org.eclipse.smarthome.core.thing.util.ThingHandlerHelper;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.util.BundleResolver;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link ThingManagerImpl} tracks all things in the {@link ThingRegistry} and
* mediates the communication between the {@link Thing} and the {@link ThingHandler} from the binding. Therefore it
* tracks {@link ThingHandlerFactory}s and calls {@link ThingHandlerFactory#registerHandler(Thing)} for each thing, that
* was added to the {@link ThingRegistry}. In addition the {@link ThingManagerImpl} acts
* as an {@link EventHandler} and subscribes to smarthome update and command
* events. Finally the {@link ThingManagerImpl} implement the {@link ThingTypeMigrationService} to offer
* a way to change the thing type of a {@link Thing}.
*
* @author Dennis Nobel - Initial contribution
* @author Michael Grammling - Added dynamic configuration update
* @author Stefan Bußweiler - Added new thing status handling, migration to new event mechanism,
* refactorings due to thing/bridge life cycle
* @author Simon Kaufmann - Added remove handling, type conversion
* @author Kai Kreuzer - Removed usage of itemRegistry and thingLinkRegistry, fixed vetoing mechanism
* @author Andre Fuechsel - Added the {@link ThingTypeMigrationService}
* @author Thomas Höfer - Added localization of thing status info
* @author Christoph Weitkamp - Moved OSGI ServiceTracker from BaseThingHandler to ThingHandlerCallback
* @author Henning Sudbrock - Consider thing type properties when migrating to new thing type
* @author Christoph Weitkamp - Added preconfigured ChannelGroupBuilder
* @author Yordan Zhelev - Added thing disabling mechanism
*/
@Component(immediate = true, service = { ThingTypeMigrationService.class, ThingManager.class })
public class ThingManagerImpl
implements ThingManager, ThingTracker, ThingTypeMigrationService, ReadyService.ReadyTracker {
static final String XML_THING_TYPE = "esh.xmlThingTypes";
private static final String THING_STATUS_STORAGE_NAME = "thing_status_storage";
private static final String FORCEREMOVE_THREADPOOL_NAME = "forceRemove";
private static final String THING_MANAGER_THREADPOOL_NAME = "thingManager";
private final Logger logger = LoggerFactory.getLogger(ThingManagerImpl.class);
private final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(THING_MANAGER_THREADPOOL_NAME);
private EventPublisher eventPublisher;
private CommunicationManager communicationManager;
private ReadyService readyService;
private final List<ThingHandlerFactory> thingHandlerFactories = new CopyOnWriteArrayList<>();
private final Map<ThingUID, ThingHandler> thingHandlers = new ConcurrentHashMap<>();
private final Map<ThingHandlerFactory, Set<ThingHandler>> thingHandlersByFactory = new HashMap<>();
private ThingTypeRegistry thingTypeRegistry;
private ChannelTypeRegistry channelTypeRegistry;
private ChannelGroupTypeRegistry channelGroupTypeRegistry;
private ItemChannelLinkRegistry itemChannelLinkRegistry;
private ThingStatusInfoI18nLocalizationService thingStatusInfoI18nLocalizationService;
private final Map<ThingUID, Lock> thingLocks = new HashMap<>();
private final Set<String> loadedXmlThingTypes = new CopyOnWriteArraySet<>();
private SafeCaller safeCaller;
private volatile boolean active = false;
private StorageService storageService;
private Storage<String> storage;
private final ThingHandlerCallback thingHandlerCallback = new ThingHandlerCallback() {
@Override
public void stateUpdated(ChannelUID channelUID, State state) {
communicationManager.stateUpdated(channelUID, state);
}
@Override
public void postCommand(ChannelUID channelUID, Command command) {
communicationManager.postCommand(channelUID, command);
}
@Override
public void channelTriggered(Thing thing, ChannelUID channelUID, String event) {
communicationManager.channelTriggered(thing, channelUID, event);
}
@Override
public void statusUpdated(Thing thing, ThingStatusInfo statusInfo) {
// note: all provoked operations based on a status update should be executed asynchronously!
ThingStatusInfo oldStatusInfo = thing.getStatusInfo();
ensureValidStatus(oldStatusInfo.getStatus(), statusInfo.getStatus());
if (ThingStatus.REMOVING.equals(oldStatusInfo.getStatus())
&& !ThingStatus.REMOVED.equals(statusInfo.getStatus())) {
// only allow REMOVING -> REMOVED transition, all others are illegal
throw new IllegalArgumentException(MessageFormat.format(
"Illegal status transition from REMOVING to {0}, only REMOVED would have been allowed.",
statusInfo.getStatus()));
}
// update thing status and send event about new status
setThingStatus(thing, statusInfo);
// if thing is a bridge
if (isBridge(thing)) {
handleBridgeStatusUpdate((Bridge) thing, statusInfo, oldStatusInfo);
}
// if thing has a bridge
if (hasBridge(thing)) {
handleBridgeChildStatusUpdate(thing, oldStatusInfo);
}
// notify thing registry about thing removal
if (ThingStatus.REMOVED.equals(thing.getStatus())) {
notifyRegistryAboutForceRemove(thing);
}
}
private void ensureValidStatus(ThingStatus oldStatus, ThingStatus newStatus) {
if (!(ThingStatus.UNKNOWN.equals(newStatus) || ThingStatus.ONLINE.equals(newStatus)
|| ThingStatus.OFFLINE.equals(newStatus) || ThingStatus.REMOVED.equals(newStatus))) {
throw new IllegalArgumentException(MessageFormat.format(
"Illegal status {0}. Bindings only may set {1}, {2}, {3} or {4}.", newStatus,
ThingStatus.UNKNOWN, ThingStatus.ONLINE, ThingStatus.OFFLINE, ThingStatus.REMOVED));
}
if (ThingStatus.REMOVED.equals(newStatus) && !ThingStatus.REMOVING.equals(oldStatus)) {
throw new IllegalArgumentException(
MessageFormat.format("Illegal status {0}. The thing was in state {1} and not in {2}", newStatus,
oldStatus, ThingStatus.REMOVING));
}
}
private void handleBridgeStatusUpdate(Bridge bridge, ThingStatusInfo statusInfo,
ThingStatusInfo oldStatusInfo) {
if (ThingHandlerHelper.isHandlerInitialized(bridge)
&& (ThingStatus.INITIALIZING.equals(oldStatusInfo.getStatus()))) {
// bridge has just been initialized: initialize child things as well
registerChildHandlers(bridge);
} else if (!statusInfo.equals(oldStatusInfo)) {
// bridge status has been changed: notify child things about status change
notifyThingsAboutBridgeStatusChange(bridge, statusInfo);
}
}
private void handleBridgeChildStatusUpdate(Thing thing, ThingStatusInfo oldStatusInfo) {
if (ThingHandlerHelper.isHandlerInitialized(thing)
&& ThingStatus.INITIALIZING.equals(oldStatusInfo.getStatus())) {
// child thing has just been initialized: notify bridge about it
notifyBridgeAboutChildHandlerInitialization(thing);
}
}
@Override
public void thingUpdated(final Thing thing) {
thingUpdatedLock.add(thing.getUID());
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
Provider<Thing> provider = thingRegistry.getProvider(thing);
if (provider == null) {
throw new IllegalArgumentException(MessageFormat.format(
"Provider for thing {0} cannot be determined because it is not known to the registry",
thing.getUID().getAsString()));
}
if (provider instanceof ManagedProvider) {
@SuppressWarnings("unchecked")
ManagedProvider<Thing, ThingUID> managedProvider = (ManagedProvider<Thing, ThingUID>) provider;
managedProvider.update(thing);
} else {
logger.debug("Only updating thing {} in the registry because provider {} is not managed.",
thing.getUID().getAsString(), provider);
thingRegistry.updated(provider, thingRegistry.get(thing.getUID()), thing);
}
return null;
}
});
thingUpdatedLock.remove(thing.getUID());
}
@Override
public void validateConfigurationParameters(Thing thing, Map<String, Object> configurationParameters) {
ThingType thingType = thingTypeRegistry.getThingType(thing.getThingTypeUID());
if (thingType != null && thingType.getConfigDescriptionURI() != null) {
configDescriptionValidator.validate(configurationParameters, thingType.getConfigDescriptionURI());
}
}
@Override
public void configurationUpdated(Thing thing) {
if (!ThingHandlerHelper.isHandlerInitialized(thing)) {
initializeHandler(thing);
}
}
@Override
public void migrateThingType(final Thing thing, final ThingTypeUID thingTypeUID,
final Configuration configuration) {
ThingManagerImpl.this.migrateThingType(thing, thingTypeUID, configuration);
}
@Override
public ChannelBuilder createChannelBuilder(ChannelUID channelUID, ChannelTypeUID channelTypeUID) {
ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID);
if (channelType == null) {
throw new IllegalArgumentException(String.format("Channel type '%s' is not known", channelTypeUID));
}
return ThingFactoryHelper.createChannelBuilder(channelUID, channelType, configDescriptionRegistry);
};
@Override
public ChannelBuilder editChannel(Thing thing, ChannelUID channelUID) {
Channel channel = thing.getChannel(channelUID.getId());
if (channel == null) {
throw new IllegalArgumentException(
String.format("Channel '%s' does not exist for thing '%s'", channelUID, thing.getUID()));
}
return ChannelBuilder.create(channel);
}
@Override
public List<ChannelBuilder> createChannelBuilders(ChannelGroupUID channelGroupUID,
ChannelGroupTypeUID channelGroupTypeUID) {
ChannelGroupType channelGroupType = channelGroupTypeRegistry.getChannelGroupType(channelGroupTypeUID);
if (channelGroupType == null) {
throw new IllegalArgumentException(
String.format("Channel group type '%s' is not known", channelGroupTypeUID));
}
List<ChannelBuilder> channelBuilders = new ArrayList<>();
for (ChannelDefinition channelDefinition : channelGroupType.getChannelDefinitions()) {
ChannelType channelType = channelTypeRegistry.getChannelType(channelDefinition.getChannelTypeUID());
if (channelType != null) {
ChannelUID channelUID = new ChannelUID(channelGroupUID, channelDefinition.getId());
channelBuilders.add(ThingFactoryHelper.createChannelBuilder(channelUID, channelType,
configDescriptionRegistry));
}
}
return channelBuilders;
}
@Override
public boolean isChannelLinked(ChannelUID channelUID) {
return itemChannelLinkRegistry.isLinked(channelUID);
}
};
private ThingRegistryImpl thingRegistry;
private BundleResolver bundleResolver;
private ConfigDescriptionRegistry configDescriptionRegistry;
private ConfigDescriptionValidator configDescriptionValidator;
private final Set<Thing> things = new CopyOnWriteArraySet<>();
private final Set<ThingUID> registerHandlerLock = new HashSet<>();
private final Set<ThingUID> thingUpdatedLock = new HashSet<>();
@Override
public void migrateThingType(final Thing thing, final ThingTypeUID thingTypeUID,
final Configuration configuration) {
final ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID);
if (thingType == null) {
throw new IllegalStateException(
MessageFormat.format("No thing type {0} registered, cannot change thing type for thing {1}",
thingTypeUID.getAsString(), thing.getUID().getAsString()));
}
scheduler.schedule(new Runnable() {
@Override
public void run() {
Lock lock = getLockForThing(thing.getUID());
try {
lock.lock();
ThingUID thingUID = thing.getUID();
waitForRunningHandlerRegistrations(thingUID);
// Remove the ThingHandler, if any
final ThingHandlerFactory oldThingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
if (oldThingHandlerFactory != null) {
ThingHandler thingHandler = thing.getHandler();
unregisterAndDisposeHandler(oldThingHandlerFactory, thing, thingHandler);
waitUntilHandlerUnregistered(thing, 60 * 1000);
} else {
logger.debug("No ThingHandlerFactory available that can handle {}", thing.getThingTypeUID());
}
// Set the new channels
List<Channel> channels = ThingFactoryHelper.createChannels(thingType, thingUID,
configDescriptionRegistry);
((ThingImpl) thing).setChannels(channels);
// Set the given configuration
ThingFactoryHelper.applyDefaultConfiguration(configuration, thingType, configDescriptionRegistry);
((ThingImpl) thing).setConfiguration(configuration);
// Set the new properties (keeping old properties, unless they have the same name as a new property)
for (Entry<String, String> entry : thingType.getProperties().entrySet()) {
((ThingImpl) thing).setProperty(entry.getKey(), entry.getValue());
}
// Change the ThingType
((ThingImpl) thing).setThingTypeUID(thingTypeUID);
// Register the new Handler - ThingManager.updateThing() is going to take care of that
thingRegistry.update(thing);
ThingHandler handler = thing.getHandler();
String handlerString = "NO HANDLER";
if (handler != null) {
handlerString = handler.toString();
}
logger.debug("Changed ThingType of Thing {} to {}. New ThingHandler is {}.",
thing.getUID().toString(), thing.getThingTypeUID(), handlerString);
} finally {
lock.unlock();
}
}
private void waitUntilHandlerUnregistered(final Thing thing, int timeout) {
for (int i = 0; i < timeout / 100; i++) {
if (thing.getHandler() == null && thingHandlers.get(thing.getUID()) == null) {
return;
}
try {
Thread.sleep(100);
logger.debug("Waiting for handler deregistration to complete for thing {}. Took already {}ms.",
thing.getUID().getAsString(), (i + 1) * 100);
} catch (InterruptedException e) {
return;
}
}
String message = MessageFormat.format(
"Thing type migration failed for {0}. The handler deregistration did not complete within {1}ms.",
thing.getUID().getAsString(), timeout);
logger.error(message);
throw new IllegalStateException(message);
}
private void waitForRunningHandlerRegistrations(ThingUID thingUID) {
for (int i = 0; i < 10 * 10; i++) {
if (!registerHandlerLock.contains(thingUID)) {
return;
}
try {
// Wait a little to give running handler registrations a chance to complete...
Thread.sleep(100);
} catch (InterruptedException e) {
return;
}
}
String message = MessageFormat.format(
"Thing type migration failed for {0}. Could not obtain lock for hander registration.",
thingUID.getAsString());
logger.error(message);
throw new IllegalStateException(message);
}
}, 0, TimeUnit.MILLISECONDS);
}
@Override
public void thingAdded(Thing thing, ThingTrackerEvent thingTrackerEvent) {
this.things.add(thing);
logger.debug("Thing '{}' is tracked by ThingManager.", thing.getUID());
if (!isHandlerRegistered(thing)) {
registerAndInitializeHandler(thing, getThingHandlerFactory(thing));
} else {
logger.debug("Handler of tracked thing '{}' already registered.", thing.getUID());
}
}
@Override
public void thingRemoving(Thing thing, ThingTrackerEvent thingTrackerEvent) {
setThingStatus(thing, ThingStatusInfoBuilder.create(ThingStatus.REMOVING).build());
notifyThingHandlerAboutRemoval(thing);
}
@Override
public void thingRemoved(final Thing thing, ThingTrackerEvent thingTrackerEvent) {
logger.debug("Thing '{}' is no longer tracked by ThingManager.", thing.getUID());
ThingHandler thingHandler = thingHandlers.get(thing.getUID());
if (thingHandler != null) {
final ThingHandlerFactory thingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
if (thingHandlerFactory != null) {
unregisterAndDisposeHandler(thingHandlerFactory, thing, thingHandler);
if (thingTrackerEvent == ThingTrackerEvent.THING_REMOVED) {
safeCaller.create(thingHandlerFactory, ThingHandlerFactory.class).build()
.removeThing(thing.getUID());
}
} else {
logger.warn("Cannot unregister handler. No handler factory for thing '{}' found.", thing.getUID());
}
}
storage.remove(thing.getUID().getAsString());
this.things.remove(thing);
}
@Override
public void thingUpdated(final Thing thing, ThingTrackerEvent thingTrackerEvent) {
ThingUID thingUID = thing.getUID();
if (thingUpdatedLock.contains(thingUID)) {
// called from the thing handler itself, therefore
// it exists, is initializing/initialized and
// must not be informed (in order to prevent infinite loops)
replaceThing(getThing(thingUID), thing);
} else {
Lock lock1 = getLockForThing(thing.getUID());
try {
lock1.lock();
Thing oldThing = getThing(thingUID);
ThingHandler thingHandler = replaceThing(oldThing, thing);
if (thingHandler != null) {
if (ThingHandlerHelper.isHandlerInitialized(thing) || isInitializing(thing)) {
if (oldThing != null) {
oldThing.setHandler(null);
}
thing.setHandler(thingHandler);
safeCaller.create(thingHandler, ThingHandler.class).build().thingUpdated(thing);
} else {
logger.debug(
"Cannot notify handler about updated thing '{}', because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE).",
thing.getThingTypeUID());
if (thingHandler.getThing() == thing) {
logger.debug("Initializing handler of thing '{}'", thing.getThingTypeUID());
if (oldThing != null) {
oldThing.setHandler(null);
}
thing.setHandler(thingHandler);
initializeHandler(thing);
} else {
logger.debug("Replacing uninitialized handler for updated thing '{}'",
thing.getThingTypeUID());
ThingHandlerFactory thingHandlerFactory = getThingHandlerFactory(thing);
unregisterHandler(thingHandler.getThing(), thingHandlerFactory);
registerAndInitializeHandler(thing, thingHandlerFactory);
}
}
} else {
registerAndInitializeHandler(thing, getThingHandlerFactory(thing));
}
} finally {
lock1.unlock();
}
}
}
private ThingHandler replaceThing(Thing oldThing, Thing newThing) {
final ThingHandler thingHandler = thingHandlers.get(newThing.getUID());
if (oldThing != newThing) {
this.things.remove(oldThing);
this.things.add(newThing);
}
return thingHandler;
}
private Thing getThing(ThingUID id) {
for (Thing thing : this.things) {
if (thing.getUID().equals(id)) {
return thing;
}
}
return null;
}
private ThingType getThingType(Thing thing) {
return thingTypeRegistry.getThingType(thing.getThingTypeUID());
}
private ThingHandlerFactory findThingHandlerFactory(ThingTypeUID thingTypeUID) {
for (ThingHandlerFactory factory : thingHandlerFactories) {
if (factory.supportsThingType(thingTypeUID)) {
return factory;
}
}
return null;
}
private void registerHandler(Thing thing, ThingHandlerFactory thingHandlerFactory) {
Lock lock = getLockForThing(thing.getUID());
try {
lock.lock();
if (!isHandlerRegistered(thing)) {
if (!hasBridge(thing)) {
doRegisterHandler(thing, thingHandlerFactory);
} else {
Bridge bridge = getBridge(thing.getBridgeUID());
if (bridge != null && ThingHandlerHelper.isHandlerInitialized(bridge)) {
doRegisterHandler(thing, thingHandlerFactory);
} else {
setThingStatus(thing,
buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED));
}
}
} else {
logger.debug("Attempt to register a handler twice for thing {} at the same time will be ignored.",
thing.getUID());
}
} finally {
lock.unlock();
}
}
private void doRegisterHandler(final Thing thing, final ThingHandlerFactory thingHandlerFactory) {
logger.debug("Calling '{}.registerHandler()' for thing '{}'.", thingHandlerFactory.getClass().getSimpleName(),
thing.getUID());
try {
ThingHandler thingHandler = thingHandlerFactory.registerHandler(thing);
thingHandler.setCallback(ThingManagerImpl.this.thingHandlerCallback);
thing.setHandler(thingHandler);
thingHandlers.put(thing.getUID(), thingHandler);
synchronized (thingHandlersByFactory) {
thingHandlersByFactory.computeIfAbsent(thingHandlerFactory, unused -> new HashSet<>())
.add(thingHandler);
}
} catch (Exception ex) {
ThingStatusInfo statusInfo = buildStatusInfo(ThingStatus.UNINITIALIZED,
ThingStatusDetail.HANDLER_REGISTERING_ERROR,
ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());
setThingStatus(thing, statusInfo);
logger.error("Exception occurred while calling thing handler factory '{}': {}", thingHandlerFactory,
ex.getMessage(), ex);
}
}
private void registerChildHandlers(final Bridge bridge) {
for (final Thing child : bridge.getThings()) {
logger.debug("Register and initialize child '{}' of bridge '{}'.", child.getUID(), bridge.getUID());
ThreadPoolManager.getPool(THING_MANAGER_THREADPOOL_NAME).execute(new Runnable() {
@Override
public void run() {
try {
registerAndInitializeHandler(child, getThingHandlerFactory(child));
} catch (Exception ex) {
logger.error(
"Registration resp. initialization of child '{}' of bridge '{}' has been failed: {}",
child.getUID(), bridge.getUID(), ex.getMessage(), ex);
}
}
});
}
}
private void initializeHandler(Thing thing) {
if (isDisabledByStorage(thing.getUID())) {
setThingStatus(thing, buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.DISABLED));
logger.debug("Thing '{}' will not be initialized. It is marked as disabled.", thing.getUID());
return;
}
if (!isHandlerRegistered(thing)) {
return;
}
Lock lock = getLockForThing(thing.getUID());
try {
lock.lock();
if (ThingHandlerHelper.isHandlerInitialized(thing)) {
logger.debug("Attempt to initialize the already initialized thing '{}' will be ignored.",
thing.getUID());
return;
}
if (isInitializing(thing)) {
logger.debug("Attempt to initialize a handler twice for thing '{}' at the same time will be ignored.",
thing.getUID());
return;
}
ThingHandler handler = thing.getHandler();
if (handler == null) {
throw new IllegalStateException("Handler should not be null here");
} else {
if (handler.getThing() != thing) {
logger.warn("The model of {} is inconsistent [thing.getHandler().getThing() != thing]",
thing.getUID());
}
}
ThingType thingType = getThingType(thing);
applyDefaultConfiguration(thing, thingType);
if (isInitializable(thing, thingType)) {
setThingStatus(thing, buildStatusInfo(ThingStatus.INITIALIZING, ThingStatusDetail.NONE));
doInitializeHandler(thing.getHandler());
} else {
logger.debug("Thing '{}' not initializable, check required configuration parameters.", thing.getUID());
setThingStatus(thing,
buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING));
}
} finally {
lock.unlock();
}
}
private void applyDefaultConfiguration(Thing thing, ThingType thingType) {
if (thingType != null) {
ThingFactoryHelper.applyDefaultConfiguration(thing.getConfiguration(), thingType,
configDescriptionRegistry);
}
}
private boolean isInitializable(Thing thing, ThingType thingType) {
if (!isComplete(thingType, thing.getUID(), tt -> tt.getConfigDescriptionURI(), thing.getConfiguration())) {
return false;
}
for (Channel channel : thing.getChannels()) {
ChannelType channelType = channelTypeRegistry.getChannelType(channel.getChannelTypeUID());
if (!isComplete(channelType, channel.getUID(), ct -> ct.getConfigDescriptionURI(),
channel.getConfiguration())) {
return false;
}
}
return true;
}
/**
* Determines if all 'required' configuration parameters are available in the configuration
*
* @param prototype the "prototype", i.e. thing type or channel type
* @param targetUID the UID of the thing or channel entity
* @param configDescriptionURIFunction a function to determine the the config description UID for the given
* prototype
* @param configuration the current configuration
* @return true if all required configuration parameters are given, false otherwise
*/
private <T extends Identifiable<?>> boolean isComplete(T prototype, UID targetUID,
Function<T, URI> configDescriptionURIFunction, Configuration configuration) {
if (prototype == null) {
logger.debug("Prototype for '{}' is not known, assuming it is initializable", targetUID);
return true;
}
ConfigDescription description = resolve(configDescriptionURIFunction.apply(prototype), null);
if (description == null) {
logger.debug("Config description for '{}' is not resolvable, assuming '{}' is initializable",
prototype.getUID(), targetUID);
return true;
}
List<String> requiredParameters = getRequiredParameters(description);
Set<String> propertyKeys = configuration.getProperties().keySet();
if (logger.isDebugEnabled()) {
logger.debug("Configuration of '{}' needs {}, has {}.", targetUID, requiredParameters, propertyKeys);
}
return propertyKeys.containsAll(requiredParameters);
}
private ConfigDescription resolve(URI configDescriptionURI, Locale locale) {
if (configDescriptionURI == null) {
return null;
}
return configDescriptionRegistry != null
? configDescriptionRegistry.getConfigDescription(configDescriptionURI, locale)
: null;
}
private List<String> getRequiredParameters(ConfigDescription description) {
List<String> requiredParameters = new ArrayList<>();
for (ConfigDescriptionParameter param : description.getParameters()) {
if (param.isRequired()) {
requiredParameters.add(param.getName());
}
}
return requiredParameters;
}
private void doInitializeHandler(final ThingHandler thingHandler) {
logger.debug("Calling initialize handler for thing '{}' at '{}'.", thingHandler.getThing().getUID(),
thingHandler);
safeCaller.create(thingHandler, ThingHandler.class).onTimeout(() -> {
logger.warn("Initializing handler for thing '{}' takes more than {}ms.", thingHandler.getThing().getUID(),
SafeCaller.DEFAULT_TIMEOUT);
}).onException(e -> {
ThingStatusInfo statusInfo = buildStatusInfo(ThingStatus.UNINITIALIZED,
ThingStatusDetail.HANDLER_INITIALIZING_ERROR, e.getMessage());
setThingStatus(thingHandler.getThing(), statusInfo);
logger.error("Exception occurred while initializing handler of thing '{}': {}",
thingHandler.getThing().getUID(), e.getMessage(), e);
}).build().initialize();
}
private boolean isInitializing(Thing thing) {
return thing.getStatus() == ThingStatus.INITIALIZING;
}
private boolean isHandlerRegistered(Thing thing) {
ThingHandler handler = thingHandlers.get(thing.getUID());
return handler != null && handler == thing.getHandler();
}
private boolean isBridge(Thing thing) {
return thing instanceof Bridge;
}
private boolean hasBridge(final Thing thing) {
return thing.getBridgeUID() != null;
}
private Bridge getBridge(ThingUID bridgeUID) {
Thing bridge = thingRegistry.get(bridgeUID);
return isBridge(bridge) ? (Bridge) bridge : null;
}
private void unregisterHandler(Thing thing, ThingHandlerFactory thingHandlerFactory) {
Lock lock = getLockForThing(thing.getUID());
try {
lock.lock();
if (isHandlerRegistered(thing)) {
doUnregisterHandler(thing, thingHandlerFactory);
}
} finally {
lock.unlock();
}
}
private void doUnregisterHandler(final Thing thing, final ThingHandlerFactory thingHandlerFactory) {
logger.debug("Calling unregisterHandler handler for thing '{}' at '{}'.", thing.getUID(), thingHandlerFactory);
safeCaller.create(() -> {
ThingHandler thingHandler = thing.getHandler();
thingHandlerFactory.unregisterHandler(thing);
if (thingHandler != null) {
thingHandler.setCallback(null);
}
thing.setHandler(null);
boolean enabled = !isDisabledByStorage(thing.getUID());
ThingStatusDetail detail = enabled ? ThingStatusDetail.HANDLER_MISSING_ERROR : ThingStatusDetail.DISABLED;
setThingStatus(thing, buildStatusInfo(ThingStatus.UNINITIALIZED, detail));
thingHandlers.remove(thing.getUID());
synchronized (thingHandlersByFactory) {
final Set<ThingHandler> thingHandlers = thingHandlersByFactory.get(thingHandlerFactory);
if (thingHandlers != null) {
thingHandlers.remove(thingHandler);
if (thingHandlers.isEmpty()) {
thingHandlersByFactory.remove(thingHandlerFactory);
}
}
}
}, Runnable.class).build().run();
}
private void disposeHandler(Thing thing, ThingHandler thingHandler) {
Lock lock = getLockForThing(thing.getUID());
try {
lock.lock();
doDisposeHandler(thingHandler);
if (hasBridge(thing)) {
notifyBridgeAboutChildHandlerDisposal(thing, thingHandler);
}
} finally {
lock.unlock();
}
}
private void doDisposeHandler(final ThingHandler thingHandler) {
logger.debug("Calling dispose handler for thing '{}' at '{}'.", thingHandler.getThing().getUID(), thingHandler);
setThingStatus(thingHandler.getThing(), buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE));
safeCaller.create(thingHandler, ThingHandler.class).onTimeout(() -> {
logger.warn("Disposing handler for thing '{}' takes more than {}ms.", thingHandler.getThing().getUID(),
SafeCaller.DEFAULT_TIMEOUT);
}).onException(e -> {
logger.error("Exception occurred while disposing handler of thing '{}': {}",
thingHandler.getThing().getUID(), e.getMessage(), e);
}).build().dispose();
}
private void unregisterAndDisposeChildHandlers(Bridge bridge, ThingHandlerFactory thingHandlerFactory) {
addThingsToBridge(bridge);
for (Thing child : bridge.getThings()) {
ThingHandler handler = child.getHandler();
if (handler != null) {
logger.debug("Unregister and dispose child '{}' of bridge '{}'.", child.getUID(), bridge.getUID());
unregisterAndDisposeHandler(thingHandlerFactory, child, handler);
}
}
}
private void unregisterAndDisposeHandler(ThingHandlerFactory thingHandlerFactory, Thing thing,
ThingHandler handler) {
if (isBridge(thing)) {
unregisterAndDisposeChildHandlers((Bridge) thing, thingHandlerFactory);
}
disposeHandler(thing, handler);
unregisterHandler(thing, thingHandlerFactory);
}
private void addThingsToBridge(Bridge bridge) {
Collection<Thing> things = thingRegistry.getAll();
for (Thing thing : things) {
ThingUID bridgeUID = thing.getBridgeUID();
if (bridgeUID != null && bridgeUID.equals(bridge.getUID())) {
if (bridge instanceof BridgeImpl && !bridge.getThings().contains(thing)) {
((BridgeImpl) bridge).addThing(thing);
}
}
}
}
private void notifyThingsAboutBridgeStatusChange(final Bridge bridge, final ThingStatusInfo bridgeStatus) {
if (ThingHandlerHelper.isHandlerInitialized(bridge)) {
for (final Thing child : bridge.getThings()) {
ThreadPoolManager.getPool(THING_MANAGER_THREADPOOL_NAME).execute(new Runnable() {
@Override
public void run() {
try {
ThingHandler handler = child.getHandler();
if (handler != null && ThingHandlerHelper.isHandlerInitialized(child)) {
handler.bridgeStatusChanged(bridgeStatus);
}
} catch (Exception e) {
logger.error(
"Exception occurred during notification about bridge status change on thing '{}': {}",
child.getUID(), e.getMessage(), e);
}
}
});
}
}
}
private void notifyBridgeAboutChildHandlerInitialization(final Thing thing) {
final Bridge bridge = getBridge(thing.getBridgeUID());
if (bridge != null) {
ThreadPoolManager.getPool(THING_MANAGER_THREADPOOL_NAME).execute(new Runnable() {
@Override
public void run() {
try {
BridgeHandler bridgeHandler = bridge.getHandler();
if (bridgeHandler != null) {
ThingHandler thingHandler = thing.getHandler();
if (thingHandler != null) {
bridgeHandler.childHandlerInitialized(thingHandler, thing);
}
}
} catch (Exception e) {
logger.error(
"Exception occurred during bridge handler ('{}') notification about handler initialization of child '{}': {}",
bridge.getUID(), thing.getUID(), e.getMessage(), e);
}
}
});
}
}
private void notifyBridgeAboutChildHandlerDisposal(final Thing thing, final ThingHandler thingHandler) {
final Bridge bridge = getBridge(thing.getBridgeUID());
if (bridge != null) {
ThreadPoolManager.getPool(THING_MANAGER_THREADPOOL_NAME).execute(new Runnable() {
@Override
public void run() {
try {
BridgeHandler bridgeHandler = bridge.getHandler();
if (bridgeHandler != null) {
bridgeHandler.childHandlerDisposed(thingHandler, thing);
}
} catch (Exception ex) {
logger.error(
"Exception occurred during bridge handler ('{}') notification about handler disposal of child '{}': {}",
bridge.getUID(), thing.getUID(), ex.getMessage(), ex);
}
}
});
}
}
private void notifyThingHandlerAboutRemoval(final Thing thing) {
logger.trace("Asking handler of thing '{}' to handle its removal.", thing.getUID());
ThreadPoolManager.getPool(THING_MANAGER_THREADPOOL_NAME).execute(new Runnable() {
@Override
public void run() {
try {
ThingHandler handler = thing.getHandler();
if (handler != null) {
handler.handleRemoval();
logger.trace("Handler of thing '{}' returned from handling its removal.", thing.getUID());
} else {
logger.trace("No handler of thing '{}' available, so deferring the removal call.",
thing.getUID());
}
} catch (Exception ex) {
logger.error("The ThingHandler caused an exception while handling the removal of its thing", ex);
}
}
});
}
private void notifyRegistryAboutForceRemove(final Thing thing) {
logger.debug("Removal handling of thing '{}' completed. Going to remove it now.", thing.getUID());
// call asynchronous to avoid deadlocks in thing handler
ThreadPoolManager.getPool(FORCEREMOVE_THREADPOOL_NAME).execute(new Runnable() {
@Override
public void run() {
try {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
thingRegistry.forceRemove(thing.getUID());
return null;
}
});
} catch (IllegalStateException ex) {
logger.debug("Could not remove thing {}. Most likely because it is not managed.", thing.getUID(),
ex);
} catch (Exception ex) {
logger.error(