]> git.basschouten.com Git - openhab-addons.git/blob
fcdfe31ff6432a8e88e9a8e2b3b4ef73ddbad0c9
[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.homematic.internal.communicator.server;
14
15 import java.io.IOException;
16
17 import org.openhab.binding.homematic.internal.HomematicBindingConstants;
18 import org.openhab.binding.homematic.internal.common.HomematicConfig;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Server implementation for receiving messages via BIN-RPC from a Homematic gateway.
24  *
25  * @author Gerhard Riegler - Initial contribution
26  */
27 public class BinRpcServer implements RpcServer {
28     private final Logger logger = LoggerFactory.getLogger(BinRpcServer.class);
29
30     private Thread networkServiceThread;
31     private BinRpcNetworkService networkService;
32     private final HomematicConfig config;
33     private final RpcEventListener listener;
34     private final String id;
35
36     public BinRpcServer(RpcEventListener listener, HomematicConfig config, String id) {
37         this.listener = listener;
38         this.config = config;
39         this.id = id;
40     }
41
42     @Override
43     public void start() throws IOException {
44         logger.debug("Initializing BIN-RPC server at port {}", config.getBinCallbackPort());
45
46         networkService = new BinRpcNetworkService(listener, config);
47         networkServiceThread = new Thread(networkService);
48         networkServiceThread
49                 .setName("OH-binding-" + HomematicBindingConstants.THING_TYPE_BRIDGE + ":" + id + "-rpcServer");
50         networkServiceThread.start();
51     }
52
53     @Override
54     public void shutdown() {
55         if (networkService != null) {
56             logger.debug("Stopping BIN-RPC server");
57             try {
58                 if (networkServiceThread != null) {
59                     networkServiceThread.interrupt();
60                 }
61             } catch (Exception e) {
62                 logger.error("{}", e.getMessage(), e);
63             }
64             networkService.shutdown();
65             networkService = null;
66         }
67     }
68 }