private void SetRelaySwitchInfo(RelaySwitch relaySwitch)
 {
     try
     {
         this.IoAddress         = relaySwitch.IoAddress;
         this.IsNetworkAttached = relaySwitch.IsNetworkAttached;
         this.IpAddress         = relaySwitch.IpAddress;
         this.IpPort            = relaySwitch.IpPort;
         this.IoAddress         = relaySwitch.IoAddress;
         this.ComPort           = relaySwitch.ComPort;
         if (relaySwitch.ComSettings != null)
         {
             this.ComSettings           = relaySwitch.ComSettings;
             this.ComSettings.BaudRate  = relaySwitch.ComSettings.BaudRate;
             this.ComSettings.DataBits  = relaySwitch.ComSettings.DataBits;
             this.ComSettings.ParityBit = relaySwitch.ComSettings.ParityBit;
             this.ComSettings.StopBit   = relaySwitch.ComSettings.StopBit;
         }
         this.SensorAlarmID = relaySwitch.SensorAlarmID;
         this.IsEnabled     = relaySwitch.IsEnabled;
         this.SwitchName    = relaySwitch.SwitchName;
         this.LastValue     = relaySwitch.LastValue;
         this.SwitchBitMask = relaySwitch.SwitchBitMask;
         this.IsDynamicNotificationCleared = true;
         this.FactoryID = relaySwitch.FactoryID;
     }
     catch (Exception ex)
     {
         LogBook.Write("  *** Error in SetRelaySwitchInfo method : " + ex.Message);
     }
 }
        public NotifyComResponse Invoke(INotifyObject notifyObject)
        {
            NotifyComResponse notifyComResponse = new NotifyComResponse();

            LogBook.Write("Executing Invoke method");
            RelaySwitch relaySwitch = null;

            try
            {
                //set the configuration, get the relay switch object
                relaySwitch = GetRelaySwitchObject(notifyObject);
                //call the send data method

                notifyComResponse.IsSucceeded = true;

                if (notifyObject.NotifierSettings["IsDynamicNotificationCleared"].ToBoolean())
                {
                    relaySwitch.ClearData(notifyObject.NotifierSettings["SensorAlarmID"].ToStr());
                    notifyComResponse.ResponseContent = "Clearing notification from " + relaySwitch.ResponseContent;
                }
                else
                {
                    relaySwitch.SendData();

                    notifyComResponse.ResponseContent = "Notification to [" + notifyObject.NotifierSettings["Name"].ToStr() + "] " + (notifyObject.NotifierSettings["SwitchInfo"].ToStr().Contains("NET") == true ? "IP:" : " ") + notifyObject.NotifierSettings["SwitchInfo"].ToStr().Replace("NET:", "") + ", Current state " + relaySwitch.CurrentState + ", New State:" + relaySwitch.NewState + ", sent successfully.";
                }
            }
            catch (Exception ex)
            {
                notifyComResponse             = new NotifyComResponse();
                notifyComResponse.IsSucceeded = false;
                notifyComResponse.IsError     = true;
                if (notifyObject.NotifierSettings["IsDynamicNotificationCleared"].ToBoolean())
                {
                    notifyComResponse.ResponseContent = "Clearing notification failed for switch(s), " + relaySwitch.ResponseContent;
                }
                else
                {
                    notifyComResponse.ResponseContent = "Notification to [" + notifyObject.NotifierSettings["Name"].ToStr() + "] " + (notifyObject.NotifierSettings["SwitchInfo"].ToStr().Contains("NET") == true ? "IP:" : "COM") + notifyObject.NotifierSettings["SwitchInfo"].ToStr().Replace("NET:", "") + ", Failed.";
                }

                LogBook.Write(ex, "NotifyEngine-SwicthNotifyCom");
            }
            return(notifyComResponse);
        }
        private RelaySwitch GetRelaySwitchObject(INotifyObject notifyObject)
        {
            //create relay switch object
            RelaySwitch relaySwitch = new RelaySwitch();

            try
            {
                //populate relay the switch settings from notifier settings hashtable
                string[] switchInfo = notifyObject.NotifierSettings["SwitchInfo"].ToStr().Split(':');

                if (switchInfo.Length == 0)
                {
                    return(relaySwitch);
                }

                //set the LPT/COM/Net settings for the switch
                switch (switchInfo[0].ToLower())
                {
                case "lpt1":
                    relaySwitch.IoAddress = (int)0x378;
                    break;

                case "lpt2":
                    relaySwitch.IoAddress = (int)0x278;
                    break;

                case "lpt3":
                    relaySwitch.IoAddress = (int)0x3BC;
                    break;

                case "net":
                    relaySwitch.IsNetworkAttached = true;
                    relaySwitch.IpAddress         = switchInfo[1];
                    relaySwitch.IpPort            = switchInfo[2].ToInt();
                    relaySwitch.IoAddress         = 0;
                    break;

                default:
                    int i = 0;
                    if (int.TryParse(switchInfo[0], out i))
                    {
                        relaySwitch.IoAddress = switchInfo[0].ToInt();
                    }
                    else
                    {
                        if (switchInfo[0].Substring(0, 3) == "COM")
                        {
                            relaySwitch.ComPort              = switchInfo[0];
                            relaySwitch.ComSettings          = new SocketManager.IOPort.ComSettings();
                            relaySwitch.ComSettings.BaudRate = Convert.ToInt32(switchInfo[1].Split(',')[0]);
                            relaySwitch.ComSettings.DataBits = Convert.ToInt32(switchInfo[1].Split(',')[2]);
                            //switch comm settings 'n' represents even parity, in the database it is stored as 'N'
                            if (switchInfo[1].Split(',')[1].ToLower() == "n")
                            {
                                relaySwitch.ComSettings.ParityBit = System.IO.Ports.Parity.Even;
                            }
                            //relaySwitch.ComSettings.ParityBit = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), switchInfo[1].Split(',')[1]);
                            relaySwitch.ComSettings.StopBit = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), switchInfo[1].Split(',')[3]);
                        }
                        else
                        {
                            LogBook.Write("Error loading switch" + notifyObject.NotifierSettings["SwitchInfo"]);
                            return(relaySwitch);
                        }
                    }
                    break;
                }
                if (notifyObject.NotifierSettings["SensorAlarmID"] != null)
                {
                    relaySwitch.SensorAlarmID = notifyObject.NotifierSettings["SensorAlarmID"].ToString();
                }

                if (notifyObject.NotifierSettings["FactoryID"] != null)
                {
                    relaySwitch.FactoryID = notifyObject.NotifierSettings["FactoryID"].ToString();
                }

                if (notifyObject.NotifierSettings["IsEnabled"] != null)
                {
                    relaySwitch.IsEnabled = notifyObject.NotifierSettings["IsEnabled"].ToBoolean();
                }
                if (notifyObject.NotifierSettings["Name"] != null)
                {
                    relaySwitch.SwitchName = notifyObject.NotifierSettings["Name"].ToString();
                }

                relaySwitch.SwitchBitMask = Convert.ToInt16(notifyObject.NotificationData);
                relaySwitch.LastValue     = GetSwitchValue(Convert.ToInt16(notifyObject.NotificationData));

                if (notifyObject.NotifierSettings["IsDynamicNotificationCleared"] != null)
                {
                    relaySwitch.IsDynamicNotificationCleared = notifyObject.NotifierSettings["IsDynamicNotificationCleared"].ToBoolean();
                }
                else
                {
                    relaySwitch.IsDynamicNotificationCleared = null;
                }
            }
            catch (Exception ex)
            {
                LogBook.Write("  *** Error in GetRelaySwitchObject method : " + ex.Message);
            }
            return(relaySwitch);
        }