Realtime coordination between the passive update of the item from the network and the active update from the UI

Hi all,
I’ve been dealing with a realtime state onff status of an item. I tackled the question as such:

  public void initialize() {
        Bridge bridge = getBridge();
        if (bridge == null || !(bridge.getHandler() instanceof MerossBridgeHandler)) {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge not set");
            return;
        }
        config = getConfigAs(MerossBulbAndPlugConfiguration.class);
        scheduler.execute(() -> {
            int onlineStatus = manager.onlineStatus(config.deviceName);
            if (onlineStatus != MerossEnum.OnlineStatus.ONLINE.value()) {
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Device offline");
            } else if (MerossBridgeHandler.connector.getDevUUIDByDevName(config.deviceName).isEmpty()) {
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
                        "No device found with that name");
            } else {
                updateStatus(ThingStatus.ONLINE);
                CompletableFuture.runAsync(() -> MerossBridgeHandler.connector.fetchDevicesAndSave());
                MerossBridgeHandler.connector.logout();
            }
        });
        updateScheduler.scheduleWithFixedDelay(() -> updateChannelStateAsync(), 1, 1, TimeUnit.SECONDS);
        try {
            updateScheduler.awaitTermination(3, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private void updateChannelStateAsync() {
        CompletableFuture.supplyAsync(() -> manager.togglexOnOffStatus(config.deviceName))
                .thenAccept(this::updateChannelState);
    }

    void updateChannelState(int status) {
        updateState(CHANNEL_TOGGLEX, status == 0 ? StringType.valueOf("Off") : StringType.valueOf("On"));
    }

    @Override
    public void handleCommand(ChannelUID channelUID, Command command) {
        if (channelUID.getId().equals(CHANNEL_TOGGLEX)) {
            handleTogglexChannel(channelUID, command);
        } else {
            logger.debug("Unsupported channelUID {}", channelUID);
        }
    }

    private void handleTogglexChannel(ChannelUID channelUID, Command command) {
        if (command instanceof StringType) {
            switch (command.toString()) {
                case "ON" ->
                    manager.executeCommand(config.deviceName, MerossEnum.Namespace.CONTROL_TOGGLEX.name(), "ON");
                case "OFF" ->
                    manager.executeCommand(config.deviceName, MerossEnum.Namespace.CONTROL_TOGGLEX.name(), "OFF");
            }
    } else {
            logger.debug("Unsupported command {} for channel {}", command, channelUID);
        }
    }
}

The togglexOnOffStatus method retrieves the item status from a network json. I run it in a scheduler with one second frequency, The question is that when I interact with the UI it becomes unresponsive. I wonder what is the best practice to coordinate the passive update of the item from the network with the active update from the UI?
Thanks

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.