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 /flow-samples | |
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 'flow-samples')
6 files changed, 0 insertions, 253 deletions
diff --git a/flow-samples/README.md b/flow-samples/README.md deleted file mode 100644 index 5460b4d..0000000 --- a/flow-samples/README.md +++ /dev/null @@ -1,3 +0,0 @@ -This directory contains fully functional application examples of flow. To run an example, change to the base directory of flow (this parent's directory) and run `sbt samples<SampleName>/run`. - -All projects, including samples, can be listed by running `sbt projects`. diff --git a/flow-samples/terminal-stream/src/main/scala/ch/jodersky/flow/samples/terminalstream/Main.scala b/flow-samples/terminal-stream/src/main/scala/ch/jodersky/flow/samples/terminalstream/Main.scala deleted file mode 100644 index 74433db..0000000 --- a/flow-samples/terminal-stream/src/main/scala/ch/jodersky/flow/samples/terminalstream/Main.scala +++ /dev/null @@ -1,66 +0,0 @@ -package ch.jodersky.flow -package samples.terminalstream - -import scala.concurrent.Future -import scala.concurrent.duration._ -import scala.io.StdIn - -import akka.actor.ActorSystem -import akka.stream.ActorMaterializer -import akka.stream.scaladsl.{Flow, Keep, Sink, Source} -import akka.util.ByteString - -import stream.Serial - -object Main { - - final val Delay = FiniteDuration(500, MILLISECONDS) - - implicit val system = ActorSystem("terminal-stream") - implicit val materializer = ActorMaterializer() - - def ask(label: String, default: String) = { - print(label + " [" + default.toString + "]: ") - val in = StdIn.readLine() - println("") - if (in.isEmpty) default else in - } - - def main(args: Array[String]): Unit = { - import system.dispatcher - - val port = ask("Device", "/dev/ttyACM0") - val baud = ask("Baud rate", "115200").toInt - val cs = ask("Char size", "8").toInt - val tsb = ask("Use two stop bits", "false").toBoolean - val parity = Parity(ask("Parity (0=None, 1=Odd, 2=Even)", "0").toInt) - val settings = SerialSettings(baud, cs, tsb, parity) - - val serial: Flow[ByteString, ByteString, Future[Serial.Connection]] = - Serial().open(port, settings) - - val printer: Sink[ByteString, _] = Sink.foreach[ByteString]{data => - println("server says: " + data.decodeString("UTF-8")) - } - - val ticker: Source[ByteString, _] = Source.tick(Delay, Delay, ()).scan(0){case (x, _) => - x + 1 - }.map{ x => - println(x) - ByteString(x.toString) - } - - val connection: Future[Serial.Connection] = ticker.viaMat(serial)(Keep.right).to(printer).run() - - connection map { conn => - println("Connected to " + conn.port) - StdIn.readLine("Press enter to exit") - } recover { case err => - println("Cannot connect: " + err) - } andThen { case _ => - system.terminate() - } - - } - -} diff --git a/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/ConsoleReader.scala b/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/ConsoleReader.scala deleted file mode 100644 index 2b30663..0000000 --- a/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/ConsoleReader.scala +++ /dev/null @@ -1,29 +0,0 @@ -package ch.jodersky.flow -package samples.terminal - -import akka.actor.Actor -import scala.io.StdIn - -class ConsoleReader extends Actor { - import context._ - import ConsoleReader._ - - def receive = { - case Read => - StdIn.readLine() match { - case ":q" | null => parent ! EOT - case s => { - parent ! ConsoleInput(s) - } - } - } - -} - -object ConsoleReader { - - case object Read - case object EOT - case class ConsoleInput(in: String) - -} diff --git a/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/Main.scala b/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/Main.scala deleted file mode 100644 index 80c0f80..0000000 --- a/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/Main.scala +++ /dev/null @@ -1,30 +0,0 @@ -package ch.jodersky.flow -package samples.terminal - -import akka.actor.ActorSystem -import scala.io.StdIn - -object Main { - - def ask(label: String, default: String) = { - print(label + " [" + default.toString + "]: ") - val in = StdIn.readLine() - println("") - if (in.isEmpty) default else in - } - - def main(args: Array[String]): Unit = { - val port = ask("Device", "/dev/ttyACM0") - val baud = ask("Baud rate", "115200").toInt - val cs = ask("Char size", "8").toInt - val tsb = ask("Use two stop bits", "false").toBoolean - val parity = Parity(ask("Parity (0=None, 1=Odd, 2=Even)", "0").toInt) - val settings = SerialSettings(baud, cs, tsb, parity) - - println("Starting terminal system, enter :q to exit.") - Serial.debug(true) - val system = ActorSystem("flow") - val terminal = system.actorOf(Terminal(port, settings), name = "terminal") - system.registerOnTermination(println("Stopped terminal system.")) - } -} diff --git a/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/Terminal.scala b/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/Terminal.scala deleted file mode 100644 index de60620..0000000 --- a/flow-samples/terminal/src/main/scala/ch/jodersky/flow/samples/terminal/Terminal.scala +++ /dev/null @@ -1,75 +0,0 @@ -package ch.jodersky.flow -package samples.terminal - -import akka.actor.{ Actor, ActorLogging, ActorRef, Props, Terminated, actorRef2Scala } -import akka.io.IO -import akka.util.ByteString - -class Terminal(port: String, settings: SerialSettings) extends Actor with ActorLogging { - import Terminal._ - import context._ - - val reader = actorOf(Props[ConsoleReader]) - - log.info(s"Requesting manager to open port: ${port}, baud: ${settings.baud}") - IO(Serial) ! Serial.Open(port, settings) - - override def postStop() = { - system.terminate() - } - - def receive = { - case Serial.CommandFailed(cmd, reason) => { - log.error(s"Connection failed, stopping terminal. Reason: ${reason}") - context stop self - } - case Serial.Opened(port) => { - log.info(s"Port ${port} is now open.") - val operator = sender - context become opened(operator) - context watch operator - reader ! ConsoleReader.Read - } - } - - def opened(operator: ActorRef): Receive = { - - case Serial.Received(data) => { - log.info(s"Received data: ${formatData(data)}") - } - - case Terminal.Wrote(data) => log.info(s"Wrote data: ${formatData(data)}") - - case Serial.Closed => { - log.info("Operator closed normally, exiting terminal.") - context unwatch operator - context stop self - } - - case Terminated(`operator`) => { - log.error("Operator crashed, exiting terminal.") - context stop self - } - - case ConsoleReader.EOT => { - log.info("Initiating close.") - operator ! Serial.Close - } - - case ConsoleReader.ConsoleInput(input) => { - val data = ByteString(input.getBytes) - operator ! Serial.Write(data, length => Wrote(data.take(length))) - reader ! ConsoleReader.Read - } - } - -} - -object Terminal { - case class Wrote(data: ByteString) extends Serial.Event - - def apply(port: String, settings: SerialSettings) = Props(classOf[Terminal], port, settings) - - private def formatData(data: ByteString) = data.mkString("[", ",", "]") + " " + (new String(data.toArray, "UTF-8")) - -} diff --git a/flow-samples/watcher/src/main/scala/ch/jodersky/flow/samples/watcher/main.scala b/flow-samples/watcher/src/main/scala/ch/jodersky/flow/samples/watcher/main.scala deleted file mode 100644 index 650d08e..0000000 --- a/flow-samples/watcher/src/main/scala/ch/jodersky/flow/samples/watcher/main.scala +++ /dev/null @@ -1,50 +0,0 @@ -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() - } - -} |