Package com.gmv.its.deepsy.hal.power

Classes

interface  Power
 Power. More...
 
enum  PowerAction
 
enum  PowerCause
 
interface  PowerEvent
 PowerEvent Event. This event arrives when an action status change happens. More...
 
enum  PowerEventType
 
class  PowerLowLevelException
 Power low level exception. More...
 
interface  PowerManager
 
class  PowerNotAvailableException
 Power not available exception. More...
 
interface  PowerObserver
 
enum  PowerRequest
 
enum  PowerStatus
 
class  PowerTimers
 It stores the timers values. More...
 
class  PowerTimersState
 It stores the state of the wakeup timers. More...
 
class  PowerTransitionIntervals
 It stores the transition intervals values. More...
 

Detailed Description

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;

powerManager.close();

Service availability

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();
}
}
}


Examples

Get current status

package com.gmv.its.dpyjavaexamples.power;
import java.util.List;
import com.gmv.its.deepsy.power.DeepsyPowerManager;
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();
}
// Wait for events for 10 seconds
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;
import com.gmv.its.deepsy.power.DeepsyPowerManager;
public class MakeArequestApp {
public static void main(String[] args) {
try (PowerManager powerManager = DeepsyPowerManager.getInstance("192.168.0.144")) {
Power myPower = powerManager.getPower();
if (myPower.getPowerStatus() == PowerStatus.IDLE) {
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();
}
// Wait for events for 10 seconds
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;
import com.gmv.its.deepsy.power.DeepsyPowerManager;
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;
import com.gmv.its.deepsy.power.DeepsyPowerManager;
public class TransitionIntervalsApp {
public static void main(String[] args) {
try (PowerManager powerManager = DeepsyPowerManager.getInstance("172.22.198.104")) {
Power myPower = powerManager.getPower();
// Get default transition intervals
System.out.println("Obtaining default transition intervals:");
// Wait for 2 seconds
Thread.sleep(2000);
PowerTransitionIntervals intervalsDef = myPower.getTransitionIntervals();
System.out.println("Cancel Time: " + intervalsDef.getCancelTimeValue());
System.out.println("Guard Time: " + intervalsDef.getGuardTimeValue());
// Wait for 1 second
Thread.sleep(1000);
// Set new values for transition intervals
int cancelT = 17;
int guardT = 8;
System.out.println("Setting new transition intervals: " + cancelT + " (cancel), " + guardT + " (guard)");
// Wait for 2 seconds
Thread.sleep(2000);
PowerTransitionIntervals intervalsNew = new PowerTransitionIntervals(cancelT, guardT);
myPower.setTransitionIntervals(intervalsNew);
// Get configured transition intervals
System.out.println("Obtaining configured transition intervals:");
// Wait for 2 seconds
Thread.sleep(2000);
PowerTransitionIntervals intervalsGot = myPower.getTransitionIntervals();
System.out.println("Cancel Time: " + intervalsGot.getCancelTimeValue());
System.out.println("Guard Time: " + intervalsGot.getGuardTimeValue());
// Wait for 1 second
Thread.sleep(1000);
// Restore default transition intervals
System.out.println("Setting default transition intervals again: "+ intervalsDef.getCancelTimeValue() + " (cancel), " + intervalsDef.getGuardTimeValue() + " (guard)");
myPower.setTransitionIntervals(intervalsDef);
// Wait for 1 second
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;
import com.gmv.its.deepsy.power.DeepsyPowerManager;
public class TimersApp {
public static void main(String[] args) {
try (PowerManager powerManager = DeepsyPowerManager.getInstance("192.168.0.144")) {
Power myPower = powerManager.getPower();
// Get default timers state
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"));
// Get default timers values
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());
// Set new status for timers
System.out.println("\nEnabling standby timer and shutdown timer");
// Wait for 1 seconds
Thread.sleep(1000);
PowerTimersState timersStateNew= new PowerTimersState(true, true);
myPower.setTimersState(timersStateNew);
// Get configured timers status
System.out.println("\nObtaining configured timers state:");
// Wait for 1 second
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"));
// Set new values for timers
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);
// Get configured timers values
System.out.println("\nObtaining configured timers values:");
// Wait for 1 second
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)");
// Wait for 1 second
Thread.sleep(1000);
myPower.setTimers(timersDef);
System.out.println("\nSetting default timers state again: " + timersStateDef.getStandbyState() + " (standby), " + timersStateDef.getShutdownState() + " (shutdown)");
// Wait for 1 second
Thread.sleep(1000);
myPower.setTimersState(timersStateDef);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (PowerNotAvailableException e) {
e.printStackTrace();
} catch (PowerLowLevelException e) {
e.printStackTrace();
}
}
}