]> git.basschouten.com Git - openhab-addons.git/blob
f731548efaf660a044a10f37c3f912c0a3dd6222
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.monopriceaudio.internal.communication;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.io.UnsupportedEncodingException;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.monopriceaudio.internal.MonopriceAudioException;
23 import org.openhab.core.io.transport.serial.PortInUseException;
24 import org.openhab.core.io.transport.serial.SerialPort;
25 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
26 import org.openhab.core.io.transport.serial.SerialPortManager;
27 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Class for communicating with the MonopriceAudio device through a serial connection
33  *
34  * @author Laurent Garnier - Initial contribution
35  * @author Michael Lobstein - Adapted for the MonopriceAudio binding
36  */
37 @NonNullByDefault
38 public class MonopriceAudioSerialConnector extends MonopriceAudioConnector {
39
40     private final Logger logger = LoggerFactory.getLogger(MonopriceAudioSerialConnector.class);
41
42     private final String serialPortName;
43     private final SerialPortManager serialPortManager;
44     private final String uid;
45
46     private @Nullable SerialPort serialPort;
47
48     /**
49      * Constructor
50      *
51      * @param serialPortManager the serial port manager
52      * @param serialPortName the serial port name to be used
53      * @param uid the thing uid string
54      * @param amp the AmplifierModel being used
55      */
56     public MonopriceAudioSerialConnector(SerialPortManager serialPortManager, String serialPortName, String uid,
57             AmplifierModel amp) {
58         this.serialPortManager = serialPortManager;
59         this.serialPortName = serialPortName;
60         this.uid = uid;
61         setAmplifierModel(amp);
62     }
63
64     @Override
65     public synchronized void open() throws MonopriceAudioException {
66         logger.debug("Opening serial connection on port {}", serialPortName);
67         try {
68             SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
69             if (portIdentifier == null) {
70                 setConnected(false);
71                 throw new MonopriceAudioException("Opening serial connection failed: No Such Port");
72             }
73
74             SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
75
76             commPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
77             commPort.enableReceiveThreshold(1);
78             commPort.enableReceiveTimeout(100);
79             commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
80
81             InputStream dataIn = commPort.getInputStream();
82             OutputStream dataOut = commPort.getOutputStream();
83
84             if (dataOut != null) {
85                 dataOut.flush();
86             }
87             if (dataIn != null && dataIn.markSupported()) {
88                 try {
89                     dataIn.reset();
90                 } catch (IOException e) {
91                 }
92             }
93
94             Thread thread = new MonopriceAudioReaderThread(this, this.uid, this.serialPortName);
95             setReaderThread(thread);
96             thread.start();
97
98             this.serialPort = commPort;
99             this.dataIn = dataIn;
100             this.dataOut = dataOut;
101
102             setConnected(true);
103
104             logger.debug("Serial connection opened");
105         } catch (PortInUseException e) {
106             setConnected(false);
107             throw new MonopriceAudioException("Opening serial connection failed: Port in Use Exception", e);
108         } catch (UnsupportedCommOperationException e) {
109             setConnected(false);
110             throw new MonopriceAudioException("Opening serial connection failed: Unsupported Comm Operation Exception",
111                     e);
112         } catch (UnsupportedEncodingException e) {
113             setConnected(false);
114             throw new MonopriceAudioException("Opening serial connection failed: Unsupported Encoding Exception", e);
115         } catch (IOException e) {
116             setConnected(false);
117             throw new MonopriceAudioException("Opening serial connection failed: IO Exception", e);
118         }
119     }
120
121     @Override
122     public synchronized void close() {
123         logger.debug("Closing serial connection");
124         SerialPort serialPort = this.serialPort;
125         if (serialPort != null) {
126             serialPort.removeEventListener();
127         }
128         super.cleanup();
129         if (serialPort != null) {
130             serialPort.close();
131             this.serialPort = null;
132         }
133         setConnected(false);
134         logger.debug("Serial connection closed");
135     }
136 }