diff options
author | Jakob Odersky <jakob@odersky.com> | 2017-01-08 21:16:25 +0100 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2017-01-21 17:22:10 -0800 |
commit | 23959966760174477a6b0fcbf9dd1e8ef37c643b (patch) | |
tree | 9a0ee44eb43a8c13af57b0d06313f3aabf9e4555 /samples/watcher/src/main | |
parent | 6c371ba6d69c891c1f0d6df00bb643e1d543cc9d (diff) | |
download | akka-serial-23959966760174477a6b0fcbf9dd1e8ef37c643b.tar.gz akka-serial-23959966760174477a6b0fcbf9dd1e8ef37c643b.tar.bz2 akka-serial-23959966760174477a6b0fcbf9dd1e8ef37c643b.zip |
Rename project to akka-serial
Diffstat (limited to 'samples/watcher/src/main')
-rw-r--r-- | samples/watcher/src/main/scala/akka/serial/samples/watcher/main.scala | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/samples/watcher/src/main/scala/akka/serial/samples/watcher/main.scala b/samples/watcher/src/main/scala/akka/serial/samples/watcher/main.scala new file mode 100644 index 0000000..d9a0d4e --- /dev/null +++ b/samples/watcher/src/main/scala/akka/serial/samples/watcher/main.scala @@ -0,0 +1,50 @@ +package akka.serial +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("akka-serial") + val watcher = system.actorOf(Props(classOf[Watcher]), name = "watcher") + StdIn.readLine() + system.terminate() + } + +} |