]> git.basschouten.com Git - openhab-addons.git/blob
15f7dcbdf6639ec71701474a4157d80d82a5f9b8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.minecraft.internal.handler;
14
15 import java.util.List;
16 import java.util.concurrent.TimeUnit;
17
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;
36
37 import rx.Observable;
38 import rx.Subscription;
39 import rx.subscriptions.CompositeSubscription;
40
41 /**
42  * The {@link MinecraftServerHandler} is responsible for handling commands, which are
43  * sent to one of the channels.
44  *
45  * @author Mattias Markehed - Initial contribution
46  */
47 public class MinecraftServerHandler extends BaseBridgeHandler {
48
49     private Logger logger = LoggerFactory.getLogger(MinecraftServerHandler.class);
50
51     private ServerConfig config;
52
53     private Observable<ServerConnection> serverConnectionRX;
54     private ServerConnection connection;
55     private CompositeSubscription subscription;
56
57     public MinecraftServerHandler(Bridge bridge) {
58         super(bridge);
59     }
60
61     @Override
62     public void initialize() {
63         subscription = new CompositeSubscription();
64         config = getConfigAs(ServerConfig.class);
65         logger.info("Initializing MinecraftHandler");
66         connectToServer();
67         updateStatus(ThingStatus.ONLINE);
68     }
69
70     /**
71      * Get server configuration
72      *
73      * @return
74      */
75     public ServerConfig getServerConfig() {
76         return config;
77     }
78
79     /**
80      * Directly connect to server.
81      * Reconnects when connection is lost
82      */
83     private void connectToServer() {
84         String host = config.getHostname();
85         int port = config.getPort();
86
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();
90
91         Subscription serverUpdateSubscription = serverConnectionRX
92                 .flatMap(connection -> connection.getSocketHandler().getServerRx())
93                 .subscribe(serverData -> updateServerState(serverData));
94
95         Subscription serverConnectionSubscription = serverConnectionRX.subscribe(connection -> {
96             this.connection = connection;
97         });
98
99         subscription.add(serverUpdateSubscription);
100         subscription.add(serverConnectionSubscription);
101     }
102
103     public Observable<List<SignData>> getSignsRx() {
104         return serverConnectionRX.switchMap(connection -> connection.getSocketHandler().getSignsRx());
105     }
106
107     public Observable<List<PlayerData>> getPlayerRx() {
108         return serverConnectionRX.switchMap(connection -> connection.getSocketHandler().getPlayersRx());
109     }
110
111     /**
112      * Update online state of server
113      *
114      * @param isOnline true if server is online
115      */
116     private void updateOnlineState(boolean isOnline) {
117         State onlineState = isOnline ? OnOffType.ON : OnOffType.OFF;
118         updateState(MinecraftBindingConstants.CHANNEL_ONLINE, onlineState);
119     }
120
121     /**
122      * Update state of minecraft server
123      *
124      * @param serverData
125      */
126     private void updateServerState(ServerData serverData) {
127         State playersState = new DecimalType(serverData.getPlayers());
128         State maxPlayersState = new DecimalType(serverData.getMaxPlayers());
129
130         updateState(MinecraftBindingConstants.CHANNEL_PLAYERS, playersState);
131         updateState(MinecraftBindingConstants.CHANNEL_MAX_PLAYERS, maxPlayersState);
132     }
133
134     /**
135      * Send message to server.
136      * Does nothing if no connection is established.
137      *
138      * @param message the message to send
139      * @return true if message was sent.
140      */
141     public boolean sendMessage(OHMessage message) {
142         if (connection != null) {
143             connection.sendMessage(message);
144             return true;
145         }
146         return false;
147     }
148
149     @Override
150     public void dispose() {
151         logger.debug("Disposing minecraft server thing");
152
153         if (subscription != null) {
154             subscription.unsubscribe();
155         }
156     }
157
158     @Override
159     public void handleCommand(ChannelUID channelUID, Command command) {
160     }
161 }