1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.chabala.brick.controllab;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.io.Closeable;
25 import java.io.IOException;
26
27 import static java.nio.charset.StandardCharsets.ISO_8859_1;
28
29
30
31
32
33 class SerialPortWriter implements Closeable {
34
35
36
37
38
39 private static final int STRING_LOGGING_THRESHOLD = 10;
40 private final Logger log = LoggerFactory.getLogger(getClass());
41 private final SerialPort serialPort;
42 private KeepAliveMonitor keepAliveMonitor;
43
44 SerialPortWriter(SerialPort serialPort) {
45 this.serialPort = serialPort;
46 }
47
48 void startKeepAlives() {
49 keepAliveMonitor = new KeepAliveMonitor(this);
50 }
51
52 String getPortName() {
53 return serialPort.getPortName();
54 }
55
56 void sendCommand(byte[] bytes, Logger log) throws IOException {
57 if (serialPort == null) {
58 throw new IOException("Not connected to control lab");
59 }
60 if (keepAliveMonitor != null) {
61 keepAliveMonitor.reset();
62 }
63 if (serialPort.isOpen()) {
64 if (log.isInfoEnabled()) {
65 if (bytes.length > STRING_LOGGING_THRESHOLD) {
66 String portName = serialPort.getPortName();
67 log.info("{} TX -> {}", portName, new String(bytes, ISO_8859_1));
68 } else if (log.isDebugEnabled()) {
69 String portName = serialPort.getPortName();
70 StringBuilder sb = new StringBuilder();
71 for (byte b : bytes) {
72 sb.append(String.format("0x%02X ", b));
73 }
74 log.debug("{} TX -> {}", portName, sb);
75 }
76 }
77 serialPort.write(bytes);
78 }
79 }
80
81 void sendCommand(byte[] bytes) throws IOException {
82 sendCommand(bytes, log);
83 }
84
85 void sendCommand(byte b) throws IOException {
86 sendCommand(new byte[]{b});
87 }
88
89 void sendCommand(byte b, Logger log) throws IOException {
90 sendCommand(new byte[]{b}, log);
91 }
92
93 void sendCommand(byte b1, byte b2) throws IOException {
94 sendCommand(new byte[]{b1, b2});
95 }
96
97 @Override
98 public void close() {
99 if (keepAliveMonitor != null) {
100 keepAliveMonitor.close();
101 keepAliveMonitor = null;
102 }
103 }
104 }