/// <summary>
        /// Creates the binfing for a connection. 
        /// </summary>
        /// <param name="config">Contains the binding configuration parameter</param>
        /// <returns>Binding; Returns the newly created binding</returns>
        private Binding CreateCustomBinding(com.EwsBindingConfig config)
        {
            HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
            httpBinding.AuthenticationScheme = config.AuthenticationScheme;
            httpBinding.AllowCookies = config.AllowCookies;
            httpBinding.DecompressionEnabled = true;
            httpBinding.KeepAliveEnabled = true;

            //httpBinding.UseDefaultWebProxy = false;
            //httpBinding.BypassProxyOnLocal = true;
            httpBinding.MaxBufferPoolSize = config.MaxBufferPoolSize;
            httpBinding.MaxBufferSize = config.MaxBufferSize;
            httpBinding.MaxReceivedMessageSize = config.MaxReceivedMessageSize;

            TextMessageEncodingBindingElement txtMsgEncoding = new TextMessageEncodingBindingElement();
            txtMsgEncoding.MessageVersion = config.MessageVersion; // default Soap12
            txtMsgEncoding.WriteEncoding = Encoding.UTF8;

            CustomBinding binding = new CustomBinding(txtMsgEncoding, httpBinding);

            binding.SendTimeout = TimeSpan.FromSeconds(config.SendTimeout);
            binding.OpenTimeout = TimeSpan.FromSeconds(config.OpenTimeout);
            binding.ReceiveTimeout = TimeSpan.FromSeconds(config.ReceiveTimeout);
            binding.CloseTimeout = TimeSpan.FromSeconds(config.CloseTimeout);

            return binding;
        }
        /// <summary>
        /// Read updated alarms form a server 
        /// </summary>
        /// <param name="connectRef">Reference to the server</param>
        /// <param name="param">parameter to get next table of the alarmlist</param>
        /// <param name="filter">Specified the some filter parameters for alarms</param>
        /// <param name="statusResp"> Return paramater for the status</param>
        /// <param name="alarmList"> Return paramater about the list of alarms</param>
        /// <returns>Returns status/error code</returns>
        public int GetUpdatedAlarmEvents(string connectRef, com.UpdatedAlarmEventsParameter param, com.AlarmEventsFilter filter, out com.AlarmResponseStatusType statusResp, out List<com.AlarmEventsType> alarmList)
        {
            int status = (int)EwsStatus.OK;
            statusResp = new com.AlarmResponseStatusType();
            alarmList = new List<com.AlarmEventsType>();

            // Communicate with server
            var response = _connectionList[connectRef].Client.GetUpdatedAlarmEvents(new ews.GetUpdatedAlarmEventsRequest()
            {
                GetUpdatedAlarmEventsFilter = new ews.GetUpdatedAlarmEventsRequestGetUpdatedAlarmEventsFilter()
                {
                    PriorityFrom = filter.PriorityFrom.ToString(),
                    PriorityTo = filter.PriorityTo.ToString(),
                    Types = filter.Types
                },
                GetUpdatedAlarmEventsParameter = new ews.GetUpdatedAlarmEventsRequestGetUpdatedAlarmEventsParameter()
                {
                    LastUpdate =  param.LastUpdate,
                    MoreDataRef = param.MoreDataRef
                }
            });

            if (response != null)
            {
                // Copy data from EWS structures in common structures of the host
                statusResp.MoreDataAvailable = response.GetUpdatedAlarmEventsResponseStatus.MoreDataAvailable;
                statusResp.MoreDataRef = response.GetUpdatedAlarmEventsResponseStatus.MoreDataRef;
                statusResp.LastUpdate = response.GetUpdatedAlarmEventsResponseStatus.LastUpdate;
                statusResp.NeedsRefresh = response.GetUpdatedAlarmEventsResponseStatus.NeedsRefresh;

                foreach (ews.AlarmEventsType alarm in response.GetUpdatedAlarmEventsAlarmEvents)
                {
                    alarmList.Add(new com.AlarmEventsType()
                    {
                        Acknowledgeable = Conversions.BooleanToInt(alarm.Acknowledgeable, _connectionList[connectRef].Tolerance.StrictContentProcessing, alarm.ID),
                        ID = alarm.ID,
                        Message = alarm.Message,
                        Priority = Int32.Parse(alarm.Priority),
                        SourceID = alarm.SourceID,
                        SourceName = alarm.SourceName,
                        State = Int32.Parse(alarm.State),
                        TimeStampOccurrence = alarm.TimeStampOccurrence,
                        TimeStampTransition = alarm.TimeStampTransition,
                        Type = alarm.Type
                    });
                }
            }
            else
            {
                status = (int)EwsStatus.ERROR_COMMUNICATION;
            }

            return status;
        }
        /// <summary>
        /// Gets WebService Information from server. 
        /// </summary>
        /// <param name="connectRef">Reference to the server</param>
        /// <param name="info">List of item Ids to read their values of</param>
        /// <returns>Returns status/error code</returns>
        public int GetWebServiceInformation(string connectRef, out com.EwsServerInfo info)
        {
            int status = (int)EwsStatus.OK;
            List<string> messages = new List<string>();

            info = new com.EwsServerInfo();

            // Get connection instance
            EwsConnection connection = _connectionList[connectRef];
            ews.DataExchangeClient client = connection.Client;

            try
            {
                // Communicate with server
                var response = client.GetWebServiceInformation(new ews.GetWebServiceInformationRequest());

                info.MajorVersion = response.GetMajorVersion();
                info.MinorVersion = response.GetMinorVersion();
                info.TargetNamespace = response.GetUsedNameSpace();
                info.EndpointAddress = connection.Server.EndpointAddress;
                info.SetSupportedOperations(response.GetSupportedOperations());
            }
            catch (Exception ex)
            {
                status = (int)EwsStatus.ERROR_COMMUNICATION;
                // TODO: Log error message
            }

            return status;
        }
        /// <summary>
        /// Read history of an item 
        /// </summary>
        /// <param name="connectRef">Reference to the server</param>
        /// <param name="param">id of the item to read the history</param>
        /// <param name="filter">Specified the time range of the history</param>
        /// <param name="statusResp"> Return parameter for the status</param>
        /// <param name="history"> Return parameter for the history</param>
        /// <returns>Returns status/error code</returns>
        public int GetHistory(string connectRef, com.HistoryParameter param, com.HistoryFilter filter, out com.HistoryResponseStatusType statusResp, out com.HistoryRecordsType history)
        {
            int status = (int)EwsStatus.OK;
            statusResp =  new com.HistoryResponseStatusType();
            history = new com.HistoryRecordsType();

            // Check input parameter

            if(filter.TimeFrom == null && filter.TimeTo == null)
            {
                filter.TimeFromSpecified =  false;
                filter.TimeToSpecified =  false;
            }
            else if(filter.TimeFrom != null && filter.TimeTo != null)
            {
                filter.TimeFromSpecified =  true;
                filter.TimeToSpecified =  true;
            }
            else
            {
                return (int)EwsStatus.ERROR_PARAMETER;
            }

            if(param.Id == null && param.MoreDataRef == null)
                return (int)EwsStatus.ERROR_PARAMETER;

            // Communicate with server
            var response = _connectionList[connectRef].Client.GetHistory(new ews.GetHistoryRequest()
            {
                GetHistoryParameter = new ews.GetHistoryRequestGetHistoryParameter()
                {
                    Id = param.Id,
                    MoreDataRef = param.MoreDataRef
                },
                GetHistoryFilter = new ews.GetHistoryRequestGetHistoryFilter()
                {
                    TimeFrom = filter.TimeFrom,
                    TimeFromSpecified = filter.TimeFromSpecified,
                    TimeTo = filter.TimeTo,
                    TimeToSpecified = filter.TimeToSpecified
                }
            });

            if (response != null)
            {
                // Copy data from EWS structures in common structures of the host
                statusResp.MoreDataAvailable = response.GetHistoryResponseStatus.MoreDataAvailable;
                statusResp.MoreDataRef = response.GetHistoryResponseStatus.MoreDataRef;
                statusResp.TimeFrom = response.GetHistoryResponseStatus.TimeFrom;
                statusResp.TimeTo = response.GetHistoryResponseStatus.TimeTo;

                history.Type = response.GetHistoryHistoryRecords.Type;
                history.Unit = response.GetHistoryHistoryRecords.Unit;
                history.ValueItemId = response.GetHistoryHistoryRecords.ValueItemId;
                history.List = new List<com.HistoryRecordType>(); ;
                foreach (ews.HistoryRecordType record in response.GetHistoryHistoryRecords.List)
                {
                    history.List.Add(new com.HistoryRecordType()
                    {
                        State = Int32.Parse(record.State),
                        TimeStamp = record.TimeStamp,
                        Value = record.Value
                    });
                }
            }
            else
            {
                status = (int)EwsStatus.ERROR_COMMUNICATION;
            }

            return status;
        }
        /// <summary>
        /// Gets items from server. 
        /// </summary>
        /// <param name="connectRef">Reference to the server</param>
        /// <param name="itemIds">List of item Ids to read</param>
        /// <param name="itemList">Returns parameter for the read items</param>
        /// <param name="errorList">Returns parameter for the read error list</param>
        /// <returns>int, Returns status/error code</returns>
        public int GetItems(string connectRef, List<string> itemIds, out com.ArrayOfItemType itemList, out List<com.ErrorResultType> errorList)
        {
            int status = (int)EwsStatus.OK;
            itemList = new com.ArrayOfItemType();
            errorList = new List<com.ErrorResultType>();

            // Communicate with server
            var response = _connectionList[connectRef].Client.GetItems(new ews.GetItemsRequest()
            {
                GetItemsIds = itemIds
            });

            if (response != null)
            {
                // Copy data from ews structures in common structures of the host
                foreach (ews.ErrorResultType error in response.GetItemsErrorResults)
                {
                    errorList.Add(new com.ErrorResultType()
                    {
                        Id = error.Id,
                        Message = error.Message
                    });
                }

                // Copy data from ews structures in common structures of the host
                itemList.AlarmItems = new List<com.AlarmItemType>();
                foreach (var entry in response.GetItemsItems.AlarmItems)
                {
                    itemList.AlarmItems.Add(new com.AlarmItemType()
                    {
                        Id = entry.Id,
                        Name = entry.Name,
                        Description = entry.Description,
                        State = Conversions.BooleanToInt(entry.State, _connectionList[connectRef].Tolerance.StrictContentProcessing, entry.Id),
                        ValueItemId = entry.ValueItemId
                    });
                }

                itemList.HistoryItems = new List<com.HistoryItemType>();
                foreach (var entry in response.GetItemsItems.HistoryItems)
                {
                    itemList.HistoryItems.Add(new com.HistoryItemType()
                    {
                        Id = entry.Id,
                        Name = entry.Name,
                        Description = entry.Description,
                        Type = entry.Type,
                        Unit = entry.Unit,
                        ValueItemId = entry.ValueItemId
                    });
                }

                itemList.ValueItems = new List<com.ValueItemType>();
                foreach (var entry in response.GetItemsItems.ValueItems)
                {
                    itemList.ValueItems.Add(new com.ValueItemType()
                    {
                        Id = entry.Id,
                        Name = entry.Name,
                        Description = entry.Description,
                        State = Conversions.BooleanToInt(entry.State, _connectionList[connectRef].Tolerance.StrictContentProcessing, entry.Id),
                        Type = entry.Type,
                        Unit = entry.Unit,
                        Value = entry.Value,
                        Writeable = Int32.Parse(entry.Writeable)
                    });
                }
            }
            else
            {
                status = (int)EwsStatus.ERROR_COMMUNICATION;
            }
            return status;
        }
        /// <summary>
        /// Get information about a connected server (internal stored data).
        /// </summary>
        /// <param name="connectRef">The connect ref.</param>
        /// <param name="server">Returns info about the connected server</param>
        /// <returns>Returns status/error code</returns>
        public int ConnectionServerInfo(string connectRef, out com.EwsServerInfo server)
        {
            int status = (int)EwsStatus.OK;

            // Get item from connection list
            EwsConnection connection = _connectionList[connectRef];
            server = connection.Server;

            return status;
        }
        /// <summary>
        /// Get information about a connection binding.
        /// </summary>
        /// <param name="connectRef">The connect ref.</param>
        /// <param name="binding">Returns info about the binding</param>
        /// <returns>Returns status/error code</returns>
        public int ConnectionBindingInfo(string connectRef, out com.EwsBindingConfig binding)
        {
            int status = (int)EwsStatus.OK;

            // Get item from connection list
            EwsConnection connection = _connectionList[connectRef];
            binding = connection.Binding;

            return status;
        }
        /// <summary>
        /// Connect to the server with some content tolerance. 
        /// </summary>
        /// <param name="address">Address of the server</param>
        /// <param name="security">Security information for the connection</param>
        /// <param name="config">Binding configuration information for the connection</param>
        /// <param name="tolerance">Tolerance conditions for the connection</param>
        /// <param name="connectRef">Returns reference id of the new connection</param>
        /// <returns>int ; Returns status/error code</returns>
        public int ConnectEx(string address, com.EwsSecurity security, com.EwsBindingConfig config, com.EwsContentTolerance tolerance, out string connectRef)
        {
            EwsConnection connection = new EwsConnection();
            connectRef = null;

            ews.DataExchangeClient client = null;
            config.AuthenticationScheme = security.AuthenticationScheme;
            var binding = CreateCustomBinding(config);
            client = new ews.DataExchangeClient(binding, new EndpointAddress(address));

            // Set security parameter for the connection
            if (security.AuthenticationScheme == System.Net.AuthenticationSchemes.Basic)
            {
                client.ClientCredentials.UserName.UserName = security.Username;
                client.ClientCredentials.UserName.Password = security.Password;
            }
            else if (security.AuthenticationScheme == System.Net.AuthenticationSchemes.Digest)
            {
                client.ClientCredentials.HttpDigest.ClientCredential = new System.Net.NetworkCredential(security.Username, security.Password);
                client.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            }

            // Check if there is some dependencies conecning the rules of the EWS message content
            if (tolerance != null)
            {
                // MessageInspector for forcing the namespace of the response
                if (tolerance.IgnoreNamespace)
                    client.Endpoint.Behaviors.Add(new RewriteNamespaceBehavior(config.MessageVersion));
            }
            else
            {
                tolerance = new com.EwsContentTolerance()
                {
                    IgnoreNamespace =  false,
                    StrictContentProcessing = false
                };
            }

            // Check if server is online and get the information about the server
            var response = client.GetWebServiceInformation(new ews.GetWebServiceInformationRequest());

            if (response != null && response.GetMajorVersion() != "Unknown")
            {
                // Set server information
                connection.Server = new com.EwsServerInfo();
                connection.Server.EndpointAddress = address;
                connection.Server.MajorVersion = response.GetMajorVersion();
                connection.Server.MinorVersion = response.GetMinorVersion();
                connection.Server.TargetNamespace = response.GetUsedNameSpace();
                connection.Server.SetSupportedOperations(response.GetSupportedOperations());

                // Set connection parameters
                connection.Security = security;
                connection.Binding = config;
                connection.Client = client;
                connection.Tolerance = tolerance;

                // Create reference id and add the connection to the list
                connectRef = Guid.NewGuid().ToString();
                connection.ConnectReference = connectRef;
                _connectionList.Add(connectRef, connection);
            }
            else
            {
                return (int)EwsStatus.ERROR_SERVER_NOT_AVAILABLE;
            }

            return (int)EwsStatus.OK;
        }
 /// <summary>
 /// Connect to a server. 
 /// </summary>
 /// <param name="address">Address of the server</param>
 /// <param name="security">Security information for the connection</param>
 /// <param name="config">Binding configuration information for the connection</param>
 /// <param name="connectRef">Returns reference id of the new c onnection</param>
 /// <returns>int ; Returns status/error code</returns>
 public int ConnectEx(string address, com.EwsSecurity security, com.EwsBindingConfig config, out string connectRef)
 {
     int status = (int)EwsStatus.OK;
     status = ConnectEx(address, security, config, null, out connectRef);
     return status;
 }
示例#10
0
        /// <summary>
        /// Connect with default communication values. 
        /// </summary>
        /// <param name="address">Address of the server</param>
        /// <param name="security">Security information for the connection</param>
        /// <param name="connectRef">Returns reference id of the new connection</param>
        /// <returns>Returns status/error code</returns>
        public int ConnectEx(string address, com.EwsSecurity security, out string connectRef)
        {
            int status = (int)EwsStatus.OK;

            // Set default values for connection
            com.EwsBindingConfig config = new com.EwsBindingConfig()
            {
                MessageVersion = MessageVersion.Soap12,
                AllowCookies = true,
                AuthenticationScheme = AuthenticationSchemes.Anonymous,

                MaxBufferPoolSize = 524288,
                MaxBufferSize = 65536,
                MaxReceivedMessageSize = 65536,

                OpenTimeout = 10,
                ReceiveTimeout = 10,
                SendTimeout = 10,
                CloseTimeout = 10
            };

            // Call main connect method
            status = ConnectEx(address, security, config, out connectRef);

            return status;
        }