Beispiel #1
0
 private void Channel_UnsolicitedEvent(object sender, UnsolicitedEventArgs e)
 {
     if (e.Line1 == "RING")
     {
         IncomingCall?.Invoke(this, new IncomingCallEventArgs());
     }
     else if (e.Line1.StartsWith("VOICE CALL: BEGIN"))
     {
         CallStarted?.Invoke(this, new CallStartedEventArgs());
     }
     else if (e.Line1.StartsWith("VOICE CALL: END"))
     {
         CallEnded?.Invoke(this, CallEndedEventArgs.CreateFromResponse(e.Line1));
     }
     else if (e.Line1.StartsWith("MISSED_CALL: "))
     {
         MissedCall?.Invoke(this, MissedCallEventArgs.CreateFromResponse(e.Line1));
     }
     else if (e.Line1.StartsWith("+CMTI: "))
     {
         SmsReceived?.Invoke(this, SmsReceivedEventArgs.CreateFromResponse(e.Line1));
     }
     else if (e.Line1.StartsWith("+CUSD: "))
     {
         UssdResponseReceived?.Invoke(this, UssdResponseEventArgs.CreateFromResponse(e.Line1));
     }
     else if (e.Line1.StartsWith("+CME ERROR:"))
     {
         ErrorReceived?.Invoke(this, ErrorEventArgs.CreateFromCmeResponse(e.Line1));
     }
     else if (e.Line1.StartsWith("+CMS ERROR:"))
     {
         ErrorReceived?.Invoke(this, ErrorEventArgs.CreateFromCmsResponse(e.Line1));
     }
 }
        public void Handle(SmsReceived sms)
        {
            BaseInstance instance = instanceRepository.Peek(sms.PhoneNumber);

            if (instance != null)
            {
                if (this.mapper.MappingExists(sms.Body) && !(instance is ForkInstance))
                {
                    ForkInstance fork = new ForkInstance()
                    {
                        ForkingFromInstance = instance,
                        ForkingFromWorkflow = this.mapper.GetStateMachine(instance.GetType()),
                        ForkingToKeyword    = sms.Body,
                        PhoneNumber         = sms.PhoneNumber
                    };

                    var stateMachine = this.mapper.GetStateMachine(fork.GetType());

                    stateMachine.RaiseAnEvent(fork, stateMachine.Start, sms.PhoneNumber);

                    instanceRepository.Push(sms.PhoneNumber, fork);
                }
                else
                {
                    //then raise the SMS received event
                    var stateMachine = this.mapper.GetStateMachine(instance.GetType());

                    stateMachine.RaiseAnEvent(instance, stateMachine.SMSReceived, sms.Body);

                    if (instance.CurrentState == stateMachine.Final)
                    {
                        instanceRepository.Pop(sms.PhoneNumber);
                    }
                }
            }
            else if (this.mapper.MappingExists(sms.Body))
            {
                //make a new state machine instance
                instance = this.mapper.GetStateMachineInstance(sms.Body);
                IWorkflow stateMachine = this.mapper.GetStateMachine(sms.Body);

                stateMachine.RaiseAnEvent(instance, stateMachine.Start, sms.PhoneNumber);
                instanceRepository.Push(sms.PhoneNumber, instance);
            }
            else
            {
                bus.Publish(new SendSms()
                {
                    PhoneNumber = sms.PhoneNumber, Body = "That command was not recognized."
                });
            }
        }
Beispiel #3
0
        public SmsReceived SaveSmsReceived(SmsReceived smsReceived)
        {
            using (var adapter = PersistenceLayer.GetDataAccessAdapter())
            {
                var entity = Mapper.Map <SmsReceived, SmsReceivedEntity>(smsReceived);
                if (!adapter.SaveEntity(entity, true))
                {
                    throw new PersistenceFailureException();
                }

                return(Mapper.Map <SmsReceivedEntity, SmsReceived>(entity));
            }
        }
 public IncomingSmsModel(CMSDataContext db, SmsRequest incomingMessage)
 {
     CurrentDatabase = db;
     To   = incomingMessage.To;
     From = incomingMessage.From.StartsWith("+1")
         ? incomingMessage.From.Substring(2)
         : incomingMessage.From;
     Body = incomingMessage.Body.Trim();
     row  = new SmsReceived
     {
         Body         = Body,
         ToNumber     = To,
         FromNumber   = From,
         DateReceived = Util.Now,
     };
 }
Beispiel #5
0
 public void SmsReceivedEvent(string message)
 {
     SmsReceived?.Invoke(message);
 }
Beispiel #6
0
        private void OnFrameReceived(object sender, FrameReceivedEventArgs e)
        {
            var content = e.FrameContent;

            if (content is ModemStatusFrame)
            {
                var modemStatusFrame = content as ModemStatusFrame;

                _modemResetTaskCompletionSource?.SetResult(modemStatusFrame.ModemStatus);
            }
            else if (content is CommandResponseFrameContent)
            {
                var commandResponse = content as CommandResponseFrameContent;

                var frameId = commandResponse.FrameId;

                TaskCompletionSource <CommandResponseFrameContent> taskCompletionSource;
                if (ExecuteTaskCompletionSources.TryRemove(frameId, out taskCompletionSource))
                {
                    taskCompletionSource.SetResult(commandResponse);
                }
                else
                {
                    Action <CommandResponseFrameContent> callback;
                    if (ExecuteCallbacks.TryGetValue(frameId, out callback))
                    {
                        callback(commandResponse);
                    }
                }
            }
            else if (content is IRxIndicatorDataFrame)
            {
                var dataFrame = content as IRxIndicatorDataFrame;
                var address   = dataFrame.GetAddress();

                _receivedDataSource.Push(new SourcedData(address, dataFrame.Data));

                DataReceived?.Invoke(this, new SourcedDataReceivedEventArgs(address, dataFrame.Data));
            }
            else if (content is IRxIndicatorSampleFrame)
            {
                var sampleFrame = content as IRxIndicatorSampleFrame;
                var address     = sampleFrame.GetAddress();
                var sample      = sampleFrame.GetSample();

                _sampleSource.Push(new SourcedSample(address, sample));

                SampleReceived?.Invoke(this,
                                       new SourcedSampleReceivedEventArgs(address, sample.DigitalChannels, sample.DigitalSampleState,
                                                                          sample.AnalogChannels, sample.AnalogSamples));
            }
            else if (content is SensorReadIndicatorFrame)
            {
                var sensorFrame  = content as SensorReadIndicatorFrame;
                var sensorSample = new SensorSample(sensorFrame.OneWireSensor,
                                                    sensorFrame.SensorValueA,
                                                    sensorFrame.SensorValueB,
                                                    sensorFrame.SensorValueC,
                                                    sensorFrame.SensorValueD,
                                                    sensorFrame.TemperatureCelsius);

                var address = sensorFrame.GetAddress();

                SensorSampleReceived?.Invoke(this,
                                             new SourcedSensorSampleReceivedEventArgs(address, sensorSample.OneWireSensor,
                                                                                      sensorSample.SensorValueA, sensorSample.SensorValueB, sensorSample.SensorValueC,
                                                                                      sensorSample.SensorValueD, sensorSample.TemperatureCelsius));
            }
            else if (content is RxSmsFrame)
            {
                var smsFrame = content as RxSmsFrame;

                SmsReceived?.Invoke(this, new SmsReceivedEventArgs(smsFrame.PhoneNumber, smsFrame.Message));
            }
            else if (content is RxIPv4Frame)
            {
                var ipv4Frame = content as RxIPv4Frame;
                var address   = new IPAddress(ipv4Frame.SourceAddress);

                InternetDataReceived?.Invoke(this,
                                             new InternetDataReceivedEventArgs(address, ipv4Frame.DestinationPort, ipv4Frame.SourcePort,
                                                                               ipv4Frame.Protocol, ipv4Frame.Data));
            }
        }
        public void SmsReceivedNotification(TwillioMessageResponse message)
        {
            if (message != null)
            {
                var phoneMobile = _phoneNumberFactory.CreatePhoneNumber(message.From, PhoneNumberType.Mobile);
                if (phoneMobile == null)
                {
                    return;
                }

                var mobilePhonNumber = phoneMobile.AreaCode + phoneMobile.Number;
                var customers        = _customerRepository.GetCustomersByPhoneNumber(mobilePhonNumber, PhoneNumberType.Mobile);
                var smsReceivedTime  = DateTime.Now;

                var smsReceived = new SmsReceived
                {
                    DateCreated = smsReceivedTime,
                    Message     = message.Body,
                    PhoneNumber = mobilePhonNumber
                };

                smsReceived = _smsReceivedRepository.SaveSmsReceived(smsReceived);

                if (customers.IsNullOrEmpty())
                {
                    _logger.Info("No customer found with matching Phone Number");
                    return;
                }

                if (!string.IsNullOrEmpty(message.Body) && SmsOptOutKeyWords.OptOutKey.Contains(message.Body.ToLower()))
                {
                    var unsubscriptModel = new CustomerUnsubscribedSmsNotification
                    {
                        DateCreated   = smsReceivedTime,
                        SmsReceivedId = smsReceived.Id,
                        StatusId      = (long)SmsNotificationSubscriptionStatus.Unsubscribe
                    };

                    foreach (var customer in customers)
                    {
                        unsubscriptModel.CustomerId = customer.CustomerId;

                        _customerUnsubscribedSmsNotificationRepository.SaveUnsubscribedSmsStatus(unsubscriptModel);

                        customer.IsSubscribed = false;
                        _customerService.SaveCustomerOnly(customer, customer.CustomerId);
                    }
                }
                else if (!string.IsNullOrEmpty(message.Body) && SmsOptInKeyWords.OptInKey.Contains(message.Body.ToLower()))
                {
                    var unsubscriptModel = new CustomerUnsubscribedSmsNotification
                    {
                        DateCreated   = smsReceivedTime,
                        SmsReceivedId = smsReceived.Id,
                        StatusId      = (long)SmsNotificationSubscriptionStatus.Subscribe
                    };

                    foreach (var customer in customers)
                    {
                        unsubscriptModel.CustomerId = customer.CustomerId;
                        _customerUnsubscribedSmsNotificationRepository.SaveUnsubscribedSmsStatus(unsubscriptModel);

                        customer.IsSubscribed = true;
                        _customerService.SaveCustomerOnly(customer, customer.CustomerId);
                    }
                }
                else
                {
                    if (!customers.IsNullOrEmpty())
                    {
                        Customer customer = null;
                        if (customers.Count() > 1)
                        {
                            var lastNotificationSentTo =
                                _notificationRepository.GetLatestNotificationByPhone(mobilePhonNumber);
                            customer = customers.FirstOrDefault(s => s.Id == lastNotificationSentTo.UserId) ?? customers.First();
                        }
                        else
                        {
                            customer = customers.First();
                        }

                        if (customer != null)
                        {
                            var screeningReminderSmsNotification = _phoneNotificationModelsFactory.GetDummyWrongSmsResponseNotificationViewModel();

                            var notification = _notifier.NotifyViaSms(NotificationTypeAlias.WrongSmsReponse, EmailTemplateAlias.WrongSmsReponse, screeningReminderSmsNotification, customer.Id, customer.CustomerId, "Wrong Sms Response");
                        }
                    }
                }
            }
        }