Exemple #1
0
        public async Task <bool> Process(CallQueueItem item)
        {
            bool success = true;

            if (Config.SystemBehaviorConfig.IsAzure)
            {
                //ProcessQueueMessage(_client.Receive());

                var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
                {
                    MaxConcurrentCalls = 1,
                    AutoComplete       = false
                };

                // Register the function that will process messages
                _client.RegisterMessageHandler(ProcessQueueMessage, messageHandlerOptions);
            }
            else
            {
                return(await ProcessCallQueueItem(item));
            }

            _queueService = null;

            return(false);
        }
        public void EnqueueCall(CallQueueItem callQueue)
        {
            string serializedObject = String.Empty;

            try
            {
                serializedObject = ObjectSerialization.Serialize(callQueue);

                // We are limited to 256KB in azure queue messages
                var size = ASCIIEncoding.Unicode.GetByteCount(serializedObject);
                if (size > 220000)
                {
                    callQueue.Profiles = null;
                    serializedObject   = ObjectSerialization.Serialize(callQueue);
                }
            }
            catch { }

            // If we get an Exception, i.e. OutOfMemmory, lets just strip out the heavy data and try.
            if (String.IsNullOrWhiteSpace(serializedObject))
            {
                callQueue.Profiles = null;
                serializedObject   = ObjectSerialization.Serialize(callQueue);
            }

            SendMessage(ServiceBusConfig.CallBroadcastQueueName, serializedObject);
        }
        private async Task OnCallQueueReceived(CallQueueItem cqi)
        {
            _logger.LogInformation($"{Name}: Call Queue Received with a number of {cqi.Call.Number}, starting processing...");
            await BroadcastCallLogic.ProcessCallQueueItem(cqi);

            _logger.LogInformation($"{Name}: Finished processing of call {cqi.Call.Number}.");
        }
Exemple #4
0
        public void Process(CallQueueItem item)
        {
            bool success = true;

            if (Config.SystemBehaviorConfig.IsAzure)
            {
                ProcessQueueMessage(_client.Receive());
            }
            else
            {
                ProcessCallQueueItem(item);
            }

            _queueService = null;
        }
Exemple #5
0
        public void EnqueueCall(CallQueueItem callQueue)
        {
            if (SystemBehaviorConfig.ServiceBusType == ServiceBusTypes.Rabbit)
            {
                _rabbitOutboundQueueProvider.EnqueueCall(callQueue);
                return;
            }

            VerifyAndCreateClients();
            string serializedObject = String.Empty;

            try
            {
                serializedObject = ObjectSerialization.Serialize(callQueue);

                // We are limited to 256KB in azure queue messages
                var size = ASCIIEncoding.Unicode.GetByteCount(serializedObject);
                if (size > 220000)
                {
                    callQueue.Profiles = null;
                    serializedObject   = ObjectSerialization.Serialize(callQueue);
                }
            }
            catch { }

            // If we get an Exception, i.e. OutOfMemmory, lets just strip out the heavy data and try.
            if (String.IsNullOrWhiteSpace(serializedObject))
            {
                callQueue.Profiles = null;
                serializedObject   = ObjectSerialization.Serialize(callQueue);
            }

            BrokeredMessage message = new BrokeredMessage(serializedObject);

            message.MessageId = string.Format("{0}|{1}", callQueue.Call.CallId, callQueue.Call.DispatchCount);

            SendMessage(_callClient, message);
        }
Exemple #6
0
        public async Task <bool> EnqueueCallBroadcastAsync(CallQueueItem cqi, CancellationToken cancellationToken = default(CancellationToken))
        {
            //if (Config.SystemBehaviorConfig.IsAzure)
            //{
            // If we have geolocation data, lets get the approx address now.
            if (!string.IsNullOrEmpty(cqi.Call.GeoLocationData) && String.IsNullOrWhiteSpace(cqi.Call.Address))
            {
                try
                {
                    string[] points = cqi.Call.GeoLocationData.Split(char.Parse(","));

                    if (points != null && points.Length == 2)
                    {
                        cqi.Address = await _geoLocationProvider.GetAproxAddressFromLatLong(double.Parse(points[0]), double.Parse(points[1]));
                    }
                }
                catch { /* Ignore */ }
            }
            else
            {
                cqi.Address = cqi.Call.Address;
            }

            //if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any())
            //{
            //	foreach (var dispatch in cqi.Call.Dispatches)
            //	{
            //		if (dispatch.User == null)
            //		{
            //			var user = cqi.Profiles.FirstOrDefault(x => x.UserId == dispatch.UserId);
            //		}
            //	}
            //}

            if (cqi.Call.Attachments != null &&
                cqi.Call.Attachments.Count(x => x.CallAttachmentType == (int)CallAttachmentTypes.DispatchAudio) > 0)
            {
                var audio = cqi.Call.Attachments.FirstOrDefault(x =>
                                                                x.CallAttachmentType == (int)CallAttachmentTypes.DispatchAudio);

                if (audio != null)
                {
                    cqi.CallDispatchAttachmentId = audio.CallAttachmentId;
                }
            }

            // We can't queue up any attachment data as it'll be too large.
            cqi.Call.Attachments = null;

            return(await _outboundQueueProvider.EnqueueCall(cqi));

            //}
            //else
            //{
            //	QueueItem item = new QueueItem();
            //	item.QueueType = (int)QueueTypes.CallBroadcast;
            //	item.SourceId = cqi.Call.CallId.ToString();
            //	item.QueuedOn = DateTime.UtcNow;

            //	await _queueItemsRepository.SaveOrUpdateAsync(item, cancellationToken);
            //}

            //return true;
        }
Exemple #7
0
        public void EnqueueCallBroadcast(CallQueueItem cqi)
        {
            if (Config.SystemBehaviorConfig.IsAzure)
            {
                // If we have geolocation data, lets get the approx address now.
                if (!string.IsNullOrEmpty(cqi.Call.GeoLocationData) && String.IsNullOrWhiteSpace(cqi.Call.Address))
                {
                    try
                    {
                        string[] points = cqi.Call.GeoLocationData.Split(char.Parse(","));

                        if (points != null && points.Length == 2)
                        {
                            cqi.Address = _geoLocationProvider.GetAproxAddressFromLatLong(double.Parse(points[0]), double.Parse(points[1]));
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    cqi.Address = cqi.Call.Address;
                }

                if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any())
                {
                    foreach (var dispatch in cqi.Call.Dispatches)
                    {
                        if (dispatch.User == null)
                        {
                            var user = cqi.Profiles.FirstOrDefault(x => x.UserId == dispatch.UserId);
                        }
                    }
                }

                if (cqi.Call.Attachments != null &&
                    cqi.Call.Attachments.Count(x => x.CallAttachmentType == (int)CallAttachmentTypes.DispatchAudio) > 0)
                {
                    var audio = cqi.Call.Attachments.FirstOrDefault(x =>
                                                                    x.CallAttachmentType == (int)CallAttachmentTypes.DispatchAudio);

                    if (audio != null)
                    {
                        cqi.CallDispatchAttachmentId = audio.CallAttachmentId;
                    }
                }

                // We can't queue up any attachment data as it'll be too large.
                cqi.Call.Attachments = null;

                _outboundQueueProvider.EnqueueCall(cqi);
            }
            else
            {
                QueueItem item = new QueueItem();
                item.QueueType = (int)QueueTypes.CallBroadcast;
                item.SourceId  = cqi.Call.CallId.ToString();
                item.QueuedOn  = DateTime.UtcNow;

                _queueItemsRepository.SaveOrUpdate(item);
            }
        }
Exemple #8
0
        /// <summary>
        /// Adds a new call into Resgrid and Dispatches the call
        /// </summary>
        /// <param name="callInput">Call data to add into the system</param>
        /// <returns></returns>
        public async Task <AddCallInput> AddCall([FromBody] AddCallInput callInput)
        {
            try
            {
                var call = new Call
                {
                    DepartmentId     = DepartmentId,
                    ReportingUserId  = UserId,
                    Priority         = callInput.Priority,
                    Name             = callInput.Name,
                    NatureOfCall     = callInput.NatureOfCall,
                    Number           = callInput.Number,
                    IsCritical       = callInput.IsCritical,
                    IncidentNumber   = callInput.IncidentNumber,
                    MapPage          = callInput.MapPage,
                    Notes            = callInput.Notes,
                    CompletedNotes   = callInput.CompletedNotes,
                    Address          = callInput.Address,
                    GeoLocationData  = callInput.GeoLocationData,
                    LoggedOn         = callInput.LoggedOn,
                    ClosedByUserId   = callInput.ClosedByUserId,
                    ClosedOn         = callInput.ClosedOn,
                    State            = callInput.State,
                    IsDeleted        = callInput.IsDeleted,
                    CallSource       = callInput.CallSource,
                    DispatchCount    = callInput.DispatchCount,
                    LastDispatchedOn = callInput.LastDispatchedOn,
                    SourceIdentifier = callInput.SourceIdentifier,
                    W3W                = callInput.W3W,
                    ContactName        = callInput.ContactName,
                    ContactNumber      = callInput.ContactNumber,
                    Public             = callInput.Public,
                    ExternalIdentifier = callInput.ExternalIdentifier,
                    ReferenceNumber    = callInput.ReferenceNumber
                };

                if (string.IsNullOrWhiteSpace(call.GeoLocationData) && !string.IsNullOrWhiteSpace(call.Address))
                {
                    call.GeoLocationData = _geoLocationProvider.GetLatLonFromAddress(call.Address);
                }

                if (string.IsNullOrWhiteSpace(call.GeoLocationData) && !string.IsNullOrWhiteSpace(call.W3W))
                {
                    var coords = await _geoLocationProvider.GetCoordinatesFromW3WAsync(call.W3W);

                    if (coords != null)
                    {
                        call.GeoLocationData = $"{coords.Latitude},{coords.Longitude}";
                    }
                }

                call.LoggedOn = DateTime.UtcNow;

                if (!String.IsNullOrWhiteSpace(callInput.Type) && callInput.Type != "No Type")
                {
                    var callTypes = await _callsService.GetCallTypesForDepartmentAsync(DepartmentId);

                    var type = callTypes.FirstOrDefault(x => x.Type == callInput.Type);

                    if (type != null)
                    {
                        call.Type = type.Type;
                    }
                }

                call.Dispatches      = new List <CallDispatch>();
                call.GroupDispatches = new List <CallDispatchGroup>();
                call.RoleDispatches  = new List <CallDispatchRole>();

                List <string> groupUserIds = new List <string>();

                var users = await _departmentsService.GetAllUsersForDepartmentAsync(DepartmentId);

                if (callInput.AllCall)
                {
                    foreach (var u in users)
                    {
                        var cd = new CallDispatch {
                            UserId = u.UserId
                        };

                        call.Dispatches.Add(cd);
                    }
                }
                else
                {
                    if (callInput.GroupCodesToDispatch != null && callInput.GroupCodesToDispatch.Count > 0)
                    {
                        var allGroups = await _departmentGroupsService.GetAllGroupsForDepartmentAsync(DepartmentId);

                        foreach (var groupCode in callInput.GroupCodesToDispatch)
                        {
                            var groupsToDispatch = allGroups.FirstOrDefault(x => x.DispatchEmail == groupCode);

                            if (groupsToDispatch != null)
                            {
                                var cd = new CallDispatchGroup {
                                    DepartmentGroupId = groupsToDispatch.DepartmentGroupId
                                };
                                call.GroupDispatches.Add(cd);

                                if (groupsToDispatch.Members != null && groupsToDispatch.Members.Any())
                                {
                                    foreach (var departmentGroupMember in groupsToDispatch.Members)
                                    {
                                        if (!groupUserIds.Contains(departmentGroupMember.UserId))
                                        {
                                            groupUserIds.Add(departmentGroupMember.UserId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (callInput.Attachments != null && callInput.Attachments.Any())
                {
                    call.Attachments = new List <CallAttachment>();

                    foreach (var attachment in callInput.Attachments)
                    {
                        var newAttachment = new CallAttachment();
                        newAttachment.Data               = attachment.Data;
                        newAttachment.Timestamp          = DateTime.UtcNow;
                        newAttachment.FileName           = attachment.FileName;
                        newAttachment.Size               = attachment.Size;
                        newAttachment.CallAttachmentType = attachment.CallAttachmentType;

                        call.Attachments.Add(newAttachment);
                    }
                }

                var savedCall = _callsService.SaveCall(call);


                OutboundEventProvider.CallAddedTopicHandler handler = new OutboundEventProvider.CallAddedTopicHandler();
                handler.Handle(new CallAddedEvent()
                {
                    DepartmentId = DepartmentId, Call = savedCall
                });

                var cqi = new CallQueueItem();
                cqi.Call = savedCall;

                if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any())
                {
                    cqi.Profiles =
                        await _userProfileService.GetSelectedUserProfilesAsync(cqi.Call.Dispatches.Select(x => x.UserId).ToList());
                }
                else
                {
                    if (groupUserIds.Any())
                    {
                        cqi.Profiles = await _userProfileService.GetSelectedUserProfilesAsync(groupUserIds);
                    }
                }

                _queueService.EnqueueCallBroadcast(cqi);

                callInput.CallId = savedCall.CallId;
                callInput.Number = savedCall.Number;
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);

                throw ex;
            }

            return(callInput);
        }
Exemple #9
0
        public async Task <ActionResult> IncomingMessage([FromQuery] TwilioMessage request)
        {
            if (request == null || string.IsNullOrWhiteSpace(request.To) || string.IsNullOrWhiteSpace(request.From) || string.IsNullOrWhiteSpace(request.Body))
            {
                return(BadRequest());
            }

            var response = new MessagingResponse();

            var textMessage = new TextMessage();

            textMessage.To        = request.To.Replace("+", "");
            textMessage.Msisdn    = request.From.Replace("+", "");
            textMessage.MessageId = request.MessageSid;
            textMessage.Timestamp = DateTime.UtcNow.ToLongDateString();
            textMessage.Data      = request.Body;
            textMessage.Text      = request.Body;

            var messageEvent = new InboundMessageEvent();

            messageEvent.MessageType = (int)InboundMessageTypes.TextMessage;
            messageEvent.RecievedOn  = DateTime.UtcNow;
            messageEvent.Type        = typeof(InboundMessageEvent).FullName;
            messageEvent.Data        = JsonConvert.SerializeObject(textMessage);
            messageEvent.Processed   = false;
            messageEvent.CustomerId  = "";

            try
            {
                var departmentId = await _departmentSettingsService.GetDepartmentIdByTextToCallNumberAsync(textMessage.To);

                if (departmentId.HasValue)
                {
                    var department = await _departmentsService.GetDepartmentByIdAsync(departmentId.Value);

                    var textToCallEnabled = await _departmentSettingsService.GetDepartmentIsTextCallImportEnabledAsync(departmentId.Value);

                    var textCommandEnabled = await _departmentSettingsService.GetDepartmentIsTextCommandEnabledAsync(departmentId.Value);

                    var dispatchNumbers = await _departmentSettingsService.GetTextToCallSourceNumbersForDepartmentAsync(departmentId.Value);

                    var authroized = await _limitsService.CanDepartmentProvisionNumberAsync(departmentId.Value);

                    var customStates = await _customStateService.GetAllActiveCustomStatesForDepartmentAsync(departmentId.Value);

                    messageEvent.CustomerId = departmentId.Value.ToString();

                    if (authroized)
                    {
                        bool isDispatchSource = false;

                        if (!String.IsNullOrWhiteSpace(dispatchNumbers))
                        {
                            isDispatchSource = _numbersService.DoesNumberMatchAnyPattern(dispatchNumbers.Split(Char.Parse(",")).ToList(), textMessage.Msisdn);
                        }

                        // If we don't have dispatchNumbers and Text Command isn't enabled it's a dispatch text
                        if (!isDispatchSource && !textCommandEnabled)
                        {
                            isDispatchSource = true;
                        }

                        if (isDispatchSource && textToCallEnabled)
                        {
                            var c = new Call();
                            c.Notes            = textMessage.Text;
                            c.NatureOfCall     = textMessage.Text;
                            c.LoggedOn         = DateTime.UtcNow;
                            c.Name             = string.Format("TTC {0}", c.LoggedOn.TimeConverter(department).ToString("g"));
                            c.Priority         = (int)CallPriority.High;
                            c.ReportingUserId  = department.ManagingUserId;
                            c.Dispatches       = new Collection <CallDispatch>();
                            c.CallSource       = (int)CallSources.EmailImport;
                            c.SourceIdentifier = textMessage.MessageId;
                            c.DepartmentId     = departmentId.Value;

                            var users = await _departmentsService.GetAllUsersForDepartmentAsync(departmentId.Value, true);

                            foreach (var u in users)
                            {
                                var cd = new CallDispatch();
                                cd.UserId = u.UserId;

                                c.Dispatches.Add(cd);
                            }

                            var savedCall = await _callsService.SaveCallAsync(c);

                            var cqi = new CallQueueItem();
                            cqi.Call     = savedCall;
                            cqi.Profiles = await _userProfileService.GetSelectedUserProfilesAsync(users.Select(x => x.UserId).ToList());

                            cqi.DepartmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(cqi.Call.DepartmentId);

                            _queueService.EnqueueCallBroadcastAsync(cqi);

                            messageEvent.Processed = true;
                        }

                        if (!isDispatchSource && textCommandEnabled)
                        {
                            var profile = await _userProfileService.GetProfileByMobileNumberAsync(textMessage.Msisdn);

                            if (profile != null)
                            {
                                var payload        = _textCommandService.DetermineType(textMessage.Text);
                                var customActions  = customStates.FirstOrDefault(x => x.Type == (int)CustomStateTypes.Personnel);
                                var customStaffing = customStates.FirstOrDefault(x => x.Type == (int)CustomStateTypes.Staffing);

                                switch (payload.Type)
                                {
                                case TextCommandTypes.None:
                                    response.Message("Resgrid (https://resgrid.com) Automated Text System. Unknown command, text help for supported commands.");
                                    break;

                                case TextCommandTypes.Help:
                                    messageEvent.Processed = true;

                                    var help = new StringBuilder();
                                    help.Append("Resgrid Text Commands" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("These are the commands you can text to alter your status and staffing. Text help for help." + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("Core Commands" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("STOP: To turn off all text messages" + Environment.NewLine);
                                    help.Append("HELP: This help text" + Environment.NewLine);
                                    help.Append("CALLS: List active calls" + Environment.NewLine);
                                    help.Append("C[CallId]: Get Call Detail i.e. C1445" + Environment.NewLine);
                                    help.Append("UNITS: List unit statuses" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("Status or Action Commands" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);

                                    if (customActions != null && customActions.IsDeleted == false && customActions.GetActiveDetails() != null && customActions.GetActiveDetails().Any())
                                    {
                                        var activeDetails = customActions.GetActiveDetails();
                                        for (int i = 0; i < activeDetails.Count; i++)
                                        {
                                            help.Append($"{activeDetails[i].ButtonText.Trim().Replace(" ", "").Replace("-", "").Replace(":", "")} or {i + 1}: {activeDetails[i].ButtonText}" + Environment.NewLine);
                                        }
                                    }
                                    else
                                    {
                                        help.Append("responding or 1: Responding" + Environment.NewLine);
                                        help.Append("notresponding or 2: Not Responding" + Environment.NewLine);
                                        help.Append("onscene or 3: On Scene" + Environment.NewLine);
                                        help.Append("available or 4: Available" + Environment.NewLine);
                                    }
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("Staffing Commands" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);

                                    if (customStaffing != null && customStaffing.IsDeleted == false && customStaffing.GetActiveDetails() != null && customStaffing.GetActiveDetails().Any())
                                    {
                                        var activeDetails = customStaffing.GetActiveDetails();
                                        for (int i = 0; i < activeDetails.Count; i++)
                                        {
                                            help.Append($"{activeDetails[i].ButtonText.Trim().Replace(" ", "").Replace("-", "").Replace(":", "")} or S{i + 1}: {activeDetails[i].ButtonText}" + Environment.NewLine);
                                        }
                                    }
                                    else
                                    {
                                        help.Append("available or s1: Available Staffing" + Environment.NewLine);
                                        help.Append("delayed or s2: Delayed Staffing" + Environment.NewLine);
                                        help.Append("unavailable or s3: Unavailable Staffing" + Environment.NewLine);
                                        help.Append("committed or s4: Committed Staffing" + Environment.NewLine);
                                        help.Append("onshift or s4: On Shift Staffing" + Environment.NewLine);
                                    }

                                    response.Message(help.ToString());

                                    //_communicationService.SendTextMessage(profile.UserId, "Resgrid TCI Help", help.ToString(), department.DepartmentId, textMessage.To, profile);
                                    break;

                                case TextCommandTypes.Action:
                                    messageEvent.Processed = true;
                                    await _actionLogsService.SetUserActionAsync(profile.UserId, department.DepartmentId, (int)payload.GetActionType());

                                    response.Message(string.Format("Resgrid received your text command. Status changed to: {0}", payload.GetActionType()));
                                    //_communicationService.SendTextMessage(profile.UserId, "Resgrid TCI Status", string.Format("Resgrid recieved your text command. Status changed to: {0}", payload.GetActionType()), department.DepartmentId, textMessage.To, profile);
                                    break;

                                case TextCommandTypes.Staffing:
                                    messageEvent.Processed = true;
                                    await _userStateService.CreateUserState(profile.UserId, department.DepartmentId, (int)payload.GetStaffingType());

                                    response.Message(string.Format("Resgrid received your text command. Staffing level changed to: {0}", payload.GetStaffingType()));
                                    //_communicationService.SendTextMessage(profile.UserId, "Resgrid TCI Staffing", string.Format("Resgrid recieved your text command. Staffing level changed to: {0}", payload.GetStaffingType()), department.DepartmentId, textMessage.To, profile);
                                    break;

                                case TextCommandTypes.Stop:
                                    messageEvent.Processed = true;
                                    await _userProfileService.DisableTextMessagesForUserAsync(profile.UserId);

                                    response.Message("Text messages are now turned off for this user, to enable again log in to Resgrid and update your profile.");
                                    break;

                                case TextCommandTypes.CustomAction:
                                    messageEvent.Processed = true;
                                    await _actionLogsService.SetUserActionAsync(profile.UserId, department.DepartmentId, payload.GetCustomActionType());

                                    if (customActions != null && customActions.IsDeleted == false && customActions.GetActiveDetails() != null && customActions.GetActiveDetails().Any() && customActions.GetActiveDetails().FirstOrDefault(x => x.CustomStateDetailId == payload.GetCustomActionType()) != null)
                                    {
                                        var detail = customActions.GetActiveDetails().FirstOrDefault(x => x.CustomStateDetailId == payload.GetCustomActionType());
                                        response.Message(string.Format("Resgrid received your text command. Status changed to: {0}", detail.ButtonText));
                                    }
                                    else
                                    {
                                        response.Message("Resgrid received your text command and updated your status");
                                    }
                                    break;

                                case TextCommandTypes.CustomStaffing:
                                    messageEvent.Processed = true;
                                    await _userStateService.CreateUserState(profile.UserId, department.DepartmentId, payload.GetCustomStaffingType());

                                    if (customStaffing != null && customStaffing.IsDeleted == false && customStaffing.GetActiveDetails() != null && customStaffing.GetActiveDetails().Any() && customStaffing.GetActiveDetails().FirstOrDefault(x => x.CustomStateDetailId == payload.GetCustomStaffingType()) != null)
                                    {
                                        var detail = customStaffing.GetActiveDetails().FirstOrDefault(x => x.CustomStateDetailId == payload.GetCustomStaffingType());
                                        response.Message(string.Format("Resgrid received your text command. Staffing changed to: {0}", detail.ButtonText));
                                    }
                                    else
                                    {
                                        response.Message("Resgrid received your text command and updated your staffing");
                                    }
                                    break;

                                case TextCommandTypes.MyStatus:
                                    messageEvent.Processed = true;


                                    var userStatus = await _actionLogsService.GetLastActionLogForUserAsync(profile.UserId);

                                    var userStaffing = await _userStateService.GetLastUserStateByUserIdAsync(profile.UserId);

                                    var customStatusLevel = await _customStateService.GetCustomPersonnelStatusAsync(department.DepartmentId, userStatus);

                                    var customStaffingLevel = await _customStateService.GetCustomPersonnelStaffingAsync(department.DepartmentId, userStaffing);

                                    response.Message($"Hello {profile.FullName.AsFirstNameLastName} at {DateTime.UtcNow.TimeConverterToString(department)} your current status is {customStatusLevel.ButtonText} and your current staffing is {customStaffingLevel.ButtonText}.");
                                    break;

                                case TextCommandTypes.Calls:
                                    messageEvent.Processed = true;

                                    var activeCalls = await _callsService.GetActiveCallsByDepartmentAsync(department.DepartmentId);

                                    var activeCallText = new StringBuilder();
                                    activeCallText.Append($"Active Calls for {department.Name}" + Environment.NewLine);
                                    activeCallText.Append("---------------------" + Environment.NewLine);

                                    foreach (var activeCall in activeCalls)
                                    {
                                        activeCallText.Append($"CallId: {activeCall.CallId} Name: {activeCall.Name} Nature:{activeCall.NatureOfCall}" + Environment.NewLine);
                                    }


                                    response.Message(activeCallText.ToString());
                                    break;

                                case TextCommandTypes.Units:
                                    messageEvent.Processed = true;

                                    var unitStatus = await _unitsService.GetAllLatestStatusForUnitsByDepartmentIdAsync(department.DepartmentId);

                                    var unitStatusesText = new StringBuilder();
                                    unitStatusesText.Append($"Unit Statuses for {department.Name}" + Environment.NewLine);
                                    unitStatusesText.Append("---------------------" + Environment.NewLine);

                                    foreach (var unit in unitStatus)
                                    {
                                        var unitState = await _customStateService.GetCustomUnitStateAsync(unit);

                                        unitStatusesText.Append($"{unit.Unit.Name} is {unitState.ButtonText}" + Environment.NewLine);
                                    }

                                    response.Message(unitStatusesText.ToString());
                                    break;

                                case TextCommandTypes.CallDetail:
                                    messageEvent.Processed = true;

                                    var call = await _callsService.GetCallByIdAsync(int.Parse(payload.Data));

                                    var callText = new StringBuilder();
                                    callText.Append($"Call Information for {call.Name}" + Environment.NewLine);
                                    callText.Append("---------------------" + Environment.NewLine);
                                    callText.Append($"Id: {call.CallId}" + Environment.NewLine);
                                    callText.Append($"Number: {call.Number}" + Environment.NewLine);
                                    callText.Append($"Logged: {call.LoggedOn.TimeConverterToString(department)}" + Environment.NewLine);
                                    callText.Append("-----Nature-----" + Environment.NewLine);
                                    callText.Append(call.NatureOfCall + Environment.NewLine);
                                    callText.Append("-----Address-----" + Environment.NewLine);

                                    if (!String.IsNullOrWhiteSpace(call.Address))
                                    {
                                        callText.Append(call.Address + Environment.NewLine);
                                    }
                                    else if (!string.IsNullOrEmpty(call.GeoLocationData))
                                    {
                                        try
                                        {
                                            string[] points = call.GeoLocationData.Split(char.Parse(","));

                                            if (points != null && points.Length == 2)
                                            {
                                                callText.Append(_geoLocationProvider.GetAproxAddressFromLatLong(double.Parse(points[0]), double.Parse(points[1])) + Environment.NewLine);
                                            }
                                        }
                                        catch
                                        {
                                        }
                                    }

                                    response.Message(callText.ToString());
                                    break;
                                }
                            }
                        }
                    }
                }
                else if (textMessage.To == "17753765253")                 // Resgrid master text number
                {
                    var profile = await _userProfileService.GetProfileByMobileNumberAsync(textMessage.Msisdn);

                    var payload = _textCommandService.DetermineType(textMessage.Text);

                    switch (payload.Type)
                    {
                    case TextCommandTypes.None:
                        response.Message("Resgrid (https://resgrid.com) Automated Text System. Unknown command, text help for supported commands.");
                        break;

                    case TextCommandTypes.Help:
                        messageEvent.Processed = true;

                        var help = new StringBuilder();
                        help.Append("Resgrid Text Commands" + Environment.NewLine);
                        help.Append("---------------------" + Environment.NewLine);
                        help.Append("This is the Resgrid system for first responders (https://resgrid.com) automated text system. Your department isn't signed up for inbound text messages, but you can send the following commands." + Environment.NewLine);
                        help.Append("---------------------" + Environment.NewLine);
                        help.Append("STOP: To turn off all text messages" + Environment.NewLine);
                        help.Append("HELP: This help text" + Environment.NewLine);

                        response.Message(help.ToString());

                        break;

                    case TextCommandTypes.Stop:
                        messageEvent.Processed = true;
                        await _userProfileService.DisableTextMessagesForUserAsync(profile.UserId);

                        response.Message("Text messages are now turned off for this user, to enable again log in to Resgrid and update your profile.");
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Framework.Logging.LogException(ex);
            }
            finally
            {
                await _numbersService.SaveInboundMessageEventAsync(messageEvent);
            }

            //Ok();

            //var response = new TwilioResponse();

            //return Request.CreateResponse(HttpStatusCode.OK, response.Element, new XmlMediaTypeFormatter());
            return(Ok(new StringContent(response.ToString(), Encoding.UTF8, "application/xml")));
        }
Exemple #10
0
        public static Tuple <bool, string> ProcessQueueMessage(BrokeredMessage message)
        {
            bool   success = true;
            string result  = "";

            if (message != null)
            {
                try
                {
                    var body = message.GetBody <string>();

                    if (!String.IsNullOrWhiteSpace(body))
                    {
                        CallQueueItem cqi = null;
                        try
                        {
                            cqi = ObjectSerialization.Deserialize <CallQueueItem>(body);
                        }
                        catch (Exception ex)
                        {
                            success = false;
                            result  = "Unable to parse message body Exception: " + ex.ToString();
                            message.DeadLetter();
                        }

                        if (cqi != null && cqi.Call != null && cqi.Call.HasAnyDispatches())
                        {
                            try
                            {
                                ProcessCallQueueItem(cqi);
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                message.Abandon();

                                success = false;
                                result  = ex.ToString();
                            }
                        }
                    }
                    else
                    {
                        success = false;
                        result  = "Message body is null or empty";
                    }

                    try
                    {
                        message.Complete();
                    }
                    catch (MessageLockLostException)
                    {
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                    result  = ex.ToString();

                    Logging.LogException(ex);
                    message.Abandon();
                }
            }

            return(new Tuple <bool, string>(success, result));
        }
Exemple #11
0
        public HttpResponseMessage SaveCall([FromBody] NewCallInput newCallInput)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            var call = new Call
            {
                DepartmentId    = DepartmentId,
                ReportingUserId = UserId,
                Priority        = (int)Enum.Parse(typeof(CallPriority), newCallInput.Pri),
                Name            = newCallInput.Nme,
                NatureOfCall    = newCallInput.Noc
            };

            if (!string.IsNullOrWhiteSpace(newCallInput.CNme))
            {
                call.ContactName = newCallInput.CNme;
            }

            if (!string.IsNullOrWhiteSpace(newCallInput.CNum))
            {
                call.ContactName = newCallInput.CNum;
            }

            if (!string.IsNullOrWhiteSpace(newCallInput.Cid))
            {
                call.IncidentNumber = newCallInput.Cid;
            }

            if (!string.IsNullOrWhiteSpace(newCallInput.Add))
            {
                call.Address = newCallInput.Add;
            }

            if (!string.IsNullOrWhiteSpace(newCallInput.W3W))
            {
                call.W3W = newCallInput.W3W;
            }

            //if (call.Address.Equals("Current Coordinates", StringComparison.InvariantCultureIgnoreCase))
            //	call.Address = "";

            if (!string.IsNullOrWhiteSpace(newCallInput.Not))
            {
                call.Notes = newCallInput.Not;
            }

            if (!string.IsNullOrWhiteSpace(newCallInput.Geo))
            {
                call.GeoLocationData = newCallInput.Geo;
            }

            if (string.IsNullOrWhiteSpace(call.GeoLocationData) && !string.IsNullOrWhiteSpace(call.Address))
            {
                call.GeoLocationData = _geoLocationProvider.GetLatLonFromAddress(call.Address);
            }

            if (string.IsNullOrWhiteSpace(call.GeoLocationData) && !string.IsNullOrWhiteSpace(call.W3W))
            {
                var coords = _geoLocationProvider.GetCoordinatesFromW3W(call.W3W);

                if (coords != null)
                {
                    call.GeoLocationData = $"{coords.Latitude},{coords.Longitude}";
                }
            }

            call.LoggedOn = DateTime.UtcNow;

            if (!String.IsNullOrWhiteSpace(newCallInput.Typ) && newCallInput.Typ != "No Type")
            {
                var callTypes = _callsService.GetCallTypesForDepartment(DepartmentId);
                var type      = callTypes.FirstOrDefault(x => x.Type == newCallInput.Typ);

                if (type != null)
                {
                    call.Type = type.Type;
                }
            }
            var users = _departmentsService.GetAllUsersForDepartment(DepartmentId);

            call.Dispatches      = new Collection <CallDispatch>();
            call.GroupDispatches = new List <CallDispatchGroup>();
            call.RoleDispatches  = new List <CallDispatchRole>();

            if (string.IsNullOrWhiteSpace(newCallInput.Dis) || newCallInput.Dis == "0")
            {
                // Use case, existing clients and non-ionic2 app this will be null dispatch all users. Or we've specified everyone (0).
                foreach (var u in users)
                {
                    var cd = new CallDispatch {
                        UserId = u.UserId
                    };

                    call.Dispatches.Add(cd);
                }
            }
            else
            {
                var dispatch = newCallInput.Dis.Split(char.Parse("|"));

                try
                {
                    var usersToDispatch = dispatch.Where(x => x.StartsWith("P:")).Select(y => y.Replace("P:", ""));
                    foreach (var user in usersToDispatch)
                    {
                        var cd = new CallDispatch {
                            UserId = user
                        };
                        call.Dispatches.Add(cd);
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(ex);
                }

                try
                {
                    var groupsToDispatch = dispatch.Where(x => x.StartsWith("G:")).Select(y => int.Parse(y.Replace("G:", "")));
                    foreach (var group in groupsToDispatch)
                    {
                        var cd = new CallDispatchGroup {
                            DepartmentGroupId = group
                        };
                        call.GroupDispatches.Add(cd);
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(ex);
                }

                try
                {
                    var rolesToDispatch = dispatch.Where(x => x.StartsWith("R:")).Select(y => int.Parse(y.Replace("R:", "")));
                    foreach (var role in rolesToDispatch)
                    {
                        var cd = new CallDispatchRole {
                            RoleId = role
                        };
                        call.RoleDispatches.Add(cd);
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(ex);
                }
            }


            var savedCall = _callsService.SaveCall(call);

            OutboundEventProvider.CallAddedTopicHandler handler = new OutboundEventProvider.CallAddedTopicHandler();
            handler.Handle(new CallAddedEvent()
            {
                DepartmentId = DepartmentId, Call = savedCall
            });

            var profiles = new List <string>();

            if (call.Dispatches != null && call.Dispatches.Any())
            {
                profiles.AddRange(call.Dispatches.Select(x => x.UserId).ToList());
            }

            if (call.GroupDispatches != null && call.GroupDispatches.Any())
            {
                foreach (var groupDispatch in call.GroupDispatches)
                {
                    var group = _departmentGroupsService.GetGroupById(groupDispatch.DepartmentGroupId);

                    if (group != null && group.Members != null)
                    {
                        profiles.AddRange(group.Members.Select(x => x.UserId));
                    }
                }
            }

            if (call.RoleDispatches != null && call.RoleDispatches.Any())
            {
                foreach (var roleDispatch in call.RoleDispatches)
                {
                    var members = _personnelRolesService.GetAllMembersOfRole(roleDispatch.RoleId);

                    if (members != null)
                    {
                        profiles.AddRange(members.Select(x => x.UserId).ToList());
                    }
                }
            }

            var cqi = new CallQueueItem();

            cqi.Call     = savedCall;
            cqi.Profiles = _userProfileService.GetSelectedUserProfiles(profiles);

            _queueService.EnqueueCallBroadcast(cqi);

            return(Request.CreateResponse(HttpStatusCode.Created));
        }
Exemple #12
0
        public static void ProcessCallQueueItem(CallQueueItem cqi)
        {
            ICommunicationService _communicationService;
            ICallsService         _callsService;

            try
            {
                if (cqi != null && cqi.Call != null && cqi.Call.HasAnyDispatches())
                {
                    _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>();
                    _callsService         = Bootstrapper.GetKernel().Resolve <ICallsService>();

                    List <int> groupIds = new List <int>();

                    /* Trying to see if I can eek out a little perf here now that profiles are in Redis. Previously the
                     * the parallel operation would cause EF errors. This shouldn't be the case now because profiles are
                     * cached and GetProfileForUser operations will hit that first.
                     */
                    if (cqi.Profiles == null || !cqi.Profiles.Any())
                    {
                        var userProfilesService = Bootstrapper.GetKernel().Resolve <IUserProfileService>();
                        cqi.Profiles = userProfilesService.GetAllProfilesForDepartment(cqi.Call.DepartmentId).Select(x => x.Value).ToList();
                    }

                    if (cqi.CallDispatchAttachmentId > 0)
                    {
                        //var callsService = Bootstrapper.GetKernel().Resolve<ICallsService>();
                        cqi.Call.ShortenedAudioUrl = _callsService.GetShortenedAudioUrl(cqi.Call.CallId, cqi.CallDispatchAttachmentId);
                    }

                    cqi.Call.ShortenedCallUrl = _callsService.GetShortenedCallLinkUrl(cqi.Call.CallId);

                    try
                    {
                        cqi.Call.CallPriority = _callsService.GetCallPrioritesById(cqi.Call.DepartmentId, cqi.Call.Priority, false);
                    }
                    catch { /* Doesn't matter */ }

                    var dispatchedUsers = new HashSet <string>();

                    // Dispatch Personnel
                    if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any())
                    {
                        foreach (var d in cqi.Call.Dispatches)
                        {
                            dispatchedUsers.Add(d.UserId);
                        }

                        Parallel.ForEach(cqi.Call.Dispatches, d =>
                        {
                            try
                            {
                                var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == d.UserId);

                                if (profile != null)
                                {
                                    _communicationService.SendCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                }
                            }
                            catch (SocketException sex)
                            {
                            }
                        });
                    }

                    var departmentGroupsService = Bootstrapper.GetKernel().Resolve <IDepartmentGroupsService>();

                    // Dispatch Groups
                    if (cqi.Call.GroupDispatches != null && cqi.Call.GroupDispatches.Any())
                    {
                        foreach (var d in cqi.Call.GroupDispatches)
                        {
                            if (!groupIds.Contains(d.DepartmentGroupId))
                            {
                                groupIds.Add(d.DepartmentGroupId);
                            }

                            var members = departmentGroupsService.GetAllMembersForGroup(d.DepartmentGroupId);

                            foreach (var member in members)
                            {
                                if (!dispatchedUsers.Contains(member.UserId))
                                {
                                    dispatchedUsers.Add(member.UserId);
                                    try
                                    {
                                        var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                        _communicationService.SendCall(cqi.Call, new CallDispatch()
                                        {
                                            UserId = member.UserId
                                        }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                    }
                                    catch (SocketException sex)
                                    {
                                    }
                                    catch (Exception ex)
                                    {
                                        Logging.LogException(ex);
                                    }
                                }
                            }
                        }
                    }

                    // Dispatch Units
                    if (cqi.Call.UnitDispatches != null && cqi.Call.UnitDispatches.Any())
                    {
                        var unitsService = Bootstrapper.GetKernel().Resolve <IUnitsService>();

                        foreach (var d in cqi.Call.UnitDispatches)
                        {
                            var unit = unitsService.GetUnitById(d.UnitId);

                            if (unit != null && unit.StationGroupId.HasValue)
                            {
                                if (!groupIds.Contains(unit.StationGroupId.Value))
                                {
                                    groupIds.Add(unit.StationGroupId.Value);
                                }
                            }

                            _communicationService.SendUnitCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Address);

                            var unitAssignedMembers = unitsService.GetCurrentRolesForUnit(d.UnitId);

                            if (unitAssignedMembers != null && unitAssignedMembers.Count() > 0)
                            {
                                foreach (var member in unitAssignedMembers)
                                {
                                    if (!dispatchedUsers.Contains(member.UserId))
                                    {
                                        dispatchedUsers.Add(member.UserId);
                                        try
                                        {
                                            var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                            _communicationService.SendCall(cqi.Call, new CallDispatch()
                                            {
                                                UserId = member.UserId
                                            }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                        }
                                        catch (SocketException sex)
                                        {
                                        }
                                        catch (Exception ex)
                                        {
                                            Logging.LogException(ex);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (unit.StationGroupId.HasValue)
                                {
                                    var members = departmentGroupsService.GetAllMembersForGroup(unit.StationGroupId.Value);

                                    foreach (var member in members)
                                    {
                                        if (!dispatchedUsers.Contains(member.UserId))
                                        {
                                            dispatchedUsers.Add(member.UserId);
                                            try
                                            {
                                                var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                                _communicationService.SendCall(cqi.Call, new CallDispatch()
                                                {
                                                    UserId = member.UserId
                                                }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                            }
                                            catch (SocketException sex)
                                            {
                                            }
                                            catch (Exception ex)
                                            {
                                                Logging.LogException(ex);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // Dispatch Roles
                    if (cqi.Call.RoleDispatches != null && cqi.Call.RoleDispatches.Any())
                    {
                        var rolesService = Bootstrapper.GetKernel().Resolve <IPersonnelRolesService>();

                        foreach (var d in cqi.Call.RoleDispatches)
                        {
                            var members = rolesService.GetAllMembersOfRole(d.RoleId);

                            foreach (var member in members)
                            {
                                if (!dispatchedUsers.Contains(member.UserId))
                                {
                                    dispatchedUsers.Add(member.UserId);
                                    try
                                    {
                                        var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                        _communicationService.SendCall(cqi.Call, new CallDispatch()
                                        {
                                            UserId = member.UserId
                                        }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                    }
                                    catch (SocketException sex)
                                    {
                                    }
                                    catch (Exception ex)
                                    {
                                        Logging.LogException(ex);
                                    }
                                }
                            }
                        }
                    }

                    // Send Call Print to Printer
                    var printerProvider = Bootstrapper.GetKernel().Resolve <IPrinterProvider>();

                    Dictionary <int, DepartmentGroup> fetchedGroups = new Dictionary <int, DepartmentGroup>();
                    if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any())
                    {
                        foreach (var d in cqi.Call.Dispatches)
                        {
                            var group = departmentGroupsService.GetGroupForUser(d.UserId, cqi.Call.DepartmentId);

                            if (group != null)
                            {
                                if (!groupIds.Contains(group.DepartmentGroupId))
                                {
                                    groupIds.Add(group.DepartmentGroupId);
                                }

                                if (!fetchedGroups.ContainsKey(group.DepartmentGroupId))
                                {
                                    fetchedGroups.Add(group.DepartmentGroupId, group);
                                }
                            }
                        }
                    }

                    foreach (var groupId in groupIds)
                    {
                        try
                        {
                            DepartmentGroup group = null;

                            if (fetchedGroups.ContainsKey(groupId))
                            {
                                group = fetchedGroups[groupId];
                            }
                            else
                            {
                                group = departmentGroupsService.GetGroupById(groupId);
                            }

                            if (!String.IsNullOrWhiteSpace(group.PrinterData) && group.DispatchToPrinter)
                            {
                                var printerData = JsonConvert.DeserializeObject <DepartmentGroupPrinter>(group.PrinterData);
                                var apiKey      = SymmetricEncryption.Decrypt(printerData.ApiKey, Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase);
                                var callUrl     = _callsService.GetShortenedCallPdfUrl(cqi.Call.CallId, true, groupId);

                                var printJob = printerProvider.SubmitPrintJob(apiKey, printerData.PrinterId, "CallPrint", callUrl);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                        }
                    }
                }
            }
            finally
            {
                _communicationService = null;
            }
        }
Exemple #13
0
        public static void ProcessCallQueueItem(CallQueueItem cqi)
        {
            ICommunicationService _communicationService;
            ICallsService         _callsService;

            try
            {
                if (cqi != null && cqi.Call != null && cqi.Call.HasAnyDispatches())
                {
                    _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>();
                    _callsService         = Bootstrapper.GetKernel().Resolve <ICallsService>();

                    /* Trying to see if I can eek out a little perf here now that profiles are in Redis. Previously the
                     * the parallel operation would cause EF errors. This shouldn't be the case now because profiles are
                     * cached and GetProfileForUser operations will hit that first.
                     */
                    if (cqi.Profiles == null || !cqi.Profiles.Any())
                    {
                        var userProfilesService = Bootstrapper.GetKernel().Resolve <IUserProfileService>();
                        cqi.Profiles = userProfilesService.GetAllProfilesForDepartment(cqi.Call.DepartmentId).Select(x => x.Value).ToList();
                    }

                    if (cqi.CallDispatchAttachmentId > 0)
                    {
                        //var callsService = Bootstrapper.GetKernel().Resolve<ICallsService>();
                        cqi.Call.ShortenedAudioUrl = _callsService.GetShortenedAudioUrl(cqi.Call.CallId, cqi.CallDispatchAttachmentId);
                    }

                    cqi.Call.ShortenedCallUrl = _callsService.GetShortenedCallLinkUrl(cqi.Call.CallId);

                    try
                    {
                        cqi.Call.CallPriority = _callsService.GetCallPrioritesById(cqi.Call.DepartmentId, cqi.Call.Priority, false);
                    }
                    catch { /* Doesn't matter */ }

                    var dispatchedUsers = new HashSet <string>();

                    // Dispatch Personnel
                    if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any())
                    {
                        foreach (var d in cqi.Call.Dispatches)
                        {
                            dispatchedUsers.Add(d.UserId);
                        }

                        Parallel.ForEach(cqi.Call.Dispatches, d =>
                        {
                            try
                            {
                                var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == d.UserId);

                                if (profile != null)
                                {
                                    _communicationService.SendCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                }
                            }
                            catch (SocketException sex)
                            {
                            }
                        });
                    }

                    // Dispatch Groups
                    if (cqi.Call.GroupDispatches != null && cqi.Call.GroupDispatches.Any())
                    {
                        var departmentGroupsService = Bootstrapper.GetKernel().Resolve <IDepartmentGroupsService>();

                        foreach (var d in cqi.Call.GroupDispatches)
                        {
                            var members = departmentGroupsService.GetAllMembersForGroup(d.DepartmentGroupId);

                            foreach (var member in members)
                            {
                                if (!dispatchedUsers.Contains(member.UserId))
                                {
                                    dispatchedUsers.Add(member.UserId);
                                    try
                                    {
                                        var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                        _communicationService.SendCall(cqi.Call, new CallDispatch()
                                        {
                                            UserId = member.UserId
                                        }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                    }
                                    catch (SocketException sex)
                                    {
                                    }
                                    catch (Exception ex)
                                    {
                                        Logging.LogException(ex);
                                    }
                                }
                            }
                        }
                    }

                    // Dispatch Units
                    if (cqi.Call.UnitDispatches != null && cqi.Call.UnitDispatches.Any())
                    {
                        var unitsService            = Bootstrapper.GetKernel().Resolve <IUnitsService>();
                        var departmentGroupsService = Bootstrapper.GetKernel().Resolve <IDepartmentGroupsService>();

                        foreach (var d in cqi.Call.UnitDispatches)
                        {
                            var unit = unitsService.GetUnitById(d.UnitId);

                            _communicationService.SendUnitCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Address);

                            var unitAssignedMembers = unitsService.GetCurrentRolesForUnit(d.UnitId);

                            if (unitAssignedMembers != null && unitAssignedMembers.Count() > 0)
                            {
                                foreach (var member in unitAssignedMembers)
                                {
                                    if (!dispatchedUsers.Contains(member.UserId))
                                    {
                                        dispatchedUsers.Add(member.UserId);
                                        try
                                        {
                                            var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                            _communicationService.SendCall(cqi.Call, new CallDispatch()
                                            {
                                                UserId = member.UserId
                                            }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                        }
                                        catch (SocketException sex)
                                        {
                                        }
                                        catch (Exception ex)
                                        {
                                            Logging.LogException(ex);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (unit.StationGroupId.HasValue)
                                {
                                    var members = departmentGroupsService.GetAllMembersForGroup(unit.StationGroupId.Value);

                                    foreach (var member in members)
                                    {
                                        if (!dispatchedUsers.Contains(member.UserId))
                                        {
                                            dispatchedUsers.Add(member.UserId);
                                            try
                                            {
                                                var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                                _communicationService.SendCall(cqi.Call, new CallDispatch()
                                                {
                                                    UserId = member.UserId
                                                }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                            }
                                            catch (SocketException sex)
                                            {
                                            }
                                            catch (Exception ex)
                                            {
                                                Logging.LogException(ex);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // Dispatch Roles
                    if (cqi.Call.RoleDispatches != null && cqi.Call.RoleDispatches.Any())
                    {
                        var rolesService = Bootstrapper.GetKernel().Resolve <IPersonnelRolesService>();

                        foreach (var d in cqi.Call.RoleDispatches)
                        {
                            var members = rolesService.GetAllMembersOfRole(d.RoleId);

                            foreach (var member in members)
                            {
                                if (!dispatchedUsers.Contains(member.UserId))
                                {
                                    dispatchedUsers.Add(member.UserId);
                                    try
                                    {
                                        var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId);
                                        _communicationService.SendCall(cqi.Call, new CallDispatch()
                                        {
                                            UserId = member.UserId
                                        }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address);
                                    }
                                    catch (SocketException sex)
                                    {
                                    }
                                    catch (Exception ex)
                                    {
                                        Logging.LogException(ex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                _communicationService = null;
            }
        }
Exemple #14
0
        private async Task StartMonitoring()
        {
            if (SystemBehaviorConfig.ServiceBusType == ServiceBusTypes.Rabbit)
            {
                var callQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                callQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        CallQueueItem cqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            cqi = ObjectSerialization.Deserialize <CallQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (cqi != null)
                            {
                                if (CallQueueReceived != null)
                                {
                                    await CallQueueReceived.Invoke(cqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var messageQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                messageQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        MessageQueueItem mqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            mqi = ObjectSerialization.Deserialize <MessageQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (mqi != null)
                            {
                                if (MessageQueueReceived != null)
                                {
                                    await MessageQueueReceived.Invoke(mqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var distributionListQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                distributionListQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        DistributionListQueueItem dlqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            dlqi = ObjectSerialization.Deserialize <DistributionListQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (dlqi != null)
                            {
                                if (DistributionListQueueReceived != null)
                                {
                                    await DistributionListQueueReceived.Invoke(dlqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var notificationQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                notificationQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        NotificationItem ni = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            ni = ObjectSerialization.Deserialize <NotificationItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (ni != null)
                            {
                                if (NotificationQueueReceived != null)
                                {
                                    await NotificationQueueReceived.Invoke(ni);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var shiftNotificationQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                shiftNotificationQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        ShiftQueueItem sqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            sqi = ObjectSerialization.Deserialize <ShiftQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (sqi != null)
                            {
                                if (ShiftNotificationQueueReceived != null)
                                {
                                    await ShiftNotificationQueueReceived.Invoke(sqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var cqrsEventQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                cqrsEventQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        CqrsEvent cqrs = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            cqrs = ObjectSerialization.Deserialize <CqrsEvent>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (cqrs != null)
                            {
                                if (CqrsEventQueueReceived != null)
                                {
                                    await CqrsEventQueueReceived.Invoke(cqrs);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var paymentEventQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                paymentEventQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        CqrsEvent cqrs = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            cqrs = ObjectSerialization.Deserialize <CqrsEvent>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (cqrs != null)
                            {
                                if (PaymentEventQueueReceived != null)
                                {
                                    await PaymentEventQueueReceived.Invoke(cqrs);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };


                String callQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.CallBroadcastQueueName),
                    autoAck: false,
                    consumer: callQueueReceivedConsumer);

                String messageQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.MessageBroadcastQueueName),
                    autoAck: false,
                    consumer: messageQueueReceivedConsumer);

                String distributionListQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.EmailBroadcastQueueName),
                    autoAck: false,
                    consumer: distributionListQueueReceivedConsumer);

                String notificationQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.NotificaitonBroadcastQueueName),
                    autoAck: false,
                    consumer: notificationQueueReceivedConsumer);

                String shiftNotificationQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.ShiftNotificationsQueueName),
                    autoAck: false,
                    consumer: shiftNotificationQueueReceivedConsumer);

                String cqrsEventQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.SystemQueueName),
                    autoAck: false,
                    consumer: cqrsEventQueueReceivedConsumer);

                String paymentEventQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.PaymentQueueName),
                    autoAck: false,
                    consumer: paymentEventQueueReceivedConsumer);
            }
        }
Exemple #15
0
        public HttpResponseMessage Receive(PostmarkInboundMessage message)
        {
            if (message != null)
            {
                try
                {
                    var mailMessage = new MimeMessage();

                    if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && message.FromFull.Email.Trim() == "*****@*****.**")
                    {
                        return(new HttpResponseMessage(HttpStatusCode.Created));
                    }

                    if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && !String.IsNullOrWhiteSpace(message.FromFull.Name))
                    {
                        mailMessage.From.Add(new MailboxAddress(message.FromFull.Name.Trim(), message.FromFull.Email.Trim()));
                    }
                    else
                    {
                        mailMessage.From.Add(new MailboxAddress("Inbound Email Dispatch", "*****@*****.**"));
                    }

                    if (!String.IsNullOrWhiteSpace(message.Subject))
                    {
                        mailMessage.Subject = message.Subject;
                    }
                    else
                    {
                        mailMessage.Subject = "Dispatch Email";
                    }

                    var builder = new BodyBuilder();

                    if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                    {
                        builder.HtmlBody = HttpUtility.HtmlDecode(message.HtmlBody);
                    }

                    if (!String.IsNullOrWhiteSpace(message.TextBody))
                    {
                        builder.TextBody = message.TextBody;
                    }

                    int    type         = 0;          // 1 = dispatch // 2 = email list // 3 = group dispatch // 4 = group message
                    string emailAddress = String.Empty;
                    string bounceEmail  = String.Empty;
                    string name         = String.Empty;

                    #region Trying to Find What type of email this is
                    foreach (var email in message.ToFull)
                    {
                        if (StringHelpers.ValidateEmail(email.Email))
                        {
                            if (email.Email.Contains($"@{Config.InboundEmailConfig.DispatchDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.DispatchTestDomain}"))
                            {
                                type = 1;

                                if (email.Email.Contains($"@{Config.InboundEmailConfig.DispatchDomain}"))
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.DispatchDomain}", "");
                                }
                                else
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.DispatchTestDomain}", "");
                                }

                                name = email.Name;
                                mailMessage.To.Clear();
                                mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                break;
                            }
                            else if (email.Email.Contains($"@{Config.InboundEmailConfig.ListsDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.ListsTestDomain}"))
                            {
                                type = 2;

                                if (email.Email.Contains($"@{Config.InboundEmailConfig.ListsDomain}"))
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.ListsDomain}", "");
                                }
                                else
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.ListsTestDomain}", "");
                                }

                                if (emailAddress.Contains("+") && emailAddress.Contains("="))
                                {
                                    var tempBounceEmail = emailAddress.Substring(emailAddress.IndexOf("+") + 1);
                                    bounceEmail = tempBounceEmail.Replace("=", "@");

                                    emailAddress = emailAddress.Replace(tempBounceEmail, "");
                                    emailAddress = emailAddress.Replace("+", "");
                                }

                                name = email.Name;
                                mailMessage.To.Clear();
                                mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                break;
                            }
                            else if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupsDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.GroupsTestDomain}"))
                            {
                                type = 3;

                                if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupsDomain}"))
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupsDomain}", "");
                                }
                                else
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupsTestDomain}", "");
                                }

                                name = email.Name;
                                mailMessage.To.Clear();
                                mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                break;
                            }
                            else if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupMessageDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.GroupTestMessageDomain}"))
                            {
                                type = 4;

                                if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupMessageDomain}"))
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupMessageDomain}", "");
                                }
                                else
                                {
                                    emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupTestMessageDomain}", "");
                                }

                                name = email.Name;
                                mailMessage.To.Clear();
                                mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                break;
                            }
                        }
                    }

                    // Some providers aren't putting email address in the To line, process the CC line
                    if (type == 0)
                    {
                        foreach (var email in message.CcFull)
                        {
                            if (StringHelpers.ValidateEmail(email.Email))
                            {
                                var proccedEmailInfo = ProcessEmailAddress(email.Email);

                                if (proccedEmailInfo.Item1 > 0)
                                {
                                    type         = proccedEmailInfo.Item1;
                                    emailAddress = proccedEmailInfo.Item2;

                                    mailMessage.To.Clear();
                                    mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));
                                }
                            }
                        }
                    }

                    // If To and CC didn't work, try the header.
                    if (type == 0)
                    {
                        try
                        {
                            if (message.Headers != null && message.Headers.Count > 0)
                            {
                                var header = message.Headers.FirstOrDefault(x => x.Name == "Received-SPF");

                                if (header != null)
                                {
                                    var lastValue = header.Value.LastIndexOf(char.Parse("="));
                                    var newEmail  = header.Value.Substring(lastValue + 1, (header.Value.Length - (lastValue + 1)));

                                    newEmail = newEmail.Trim();

                                    if (StringHelpers.ValidateEmail(newEmail))
                                    {
                                        emailAddress = newEmail;
                                        var proccedEmailInfo = ProcessEmailAddress(newEmail);

                                        type         = proccedEmailInfo.Item1;
                                        emailAddress = proccedEmailInfo.Item2;

                                        mailMessage.To.Clear();
                                        mailMessage.To.Add(new MailboxAddress("Email Importer", newEmail));
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                        }
                    }
                    #endregion Trying to Find What type of email this is

                    if (type == 1)                      // Dispatch
                    {
                        #region Dispatch Email
                        var departmentId = _departmentSettingsService.GetDepartmentIdForDispatchEmail(emailAddress);

                        if (departmentId.HasValue)
                        {
                            try
                            {
                                var emailSettings = _departmentsService.GetDepartmentEmailSettings(departmentId.Value);
                                List <IdentityUser> departmentUsers = _departmentsService.GetAllUsersForDepartment(departmentId.Value, true);

                                var callEmail = new CallEmail();

                                if (!String.IsNullOrWhiteSpace(message.Subject))
                                {
                                    callEmail.Subject = message.Subject;
                                }

                                else
                                {
                                    callEmail.Subject = "Dispatch Email";
                                }

                                if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                                {
                                    callEmail.Body = HttpUtility.HtmlDecode(message.HtmlBody);
                                }
                                else
                                {
                                    callEmail.Body = message.TextBody;
                                }

                                callEmail.TextBody = message.TextBody;

                                foreach (var attachment in message.Attachments)
                                {
                                    try
                                    {
                                        if (Convert.ToInt32(attachment.ContentLength) > 0)
                                        {
                                            if (attachment.Name.Contains(".mp3") || attachment.Name.Contains(".amr"))
                                            {
                                                byte[] filebytes = Convert.FromBase64String(attachment.Content);

                                                callEmail.DispatchAudioFileName = attachment.Name;
                                                callEmail.DispatchAudio         = filebytes;
                                            }
                                        }
                                    }
                                    catch { }
                                }

                                if (emailSettings == null)
                                {
                                    emailSettings              = new DepartmentCallEmail();
                                    emailSettings.FormatType   = (int)CallEmailTypes.Generic;
                                    emailSettings.DepartmentId = departmentId.Value;
                                    emailSettings.Department   = _departmentsService.GetDepartmentById(departmentId.Value, false);
                                }
                                else if (emailSettings.Department == null)
                                {
                                    emailSettings.Department = _departmentsService.GetDepartmentById(departmentId.Value);
                                }

                                var activeCalls     = _callsService.GetLatest10ActiveCallsByDepartment(emailSettings.Department.DepartmentId);
                                var units           = _unitsService.GetUnitsForDepartment(emailSettings.Department.DepartmentId);
                                var priorities      = _callsService.GetActiveCallPrioritesForDepartment(emailSettings.Department.DepartmentId);
                                int defaultPriority = (int)CallPriority.High;

                                if (priorities != null && priorities.Any())
                                {
                                    var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false);

                                    if (defaultPrio != null)
                                    {
                                        defaultPriority = defaultPrio.DepartmentCallPriorityId;
                                    }
                                }

                                var call = _callsService.GenerateCallFromEmail(emailSettings.FormatType, callEmail,
                                                                               emailSettings.Department.ManagingUserId,
                                                                               departmentUsers, emailSettings.Department, activeCalls, units, defaultPriority);

                                if (call != null)
                                {
                                    call.DepartmentId = departmentId.Value;

                                    var savedCall = _callsService.SaveCall(call);

                                    var cqi = new CallQueueItem();
                                    cqi.Call                 = savedCall;
                                    cqi.Profiles             = _userProfileService.GetAllProfilesForDepartment(call.DepartmentId).Select(x => x.Value).ToList();
                                    cqi.DepartmentTextNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(cqi.Call.DepartmentId);

                                    _queueService.EnqueueCallBroadcast(cqi);

                                    return(new HttpResponseMessage(HttpStatusCode.Created));
                                }
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                            }
                        }
                        #endregion Dispatch
                    }
                    else if (type == 2)                     // Email List
                    {
                        #region Distribution Email
                        var list = _distributionListsService.GetDistributionListByAddress(emailAddress);

                        if (list != null)
                        {
                            if (String.IsNullOrWhiteSpace(bounceEmail))
                            {
                                try
                                {
                                    List <Model.File> files = new List <Model.File>();

                                    try
                                    {
                                        if (message.Attachments != null && message.Attachments.Any())
                                        {
                                            foreach (var attachment in message.Attachments)
                                            {
                                                if (Convert.ToInt32(attachment.ContentLength) > 0)
                                                {
                                                    Model.File file = new Model.File();

                                                    byte[] filebytes = Convert.FromBase64String(attachment.Content);

                                                    file.Data         = filebytes;
                                                    file.FileName     = attachment.Name;
                                                    file.DepartmentId = list.DepartmentId;
                                                    file.ContentId    = attachment.ContentID;
                                                    file.FileType     = attachment.ContentType;
                                                    file.Timestamp    = DateTime.UtcNow;

                                                    files.Add(_fileService.SaveFile(file));
                                                }
                                            }
                                        }
                                    }
                                    catch { }

                                    var dlqi = new DistributionListQueueItem();
                                    dlqi.List  = list;
                                    dlqi.Users = _departmentsService.GetAllUsersForDepartment(list.DepartmentId);

                                    if (files != null && files.Any())
                                    {
                                        dlqi.FileIds = new List <int>();
                                        dlqi.FileIds.AddRange(files.Select(x => x.FileId).ToList());
                                    }

                                    dlqi.Message             = new InboundMessage();
                                    dlqi.Message.Attachments = new List <InboundMessageAttachment>();

                                    if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && !String.IsNullOrWhiteSpace(message.FromFull.Name))
                                    {
                                        dlqi.Message.FromEmail = message.FromFull.Email.Trim();
                                        dlqi.Message.FromName  = message.FromFull.Name.Trim();
                                    }

                                    dlqi.Message.Subject   = message.Subject;
                                    dlqi.Message.HtmlBody  = message.HtmlBody;
                                    dlqi.Message.TextBody  = message.TextBody;
                                    dlqi.Message.MessageID = message.MessageID;

                                    _queueService.EnqueueDistributionListBroadcast(dlqi);
                                }
                                catch (Exception ex)
                                {
                                    Logging.LogException(ex);
                                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                                }
                            }
                            else
                            {
                                return(new HttpResponseMessage(HttpStatusCode.Created));
                            }
                        }

                        return(new HttpResponseMessage(HttpStatusCode.Created));

                        #endregion Distribution Email
                    }
                    if (type == 3)                      // Group Dispatch
                    {
                        #region Group Dispatch Email
                        var departmentGroup = _departmentGroupsService.GetGroupByDispatchEmailCode(emailAddress);

                        if (departmentGroup != null)
                        {
                            try
                            {
                                var emailSettings = _departmentsService.GetDepartmentEmailSettings(departmentGroup.DepartmentId);
                                //var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroup(departmentGroup.DepartmentGroupId);
                                var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroupAndChildGroups(departmentGroup);

                                var callEmail = new CallEmail();
                                callEmail.Subject = message.Subject;

                                if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                                {
                                    callEmail.Body = HttpUtility.HtmlDecode(message.HtmlBody);
                                }
                                else
                                {
                                    callEmail.Body = message.TextBody;
                                }

                                foreach (var attachment in message.Attachments)
                                {
                                    try
                                    {
                                        if (Convert.ToInt32(attachment.ContentLength) > 0)
                                        {
                                            if (attachment.Name.Contains(".mp3") || attachment.Name.Contains(".amr"))
                                            {
                                                byte[] filebytes = Convert.FromBase64String(attachment.Content);

                                                callEmail.DispatchAudioFileName = attachment.Name;
                                                callEmail.DispatchAudio         = filebytes;
                                            }
                                        }
                                    }
                                    catch { }
                                }

                                if (emailSettings == null)
                                {
                                    emailSettings              = new DepartmentCallEmail();
                                    emailSettings.FormatType   = (int)CallEmailTypes.Generic;
                                    emailSettings.DepartmentId = departmentGroup.DepartmentId;

                                    if (departmentGroup.Department != null)
                                    {
                                        emailSettings.Department = departmentGroup.Department;
                                    }
                                    else
                                    {
                                        emailSettings.Department = _departmentsService.GetDepartmentById(departmentGroup.DepartmentId);
                                    }
                                }

                                var activeCalls = _callsService.GetActiveCallsByDepartment(emailSettings.Department.DepartmentId);
                                var units       = _unitsService.GetAllUnitsForGroup(departmentGroup.DepartmentGroupId);

                                var priorities      = _callsService.GetActiveCallPrioritesForDepartment(emailSettings.Department.DepartmentId);
                                int defaultPriority = (int)CallPriority.High;

                                if (priorities != null && priorities.Any())
                                {
                                    var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false);

                                    if (defaultPrio != null)
                                    {
                                        defaultPriority = defaultPrio.DepartmentCallPriorityId;
                                    }
                                }

                                var call = _callsService.GenerateCallFromEmail(emailSettings.FormatType, callEmail,
                                                                               emailSettings.Department.ManagingUserId,
                                                                               departmentGroupUsers.Select(x => x.User).ToList(), emailSettings.Department, activeCalls, units, defaultPriority);

                                if (call != null)
                                {
                                    call.DepartmentId = departmentGroup.DepartmentId;

                                    var savedCall = _callsService.SaveCall(call);

                                    var cqi = new CallQueueItem();
                                    cqi.Call                 = savedCall;
                                    cqi.Profiles             = _userProfileService.GetSelectedUserProfiles(departmentGroupUsers.Select(x => x.UserId).ToList());
                                    cqi.DepartmentTextNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(cqi.Call.DepartmentId);

                                    _queueService.EnqueueCallBroadcast(cqi);

                                    return(new HttpResponseMessage(HttpStatusCode.Created));
                                }
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                            }
                        }
                        #endregion Group Dispatch Email
                    }
                    if (type == 4)                      // Group Message
                    {
                        #region Group Message
                        var departmentGroup = _departmentGroupsService.GetGroupByMessageEmailCode(emailAddress);

                        if (departmentGroup != null)
                        {
                            try
                            {
                                //var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroup(departmentGroup.DepartmentGroupId);
                                var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroupAndChildGroups(departmentGroup);

                                var newMessage = new Message();
                                newMessage.SentOn          = DateTime.UtcNow;
                                newMessage.SendingUserId   = departmentGroup.Department.ManagingUserId;
                                newMessage.IsBroadcast     = true;
                                newMessage.Subject         = message.Subject;
                                newMessage.SystemGenerated = true;

                                if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                                {
                                    newMessage.Body = HttpUtility.HtmlDecode(message.HtmlBody);
                                }
                                else
                                {
                                    newMessage.Body = message.TextBody;
                                }

                                foreach (var member in departmentGroupUsers)
                                {
                                    if (newMessage.GetRecipients().All(x => x != member.UserId))
                                    {
                                        newMessage.AddRecipient(member.UserId);
                                    }
                                }

                                var savedMessage = _messageService.SaveMessage(newMessage);
                                _messageService.SendMessage(savedMessage, "", departmentGroup.DepartmentId, false);

                                return(new HttpResponseMessage(HttpStatusCode.Created));
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                            }
                        }

                        #endregion Group Message
                    }

                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }
                catch (Exception ex)
                {
                    Framework.Logging.LogException(ex);
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent(ex.ToString())
                    });
                }
            }
            else
            {
                // If our message was null, we throw an exception
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("Error parsing Inbound Message, message is null.")
                });
            }
        }
Exemple #16
0
        public async Task <Tuple <bool, string> > ProcessQueueMessage(Message message, CancellationToken token)
        {
            bool   success = true;
            string result  = "";

            if (message != null)
            {
                try
                {
                    var body = message.GetBody <string>();

                    if (!String.IsNullOrWhiteSpace(body))
                    {
                        CallQueueItem cqi = null;
                        try
                        {
                            cqi = ObjectSerialization.Deserialize <CallQueueItem>(body);
                        }
                        catch (Exception ex)
                        {
                            success = false;
                            result  = "Unable to parse message body Exception: " + ex.ToString();
                            //message.DeadLetter();
                            await _client.DeadLetterAsync(message.SystemProperties.LockToken);
                        }

                        if (cqi != null && cqi.Call != null && cqi.Call.HasAnyDispatches())
                        {
                            try
                            {
                                await ProcessCallQueueItem(cqi);
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                //message.Abandon();
                                await _client.DeadLetterAsync(message.SystemProperties.LockToken);

                                success = false;
                                result  = ex.ToString();
                            }
                        }
                    }
                    else
                    {
                        success = false;
                        result  = "Message body is null or empty";
                    }

                    try
                    {
                        //message.Complete();
                        await _client.CompleteAsync(message.SystemProperties.LockToken);
                    }
                    catch (MessageLockLostException)
                    {
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                    result  = ex.ToString();

                    Logging.LogException(ex);
                    //message.Abandon();
                    await _client.DeadLetterAsync(message.SystemProperties.LockToken);
                }
            }

            return(new Tuple <bool, string>(success, result));
        }
Exemple #17
0
        //[AcceptVerbs("POST")]
        public HttpResponseMessage Process()        //([FromBody]TextMessage textMessage2)
        {
            var queryValues = Request.RequestUri.ParseQueryString();

            var textMessage = new TextMessage();

            textMessage.Type        = queryValues["type"];
            textMessage.To          = queryValues["to"];
            textMessage.Msisdn      = queryValues["msisdn"];
            textMessage.NetworkCode = queryValues["network-code"];
            textMessage.MessageId   = queryValues["messageId"];
            textMessage.Timestamp   = queryValues["message-timestamp"];
            textMessage.Concat      = queryValues["concat"];
            textMessage.ConcatRef   = queryValues["concat-ref"];
            textMessage.ConcatTotal = queryValues["concat-total"];
            textMessage.ConcatPart  = queryValues["concat-part"];
            textMessage.Data        = queryValues["data"];
            textMessage.Udh         = queryValues["udh"];
            textMessage.Text        = queryValues["text"];

            var messageEvent = new InboundMessageEvent();

            messageEvent.MessageType = (int)InboundMessageTypes.TextMessage;
            messageEvent.RecievedOn  = DateTime.UtcNow;
            messageEvent.Type        = typeof(InboundMessageEvent).FullName;
            messageEvent.Data        = JsonConvert.SerializeObject(textMessage);
            messageEvent.Processed   = false;
            messageEvent.CustomerId  = "";

            try
            {
                var departmentId = _departmentSettingsService.GetDepartmentIdByTextToCallNumber(textMessage.To);

                if (departmentId.HasValue)
                {
                    var department         = _departmentsService.GetDepartmentById(departmentId.Value);
                    var textToCallEnabled  = _departmentSettingsService.GetDepartmentIsTextCallImportEnabled(departmentId.Value);
                    var textCommandEnabled = _departmentSettingsService.GetDepartmentIsTextCommandEnabled(departmentId.Value);
                    var dispatchNumbers    = _departmentSettingsService.GetTextToCallSourceNumbersForDepartment(departmentId.Value);
                    var authroized         = _limitsService.CanDepartmentProvisionNumber(departmentId.Value);

                    messageEvent.CustomerId = departmentId.Value.ToString();

                    if (authroized)
                    {
                        bool isDispatchSource = false;

                        if (!String.IsNullOrWhiteSpace(dispatchNumbers))
                        {
                            isDispatchSource = _numbersService.DoesNumberMatchAnyPattern(dispatchNumbers.Split(Char.Parse(",")).ToList(), textMessage.Msisdn);
                        }

                        // If we don't have dispatchNumbers and Text Command isn't
                        // enabled it's a dispatch text
                        if (!isDispatchSource && !textCommandEnabled)
                        {
                            isDispatchSource = true;
                        }

                        if (isDispatchSource && textToCallEnabled)
                        {
                            var c = new Call();
                            c.Notes            = textMessage.Text;
                            c.NatureOfCall     = textMessage.Text;
                            c.LoggedOn         = DateTime.UtcNow;
                            c.Name             = string.Format("TTC {0}", c.LoggedOn.TimeConverter(department).ToString("g"));
                            c.Priority         = (int)CallPriority.High;
                            c.ReportingUserId  = department.ManagingUserId;
                            c.Dispatches       = new Collection <CallDispatch>();
                            c.CallSource       = (int)CallSources.EmailImport;
                            c.SourceIdentifier = textMessage.MessageId;
                            c.DepartmentId     = departmentId.Value;

                            var users = _departmentsService.GetAllUsersForDepartment(departmentId.Value, true);
                            foreach (var u in users)
                            {
                                var cd = new CallDispatch();
                                cd.UserId = u.UserId;

                                c.Dispatches.Add(cd);
                            }

                            var savedCall = _callsService.SaveCall(c);

                            var cqi = new CallQueueItem();
                            cqi.Call                 = savedCall;
                            cqi.Profiles             = _userProfileService.GetSelectedUserProfiles(users.Select(x => x.UserId).ToList());
                            cqi.DepartmentTextNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(cqi.Call.DepartmentId);

                            _queueService.EnqueueCallBroadcast(cqi);

                            messageEvent.Processed = true;
                        }

                        if (!isDispatchSource && textCommandEnabled)
                        {
                            var profile = _userProfileService.FindProfileByMobileNumber(textMessage.Msisdn);

                            if (profile != null)
                            {
                                var payload = _textCommandService.DetermineType(textMessage.Text);

                                switch (payload.Type)
                                {
                                case TextCommandTypes.None:
                                    break;

                                case TextCommandTypes.Help:
                                    messageEvent.Processed = true;

                                    var help = new StringBuilder();
                                    help.Append("Resgrid Text Commands" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("These are the commands you can text to alter your status and staffing. Text help for help." + Environment.NewLine);
                                    help.Append("Status Commands" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("responding or 1: Responding" + Environment.NewLine);
                                    help.Append("notresponding or 2: Not Responding" + Environment.NewLine);
                                    help.Append("onscene or 3: On Scene" + Environment.NewLine);
                                    help.Append("available or 4: Available" + Environment.NewLine);
                                    help.Append("Staffing Commands" + Environment.NewLine);
                                    help.Append("---------------------" + Environment.NewLine);
                                    help.Append("normal or s1: Available Staffing" + Environment.NewLine);
                                    help.Append("delayed or s2: Delayed Staffing" + Environment.NewLine);
                                    help.Append("unavailable or s3: Unavailable Staffing" + Environment.NewLine);
                                    help.Append("committed or s4: Committed Staffing" + Environment.NewLine);
                                    help.Append("onshift or s4: On Shift Staffing" + Environment.NewLine);

                                    _communicationService.SendTextMessage(profile.UserId, "Resgrid TCI Help", help.ToString(), department.DepartmentId, textMessage.To, profile);
                                    break;

                                case TextCommandTypes.Action:
                                    messageEvent.Processed = true;
                                    _actionLogsService.SetUserAction(profile.UserId, department.DepartmentId, (int)payload.GetActionType());
                                    _communicationService.SendTextMessage(profile.UserId, "Resgrid TCI Status", string.Format("Resgrid received your text command. Status changed to: {0}", payload.GetActionType()), department.DepartmentId, textMessage.To, profile);
                                    break;

                                case TextCommandTypes.Staffing:
                                    messageEvent.Processed = true;
                                    _userStateService.CreateUserState(profile.UserId, department.DepartmentId, (int)payload.GetStaffingType());
                                    _communicationService.SendTextMessage(profile.UserId, "Resgrid TCI Staffing", string.Format("Resgrid received your text command. Staffing level changed to: {0}", payload.GetStaffingType()), department.DepartmentId, textMessage.To, profile);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Framework.Logging.LogException(ex);
            }
            finally
            {
                _numbersService.SaveInboundMessageEvent(messageEvent);
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Exemple #18
0
        public async Task <Tuple <bool, string> > Process(CallEmailQueueItem item)
        {
            bool   success = true;
            string result  = "";

            _callEmailProvider = Bootstrapper.GetKernel().Resolve <ICallEmailProvider>();

            if (!String.IsNullOrWhiteSpace(item?.EmailSettings?.Hostname))
            {
                CallEmailsResult emailResult = _callEmailProvider.GetAllCallEmailsFromServer(item.EmailSettings);

                if (emailResult?.Emails != null && emailResult.Emails.Count > 0)
                {
                    var calls = new List <Call>();

                    _callsService              = Bootstrapper.GetKernel().Resolve <ICallsService>();
                    _queueService              = Bootstrapper.GetKernel().Resolve <IQueueService>();
                    _departmentsService        = Bootstrapper.GetKernel().Resolve <IDepartmentsService>();
                    _userProfileService        = Bootstrapper.GetKernel().Resolve <IUserProfileService>();
                    _departmentSettingsService = Bootstrapper.GetKernel().Resolve <IDepartmentSettingsService>();
                    _unitsService              = Bootstrapper.GetKernel().Resolve <IUnitsService>();

                    // Ran into an issue where the department users didn't come back. We can't put the email back in the POP
                    // email box so just added some simple retry logic here.
                    List <IdentityUser> departmentUsers = await _departmentsService.GetAllUsersForDepartmentAsync(item.EmailSettings.DepartmentId, true);

                    var profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(item.EmailSettings.DepartmentId);

                    int retry = 0;
                    while (retry < 3 && departmentUsers == null)
                    {
                        Thread.Sleep(150);
                        departmentUsers = await _departmentsService.GetAllUsersForDepartmentAsync(item.EmailSettings.DepartmentId, true);

                        retry++;
                    }

                    foreach (var email in emailResult.Emails)
                    {
                        var activeCalls = await _callsService.GetActiveCallsByDepartmentAsync(item.EmailSettings.Department.DepartmentId);

                        var units = await _unitsService.GetUnitsForDepartmentAsync(item.EmailSettings.Department.DepartmentId);

                        var priorities = await _callsService.GetActiveCallPrioritiesForDepartmentAsync(item.EmailSettings.Department.DepartmentId);

                        int defaultPriority = (int)CallPriority.High;

                        if (priorities != null && priorities.Any())
                        {
                            var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false);

                            if (defaultPrio != null)
                            {
                                defaultPriority = defaultPrio.DepartmentCallPriorityId;
                            }
                        }

                        var call = _callsService.GenerateCallFromEmail(item.EmailSettings.FormatType, email,
                                                                       item.EmailSettings.Department.ManagingUserId, departmentUsers, item.EmailSettings.Department, activeCalls, units, defaultPriority, priorities);

                        if (call != null)
                        {
                            call.DepartmentId = item.EmailSettings.DepartmentId;

                            if (!calls.Any(x => x.Name == call.Name && x.NatureOfCall == call.NatureOfCall))
                            {
                                calls.Add(call);
                            }
                        }
                    }

                    if (calls.Any())
                    {
                        var departmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(item.EmailSettings.DepartmentId);

                        foreach (var call in calls)
                        {
                            try
                            {
                                // Adding this in here to try and fix the error below with ObjectContext issues.
                                var newCall = CreateNewCallFromCall(call);

                                if (newCall.Dispatches != null && newCall.Dispatches.Any())
                                {
                                    // We've been having this error here:
                                    //      The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
                                    // So I'm wrapping this in a try catch to prevent all calls form being dropped.
                                    var savedCall = await _callsService.SaveCallAsync(newCall);

                                    var cqi = new CallQueueItem();
                                    cqi.Call = savedCall;

                                    cqi.Profiles             = profiles.Values.ToList();
                                    cqi.DepartmentTextNumber = departmentTextNumber;

                                    await _queueService.EnqueueCallBroadcastAsync(cqi);
                                }
                            }
                            catch (Exception ex)
                            {
                                result = ex.ToString();
                                Logging.LogException(ex);
                            }
                        }
                    }

                    await _departmentsService.SaveDepartmentEmailSettingsAsync(emailResult.EmailSettings);

                    _callsService              = null;
                    _queueService              = null;
                    _departmentsService        = null;
                    _callEmailProvider         = null;
                    _userProfileService        = null;
                    _departmentSettingsService = null;
                }
            }

            return(new Tuple <bool, string>(success, result));
        }