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.russound.internal.rio;
15 import java.io.IOException;
17 import org.openhab.binding.russound.internal.net.SocketSession;
18 import org.openhab.binding.russound.internal.net.SocketSessionListener;
19 import org.openhab.binding.russound.internal.rio.system.RioSystemHandler;
20 import org.openhab.core.thing.ThingStatus;
21 import org.openhab.core.thing.ThingStatusDetail;
22 import org.openhab.core.types.State;
25 * Defines the abstract base for a protocol handler. This base provides managment of the {@link SocketSession} and
26 * provides helper methods that will callback {@link RioHandlerCallback}
28 * @author Tim Roberts - Initial contribution
30 public abstract class AbstractRioProtocol implements SocketSessionListener {
32 * The {@link SocketSession} used by this protocol handler
34 private final SocketSession session;
37 * The {@link RioSystemHandler} to call back to update status and state
39 private final RioHandlerCallback callback;
42 * Constructs the protocol handler from given parameters and will add this handler as a
43 * {@link SocketSessionListener} to the specified {@link SocketSession} via
44 * {@link SocketSession#addListener(SocketSessionListener)}
46 * @param session a non-null {@link SocketSession} (may be connected or disconnected)
47 * @param callback a non-null {@link RioHandlerCallback} to update state and status
49 protected AbstractRioProtocol(SocketSession session, RioHandlerCallback callback) {
50 if (session == null) {
51 throw new IllegalArgumentException("session cannot be null");
54 if (callback == null) {
55 throw new IllegalArgumentException("callback cannot be null");
58 this.session = session;
59 this.session.addListener(this);
60 this.callback = callback;
64 * Sends the command and puts the thing into {@link ThingStatus#OFFLINE} if an IOException occurs
66 * @param command a non-null, non-empty command to send
68 protected void sendCommand(String command) {
69 if (command == null) {
70 throw new IllegalArgumentException("command cannot be null");
73 session.sendCommand(command);
74 } catch (IOException e) {
75 getCallback().statusChanged(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
76 "Exception occurred sending command: " + e);
81 * Updates the state via the {@link RioHandlerCallback#stateChanged(String, State)}
83 * @param channelId the channel id to update state
84 * @param newState the new state
86 protected void stateChanged(String channelId, State newState) {
87 getCallback().stateChanged(channelId, newState);
91 * Updates a property via the {@link RioHandlerCallback#setProperty(String, String)}
93 * @param propertyName a non-null, non-empty property name
94 * @param propertyValue a non-null, possibly empty property value
96 protected void setProperty(String propertyName, String propertyValue) {
97 getCallback().setProperty(propertyName, propertyValue);
101 * Updates the status via {@link RioHandlerCallback#statusChanged(ThingStatus, ThingStatusDetail, String)}
103 * @param status the new status
104 * @param statusDetail the status detail
105 * @param msg the status detail message
107 protected void statusChanged(ThingStatus status, ThingStatusDetail statusDetail, String msg) {
108 getCallback().statusChanged(status, statusDetail, msg);
112 * Disposes of the protocol by removing ourselves from listening to the socket via
113 * {@link SocketSession#removeListener(SocketSessionListener)}
115 public void dispose() {
116 session.removeListener(this);
120 * Implements the {@link SocketSessionListener#responseException(Exception)} to automatically take the thing offline
121 * via {@link RioHandlerCallback#statusChanged(ThingStatus, ThingStatusDetail, String)}
123 * @param e the exception
126 public void responseException(IOException e) {
127 getCallback().statusChanged(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
128 "Exception occurred reading from the socket: " + e);
132 * Returns the {@link RioHandlerCallback} used by this protocol
134 * @return a non-null {@link RioHandlerCallback}
136 public RioHandlerCallback getCallback() {