From a5d6ef930cf26d863bed15ebc705fedcad6f52bd Mon Sep 17 00:00:00 2001 From: Alex Volkmann Date: Mon, 11 Mar 2019 17:14:42 +0100 Subject: [PATCH] =?UTF-8?q?Status=20abfrage=20erg=C3=A4nzt=20und=20Source?= =?UTF-8?q?=20Code=20kommentiert=20(englisch).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gpio_java.java | 204 -------------------------------------- src/gpio_java.java | 241 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 204 deletions(-) delete mode 100644 gpio_java.java create mode 100644 src/gpio_java.java diff --git a/gpio_java.java b/gpio_java.java deleted file mode 100644 index 021620e..0000000 --- a/gpio_java.java +++ /dev/null @@ -1,204 +0,0 @@ -/** - * @author Alex Volkmann - * @date 18.02.2019 - * @function Das programm steuert ein relle an und macht es an oder aus -*/ - -import java.io.IOException; -import java.util.Arrays; -import java.util.concurrent.TimeUnit; - -// todo: add Dokumentation -public class gpio_java -{ - public static String rel_map [][] = { - {"rel1","14"}, - {"rel2","15"}, - {"rel3","18"}, - {"rel4","23"}, - {"rel5","24"}, - {"rel6","25"}, - {"rel7","8"}, - {"rel8","7"} - }; - public static String [] action = {"on", "off", "status"}; - - /** Die main-Methode ruft die Funktionen in der gegebenen Reihnfolge auf, - * und gibt die @param args weiter. - * @param args Commandline argument der main funktion - */ - public static void main (String [] args) - { - // Zum checken der Anzahl der Argumente - checkargs(args); - - // todo: initialisiere alle acht ports (outputs) - init_gpio(); - - // setze gpio (gemaes der parameter) - gpio_getport(args[0]); - - - gpio_cmdhandler(args[0], args[1]); - - return; - } - - /** Die checkargs-Methode prüft ob zwei korrekte Argumente eingetragen wurden. - * @param argumens - Commandline argument - */ - public static void checkargs(String[] argumens) - { - int laenge = argumens.length; - String [] h = {"-h"}; - int idx; - boolean parameter1 = false; - boolean parameter2 = false; - - //Prüft ob zwei args angegeben wurden. - if (laenge == 2) - { - - //Prüft ob ein richtiges erstes args angegeben wurde. - for (idx = 0; idx<8 ; idx++) - { - if (argumens[0].equals(rel_map[idx][0])) - { - parameter1 = true; - break; - } - } - - //Prüft ob ein richtiges zweites args angegeben wurde. - for (idx = 0; idx<3 ; idx++) - { - if (argumens[1].equals(action[idx])) - { - parameter2 = true; - break; - } - } - - } - - // Wenn es nicht zwei Args sind dann wird getestet ob ein h drin ist. - // Wenn ja führt die Funktion show_usage aus. - else if (Arrays.equals (argumens, h) == true) - { - - show_usage(); - - System.exit(0); - } - - //Wenn Kein h drin steht und es kein der beiden args enthält kommt eine Fehlermeldung - else - { - System.out.println("Error. If you need help, type the command -h"); - - System.exit(0); - } - } - - /** Die init_gpio-Methode ist eine schleife die alle rel als output definiert.*/ - public static void init_gpio() - { - int idx; - String port; - String cmd; - - for(idx = 0; idx<8 ; idx++) - { - port = rel_map[idx][1]; - cmd = "/usr/bin/gpio -g mode " + port + " out"; - launchcmd(cmd); - - } - return; - } - - /** Die gpio_getport-Methode vergleicht im rel_map welcher port gesucht ist - * @param port - erster eingabe Wert der CMD Zeile - * @return idx - gibt den idx Wert aus rel_map zurück - * @return -1 - Fehler ausgabe - * */ - public static int gpio_getport(String port) - { - int idx; - - for(idx = 0; idx<8 ; idx++) - { - if(port.equals(rel_map[idx][0])) - { - return idx; - } - } - - return -1; - } - - /** Die gpio_cmdhandler-Methode schaltet die Relais an oder aus. - * @param port Relais(1-8) - * @param cmd Aktion (on/off/status) - * */ - public static void gpio_cmdhandler(String port, String cmd) - { - int idx_port; - - idx_port = gpio_getport(port); - - - if (cmd.equals(action [0])) - { - //Schalte on - cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 1"; - launchcmd(cmd); - - } - else if (cmd.equals(action [1])) - { - //Schalte off - cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 0"; - launchcmd(cmd); - } - /*if (cmd.equals(action [2])) - { - //Stats geben - cmd = "/usr/bin/gpio -g write " + port + status(); - launchcmd(cmd); - }*/ - return; - } - - /** Die show_usage-Methode gibt eine Hilfestellung bei falscher Eingabe*/ - public static void show_usage() - { - System.out.println("You have to enter exactly 2 arguments."); - System.out.println("Example: java gpio_java Argument1 Argument2"); - System.out.println("Argument1: rel1, rel2, rel3, ..., rel8"); - System.out.println("Argument2: on,off or status"); - } - - /** Die launchcmd-Methode führt den Prozess aus. - * @param cmd - erster Eingabe Wert der CMD Zeile */ - public static void launchcmd(String cmd) - { - // Wenn Simulation True ist kann sie auf dem PC ausfuehren - // wenn die Simulation false is auf dem Raspberry PI - boolean simulation = false; - - if(simulation==true){ - System.out.println(cmd); - } - else { - try{ - Runtime.getRuntime().exec(cmd); - } - catch(IOException e) - { - System.out.println("process error IOException"); - } - } - } -} - diff --git a/src/gpio_java.java b/src/gpio_java.java new file mode 100644 index 0000000..ccaca72 --- /dev/null +++ b/src/gpio_java.java @@ -0,0 +1,241 @@ +/** + * @author Alex Volkmann + * @date 18.02.2019 + * @function Das programm steuert ein relle an und macht es an oder aus +*/ + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; + +public class gpio_java +{ + public static String rel_map [][] = { + {"rel1","14"}, + {"rel2","15"}, + {"rel3","18"}, + {"rel4","23"}, + {"rel5","24"}, + {"rel6","25"}, + {"rel7","8"}, + {"rel8","7"} + }; + public static String [] action = {"on", "off", "status"}; + + /** The main method calls the functions in the given order and passes the @param args. + * @param args The passing argument in the commandline. + */ + public static void main (String [] args) + { + // To check the number of arguments + checkargs(args); + + // Sets all ports to outputs + init_gpio(); + + // Sets Gpio (according to the parameters) + gpio_getport(args[0]); + + // Turns Gpio on, off or returns status (according to the parameters) + gpio_cmdhandler(args[0], args[1]); + + return; + } + + /** The checkargs method checks whether two correct arguments have been entered. + * If not, an error message appears. + * @param argumens Commandline argument + */ + public static void checkargs(String[] argumens) + { + int laenge = argumens.length; + String [] h = {"-h"}; + int idx; + //Checks if two arguments were given. + if (laenge == 2) + { + + //Checks if the first argument is correct. + for (idx = 0; idx<8 ; idx++) + { + if (argumens[0].equals(rel_map[idx][0])) + { + break; + } + } + + //Checks if the second argument is correct. + for (idx = 0; idx<3 ; idx++) + { + if (argumens[1].equals(action[idx])) + { + break; + } + } + + } + + //If there are not two correct arguments then it is tested whether there is an h in it. + //If so, the show_usage function executes. + else if (Arrays.equals (argumens, h) == true) + { + + show_usage(); + + System.exit(0); + } + + //If it is a wrong input, an error message will be displayed. + else + { + System.out.println("Error. If you need help, type the command -h"); + + System.exit(0); + } + } + + /** The init_gpio method is a loop that defines all relays as output.*/ + public static void init_gpio() + { + int idx; + String port; + String cmd; + + for(idx = 0; idx<8 ; idx++) + { + port = rel_map[idx][1]; + cmd = "/usr/bin/gpio -g mode " + port + " out"; + launchcmd(cmd); + + } + return; + } + + /** The gpio_getport method searches in the rel_map variable for the correct port and using a loop for this. + * @param port relay(rel1-rel8) + * @return idx returns an index. The index allows access to the array rel_map. If an error occurs in the function, the value -1 is returned. + * */ + public static int gpio_getport(String port) + { + int idx; + + for(idx = 0; idx<8 ; idx++) + { + if(port.equals(rel_map[idx][0])) + { + return idx; + } + } + + return -1; + } + + /** The gpio_cmdhandler method switches the relays on, off or returns the status. + * @param port relay(rel1-rel8) + * @param cmd action (on/off/status) + * */ + public static void gpio_cmdhandler(String port, String cmd) + { + int idx_port; + + idx_port = gpio_getport(port); + + + if (cmd.equals(action [0])) + { + //Turn on + cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 1"; + launchcmd(cmd); + + } + else if (cmd.equals(action [1])) + { + //Turn off + cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 0"; + launchcmd(cmd); + } + else + { + //Show status + gpio_read_status(port); + } + return; + } + + /** The method checks the status and displays it in the command line. + * @param port the relay board*/ + public static void gpio_read_status(String port) + { + int idx_port; + idx_port = gpio_getport(port); + String cmd = "/usr/bin/gpio -g read " + rel_map[idx_port][1]; + String response; + + response = launchcmd(cmd); + + + if(response.equals("1")) + { + System.out.println("on"); + } + + else + { + System.out.println("off"); + } + return; + } + + + /** Die show_usage-Methode gibt eine Hilfestellung bei falscher Eingabe*/ + public static void show_usage() + { + System.out.println("gpio_java allows you to control an 8 port relay board."); + System.out.println("Usage: java gpio_java port action"); + System.out.println("port: the port indicates which relay is controlled(rel1, rel2, rel3, ..., rel8)."); + System.out.println("action: the action indicates which operation will be performed (on = switch relay on,off = switch relay off or status = relay request status)."); + System.out.println(""); + System.out.println("Example : switch first relais on."); + System.out.println("java gpio_java rel1 on"); + System.out.println(""); + System.out.println("Example : switch first relais off."); + System.out.println("java gpio_java rel1 off"); + System.out.println(""); + System.out.println("Example : read status from first relais."); + System.out.println("java gpio_java rel1 status"); + + } + + /** The launchcmd method runs the command. + * @param cmd + * @return retvalue returns a string + * */ + public static String launchcmd(String cmd) + { + // If simulation is True it can run on PC + // if the simulation is false on the Raspberry PI + boolean simulation = false; + String line = null; + String retValue = ""; + + if(simulation==true){ + System.out.println(cmd); + } + else { + try{ + Process p = Runtime.getRuntime().exec(cmd); + BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); + + while ((line = in.readLine()) != null) { + retValue += line; + } + } + catch(IOException e) + { + System.out.println("process error IOException"); + } + } + return retValue; + } +} \ No newline at end of file