internal void AssignQueuedInteraction(SwitchInteraction interaction)
        {
            if (interaction == null)
            {
                return;
            }

            Func <Device, bool> predicate = null;

            if (interaction.Type == InteractionType.Call)
            {
                predicate = d => (d.Agent.State == SwitchAgentState.Available || d.Agent.State == SwitchAgentState.Busy || d.Agent.State == SwitchAgentState.WrapUp) && !d.Agent.Interactions.Any(i => i.Type == InteractionType.Call);
            }
            else
            {
                predicate = d => d.Agent.State == SwitchAgentState.Available;
            }

            var device = Devices.Where(predicate)
                         .OrderBy(d => d.Agent.LastStateChange)
                         .FirstOrDefault();

            if (device != null)
            {
                AssignInteraction(device, interaction);
            }
        }
        public void AssignInteraction(Device device, SwitchInteraction interaction = null)
        {
            if (interaction == null)
            {
                Func <SwitchInteraction, bool> predicate = null;

                if (device.Agent.Interactions.Any(i => i.Type == InteractionType.Call))
                {
                    predicate = i => i.State == SwitchInteractionState.Queued && i.Type != InteractionType.Call;
                }
                else
                {
                    predicate = i => i.State == SwitchInteractionState.Queued;
                }

                interaction = Interactions.Where(predicate)
                              .OrderBy(i => i.LastStateChange)
                              .FirstOrDefault();
            }

            if (interaction != null)
            {
                interaction.Agent = device.Agent;
                device.Agent.Interactions.Add(interaction);
                // Send the interaction
                SendMessage(device, new InteractionMessage()
                {
                    Interaction = interaction,
                    Action      = InteractionMessageAction.Created
                }, false);

                interaction.State = interaction.Type == InteractionType.Call
                    ? SwitchInteractionState.Offered
                    : SwitchInteractionState.Active;

                SendMessage(device, new InteractionMessage {
                    Interaction = interaction,
                    Action      = InteractionMessageAction.StateChanged
                });

                if (interaction.State == SwitchInteractionState.Active)
                {
                    SendMessage(device, new AgentStateMessage {
                        AgentId  = device.Agent.Id,
                        DeviceId = device.Id,
                        State    = SwitchAgentState.HandlingInteraction,
                    });
                }
            }
        }
        public void QueueInteraction(SwitchInteraction interaction)
        {
            interaction.State = SwitchInteractionState.Queued;
            Interactions.Add(interaction);

            var message = new InteractionMessage {
                Interaction = interaction,
                Action      = InteractionMessageAction.Queued
            };

            Parallel.ForEach(GlobalSubscribers, d => _messagingHandler.SendMessage(d, message));

            AssignQueuedInteraction(interaction);
        }
        private void retrieveIncidents(object state)
        {
            IList <IncidentInfo> incidents = _objectProvider.GetPendingIncidents(_lastPollingTime);

            foreach (var incident in incidents)
            {
                var interaction = new SwitchInteraction {
                    Id = Guid.NewGuid(),
                    LastStateChange = DateTime.Now,
                    Queue           = incident.QueueName,
                    ReferenceId     = incident.Id.ToString(),
                    SourceAddress   = incident.ContactEmail,
                    Type            = getInteractionType(incident.Channel)
                };
                Switch.QueueInteraction(interaction);
            }

            if (incidents.Count > 0)
            {
                _lastPollingTime = incidents.Max(i => i.LastUpdate).ToUniversalTime();
            }
        }