/
GenericItem.java
522 lines (449 loc) · 16.7 KB
/
GenericItem.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
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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.openhab.core.items;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.events.ItemEventFactory;
import org.openhab.core.service.CommandDescriptionService;
import org.openhab.core.service.StateDescriptionService;
import org.openhab.core.types.Command;
import org.openhab.core.types.CommandDescription;
import org.openhab.core.types.CommandDescriptionBuilder;
import org.openhab.core.types.CommandOption;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.TimeSeries;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The abstract base class for all items. It provides all relevant logic
* for the infrastructure, such as publishing updates to the event bus
* or notifying listeners.
*
* @author Kai Kreuzer - Initial contribution
* @author Andre Fuechsel - Added tags
* @author Stefan Bußweiler - Migration to new ESH event concept
* @author Jan N. Klug - Added time series support
*/
@NonNullByDefault
public abstract class GenericItem implements ActiveItem {
private final Logger logger = LoggerFactory.getLogger(GenericItem.class);
private static final String ITEM_THREADPOOLNAME = "items";
protected @Nullable EventPublisher eventPublisher;
protected Set<StateChangeListener> listeners = new CopyOnWriteArraySet<>(
Collections.newSetFromMap(new WeakHashMap<>()));
protected Set<TimeSeriesListener> timeSeriesListeners = new CopyOnWriteArraySet<>(
Collections.newSetFromMap(new WeakHashMap<>()));
protected List<String> groupNames = new ArrayList<>();
protected Set<String> tags = new HashSet<>();
protected final String name;
protected final String type;
protected State state = UnDefType.NULL;
protected @Nullable String label;
protected @Nullable String category;
private @Nullable StateDescriptionService stateDescriptionService;
private @Nullable CommandDescriptionService commandDescriptionService;
protected @Nullable ItemStateConverter itemStateConverter;
public GenericItem(String type, String name) {
this.name = name;
this.type = type;
}
@Override
public State getState() {
return state;
}
@Override
public <T extends State> @Nullable T getStateAs(Class<T> typeClass) {
return state.as(typeClass);
}
@Override
public String getUID() {
return getName();
}
@Override
public String getName() {
return name;
}
@Override
public String getType() {
return type;
}
@Override
public List<String> getGroupNames() {
return List.copyOf(groupNames);
}
/**
* Adds a group name to the {@link GenericItem}.
*
* @param groupItemName group item name to add
* @throws IllegalArgumentException if groupItemName is {@code null}
*/
@Override
public void addGroupName(String groupItemName) {
if (!groupNames.contains(groupItemName)) {
groupNames.add(groupItemName);
}
}
@Override
public void addGroupNames(String... groupItemNames) {
for (String groupItemName : groupItemNames) {
addGroupName(groupItemName);
}
}
@Override
public void addGroupNames(List<String> groupItemNames) {
for (String groupItemName : groupItemNames) {
addGroupName(groupItemName);
}
}
/**
* Removes a group item name from the {@link GenericItem}.
*
* @param groupItemName group item name to remove
* @throws IllegalArgumentException if groupItemName is {@code null}
*/
@Override
public void removeGroupName(String groupItemName) {
groupNames.remove(groupItemName);
}
/**
* Disposes this item. Clears all injected services and unregisters all change listeners.
* This does not remove this item from its groups. Removing from groups should be done externally to retain the
* member order in case this item is exchanged in a group.
*/
public void dispose() {
this.listeners.clear();
this.eventPublisher = null;
this.stateDescriptionService = null;
this.commandDescriptionService = null;
this.itemStateConverter = null;
}
public void setEventPublisher(@Nullable EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
public void setStateDescriptionService(@Nullable StateDescriptionService stateDescriptionService) {
this.stateDescriptionService = stateDescriptionService;
}
public void setCommandDescriptionService(@Nullable CommandDescriptionService commandDescriptionService) {
this.commandDescriptionService = commandDescriptionService;
}
public void setItemStateConverter(@Nullable ItemStateConverter itemStateConverter) {
this.itemStateConverter = itemStateConverter;
}
protected void internalSend(Command command) {
// try to send the command to the bus
if (eventPublisher != null) {
eventPublisher.post(ItemEventFactory.createCommandEvent(this.getName(), command));
}
}
/**
* Set a new state.
*
* Subclasses may override this method in order to do necessary conversions upfront. Afterwards,
* {@link #applyState(State)} should be called by classes overriding this method.
*
* @param state new state of this item
*/
public void setState(State state) {
applyState(state);
}
/**
* Sets new state, notifies listeners and sends events.
*
* Classes overriding the {@link #setState(State)} method should call this method in order to actually set the
* state, inform listeners and send the event.
*
* @param state new state of this item
*/
protected final void applyState(State state) {
State oldState = this.state;
this.state = state;
notifyListeners(oldState, state);
sendStateUpdatedEvent(state);
if (!oldState.equals(state)) {
sendStateChangedEvent(state, oldState);
}
}
/**
* Set a new time series.
* <p/>
* Subclasses may override this method in order to do necessary conversions upfront. Afterwards,
* {@link #applyTimeSeries(TimeSeries)} should be called by classes overriding this method.
* <p/>
* A time series may only contain events that are compatible with the item's internal state.
*
* @param timeSeries new time series of this item
*/
public void setTimeSeries(TimeSeries timeSeries) {
applyTimeSeries(timeSeries);
}
/**
* Sets new time series, notifies listeners and sends events.
* <p />
* Classes overriding the {@link #setTimeSeries(TimeSeries)} method should call this method in order to actually set
* the time series, inform listeners and send the event.
* <p/>
* A time series may only contain events that are compatible with the item's internal state.
*
* @param timeSeries new time series of this item
*/
protected final void applyTimeSeries(TimeSeries timeSeries) {
// notify listeners
Set<TimeSeriesListener> clonedListeners = new CopyOnWriteArraySet<>(timeSeriesListeners);
ExecutorService pool = ThreadPoolManager.getPool(ITEM_THREADPOOLNAME);
clonedListeners.forEach(listener -> pool.execute(() -> {
try {
listener.timeSeriesUpdated(GenericItem.this, timeSeries);
} catch (Exception e) {
logger.warn("failed notifying listener '{}' about timeseries update of item {}: {}", listener,
GenericItem.this.getName(), e.getMessage(), e);
}
}));
// send event
EventPublisher eventPublisher1 = this.eventPublisher;
if (eventPublisher1 != null) {
eventPublisher1.post(ItemEventFactory.createTimeSeriesUpdatedEvent(this.name, timeSeries, null));
}
}
private void sendStateUpdatedEvent(State newState) {
EventPublisher eventPublisher1 = this.eventPublisher;
if (eventPublisher1 != null) {
eventPublisher1.post(ItemEventFactory.createStateUpdatedEvent(this.name, newState, null));
}
}
private void sendStateChangedEvent(State newState, State oldState) {
EventPublisher eventPublisher1 = this.eventPublisher;
if (eventPublisher1 != null) {
eventPublisher1.post(ItemEventFactory.createStateChangedEvent(this.name, newState, oldState));
}
}
public void send(RefreshType command) {
internalSend(command);
}
protected void notifyListeners(final State oldState, final State newState) {
// if nothing has changed, we send update notifications
Set<StateChangeListener> clonedListeners = new CopyOnWriteArraySet<>(listeners);
ExecutorService pool = ThreadPoolManager.getPool(ITEM_THREADPOOLNAME);
try {
final boolean stateChanged = newState != null && !newState.equals(oldState);
clonedListeners.forEach(listener -> pool.execute(() -> {
try {
listener.stateUpdated(GenericItem.this, newState);
if (stateChanged) {
listener.stateChanged(GenericItem.this, oldState, newState);
}
} catch (Exception e) {
logger.warn("failed notifying listener '{}' about state update of item {}: {}", listener,
GenericItem.this.getName(), e.getMessage(), e);
}
}));
} catch (IllegalArgumentException e) {
logger.warn("failed comparing oldState '{}' to newState '{}' for item {}: {}", oldState, newState,
GenericItem.this.getName(), e.getMessage(), e);
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName());
sb.append(" (");
sb.append("Type=");
sb.append(getClass().getSimpleName());
sb.append(", ");
sb.append("State=");
sb.append(getState());
sb.append(", ");
sb.append("Label=");
sb.append(getLabel());
sb.append(", ");
sb.append("Category=");
sb.append(getCategory());
if (!getTags().isEmpty()) {
sb.append(", ");
sb.append("Tags=[");
sb.append(String.join(", ", getTags()));
sb.append("]");
}
if (!getGroupNames().isEmpty()) {
sb.append(", ");
sb.append("Groups=[");
sb.append(String.join(", ", getGroupNames()));
sb.append("]");
}
sb.append(")");
return sb.toString();
}
public void addStateChangeListener(StateChangeListener listener) {
synchronized (listeners) {
listeners.add(listener);
}
}
public void removeStateChangeListener(StateChangeListener listener) {
synchronized (listeners) {
listeners.remove(listener);
}
}
public void addTimeSeriesListener(TimeSeriesListener listener) {
synchronized (timeSeriesListeners) {
timeSeriesListeners.add(listener);
}
}
public void removeTimeSeriesListener(TimeSeriesListener listener) {
synchronized (timeSeriesListeners) {
timeSeriesListeners.remove(listener);
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
GenericItem other = (GenericItem) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
@Override
public Set<String> getTags() {
return Set.copyOf(tags);
}
@Override
public boolean hasTag(String tag) {
return tags.stream().anyMatch(t -> t.equalsIgnoreCase(tag));
}
@Override
public void addTag(String tag) {
tags.add(tag);
}
@Override
public void addTags(Collection<String> tags) {
this.tags.addAll(tags);
}
@Override
public void addTags(String... tags) {
this.tags.addAll(Arrays.asList(tags));
}
@Override
public void removeTag(String tag) {
tags.remove(tags.stream().filter(t -> t.equalsIgnoreCase(tag)).findFirst().orElse(tag));
}
@Override
public void removeAllTags() {
tags.clear();
}
@Override
public @Nullable String getLabel() {
return this.label;
}
@Override
public void setLabel(@Nullable String label) {
this.label = label;
}
@Override
public @Nullable String getCategory() {
return category;
}
@Override
public void setCategory(@Nullable String category) {
this.category = category;
}
@Override
public @Nullable StateDescription getStateDescription() {
return getStateDescription(null);
}
@Override
public @Nullable StateDescription getStateDescription(@Nullable Locale locale) {
if (stateDescriptionService != null) {
return stateDescriptionService.getStateDescription(this.name, locale);
}
return null;
}
@Override
public @Nullable CommandDescription getCommandDescription(@Nullable Locale locale) {
CommandDescription commandOptions = getCommandOptions(locale);
if (commandOptions != null) {
return commandOptions;
}
StateDescription stateDescription = getStateDescription(locale);
if (stateDescription != null && !stateDescription.getOptions().isEmpty()) {
return stateOptions2CommandOptions(stateDescription);
}
return null;
}
/**
* Tests if state is within acceptedDataTypes list or a subclass of one of them
*
* @param acceptedDataTypes list of datatypes this items accepts as a state
* @param state to be tested
* @return true if state is an acceptedDataType or subclass thereof
*/
public boolean isAcceptedState(List<Class<? extends State>> acceptedDataTypes, State state) {
return acceptedDataTypes.stream().anyMatch(clazz -> clazz.isAssignableFrom(state.getClass()));
}
protected void logSetTypeError(State state) {
logger.error("Tried to set invalid state {} ({}) on item {} of type {}, ignoring it", state,
state.getClass().getSimpleName(), getName(), getClass().getSimpleName());
}
protected void logSetTypeError(TimeSeries timeSeries) {
logger.error("Tried to set invalid state in time series {} on item {} of type {}, ignoring it", timeSeries,
getName(), getClass().getSimpleName());
}
protected @Nullable CommandDescription getCommandOptions(@Nullable Locale locale) {
if (commandDescriptionService != null) {
CommandDescription commandDescription = commandDescriptionService.getCommandDescription(this.name, locale);
if (commandDescription != null) {
return commandDescription;
}
}
return null;
}
private CommandDescription stateOptions2CommandOptions(StateDescription stateDescription) {
CommandDescriptionBuilder builder = CommandDescriptionBuilder.create();
stateDescription.getOptions()
.forEach(so -> builder.withCommandOption(new CommandOption(so.getValue(), so.getLabel())));
return builder.build();
}
}