aboutsummaryrefslogtreecommitdiff
path: root/flow-samples/watcher/src/main/scala/ch/jodersky/flow/samples/watcher/main.scala
blob: 650d08e650825b880728547f4718a5ed3dc277ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package ch.jodersky.flow
package samples.watcher

import akka.actor.{ Actor, ActorLogging, ActorSystem, Props }
import akka.io.IO
import scala.io.StdIn

class Watcher extends Actor with ActorLogging {
  import context._

  val ports = List(
    "/dev/ttyUSB\\d+",
    "/dev/ttyACM\\d+",
    "/dev/cu\\d+",
    "/dev/ttyS\\d+"
  )

  override def preStart() = {
    val cmd = Serial.Watch()
    IO(Serial) ! cmd //watch for new devices
    log.info(s"Watching ${cmd.directory} for new devices.")
  }

  def receive = {

    case Serial.CommandFailed(w: Serial.Watch, err) =>
      log.error(err, s"Could not get a watch on ${w.directory}.")
      context stop self

    case Serial.Connected(path) =>
      log.info(s"New device: ${path}")
      ports.find(path matches _) match {
        case Some(port) => log.info(s"Device is a serial device.")
        case None => log.warning(s"Device is NOT serial device.")
      }

  }

}

object Main {

  def main(args: Array[String]): Unit = {
    val system = ActorSystem("flow")
    val watcher = system.actorOf(Props(classOf[Watcher]), name = "watcher")
    StdIn.readLine()
    system.terminate()
  }

}