C# Context API
Collaboration diagram for C# Context API:

Modules

 Enumerations
 
 Events related with the context devices
 

Classes

class  DeepsyContextManager
 Manager for access the Context functionalities and events raised More...
 
interface  IContext
 Interface for context service More...
 
interface  IContextDevice
 Interface for context device More...
 

Detailed Description

To work with Deepsy context service it is necessary to use DeepsyContextManager located in GMV.ITS.HAL.DEEPSY.Context.Facade for getting the initial Context object.

DeepsyContextManager contextManager = new DeepsyContextManager("192.168.1.234");


To obtain a list of devices objects:

try
{
DeepsyContextManager contextManager = new DeepsyContextManager("192.168.1.234");
IContext context = contextManager.GetContext().First();
IEnumerable<IContextDevice> devices = context.GetDevices();
}
catch (NotAvailableException e)
{
Console.WriteLine(e.Message);
}
catch (LowLevelException e)
{
Console.WriteLine(e.Message);
}

Examples

  • Get devices parameters
    Getting all the devices list and their parameters.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.Context
    {
    class GetContextDeviceParams
    {
    static void Main(string[] args)
    {
    Console.WriteLine($"Context. Get ContextDevice parameters");
    try
    {
    string deepsyServerIp = args.Length == 0 ? "127.0.0.1" : args[0];
    using (DeepsyContextManager contextManager = new DeepsyContextManager(deepsyServerIp))
    {
    IContext context = contextManager.GetContext().First();
    IEnumerable<IContextDevice> devices = context.GetDevices();
    Console.WriteLine($"There are {devices.Count()} devices");
    devices.ToList().ForEach(d => PrintDevicesInfo(d));
    }
    }
    catch (HalException deepsyEx)
    {
    Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error: [{ex.Message}]");
    }
    }
    public static void PrintDevicesInfo(IContextDevice d)
    {
    try
    {
    Console.WriteLine($"{d.Id} :");
    Console.WriteLine($"\t- memory type: {d.GetMemoryType()}");
    Console.WriteLine($"\t- context size: {d.GetContextSize()}");
    }
    catch (Exception e)
    {
    Console.WriteLine("ERROR: " + e.Message);
    }
    }
    }
    }


  • Get and set memory values
    Doing different get and set operations.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.Context
    {
    class GetSetMemoryValues
    {
    static void Main(string[] args)
    {
    Console.WriteLine($"Context. Get set memory values");
    try
    {
    string deepsyServerIp = args.Length == 0 ? "127.0.0.1" : args[0];
    string dataWrite = args.Length <= 1 ? "Test\x24" : args[1];
    using (DeepsyContextManager contextManager = new DeepsyContextManager(deepsyServerIp))
    {
    IContext context = contextManager.GetContext().First();
    IEnumerable<IContextDevice> devices = context.GetDevices();
    Console.WriteLine($"There are {devices.Count()} devices");
    IContextDevice device = devices.First();
    IMemoryValuesDto initValues = device.DumpContext();
    Console.WriteLine($"Showing values before test");
    Console.WriteLine($"Data: {System.Text.Encoding.UTF8.GetString(initValues.Values)}");
    //string dataWrite = "Test";
    device.SetMemoryValues(10, dataWrite);
    IMemoryValuesDto writtenValue = device.GetMemoryValues(10, (uint)dataWrite.Length);
    Console.WriteLine($"Showing data written");
    Console.WriteLine($"Data: {System.Text.Encoding.UTF8.GetString(writtenValue.Values)}");
    writtenValue = device.DumpContext();
    Console.WriteLine($"Showing all memory context");
    Console.WriteLine($"Data: {System.Text.Encoding.UTF8.GetString(writtenValue.Values)}");
    device.SetMemoryValues(0, System.Text.Encoding.UTF8.GetString(initValues.Values));
    writtenValue = device.DumpContext();
    Console.WriteLine($"Restoring memory context");
    Console.WriteLine($"Data: {System.Text.Encoding.UTF8.GetString(writtenValue.Values)}");
    }
    }
    catch (HalException deepsyEx)
    {
    Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error: [{ex.Message}]");
    }
    }
    }
    }


  • Subscribe to events
    There are 1 type of events in the context system:

    • DeviceListEvent : This event happens when a device is added to or removed from the system. To receive this kind of events, it is necessary to subscribe to a Context object

    This example subscribes to every event.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace GMV.ITS.HAL.DEEPSY.Examples.Context
    {
    class ContextEvents
    {
    static void Main(string[] args)
    {
    Console.WriteLine($"Context. Events");
    try
    {
    string deepsyServerIp = args.Length == 0 ? "192.168.1.140" : args[0];
    using (IDeepsyContextManager contextManager = new DeepsyContextManager(deepsyServerIp))
    {
    IContext context = contextManager.GetContext().First();
    Console.WriteLine("Adding \"ping\" process with a preScript and post script");
    context.DeviceListEventRaised += Context_DeviceList;
    Console.WriteLine("Press ESC for exit");
    while (Console.ReadKey().Key != ConsoleKey.Escape)
    {
    }
    }
    }
    catch (HalException deepsyEx)
    {
    Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error: [{ex.Message}]");
    }
    }
    private static void Context_DeviceList(object sender, HAL.DEEPSY.Context.Interface.Event.DeviceListEvent e)
    {
    Console.WriteLine($"ContextDevice List event: Process {e.device.Id} {e.action}");
    }
    }
    }