To work with Deepsy Power service it is necessary to use DeepsyPowerManager located in com.gmv.its.deepsy.power for getting the initial PowerManager object.
PowerManager powerManager = DeepsyPowerManager.getInstance();
DeepsyPowerManager.getInstance has an optional parameter indicating the IP address of remote service. For instance:
PowerManager powerManager = DeepsyPowerManager.getInstance("192.168.1.10");
PowerManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a power object:
try (PowerManager powerManager = DeepsyPowerManager.getInstance()){
Power myPower = powerManager.getPower();
} catch (PowerNotAvailableException e) {
e.printStackTrace();
} catch (PowerLowLevelException e) {
e.printStackTrace();
}
If this object is not inside a try with resources block, it is recommended to call to powerManager.close() in order to finalize properly;
To detect service availability the DeepsyPowerManager implements Manager interface which provides methods for getting availability and subscribing to availability events.
class MyManagerObserver implements ManagerObserver{
public void notify(ManagerConnectionEvent event) {
System.out.println(event.getManager().getName() +
" in "+
event.getManager().getIp() +
" "+
event.getAction().toString());
}
}
public class App {
public static void main(String[] args) {
ManagerObserver observer = new MyManagerObserver();
try (PowerManager powerManager = DeepsyPowerManager.getInstance()){
powerManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}
Get current status
package com.gmv.its.dpyjavaexamples.power;
import java.util.List;
public class GetCurrentStatusApp {
public static void main(String[] args) {
try (PowerManager powerManager = DeepsyPowerManager.getInstance("192.168.0.144")) {
Power myPower = powerManager.getPower();
System.out.println(myPower.getPowerStatus());
} catch (PowerNotAvailableException e) {
e.printStackTrace();
} catch (PowerLowLevelException e) {
e.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Make a request and abort it
When you made a request such as REBOOT, SHUTDOWN or STANDBY, it can be cancelled before receiving an event of type COMPLETED_MSG using the abortRequest() method. It is important to note that when the state of the Power is one of the following: REBOOTING, POWERING_OFF or SLEEPING the request cannot be cancelled.
package com.gmv.its.dpyjavaexamples.power;
import java.util.List;
public class MakeArequestApp {
public static void main(String[] args) {
try (PowerManager powerManager = DeepsyPowerManager.getInstance("192.168.0.144")) {
Power myPower = powerManager.getPower();
myPower.rebootRequest();
}
System.out.println(myPower.getPowerStatus());
if (myPower.getPowerStatus() ==
PowerStatus.GOING_TO_REBOOT) {
myPower.abortRequest();
}
System.out.println(myPower.getPowerStatus());
} catch (PowerNotAvailableException e) {
e.printStackTrace();
} catch (PowerLowLevelException e) {
e.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Subscribe to events
-
PowerEvent : This event happens when the Power change its status PowerStatus . To receive this kind of events, it is necessary to subscribe to the Power object with an object implementing the interface PowerObserver
package com.gmv.its.dpyjavaexamples.power;
import java.util.List;
class MyPowerObserver implements PowerObserver {
public void notify(PowerEvent event) {
System.out.println(event.getTimestamp());
System.out.println(event.getCause());
System.out.println(event.getAction());
System.out.println(event.getType());
}
}
public class PowerEventsApp {
public static void main(String[] args) {
MyPowerObserver powerObserver = new MyPowerObserver();
try (PowerManager powerManager = DeepsyPowerManager.getInstance("192.168.0.144")) {
Power myPower = powerManager.getPower();
myPower.subscribe(powerObserver);
Thread.sleep(60000);
} catch (PowerNotAvailableException e) {
e.printStackTrace();
} catch (PowerLowLevelException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Transition Intervals
-
cancelTime: seconds within it's possible to abort any power transition request
-
guardTime: seconds after cancel time expires before requested power transition takes effect
package com.gmv.its.dpyjavaexamples.power;
import java.util.List;
public class TransitionIntervalsApp {
public static void main(String[] args) {
try (PowerManager powerManager = DeepsyPowerManager.getInstance("172.22.198.104")) {
Power myPower = powerManager.getPower();
System.out.println(
"Obtaining default transition intervals:");
Thread.sleep(2000);
PowerTransitionIntervals intervalsDef = myPower.getTransitionIntervals();
System.out.println(
"Cancel Time: " + intervalsDef.getCancelTimeValue());
System.out.println(
"Guard Time: " + intervalsDef.getGuardTimeValue());
Thread.sleep(1000);
int cancelT = 17;
int guardT = 8;
System.out.println(
"Setting new transition intervals: " + cancelT +
" (cancel), " + guardT +
" (guard)");
Thread.sleep(2000);
PowerTransitionIntervals intervalsNew = new PowerTransitionIntervals(cancelT, guardT);
myPower.setTransitionIntervals(intervalsNew);
System.out.println(
"Obtaining configured transition intervals:");
Thread.sleep(2000);
PowerTransitionIntervals intervalsGot = myPower.getTransitionIntervals();
System.out.println(
"Cancel Time: " + intervalsGot.getCancelTimeValue());
System.out.println(
"Guard Time: " + intervalsGot.getGuardTimeValue());
Thread.sleep(1000);
System.out.println(
"Setting default transition intervals again: "+ intervalsDef.getCancelTimeValue() +
" (cancel), " + intervalsDef.getGuardTimeValue() +
" (guard)");
myPower.setTransitionIntervals(intervalsDef);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (PowerNotAvailableException e) {
e.printStackTrace();
} catch (PowerLowLevelException e) {
e.printStackTrace();
}
}
}
Timers
package com.gmv.its.dpyjavaexamples.power;
import java.util.List;
public class TimersApp {
public static void main(String[] args) {
try (PowerManager powerManager = DeepsyPowerManager.getInstance("192.168.0.144")) {
Power myPower = powerManager.getPower();
System.out.println(
"Obtaining default timers state: ");
PowerTimersState timersStateDef = myPower.getTimersState();
System.out.println(
"Standby Timer " + ((timersStateDef.getStandbyState()) ?
"is enabled" :
"is disabled"));
System.out.println(
"Shutdown Timer " + ((timersStateDef.getShutdownState()) ?
"is enabled" :
"is disabled"));
System.out.println(
"\nObtaining default timers values: ");
PowerTimers timersDef = myPower.getTimers();
System.out.println(
"Standby Timer: " + timersDef.getStandbyValue());
System.out.println(
"Shutdown Timer: " + timersDef.getShutdownValue());
System.out.println(
"\nEnabling standby timer and shutdown timer");
Thread.sleep(1000);
PowerTimersState timersStateNew= new PowerTimersState(true, true);
myPower.setTimersState(timersStateNew);
System.out.println(
"\nObtaining configured timers state:");
Thread.sleep(1000);
PowerTimersState timersStateObtained= myPower.getTimersState();
System.out.println(
"Standby Timer " + ((timersStateObtained.getStandbyState()) ?
"is enabled" :
"is disabled"));
System.out.println(
"Shutdown Timer " + ((timersStateObtained.getShutdownState()) ?
"is enabled" :
"is disabled"));
int standbyTimer = 5;
int shutdownTimer = 8;
System.out.println(
"\nSetting new timers values: " + standbyTimer +
" (standby), " + shutdownTimer +
" (shutdown)");
Thread.sleep(1000);
PowerTimers timersNew= new PowerTimers(standbyTimer, shutdownTimer);
myPower.setTimers(timersNew);
System.out.println(
"\nObtaining configured timers values:");
Thread.sleep(1000);
PowerTimers timersObtained= myPower.getTimers();
System.out.println(
"Standby Timer: " + timersObtained.getStandbyValue());
System.out.println(
"Shutdown Timer: " + timersObtained.getShutdownValue());
System.out.println(
"\nSetting default timers values again: " + timersDef.getStandbyValue() +
" (standby), " + timersDef.getShutdownValue() +
" (shutdown)");
Thread.sleep(1000);
myPower.setTimers(timersDef);
System.out.println(
"\nSetting default timers state again: " + timersStateDef.getStandbyState() +
" (standby), " + timersStateDef.getShutdownState() +
" (shutdown)");
Thread.sleep(1000);
myPower.setTimersState(timersStateDef);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (PowerNotAvailableException e) {
e.printStackTrace();
} catch (PowerLowLevelException e) {
e.printStackTrace();
}
}
}