|
| 1 | +package org.codeoverflow.chatoverflow.requirement.service.serial |
| 2 | + |
| 3 | +import java.io.{InputStream, PrintStream} |
| 4 | + |
| 5 | +import com.fazecast.jSerialComm.{SerialPort, SerialPortInvalidPortException} |
| 6 | +import org.codeoverflow.chatoverflow.WithLogger |
| 7 | +import org.codeoverflow.chatoverflow.connector.Connector |
| 8 | + |
| 9 | +/** |
| 10 | + * The serial connector allows to communicate with a device connected to the pcs serial port (like an Arduino) |
| 11 | + * |
| 12 | + * @param sourceIdentifier r the unique source identifier to identify this connector |
| 13 | + */ |
| 14 | +class SerialConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger { |
| 15 | + |
| 16 | + override protected var optionalCredentialKeys: List[String] = List("baudRate") |
| 17 | + override protected var requiredCredentialKeys: List[String] = List("port") |
| 18 | + |
| 19 | + private var serialPort: Option[SerialPort] = None |
| 20 | + private var out: Option[PrintStream] = None |
| 21 | + private var in: Option[InputStream] = None |
| 22 | + private val serialPortInputListener = new SerialPortInputListener |
| 23 | + |
| 24 | + /** |
| 25 | + * @throws java.lang.IllegalStateException if the serial port is not available yet |
| 26 | + * @return print stream that outputs to the port |
| 27 | + */ |
| 28 | + @throws(classOf[IllegalStateException]) |
| 29 | + def getPrintStream: PrintStream = { |
| 30 | + if (serialPort.isEmpty) throw new IllegalStateException("Serial port is not available yet") |
| 31 | + out.get |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @throws java.lang.IllegalStateException if the serial port is not available yet |
| 36 | + * @return a inputstream that receives all data from the port |
| 37 | + */ |
| 38 | + @throws(classOf[IllegalStateException]) |
| 39 | + def getInputStream: InputStream = { |
| 40 | + if (serialPort.isEmpty) throw new IllegalStateException("Serial port is not available yet") |
| 41 | + in.get |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Adds a new input listener that receives all data |
| 46 | + * @param listener a listener that handles incoming data in a byte array |
| 47 | + * @throws java.lang.IllegalStateException if the serial port is not available yet |
| 48 | + */ |
| 49 | + @throws(classOf[IllegalStateException]) |
| 50 | + def addInputListener(listener: Array[Byte] => Unit): Unit = { |
| 51 | + if (serialPort.isEmpty) throw new IllegalStateException("Serial port is not available yet") |
| 52 | + serialPortInputListener.addDataAvailableListener(_ => { |
| 53 | + val buffer = new Array[Byte](serialPort.get.bytesAvailable()) |
| 54 | + serialPort.get.readBytes(buffer, buffer.length) //FIXME DOES IT CRASH? |
| 55 | + listener(buffer) |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Opens a connection with the serial port |
| 61 | + */ |
| 62 | + override def start(): Boolean = { |
| 63 | + //TODO Test if connector is working this way or if it requires an actor |
| 64 | + try { |
| 65 | + serialPort = Some(SerialPort.getCommPort(credentials.get.getValue("port").get)) |
| 66 | + credentials.get.getValue("baudRate") match { |
| 67 | + case Some(baudRate) if baudRate.matches("\\s*\\d+\\s*") => serialPort.get.setBaudRate(baudRate.trim.toInt) |
| 68 | + case Some(ivalidBaudrate) => |
| 69 | + logger error s"Invalid baud rate: $ivalidBaudrate" |
| 70 | + return false |
| 71 | + case None => //Do nothing |
| 72 | + } |
| 73 | + logger info s"Waiting for serial port to open..." |
| 74 | + if (serialPort.get.openPort(1000)) { |
| 75 | + Thread.sleep(1500)//Sleep to wait for |
| 76 | + serialPort.get.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0) |
| 77 | + out = Some(new PrintStream(serialPort.get.getOutputStream, true, "US-ASCII")) |
| 78 | + in = Some(serialPort.get.getInputStream) |
| 79 | + serialPort.get.addDataListener(serialPortInputListener) |
| 80 | + logger info "Opened serial port!" |
| 81 | + true |
| 82 | + } else { |
| 83 | + logger error s"Could not open serial port $sourceIdentifier" |
| 84 | + false |
| 85 | + } |
| 86 | + } catch { |
| 87 | + case e: SerialPortInvalidPortException => |
| 88 | + logger error s"Source identifier $sourceIdentifier is invalid: ${e.getMessage}" |
| 89 | + false |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Closes the connection with the port |
| 95 | + */ |
| 96 | + override def stop(): Boolean = { |
| 97 | + serialPort.foreach(_.closePort()) |
| 98 | + true |
| 99 | + } |
| 100 | +} |
0 commit comments