From b50ba2e44aa845381d5b8f4d07edf6ac286c38f8 Mon Sep 17 00:00:00 2001 From: pbuckley Date: Mon, 12 Dec 2016 18:27:38 +0000 Subject: [PATCH] Adding basic java support --- tplink-smartplug.java | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tplink-smartplug.java diff --git a/tplink-smartplug.java b/tplink-smartplug.java new file mode 100644 index 0000000..79ef061 --- /dev/null +++ b/tplink-smartplug.java @@ -0,0 +1,60 @@ +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; + +/** + * + * @author pbuckley + * + * This class is a java implementation of sending commands to a TPLink smart plug. + * + * At the moment, it only encrypts and sends the commands. + */ +public class TPLink { + + private final static int PORT = 9999; + + public static void main(String[] args) { + + Socket s = null; + try { + s = new Socket(args[0], PORT); + System.out.println(s.isConnected() ? "Connected to Device" : "Device Connection Failed"); + System.out.println("Sending Command: " + args[1]); + + DataOutputStream writer = new DataOutputStream(s.getOutputStream()); + writer.write(encrypt(args[1])); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + System.out.println("Closing"); + + } + + public static byte[] encrypt(String s) { + int key = 171; + + byte[] bs = new byte[s.length() + 4]; + int count = 4; + + for (char c : s.toCharArray()) { + char a = (char) (key ^ c); + if (a < 0) + a += 256; + + key = a; + bs[count++] = (byte) key; + } + return bs; + } +}