|
@@ -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; |
|
|
|
|
|
} |
|
|
|
|
|
} |