]> git.basschouten.com Git - openhab-addons.git/blob
6bfd07fb6fdcc78d1667d1d659222e99ada37a3d
[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.lcn.internal.connection;
14
15 import java.util.concurrent.TimeUnit;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.lcn.internal.common.LcnAddr;
19 import org.openhab.binding.lcn.internal.common.PckGenerator;
20
21 /**
22  * This state is active when the connection to the LCN bus has been established successfully and data can be sent and
23  * retrieved.
24  *
25  * @author Fabian Wolter - Initial Contribution
26  */
27 @NonNullByDefault
28 public class ConnectionStateConnected extends AbstractConnectionState {
29     private static final int PING_INTERVAL_SEC = 60;
30     private int pingCounter;
31
32     public ConnectionStateConnected(ConnectionStateMachine context) {
33         super(context);
34     }
35
36     @Override
37     public void startWorking() {
38         // send periodic keep-alives to keep the connection open
39         addTimer(getScheduler().scheduleWithFixedDelay(
40                 () -> connection.queueDirectlyPlainText(PckGenerator.ping(++pingCounter)), PING_INTERVAL_SEC,
41                 PING_INTERVAL_SEC, TimeUnit.SECONDS));
42
43         // run ModInfo.update() for every LCN module
44         addTimer(getScheduler().scheduleWithFixedDelay(connection::updateModInfos, 0, 1, TimeUnit.SECONDS));
45
46         connection.sendOfflineQueue();
47     }
48
49     @Override
50     public void queue(LcnAddr addr, boolean wantsAck, byte[] data) {
51         connection.queueDirectly(addr, wantsAck, data);
52     }
53
54     @Override
55     public void onPckMessageReceived(String data) {
56         parseLcnBusDiconnectMessage(data);
57     }
58 }