C# PowerManager API
Collaboration diagram for C# PowerManager API:

Modules

 Enumerations
 
 Events related with Power Manager
 
 Exceptions thrown by Power Manager
 

Classes

class  PowerManagerDeepsyService
 Power Manager Deepsy Service. Allows to get the power object and sending commands to the power service More...
 
class  PowerService
 Power service object that allows to request power operations, set configuration and get values. Also allows to get power events
 
class  PowerTimer
 Timers for the power service More...
 

Detailed Description

To work with Deepsy Power service it is necessary to use PowerManagerDeepsyService located in GMV.ITS.HAL.DEEPSY.PowerManager.Facade

PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService();


PowerManagerDeepsyService has an optional parameter indicating the IP address of remote service. For instance:

PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService("192.168.1.10");

To obtain a power object:

PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService("192.168.1.10");
IPowerManager powerService = powerManagerService.GetPower();


PowerManagerDeepsyService is an IDisposable interface. An object that may hold resources (such as file or socket handles) until it is closed. The Dispose() method of an IDisposable object is called automatically when exiting a try-with-resources block, or exiting a using clause where it was declared. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur:

using (PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService("192.168.1.10")){
}


If this object is not inside a try with resources block, it is recommended to call to Dispose() in order to finalize properly;

powerManagerService.Dispose();

Examples

  • Get current status

    using System;
    namespace GMV.ITS.HAL.DEEPSY.Examples.PowerManager
    {
    class PowerCurrentStatusApp
    {
    static void Main(string[] args)
    {
    PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService("192.168.0.144");
    IPowerManager myPower = powerManagerService.GetPower();
    PowerStatus.Status status = myPower.GetPowerStatus();
    Console.WriteLine($"Current power status: {status}");
    Console.WriteLine($"Current power status: {decodeStatusToString(status)}");
    }
    private static string decodeStatusToString(PowerStatus.Status status)
    {
    switch (status)
    {
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.GOING_TO_POWER_OFF:
    return "GOING_TO_POWER_OFF";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.GOING_TO_REBOOT:
    return "GOING_TO_REBOOT";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.GOING_TO_SLEEP:
    return "GOING_TO_SLEEP";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.IDLE:
    return "IDLE";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.POWERING_OFF:
    return "POWERING_OFF";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.REBOOTING:
    return "REBOOTING";
    default:
    return "SLEEPING";
    }
    }
    }
    }


  • 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 Abort() 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.

    using System;
    namespace GMV.ITS.HAL.DEEPSY.Examples.PowerManager
    {
    class PowerRebootAbortApp
    {
    static void Main(string[] args)
    {
    IPowerManagerService powerManagerService = new PowerManagerDeepsyService("192.168.0.144");
    IPowerManager myPower = powerManagerService.GetPower();
    myPower.PowerEvent += MyPower_PowerEvent;
    PowerStatus.Status status = myPower.GetPowerStatus();
    Console.WriteLine($"Current power status: {decodeStatusToString(status)} [{status}]");
    if (status == PowerStatus.Status.IDLE)
    {
    myPower.Reboot();
    }
    status = myPower.GetPowerStatus();
    Console.WriteLine($"Current power status: {decodeStatusToString(status)} [{status}]");
    if (status == PowerStatus.Status.GOING_TO_REBOOT)
    {
    myPower.Abort();
    }
    status = myPower.GetPowerStatus();
    Console.WriteLine($"Current power status: {decodeStatusToString(status)} [{status}]");
    }
    private static void MyPower_PowerEvent(object sender, DEEPSY.PowerManager.Interface.Event.IPowerEvent e)
    {
    }
    private static string decodeStatusToString(PowerStatus.Status status)
    {
    switch (status)
    {
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.GOING_TO_POWER_OFF:
    return "GOING_TO_POWER_OFF";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.GOING_TO_REBOOT:
    return "GOING_TO_REBOOT";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.GOING_TO_SLEEP:
    return "GOING_TO_SLEEP";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.IDLE:
    return "IDLE";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.POWERING_OFF:
    return "POWERING_OFF";
    case DEEPSY.PowerManager.Interface.Enum.PowerStatus.Status.REBOOTING:
    return "REBOOTING";
    default:
    return "SLEEPING";
    }
    }
    }
    }


  • 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 PowerManager

    using System;
    namespace GMV.ITS.HAL.DEEPSY.Examples.PowerManager
    {
    class PowerEventsApp
    {
    static void Main(string[] args)
    {
    PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService("192.168.0.144");
    IPowerManager myPower = powerManagerService.GetPower();
    myPower.PowerEvent += MyPower_PowerEvent;
    Console.WriteLine("Press ESC for exit");
    while (Console.ReadKey().Key != ConsoleKey.Escape)
    {
    }
    }
    private static void MyPower_PowerEvent(object sender, DEEPSY.PowerManager.Interface.Event.IPowerEvent e)
    {
    Console.WriteLine("Power Event received:");
    Console.WriteLine($"Timestamp: {e.TimeStamp}");
    Console.WriteLine($"Cause: {e.PowerCause}");
    Console.WriteLine($"Action: {e.PowerAction}");
    Console.WriteLine($"Type: {e.PowerType}");
    }
    }
    }


  • 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

    using System;
    namespace GMV.ITS.HAL.DEEPSY.Examples.PowerManager
    {
    class TransitionIntervalsApp
    {
    static void Main(string[] args)
    {
    using (PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService("192.168.0.144"))
    {
    IPowerManager myPower = powerManagerService.GetPower();
    // Get default transition intervals
    Console.WriteLine($"Obtaining default transition intervals:");
    // Wait for 2 seconds
    System.Threading.Thread.Sleep(2000);
    IPowerTransitionIntervals intervalsDef = myPower.GetTransitionIntervals();
    Console.WriteLine($"Cancel Time: {intervalsDef.CancelTime}");
    Console.WriteLine($"Guard Time: {intervalsDef.GuardTime}");
    // Wait for 1 second
    System.Threading.Thread.Sleep(1000);
    // Set new values for transition intervals
    int cancelT = 17;
    int guardT = 8;
    Console.WriteLine($"Setting new transition intervals: {cancelT} (cancel), {guardT} (guard)");
    // Wait for 2 seconds
    System.Threading.Thread.Sleep(2000);
    PowerTransitionIntervals intervalsNew = new PowerTransitionIntervals(cancelT, guardT);
    myPower.SetTransitionIntervals(intervalsNew);
    // Get configured transition intervals
    Console.WriteLine($"Obtaining configured transition intervals:");
    // Wait for 2 seconds
    System.Threading.Thread.Sleep(2000);
    IPowerTransitionIntervals intervalsGot = myPower.GetTransitionIntervals();
    Console.WriteLine($"Cancel Time: " + intervalsGot.CancelTime);
    Console.WriteLine($"Guard Time: " + intervalsGot.GuardTime);
    // Wait for 1 second
    System.Threading.Thread.Sleep(1000);
    // Restore default transition intervals
    Console.WriteLine($"Setting default transition intervals again: {intervalsDef.CancelTime} (cancel), {intervalsDef.GuardTime} (guard)");
    myPower.SetTransitionIntervals(intervalsDef);
    // Wait for 1 second
    System.Threading.Thread.Sleep(1000);
    }
    }
    }
    }


  • Wakeup Time
    wakeupTime: date and time at which the system will wake up from a shutdown/standby state.
    using System;
    namespace GMV.ITS.HAL.DEEPSY.Examples.PowerManager
    {
    class PowerWakeupTimeApp
    {
    static void Main(string[] args)
    {
    using (PowerManagerDeepsyService powerManagerService = new PowerManagerDeepsyService("192.168.0.140"))
    {
    IPowerManager myPower = powerManagerService.GetPower();
    String dateTimeFormat = "ddd MMM dd yyyy HH:mm:ss";
    // Get default wakeup time
    Console.WriteLine($"Obtaining default wakeup date and time:");
    // Wait for 1 second
    System.Threading.Thread.Sleep(1000);
    DateTime wakeupTimeDefault = myPower.GetWakeupTime();
    Console.WriteLine($"Wake up date and time: {wakeupTimeDefault.ToString(dateTimeFormat)}");
    Console.WriteLine($"Setting wakeup date and time to Thu Dec 14 2023 16:13:00 (local time)");
    //assigns year, month, day, hour, min, seconds
    DateTime localWakeupTimeNew = new DateTime(2023, 12, 14, 16, 13, 0, DateTimeKind.Local);
    myPower.SetWakeupTime(localWakeupTimeNew);
    Console.WriteLine($"Obtaining configured wakeup date and time:");
    // Wait for 1 second
    System.Threading.Thread.Sleep(1000);
    DateTime wakeupTimeGot = myPower.GetWakeupTime();
    Console.WriteLine($"Wake up date and time: {wakeupTimeGot.ToString(dateTimeFormat)}");
    Console.WriteLine($"Setting wakeup date and time to Thu Dec 14 2023 16:13:00 (UTC time)");
    //assigns year, month, day, hour, min, seconds
    DateTime utcWakeupTimeNew = new DateTime(2023, 12, 14, 16, 13, 0, DateTimeKind.Utc);
    myPower.SetWakeupTime(utcWakeupTimeNew);
    Console.WriteLine($"Obtaining configured wakeup date and time:");
    // Wait for 1 second
    System.Threading.Thread.Sleep(1000);
    wakeupTimeGot = myPower.GetWakeupTime();
    Console.WriteLine($"Wake up date and time: {wakeupTimeGot.ToString(dateTimeFormat)}");
    // Restore default wakeup time
    Console.WriteLine($"Setting default wakeup time again: {wakeupTimeDefault.ToString(dateTimeFormat)}");
    myPower.SetWakeupTime(wakeupTimeDefault);
    // Wait for 1 second
    System.Threading.Thread.Sleep(1000);
    }
    }
    }
    }