-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShutdown.java
44 lines (35 loc) · 1.32 KB
/
Shutdown.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.IOException;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListener;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
public class Shutdown {
public static void main(String[] args) throws InterruptedException {
final GpioController gpio = GpioFactory.getInstance();
final GpioPinDigitalInput input = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);
input.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
System.out.println(event.getPin() + " = " + event.getState());
//Wenn der Pin#2 auf High geht, fährt sich der Rasperry Pi runter.
if (input.getState()==PinState.HIGH) {
try {
Process p = Runtime.getRuntime().exec("sudo shutdown -h now");
p.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
});
while (true) {
Thread.sleep(1000);
}
}
}