From 7d11758390001c82b19e792f7d81471903fcb26b Mon Sep 17 00:00:00 2001 From: Amir Karimi Date: Mon, 7 Dec 2015 13:31:01 +0330 Subject: [PATCH 1/2] Implement Scala version of the fucking-coffee script using Ammonite shell --- scala/fucking-coffee.scala | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 scala/fucking-coffee.scala diff --git a/scala/fucking-coffee.scala b/scala/fucking-coffee.scala new file mode 100644 index 0000000..20432bc --- /dev/null +++ b/scala/fucking-coffee.scala @@ -0,0 +1,43 @@ +/******************************************* + * + * Get and run Ammonite (http://lihaoyi.github.io/Ammonite/#Ammonite-Shell): + * $ mkdir ~/.ammonite; curl -L -o ~/.ammonite/predef.scala http://git.io/vR04f + * $ curl -L -o amm http://git.io/vR08A; chmod +x amm; ./amm + * + * Run script: + * @ load.exec("fucking-coffee.scala") + * + *******************************************/ + +import java.net._ +import java.io._ +import ammonite.ops._ + +val coffeeMachineIP = "10.10.42.42" +val password = "1234" +val passwordPrompt = "Password: " +val delayBeforeBrew = 17 +val delay = 24 + +if ((%%who "-q").out.string.contains(sys.props("user.name"))) { + + val telnet = new Socket(coffeeMachineIP, 23) + val out = new PrintWriter(telnet.getOutputStream, true) + val in = new BufferedReader(new InputStreamReader(telnet.getInputStream)) + + Thread.sleep(delayBeforeBrew * 1000); + + if(in.readLine == passwordPrompt){ + out.println(password) + + out.println("sys brew") + Thread.sleep(delay * 1000) + out.println("sys pour") + + } + + out.close() + in.close() + telnet.close() + +} \ No newline at end of file From 895b9228d3d2f7bf7ec7b950e8d03f446bbbf043 Mon Sep 17 00:00:00 2001 From: Amir Karimi Date: Mon, 7 Dec 2015 14:54:43 +0330 Subject: [PATCH 2/2] Run script from bash --- scala/fucking-coffee.scala | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scala/fucking-coffee.scala b/scala/fucking-coffee.scala index 20432bc..b40d083 100644 --- a/scala/fucking-coffee.scala +++ b/scala/fucking-coffee.scala @@ -1,17 +1,18 @@ /******************************************* * - * Get and run Ammonite (http://lihaoyi.github.io/Ammonite/#Ammonite-Shell): + * Get Ammonite (http://lihaoyi.github.io/Ammonite/#Ammonite-Shell): * $ mkdir ~/.ammonite; curl -L -o ~/.ammonite/predef.scala http://git.io/vR04f - * $ curl -L -o amm http://git.io/vR08A; chmod +x amm; ./amm + * $ curl -L -o amm http://git.io/vR08A; chmod +x amm * - * Run script: - * @ load.exec("fucking-coffee.scala") + * Run script + * $ ./amm fucking-coffee.scala * *******************************************/ import java.net._ import java.io._ import ammonite.ops._ +import ammonite.ops.ImplicitWd._ val coffeeMachineIP = "10.10.42.42" val password = "1234" @@ -25,6 +26,7 @@ if ((%%who "-q").out.string.contains(sys.props("user.name"))) { val out = new PrintWriter(telnet.getOutputStream, true) val in = new BufferedReader(new InputStreamReader(telnet.getInputStream)) + println(s"Wait for $delayBeforeBrew seconds") Thread.sleep(delayBeforeBrew * 1000); if(in.readLine == passwordPrompt){