public bool CanMoveAxis(TelescopeAxes axis)
        {
            bool                 retval      = false;
            Exception            xcp         = null;
            ActivityMessageTypes messageType = ActivityMessageTypes.Capabilities;

            string axisName = GetNameFromAxis(axis);
            string msg      = String.Format("Calling CanMoveAxis( {0} )", axisName);

            try
            {
                CheckDevice();
                retval = Service.CanMoveAxis(axis);
                msg   += retval.ToString();
            }
            catch (Exception ex)
            {
                xcp  = ex;
                msg += Failed;
                throw;
            }

            finally
            {
                LogActivityLine(messageType, msg);

                if (xcp != null)
                {
                    LogActivityLine(messageType, "CanMoveAxis( {0} ) Exception: {1}", axisName, xcp.Message);
                }
            }

            return(retval);
        }
        protected void SetServiceProperty <T>(Action setCmd, string name, T value, ActivityMessageTypes messageType = ActivityMessageTypes.Status)
        {
            Exception xcp = null;
            string    msg = String.Format("Set {0} -> {1} ", name, value);

            CheckDevice();

            try
            {
                setCmd();
                msg += Done;
            }
            catch (Exception ex)
            {
                xcp  = ex;
                msg += Failed;
            }
            finally
            {
                LogActivityLine(messageType, msg);

                if (xcp != null)
                {
                    LogActivityLine(messageType, "Set {0} Exception: {1}", name, xcp.Message);
                }
            }
        }
        private string GetMessageTypeName(ActivityMessageTypes msgType)
        {
            string typeText = "              ";

            switch (msgType)
            {
            case ActivityMessageTypes.Capabilities:
                typeText = "Capabilities: ";
                break;

            case ActivityMessageTypes.Commands:
                typeText = "Commands:     ";
                break;

            case ActivityMessageTypes.Other:
                typeText = "Others:       ";
                break;

            case ActivityMessageTypes.Parameters:
                typeText = "Parameters:   ";
                break;

            case ActivityMessageTypes.Status:
                typeText = "Statuses:     ";
                break;
            }

            return(typeText);
        }
        protected void SetServiceProperty <T>(Action setCmd, T value
                                              , ActivityMessageTypes messageType = ActivityMessageTypes.Status
                                              , [System.Runtime.CompilerServices.CallerMemberName] string propName = "???")
        {
            Exception xcp = null;
            string    msg = String.Format("Set {0} -> {1} ", propName, value);

            CheckDevice();

            try
            {
                setCmd();
                msg += Done;
            }
            catch (Exception ex)
            {
                xcp  = ex;
                msg += Failed;
                throw;
            }
            finally
            {
                LogActivityLine(messageType, msg);

                if (xcp != null)
                {
                    LogActivityLine(messageType, "Set {0} Exception: {1}", propName, xcp.Message);
                }
            }
        }
        protected void LogActivityStart(ActivityMessageTypes msgType, string msg)
        {
            string deviceTypeText  = DeviceType.ToString();
            string messageTypeText = GetMessageTypeName(msgType);
            string timestamp       = DateTime.Now.ToString("HH:mm:ss.fff");
            string text            = String.Format("{0}: {1} - {2}{3}", timestamp, deviceTypeText, messageTypeText, msg);

            LogMessage(msgType, text);
        }
        public IAxisRates AxisRates(TelescopeAxes axis)
        {
            IAxisRates retval;

            Exception            xcp         = null;
            ActivityMessageTypes messageType = ActivityMessageTypes.Capabilities;

            string axisName = GetNameFromAxis(axis);
            string msg      = String.Format("Calling AxisRates( {0} )", axisName);

            try
            {
                CheckDevice();

                if (Service.CanMoveAxis(axis))
                {
                    retval = Service.AxisRates(axis);
                }
                else
                {
                    IRate[] rateArr = new IRate[0];
                    retval = new ScopeAxisRates(rateArr);
                }

                msg += Done;
            }
            catch (Exception ex)
            {
                xcp  = ex;
                msg += Failed;

                // Return an empty IAxisRates collection

                IRate[] rateArr = new IRate[0];
                retval = new ScopeAxisRates(rateArr);
            }
            finally
            {
                LogActivityLine(messageType, msg);

                if (xcp != null)
                {
                    LogActivityLine(messageType, "AxisRates( {0} ) Exception: {1}", axisName, xcp.Message);
                }
            }

            return(retval);
        }
        protected T GetServiceProperty <T>(Func <T> getCmd, T defaultValue
                                           , ActivityMessageTypes messageType = ActivityMessageTypes.Status
                                           , [System.Runtime.CompilerServices.CallerMemberName] string propName = "???")
        {
            T         retval = defaultValue;
            Exception xcp    = null;

            CheckDevice();

            try
            {
                retval = getCmd();
            }
            catch (Exception ex)
            {
                xcp = ex;
                string name = ex.GetType().Name;
            }

            if (xcp == null)
            {
                if (retval is ArrayList)
                {
                    LogActivityStart(messageType, "Get {0}: {1}", propName, retval);

                    StringBuilder sb   = new StringBuilder();
                    ArrayList     list = retval as ArrayList;

                    foreach (var item in list)
                    {
                        sb.Append((sb.Length > 0) ? ", " : "");
                        sb.Append(item.ToString());
                    }

                    LogActivityEnd(messageType, sb.ToString());
                }
                else if (retval is TrackingRates)
                {
                    LogActivityStart(ActivityMessageTypes.Parameters, "Get {0}:", propName);
                    StringBuilder sb = new StringBuilder();

                    TrackingRates rates = retval as TrackingRates;

                    if (rates == null)
                    {
                        sb.Append(" Did not return expected ITrackingRates collection!");
                    }
                    else if (rates.Count == 0)
                    {
                        sb.Append("Received invalid Tracking Rates collection.");
                    }
                    else
                    {
                        for (int i = 1; i <= rates.Count; ++i)
                        {
                            if (sb.Length > 0)
                            {
                                sb.Append(", ");
                            }

                            sb.Append(rates[i].ToString());
                        }
                    }

                    LogActivityEnd(ActivityMessageTypes.Parameters, sb.ToString());
                }
                else
                {
                    LogActivityLine(messageType, "Get {0}: {1}", propName, retval);
                }
            }
            else
            {
                LogActivityLine(messageType, "Get {0} Exception: {1}", propName, xcp.Message);
            }

            Exceptions.SetException(propName, xcp);

            return(retval);
        }
 private void LogMessage(ActivityMessageTypes msgType, string msgText)
 {
     Messenger.Default.Send(new ActivityMessage(DeviceType, msgType, msgText));
 }
        protected void LogActivityLine(ActivityMessageTypes msgType, string msgFormat, params object[] parms)
        {
            string msg = String.Format(msgFormat, parms);

            LogActivityLine(msgType, msg);
        }
        protected void LogActivityEnd(ActivityMessageTypes msgType, string msg)
        {
            msg = " " + msg + "\r\n";

            LogMessage(msgType, msg);
        }
        protected void LogActivityEnd(ActivityMessageTypes msgType, object objVal)
        {
            string msg = objVal.ToString();

            LogActivityEnd(msgType, msg);
        }
 public ActivityMessage(DeviceTypeEnum deviceType, ActivityMessageTypes messageType, string messageText)
 {
     DeviceType  = deviceType;
     MessageType = messageType;
     MessageText = messageText;
 }