Class SubscriptionRequest
Which traits to watch, and how often you are willing to hear about them.
SubscriptionRequest req = new SubscriptionRequest()
.add(lamp, lamp.getPrimaryService(), Trait.ON_OFF)
.add(lamp, lamp.getPrimaryService(), Trait.BRIGHTNESS)
.setDeliverInitialValues(true);
TraitSubscription sub = SmartHome.getInstance().subscribe(req, listener);
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default coalescing window, in milliseconds. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(Accessory accessory, AccessoryService service, Trait trait) Watches one trait on one service.Watches one trait on one service, by identifier.The accessory ids, positionally aligned withgetServiceIds()andgetTraits().intThe coalescing window in milliseconds.The service ids, positionally aligned withgetAccessoryIds()andgetTraits().The traits, positionally aligned withgetAccessoryIds()andgetServiceIds().booleanWhether current values are delivered up front.booleanisEmpty()Whether this request watches nothing.setDeliverInitialValues(boolean deliverInitialValues) Whether to deliver each watched trait's current value once, up front, as though it had just changed.setMinIntervalMillis(int minIntervalMillis) How long to gather changes before delivering them, in milliseconds.intsize()How many traits this subscription watches.
-
Field Details
-
DEFAULT_MIN_INTERVAL_MILLIS
public static final int DEFAULT_MIN_INTERVAL_MILLISThe default coalescing window, in milliseconds.
A fifth of a second: fast enough that a light responding to a switch looks instant, slow enough that dragging a dimmer does not put a hundred deliveries through the EDT.
- See Also:
-
-
Constructor Details
-
SubscriptionRequest
public SubscriptionRequest()
-
-
Method Details
-
add
Watches one trait on one service.
Parameters
-
accessory: the accessory to watch -
service: the service on it -
trait: the trait to watch
Returns
this request, for chaining
Throws
IllegalArgumentException: when any argument isnull
-
-
add
Watches one trait on one service, by identifier.
Parameters
-
accessoryId: the accessory to watch -
serviceId: the service on it -
trait: the trait to watch
Returns
this request, for chaining
Throws
IllegalArgumentException: when any argument isnullor empty
-
-
setMinIntervalMillis
How long to gather changes before delivering them, in milliseconds.
Within the window, changes are coalesced per accessory, service and trait, keeping only the newest value; the batch then crosses to the EDT once. Dragging a dimmer emits a notification per step, and a home with a hundred watched accessories can produce a steady stream, so the window is what keeps the event loop usable.
Zero delivers every change as it arrives. That is occasionally right -- a diagnostic view, a trace -- and is a decision to make deliberately rather than a default to inherit.
Parameters
minIntervalMillis: the window; zero to deliver everything
Returns
this request, for chaining
Throws
IllegalArgumentException: when the value is negative
-
getMinIntervalMillis
public int getMinIntervalMillis() -
setDeliverInitialValues
Whether to deliver each watched trait's current value once, up front, as though it had just changed.
falseby default. Turn it on when the subscription is what populates a screen, so the same listener that keeps the screen live also fills it, and there is no separate read whose result can race the first change. The batch is marked withTraitChangeBatch.isInitialDelivery().Parameters
deliverInitialValues: whether to send current values first
Returns
this request, for chaining
-
isDeliverInitialValues
public boolean isDeliverInitialValues()Whether current values are delivered up front.
Returns
truewhen they are -
size
public int size()How many traits this subscription watches.
Returns
the count, zero for an empty request
-
isEmpty
public boolean isEmpty()Whether this request watches nothing.
Returns
truewhen nothing has been added -
getAccessoryIds
The accessory ids, positionally aligned with
getServiceIds()andgetTraits().Returns
an immutable list
-
getServiceIds
The service ids, positionally aligned with
getAccessoryIds()andgetTraits().Returns
an immutable list
-
getTraits
The traits, positionally aligned with
getAccessoryIds()andgetServiceIds().Returns
an immutable list
-