AutoUpdateManager { public void receiveCommand(ItemCommandEvent commandEvent, Item item) { if (!enabled) { return; } final String itemName = commandEvent.getItemName(); final Command command = commandEvent.getItemCommand(); if (command instanceof State) { final State state = (State) command; Recommendation autoUpdate = shouldAutoUpdate(itemName); MetadataKey key = new MetadataKey(AUTOUPDATE_KEY, itemName); Metadata metadata = metadataRegistry.get(key); if (metadata != null && !metadata.getValue().trim().isEmpty()) { boolean override = Boolean.parseBoolean(metadata.getValue()); if (override) { logger.trace("Auto update strategy {} overriden by item metadata to REQUIRED", autoUpdate); autoUpdate = Recommendation.REQUIRED; } else { logger.trace("Auto update strategy {} overriden by item metadata to DONT", autoUpdate); autoUpdate = Recommendation.DONT; } } switch (autoUpdate) { case REQUIRED: logger.trace("Automatically updating item '{}' because no channel is linked", itemName); postUpdate(item, state, EVENT_SOURCE); break; case RECOMMENDED: logger.trace("Automatically updating item '{}' because no channel does it", itemName); postUpdate(item, state, EVENT_SOURCE); break; case OPTIMISTIC: logger.trace("Optimistically updating item '{}'", itemName); postPrediction(item, state, false); if (sendOptimisticUpdates) { postUpdate(item, state, EVENT_SOURCE_OPTIMISTIC); } break; case DONT: logger.trace("Won't update item '{}' as it was vetoed.", itemName); break; case REVERT: logger.trace("Sending current item state to revert controls '{}'", itemName); postPrediction(item, item.getState(), true); break; } } } @Activate  AutoUpdateManager(Map<String, @Nullable Object> configuration,
            final @Reference ChannelTypeRegistry channelTypeRegistry, //
            final @Reference EventPublisher eventPublisher,
            final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry,
            final @Reference MetadataRegistry metadataRegistry, //
            final @Reference ThingRegistry thingRegistry); void receiveCommand(ItemCommandEvent commandEvent, Item item);  }
@Test public void testAutoUpdateNoLink() { aum.receiveCommand(event, item); assertStateEvent("AFTER", AutoUpdateManager.EVENT_SOURCE); }
@Test public void testAutoUpdateNoPolicy() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); aum.receiveCommand(event, item); assertChangeStateTo(); }
@Test public void testAutoUpdateNoPolicyThingOFFLINE() { links.add(new ItemChannelLink("test", CHANNEL_UID_OFFLINE_1)); aum.receiveCommand(event, item); assertKeepCurrentState(); }
@Test public void testAutoUpdateNoPolicyThingOFFLINEandThingONLINE() { links.add(new ItemChannelLink("test", CHANNEL_UID_OFFLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); aum.receiveCommand(event, item); assertChangeStateTo(); }
@Test public void testAutoUpdateNoPolicyThingONLINEandThingOFFLINE() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_OFFLINE_1)); aum.receiveCommand(event, item); assertChangeStateTo(); }
@Test public void testAutoUpdateNoPolicyNoHandler() { links.add(new ItemChannelLink("test", CHANNEL_UID_HANDLER_MISSING)); aum.receiveCommand(event, item); assertKeepCurrentState(); }
@Test public void testAutoUpdateNoPolicyNoThing() { links.add(new ItemChannelLink("test", new ChannelUID(new ThingUID("test::missing"), "gone"))); aum.receiveCommand(event, item); assertKeepCurrentState(); }
@Test public void testAutoUpdateNoPolicyNoChannel() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE)); aum.receiveCommand(event, item); assertKeepCurrentState(); }
@Test public void testAutoUpdatePolicyVETOThingONLINE() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.VETO); aum.receiveCommand(event, item); assertNothingHappened(); }
@Test public void testAutoUpdatePolicyRECOMMEND() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.RECOMMEND); aum.receiveCommand(event, item); assertStateEvent("AFTER", AutoUpdateManager.EVENT_SOURCE); }
@Test public void testAutoUpdatePolicyVETObeatsDEFAULT() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_2)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.VETO); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_2, AutoUpdatePolicy.DEFAULT); aum.receiveCommand(event, item); assertNothingHappened(); }
@Test public void testAutoUpdatePolicyVETObeatsRECOMMEND() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_2)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.VETO); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_2, AutoUpdatePolicy.RECOMMEND); aum.receiveCommand(event, item); assertNothingHappened(); }
@Test public void testAutoUpdatePolicyDEFAULTbeatsRECOMMEND() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_2)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.DEFAULT); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_2, AutoUpdatePolicy.RECOMMEND); aum.receiveCommand(event, item); assertChangeStateTo(); }
@Test public void testAutoUpdateErrorInvalidatesVETO() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.RECOMMEND); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.VETO); aum.receiveCommand(event, item); assertStateEvent("AFTER", AutoUpdateManager.EVENT_SOURCE); }
@Test public void testAutoUpdateErrorInvalidatesVETO2() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.DEFAULT); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.VETO); aum.receiveCommand(event, item); assertChangeStateTo(); }
@Test public void testAutoUpdateErrorInvalidatesDEFAULT() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.RECOMMEND); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.DEFAULT); aum.receiveCommand(event, item); assertStateEvent("AFTER", AutoUpdateManager.EVENT_SOURCE); }
@Test public void testAutoUpdateMultipleErrors() { links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE)); links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE)); setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.DEFAULT); aum.receiveCommand(event, item); assertKeepCurrentState(); }