Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ezfit:clarujust:clarujust_api [2009/09/17 08:55]
smayr
ezfit:clarujust:clarujust_api [2014/02/11 18:04] (current)
smayr [Exposed Clarujust Methods/Objects]
Line 2: Line 2:
 The following represents the expected methods/functionality provided to Clarujust when it is initialized from an external application.  Also, the following shows the functionality provided by Clarujust to the external application.  For the rest of this document, the external application will be referred to as ezFIT. The following represents the expected methods/functionality provided to Clarujust when it is initialized from an external application.  Also, the following shows the functionality provided by Clarujust to the external application.  For the rest of this document, the external application will be referred to as ezFIT.
  
-== The Audina API ==+== The ezFIT API ==
 It is assumed Clarujust will be initialized with a reference to some kind of object that communicates with the hearing aids.  The following shows this interface: It is assumed Clarujust will be initialized with a reference to some kind of object that communicates with the hearing aids.  The following shows this interface:
  
Line 11: Line 11:
 Here’s a short description of the methods: Here’s a short description of the methods:
  
 +=== Initialize Hearing Instrument ===
 <code csharp> <code csharp>
 Boolean GetHAInitialized() Boolean GetHAInitialized()
Line 16: Line 17:
 Returns true if the API has initialized the hearing devices; otherwise, false. Returns true if the API has initialized the hearing devices; otherwise, false.
  
 +=== Ear Side ===
 <code csharp> <code csharp>
 Int16 GetEar() Int16 GetEar()
Line 37: Line 39:
 </code> </code>
  
 +=== Memory ===
 <code csharp> <code csharp>
 short GetMemory() short GetMemory()
Line 42: Line 45:
 Returns the current memory slot of the current ear. Returns the current memory slot of the current ear.
  
 +=== Circuit Parameters ===
 <code csharp> <code csharp>
 int[] GetEthosArray() int[] GetEthosArray()
Line 47: Line 51:
 Returns an array containing all current hearing device parameters in the current ear. Returns an array containing all current hearing device parameters in the current ear.
  
 +=== Bump ===
 <code csharp> <code csharp>
 short GetBumpCF() short GetBumpCF()
Line 61: Line 66:
 Gets or sets the associated parameter values in the current ear. Gets or sets the associated parameter values in the current ear.
  
-NOTE: It is unclear if we need the ''GetBias()'' or SetBias methods, or if we’re going to handle the compression via the SetCompression method, but those can be added if needed.+NOTE: It is unclear if we need the ''GetBias()'' or ''SetBias()'' methods, or if we’re going to handle the compression via the ''SetCompression()'' method, but those can be added if needed.
  
 == Exposed Clarujust Methods/Objects == == Exposed Clarujust Methods/Objects ==
-The following shows what will be exposed to ezFit.+The following shows what will be exposed to ezFIT:   
 +(see full source code in [[ezfit:clarujust:clarujust_api&#sample_clarujustapp_proxy_source_code|Sample ClarujustApp proxy source code]] below)
  
  
 +=== Instantiating Clarujust ===
 The ClarujustApp static class provides one method: The ClarujustApp static class provides one method:
 <code csharp> <code csharp>
Line 73: Line 80:
 Returns an instance of a Clarujust window. Returns an instance of a Clarujust window.
  
-The IClarujustWindow interface defines the methods that will be exposed to ezFit while Clarujust is running.  We can add/remove/modify methods in this interface depending on what we decide should be provided to ezFit, but for the time being, here is the interface and its associated methods:+=== Clarujust Window === 
 +The IClarujustWindow interface defines the methods that will be exposed to ezFIT while Clarujust is running.  We can add/remove/modify methods in this interface depending on what we decide should be provided to ezFIT, but for the time being, here is the interface and its associated methods:
  
 <code csharp> <code csharp>
Line 90: Line 98:
 Hides the Clarujust window. Hides the Clarujust window.
  
 +== Running Clarujust Proxy ==
 +The DLL files needed to run the Clarujust proxy server.  The only one that needs to be registered is ''ClarujustProxyServer.dll''.
  
 +COM DLLs built in .NET need to use a different app to register (''regasm.exe'').  If you have the latest version of .NET installed on your machine, it should be located in the following directory:
 +''C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe''
 +
 +Use that to register the libraries.  For example, place the DLLs in C:\Temp.  Then create these batch files, one to register and one to unregister:
 +
 +To register:
 +
 +<code msdos>
 +path C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\
 +regasm C:\Temp\ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /codebase
 +pause
 +</code>
 +Or:
 +<code msdos>
 +CD C:\TEMP
 +C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /codebase
 +pause
 +</code>
 +
 +To unregister:
 +
 +<code msdos>
 +path C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\
 +regasm C:\Temp\ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /unregister
 +pause
 +</code>
 +Or:
 +<code msdos>
 +CD C:\TEMP
 +C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /unregister
 +pause
 +</code>
 +
 +The other libraries just need to be placed in the same location as the proxy DLL and it should work.  
 +
 +Sample code (in C#) on how to call it:
 +
 +<code csharp>
 +static void Main(string[] args)
 +{
 +    //Create and initialize the optimizer object however you choose...
 +    EzFITOptimizerSvr.IEzFITOptimizerClarujust optimizer = ...
 +    Clarujust.Proxy.IClarujustApp app = new Clarujust.Proxy.ClarujustApp();
 +    app.Initialize(optimizer);
 +    app.ShowModal();
 +}
 +</code>
 +
 +Sample code in Delphi:
 +<code delphi>
 +procedure TEzFITOptimizerClarujust.LaunchClarujustApp;
 +var
 +//  ClarujustApp: Variant;
 +  ClarujustApp: IClarujustApp; 
 +  ThisOptSvr: OleVariant;
 +begin
 +  ThisOptSvr := IEzFitOptimizerClarujust(self);
 +  ClarujustApp := CoClarujustApp.Create();
 +  ClarujustApp.Initialize(ThisOptSvr);
 +  ClarujustApp.ShowModal;
 +
 +//  ThisOptSvr := IEzFitOptimizerClarujust(self);
 +//  ClarujustApp := CreateOleObject('Clarujust.Proxy.ClarujustApp');
 +//  ClarujustApp.Initialize(ThisOptSvr);
 +//  ClarujustApp.ShowModal;
 +end;
 +</code>
 +
 +== Sample ClarujustApp proxy source code ==
 +<code csharp>
 +using System;
 +using System.IO;
 +using System.Text;
 +using System.Threading;
 +using System.Reflection;
 +using System.Windows.Forms;
 +using System.Runtime.InteropServices;
 +
 +using ezFITOptimizerSvr;
 +
 +namespace Clarujust.Proxy
 +{
 +    /// <summary>
 +    /// Represents the worker class responsible for launching and managing the Clarujust application.
 +    /// </summary>
 +    [Guid("A85C954C-F13F-4323-A846-84222AE79D27"),
 +    ClassInterface(ClassInterfaceType.None),
 +    ComVisible(true)]
 +    public class ClarujustApp : IClarujustApp
 +    {
 +        #region Properties
 +
 +        /// <summary>
 +        /// Gets or sets the worker thread representing the Clarujust application.
 +        /// </summary>
 +        /// <value>A <see cref="Thread"/> object representing the Clarujust application thread.</value>
 +        private static Thread ClarujustThread { get; set; }
 +
 +        #endregion
 +
 +        #region Methods
 +
 +        #region Public
 +
 +        /// <summary>
 +        /// Initializes the Clarujust application.
 +        /// </summary>
 +        /// <param name="framework">The Audina framework object.</param>
 +        /// <remarks>
 +        /// This method handles any initialization required by the Clarujust application.
 +        /// The <paramref name="framework"/> parameter represents the Audina interface responsible
 +        /// for communicating with the hearing aids and the ezFit application.  This parameter
 +        /// must be specified, and must be an object of type <see cref="ezFITOptimizerSvr.IEzFITOptimizerClarujust"/>.</remarks>
 +        [STAThread()]
 +        public void Initialize(Object framework)
 +        {
 +            // Stop any existing threads
 +            ClarujustApp.JoinThread();
 +
 +            if (framework == null)
 +                MessageBox.Show("An IEzFITOptimizerClarujust type object must be provided.");
 +            else
 +            {
 +                // Check the framework object
 +                ClarujustApp.LogMessage(String.Format("Initializing Clarujust proxy server."));
 +                IEzFITOptimizerClarujust fwk = framework as IEzFITOptimizerClarujust;
 +                if (fwk == null)
 +                    MessageBox.Show(String.Format(
 +                        "An IEzFITOptimizerClarujust type object must be provided.  The specified object is of type {0}.",
 +                        framework.GetType().ToString()));
 +                else
 +                {
 +                    // Create and start the Clarujust thread
 +                    ClarujustApp.ClarujustThread = new Thread(new ParameterizedThreadStart(thread_Clarujust));
 +                    ClarujustApp.ClarujustThread.Name = "Main Clarujust Thread";
 +                    ClarujustApp.ClarujustThread.SetApartmentState(ApartmentState.STA);
 +                    ClarujustApp.ClarujustThread.Start(framework);
 +                }
 +            }
 +        }
 +
 +        /// <summary>
 +        /// Shows the Clarujust application window.
 +        /// </summary>
 +        [STAThread()]
 +        public void Show()
 +        {
 +        }
 +
 +        /// <summary>
 +        /// Shows the Clarujust application window as a modal dialog box.
 +        /// </summary>
 +        [STAThread()]
 +        public void ShowModal()
 +        {
 +        }
 +
 +        /// <summary>
 +        /// Closes the Clarujust application window.
 +        /// </summary>
 +        [STAThread()]
 +        public void Close()
 +        {
 +        }
 +
 +        #endregion
 +
 +        #region Private
 +
 +        /// <summary>
 +        /// Prepares the ezFit optimizer framework interface.
 +        /// </summary>
 +        /// <param name="fwk">ezFit optimizer framework interface.</param>
 +        /// <returns>True if the interface has been prepared and is ready to be used; otherwise, false.</returns>
 +        private static Boolean PrepareInterface(IEzFITOptimizerClarujust fwk)
 +        {
 +            Boolean isInitialized = false;
 +            String deviceId = null;
 +            String serialNumber = null;
 +            Int32 earCount = 0;
 +            Int32 error = 0;
 +            String appDirectory = null;
 +
 +            try
 +            {
 +                ClarujustApp.LogMessage(String.Format("Getting executing assembly location."));
 +                appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
 +                ClarujustApp.LogMessage(String.Format("Executing assembly = {0}.", appDirectory));
 +            }
 +            catch (Exception e)
 +            {
 +                ClarujustApp.LogException(e);
 +            }
 +            catch
 +            {
 +                ClarujustApp.LogMessage(String.Format("Some exception occurred while checking initialization."));
 +            }
 +
 +            try
 +            {
 +                ClarujustApp.LogMessage(String.Format("Setting environment directory to executing assembly location."));
 +                Environment.CurrentDirectory = appDirectory;
 +                ClarujustApp.LogMessage(String.Format("Environment directory set."));
 +            }
 +            catch (Exception e)
 +            {
 +                ClarujustApp.LogException(e);
 +            }
 +            catch
 +            {
 +                ClarujustApp.LogMessage(String.Format("Some exception occurred while checking initialization."));
 +            }
 +
 +            try
 +            {
 +                ClarujustApp.LogMessage(String.Format("Checking initialization."));
 +                error = fwk.GetHAInitialized(out isInitialized);
 +                ClarujustApp.LogMessage(String.Format("Is initialized = {0} (return code = {1}).", isInitialized, error));
 +            }
 +            catch (Exception e)
 +            {
 +                ClarujustApp.LogException(e);
 +            }
 +            catch
 +            {
 +                ClarujustApp.LogMessage(String.Format("Some exception occurred while checking initialization."));
 +            }
 +
 +            try
 +            {
 +                ClarujustApp.LogMessage(String.Format("Setting up 60 (initial)."));
 +                error = fwk.Setup60();
 +                ClarujustApp.LogMessage(String.Format("Setup 60 complete (initial) (return code = {0}).", error));
 +            }
 +            catch (Exception e)
 +            {
 +                ClarujustApp.LogException(e);
 +            }
 +            catch
 +            {
 +                ClarujustApp.LogMessage(String.Format("Some exception occurred while setting up 60."));
 +            }
 +
 +            try
 +            {
 +                ClarujustApp.LogMessage(String.Format("Getting ear count."));
 +                error = fwk.GetEarCount(out earCount);
 +                ClarujustApp.LogMessage(String.Format("Ear count = {0} (ret code = {1}).", earCount, error));
 +            }
 +            catch (Exception e)
 +            {
 +                ClarujustApp.LogException(e);
 +            }
 +            catch
 +            {
 +                ClarujustApp.LogMessage(String.Format("Some exception occurred while getting ear count."));
 +            }
 +
 +            if (earCount == EAR_COUNT_LEFT ||
 +                earCount == EAR_COUNT_BOTH)
 +            {
 +                try
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Getting left device ID."));
 +                    error = fwk.GetDeviceID(out deviceId, 0);
 +                    ClarujustApp.LogMessage(String.Format("left device ID = {0} (ret code = {1}).", deviceId, error));
 +                }
 +                catch (Exception e)
 +                {
 +                    ClarujustApp.LogException(e);
 +                }
 +                catch
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting left device ID."));
 +                }
 +
 +                try
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Getting left serial number."));
 +                    error = fwk.GetSerialNumber(out serialNumber, 0);
 +                    ClarujustApp.LogMessage(String.Format("left serial = {0} (ret code = {1}).", serialNumber, error));
 +                }
 +                catch (Exception e)
 +                {
 +                    ClarujustApp.LogException(e);
 +                }
 +                catch
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting left serial."));
 +                }
 +            }
 +            else if (earCount == EAR_COUNT_RIGHT)
 +            {
 +                try
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Getting right device ID."));
 +                    error = fwk.GetDeviceID(out deviceId, 1);
 +                    ClarujustApp.LogMessage(String.Format("right device ID = {0} (ret code = {1}).", deviceId, error));
 +                }
 +                catch (Exception e)
 +                {
 +                    ClarujustApp.LogException(e);
 +                }
 +                catch
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting right device ID."));
 +                }
 +
 +                try
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Getting right serial number."));
 +                    error = fwk.GetSerialNumber(out serialNumber, 1);
 +                    ClarujustApp.LogMessage(String.Format("right serial = {0} (ret code = {1}).", serialNumber, error));
 +                }
 +                catch (Exception e)
 +                {
 +                    ClarujustApp.LogException(e);
 +                }
 +                catch
 +                {
 +                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting right serial."));
 +                }
 +            }
 +            return true;
 +        }
 +
 +        /// <summary>
 +        /// Stops and joins the Clarujust worker thread.
 +        /// </summary>
 +        private static void JoinThread()
 +        {
 +            if (ClarujustApp.ClarujustThread != null)
 +            {
 +                ClarujustApp.ClarujustThread.Abort();
 +                ClarujustApp.ClarujustThread.Join();
 +            }
 +        }
 +
 +        /// <summary>
 +        /// Logs a message to the log file.
 +        /// </summary>
 +        /// <param name="message">An application-defined message.</param>
 +        private static void LogMessage(String message)
 +        {
 +            String text = String.Format(
 +                "Message: {0} {1}",
 +                DateTime.Now.ToString("MM/dd/yy HH:mm:ss"),
 +                message);
 +            using (StreamWriter writer = new StreamWriter(new FileStream("log.txt", FileMode.OpenOrCreate)))
 +            {
 +                writer.BaseStream.Position = writer.BaseStream.Length;
 +                writer.WriteLine(message);
 +            }
 +        }
 +
 +        /// <summary>
 +        /// Logs the exception message to the log file.
 +        /// </summary>
 +        /// <param name="e">The exception to log.</param>
 +        private static void LogException(Exception e)
 +        {
 +            if (e == null)
 +                return;
 +
 +            StringBuilder text = new StringBuilder();
 +            text.Append(String.Empty.PadLeft(100, '-') + "\r\n");
 +            text.Append("Exception Type: " + e.GetType().ToString() + "\r\n");
 +            text.Append("Exception Msg: " + e.Message + "\r\n");
 +            text.Append("Stack Trace: \r\n");
 +            text.Append(e.StackTrace + "\r\n");
 +            while (e.InnerException != null)
 +            {
 +                text.Append(String.Empty.PadLeft(100, '-') + "\r\n");
 +                text.Append("Inner Exception Type: " + e.InnerException.GetType().ToString() + "\r\n");
 +                text.Append("Inner Exception Msg: " + e.InnerException.Message + "\r\n");
 +                text.Append("Stack Trace: \r\n");
 +                text.Append(e.InnerException.StackTrace + "\r\n");
 +                e = e.InnerException;
 +            }
 +
 +            ClarujustApp.LogMessage(text.ToString());
 +        }
 +
 +        #endregion
 +
 +        #region Threads
 +
 +        /// <summary>
 +        /// The Clarujust worker thread.
 +        /// </summary>
 +        /// <param name="data">The ezFit optimizer framework interface.</param>
 +        private static void thread_Clarujust(Object data)
 +        {
 +            ClarujustApp.PrepareInterface((IEzFITOptimizerClarujust)data);
 +        }
 +
 +        #endregion
 +
 +        #endregion
 +
 +        #region Data Constants
 +
 +        private static readonly Int32 EAR_COUNT_LEFT = 1;
 +        private static readonly Int32 EAR_COUNT_RIGHT = 2;
 +        private static readonly Int32 EAR_COUNT_BOTH = 3;
 +
 +        #endregion
 +    }
 +}
 +</code>