This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
CommunicationManager.java
326 lines (285 loc) · 12.7 KB
/
CommunicationManager.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
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.core.thing.internal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.NotImplementedException;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.eclipse.smarthome.core.events.Event;
import org.eclipse.smarthome.core.events.EventFilter;
import org.eclipse.smarthome.core.events.EventPublisher;
import org.eclipse.smarthome.core.events.EventSubscriber;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.items.events.ItemCommandEvent;
import org.eclipse.smarthome.core.items.events.ItemStateEvent;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.DefaultSystemChannelTypeProvider;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingRegistry;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.events.ChannelTriggeredEvent;
import org.eclipse.smarthome.core.thing.events.ThingEventFactory;
import org.eclipse.smarthome.core.thing.internal.profiles.DefaultMasterProfile;
import org.eclipse.smarthome.core.thing.internal.profiles.RawButtonTriggerProfile;
import org.eclipse.smarthome.core.thing.link.ItemChannelLink;
import org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry;
import org.eclipse.smarthome.core.thing.profiles.Profile;
import org.eclipse.smarthome.core.thing.profiles.ProfileFactory;
import org.eclipse.smarthome.core.thing.profiles.StateProfile;
import org.eclipse.smarthome.core.thing.profiles.TriggerProfile;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.State;
import org.osgi.service.component.annotations.Component;
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;
/**
* This class manages the state related communication between bindings and the framework.
*
* It mainly mediates commands, state updates and triggers from ThingHandlers to the framework and vice versa.
*
* @author Simon Kaufmann - initial contribution and API, factored out of ThingManger
*
*/
@Component(service = { EventSubscriber.class, CommunicationManager.class }, immediate = true)
public class CommunicationManager implements EventSubscriber, RegistryChangeListener<ItemChannelLink> {
private final Logger logger = LoggerFactory.getLogger(CommunicationManager.class);
private final Set<String> subscribedEventTypes = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList(ItemStateEvent.TYPE, ItemCommandEvent.TYPE, ChannelTriggeredEvent.TYPE)));
private ItemChannelLinkRegistry itemChannelLinkRegistry;
private ThingRegistry thingRegistry;
private ItemRegistry itemRegistry;
private EventPublisher eventPublisher;
private final Map<ChannelUID, Profile> profiles = new ConcurrentHashMap<>();
private final Map<ProfileFactory, Set<ChannelUID>> profileFactories = new ConcurrentHashMap<>();
@Override
public Set<String> getSubscribedEventTypes() {
return subscribedEventTypes;
}
@Override
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event event) {
if (event instanceof ItemStateEvent) {
receiveUpdate((ItemStateEvent) event);
} else if (event instanceof ItemCommandEvent) {
receiveCommand((ItemCommandEvent) event);
} else if (event instanceof ChannelTriggeredEvent) {
receiveTrigger((ChannelTriggeredEvent) event);
}
}
private Thing getThing(ThingUID thingUID) {
return thingRegistry.get(thingUID);
}
private Profile getProfile(ItemChannelLink link, Item item, Thing thing) {
if (thing == null) {
return new NoOpProfile();
}
Channel channel = thing.getChannel(link.getLinkedUID().getId());
if (channel == null) {
return new NoOpProfile();
}
Profile profile = null;
synchronized (profiles) {
profile = profiles.get(link.getLinkedUID());
if (profile == null) {
profile = getProfileFromFactories(link, item, channel);
if (profile == null) {
logger.trace("No profile factory found for link {}, falling back to the defaults", link);
profile = createDefaultProfile(channel);
}
if (profile != null) {
profiles.put(link.getLinkedUID(), profile);
}
}
}
return profile != null ? profile : new NoOpProfile();
}
private Profile getProfileFromFactories(ItemChannelLink link, Item item, Channel channel) {
for (ProfileFactory profileFactory : profileFactories.keySet()) {
Profile profile = profileFactory.createProfile(link, item, channel);
if (profile != null) {
profileFactories.get(profileFactory).add(link.getLinkedUID());
logger.trace("Going to use profile {} for link {}", profile, link);
return profile;
}
}
return null;
}
private Profile createDefaultProfile(Channel channel) {
switch (channel.getKind()) {
case STATE:
return new DefaultMasterProfile();
case TRIGGER:
if (DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON.getUID().equals(channel.getChannelTypeUID())) {
return new RawButtonTriggerProfile();
}
break;
default:
throw new NotImplementedException();
}
return null;
}
private void receiveCommand(ItemCommandEvent commandEvent) {
final String itemName = commandEvent.getItemName();
final Command command = commandEvent.getItemCommand();
final Item item = itemRegistry.get(itemName);
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the item
return link.getItemName().equals(itemName);
}).filter(link -> {
// make sure a command event is not sent back to its source
return !link.getLinkedUID().toString().equals(commandEvent.getSource());
}).forEach(link -> {
ChannelUID channelUID = link.getLinkedUID();
Thing thing = getThing(channelUID.getThingUID());
Profile profile = getProfile(link, item, thing);
if (profile instanceof StateProfile) {
((StateProfile) profile).onCommand(link, thing, command);
}
});
}
private void receiveUpdate(ItemStateEvent updateEvent) {
final String itemName = updateEvent.getItemName();
final State newState = updateEvent.getItemState();
final Item item = itemRegistry.get(itemName);
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the item
return link.getItemName().equals(itemName);
}).filter(link -> {
// make sure a command event is not sent back to its source
return !link.getLinkedUID().toString().equals(updateEvent.getSource());
}).forEach(link -> {
ChannelUID channelUID = link.getLinkedUID();
Thing thing = getThing(channelUID.getThingUID());
Profile profile = getProfile(link, item, thing);
if (profile instanceof StateProfile) {
((StateProfile) profile).onUpdate(link, thing, newState);
}
});
}
private void receiveTrigger(ChannelTriggeredEvent channelTriggeredEvent) {
final ChannelUID channelUID = channelTriggeredEvent.getChannel();
final String event = channelTriggeredEvent.getEvent();
final Thing thing = getThing(channelUID.getThingUID());
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the channel
return link.getLinkedUID().equals(channelUID);
}).forEach(link -> {
Item item = itemRegistry.get(link.getItemName());
if (item != null) {
Profile profile = getProfile(link, item, thing);
if (profile instanceof TriggerProfile) {
((TriggerProfile) profile).onTrigger(eventPublisher, link, event, item);
}
}
});
}
public void stateUpdated(ChannelUID channelUID, State state) {
final Thing thing = getThing(channelUID.getThingUID());
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the channel
return link.getLinkedUID().equals(channelUID);
}).forEach(link -> {
Item item = itemRegistry.get(link.getItemName());
if (item != null) {
Profile profile = getProfile(link, item, thing);
if (profile instanceof StateProfile) {
((StateProfile) profile).stateUpdated(eventPublisher, link, state, item);
}
}
});
}
public void postCommand(ChannelUID channelUID, Command command) {
final Thing thing = getThing(channelUID.getThingUID());
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the channel
return link.getLinkedUID().equals(channelUID);
}).forEach(link -> {
Item item = itemRegistry.get(link.getItemName());
if (item != null) {
Profile profile = getProfile(link, item, thing);
if (profile instanceof StateProfile) {
((StateProfile) profile).postCommand(eventPublisher, link, command, item);
}
}
});
}
public void channelTriggered(Thing thing, ChannelUID channelUID, String event) {
eventPublisher.post(ThingEventFactory.createTriggerEvent(event, channelUID));
}
private void cleanup(ChannelUID channelUID) {
synchronized (profiles) {
profiles.remove(channelUID);
}
profileFactories.values().forEach(list -> list.remove(channelUID));
}
@Override
public void added(ItemChannelLink element) {
// nothing to do
}
@Override
public void removed(ItemChannelLink element) {
cleanup(element.getLinkedUID());
}
@Override
public void updated(ItemChannelLink oldElement, ItemChannelLink element) {
cleanup(oldElement.getLinkedUID());
}
@Reference
protected void setItemChannelLinkRegistry(ItemChannelLinkRegistry itemChannelLinkRegistry) {
this.itemChannelLinkRegistry = itemChannelLinkRegistry;
itemChannelLinkRegistry.addRegistryChangeListener(this);
}
protected void unsetItemChannelLinkRegistry(ItemChannelLinkRegistry itemChannelLinkRegistry) {
this.itemChannelLinkRegistry = null;
}
@Reference
protected void setThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = thingRegistry;
}
protected void unsetThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = null;
}
@Reference
protected void setEventPublisher(EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
protected void unsetEventPublisher(EventPublisher eventPublisher) {
this.eventPublisher = null;
}
@Reference
protected void setItemRegistry(ItemRegistry itemRegistry) {
this.itemRegistry = itemRegistry;
}
protected void unsetItemRegistry(ItemRegistry itemRegistry) {
this.itemRegistry = null;
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addProfileFactory(ProfileFactory profileFactory) {
this.profileFactories.put(profileFactory, ConcurrentHashMap.newKeySet());
}
protected void removeProfileFactory(ProfileFactory profileFactory) {
Set<ChannelUID> channelUIDs = this.profileFactories.remove(profileFactory);
synchronized (profiles) {
channelUIDs.forEach(channelUID -> profiles.remove(channelUID));
}
}
private static class NoOpProfile implements Profile {
}
}