2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.minecraft.internal.handler;
15 import java.util.List;
16 import java.util.concurrent.TimeUnit;
18 import org.openhab.binding.minecraft.internal.MinecraftBindingConstants;
19 import org.openhab.binding.minecraft.internal.config.ServerConfig;
20 import org.openhab.binding.minecraft.internal.message.OHMessage;
21 import org.openhab.binding.minecraft.internal.message.data.PlayerData;
22 import org.openhab.binding.minecraft.internal.message.data.ServerData;
23 import org.openhab.binding.minecraft.internal.message.data.SignData;
24 import org.openhab.binding.minecraft.internal.server.ServerConnection;
25 import org.openhab.binding.minecraft.internal.util.RetryWithDelay;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.binding.BaseBridgeHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 import rx.Subscription;
39 import rx.subscriptions.CompositeSubscription;
42 * The {@link MinecraftServerHandler} is responsible for handling commands, which are
43 * sent to one of the channels.
45 * @author Mattias Markehed - Initial contribution
47 public class MinecraftServerHandler extends BaseBridgeHandler {
49 private Logger logger = LoggerFactory.getLogger(MinecraftServerHandler.class);
51 private ServerConfig config;
53 private Observable<ServerConnection> serverConnectionRX;
54 private ServerConnection connection;
55 private CompositeSubscription subscription;
57 public MinecraftServerHandler(Bridge bridge) {
62 public void initialize() {
63 subscription = new CompositeSubscription();
64 config = getConfigAs(ServerConfig.class);
65 logger.info("Initializing MinecraftHandler");
67 updateStatus(ThingStatus.ONLINE);
71 * Get server configuration
75 public ServerConfig getServerConfig() {
80 * Directly connect to server.
81 * Reconnects when connection is lost
83 private void connectToServer() {
84 String host = config.getHostname();
85 int port = config.getPort();
87 serverConnectionRX = ServerConnection.create(getThing().getUID(), host, port)
88 .doOnNext(item -> updateOnlineState(true)).doOnError(e -> updateOnlineState(false))
89 .retryWhen(new RetryWithDelay(1, TimeUnit.MINUTES)).repeat().replay(1).refCount();
91 Subscription serverUpdateSubscription = serverConnectionRX
92 .flatMap(connection -> connection.getSocketHandler().getServerRx())
93 .subscribe(serverData -> updateServerState(serverData));
95 Subscription serverConnectionSubscription = serverConnectionRX.subscribe(connection -> {
96 this.connection = connection;
99 subscription.add(serverUpdateSubscription);
100 subscription.add(serverConnectionSubscription);
103 public Observable<List<SignData>> getSignsRx() {
104 return serverConnectionRX.switchMap(connection -> connection.getSocketHandler().getSignsRx());
107 public Observable<List<PlayerData>> getPlayerRx() {
108 return serverConnectionRX.switchMap(connection -> connection.getSocketHandler().getPlayersRx());
112 * Update online state of server
114 * @param isOnline true if server is online
116 private void updateOnlineState(boolean isOnline) {
117 State onlineState = isOnline ? OnOffType.ON : OnOffType.OFF;
118 updateState(MinecraftBindingConstants.CHANNEL_ONLINE, onlineState);
122 * Update state of minecraft server
126 private void updateServerState(ServerData serverData) {
127 State playersState = new DecimalType(serverData.getPlayers());
128 State maxPlayersState = new DecimalType(serverData.getMaxPlayers());
130 updateState(MinecraftBindingConstants.CHANNEL_PLAYERS, playersState);
131 updateState(MinecraftBindingConstants.CHANNEL_MAX_PLAYERS, maxPlayersState);
135 * Send message to server.
136 * Does nothing if no connection is established.
138 * @param message the message to send
139 * @return true if message was sent.
141 public boolean sendMessage(OHMessage message) {
142 if (connection != null) {
143 connection.sendMessage(message);
150 public void dispose() {
151 logger.debug("Disposing minecraft server thing");
153 if (subscription != null) {
154 subscription.unsubscribe();
159 public void handleCommand(ChannelUID channelUID, Command command) {