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 [2012/05/15 13:46]
smayr [Running Clarujust Proxy]
ezfit:clarujust:clarujust_api [2014/02/11 18:04] (current)
smayr [Exposed Clarujust Methods/Objects]
Line 69: Line 69:
  
 == 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)
  
  
Line 166: Line 167:
 //  ClarujustApp.ShowModal; //  ClarujustApp.ShowModal;
 end; 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> </code>