Ejemplo n.º 1
0
        private void EnqueuePendingMessages()
        {
            var TargetDir = _settings.ConversationsPath;
            var files     = Directory.EnumerateFiles(TargetDir, "*.utap", SearchOption.AllDirectories).Where(f => f.Contains("Pending"));

            foreach (var file in files)
            {
                try
                {
                    var message = new SingleMessage();

                    var foldersInRoot = _settings.ConversationsPath.Split(Path.DirectorySeparatorChar).Length;
                    var PIN           = file.Split(Path.DirectorySeparatorChar)[foldersInRoot];
                    var PAN           = file.Split(Path.DirectorySeparatorChar)[foldersInRoot + 1];

                    message.PIN  = Convert.ToInt32(PIN);
                    message.PAN  = PAN;
                    message.Body = JsonConvert.DeserializeObject <Body>(File.ReadAllText(file));

                    if (message.Body.Status == MessageStatus.SEND_PENDING)
                    {
                        _queue.Post(message);
                    }
                }
                catch (Exception ex)
                {
                    Log.Information("EnqueuePendingMessages - Failed to enqueue message at: {File} ex: {Ex}", file, ex.Message);
                }
            }
        }
Ejemplo n.º 2
0
        private void UpdatePendingMessage(SingleMessage message)
        {
            var TargetDir = Path.Combine(_settings.ConversationsPath, message.PIN.ToString(), message.PAN, "Pending");

            if (!Directory.Exists(TargetDir))
            {
                Directory.CreateDirectory(TargetDir);
            }

            var TargetPath = Path.Combine(TargetDir, message.Body.MessageId.ToString()) + ".utap";

            var json = JsonConvert.SerializeObject(message.Body);

            File.WriteAllText(TargetPath, json + Environment.NewLine);
        }
Ejemplo n.º 3
0
        public async Task <bool> EnqueueMessage(SingleMessage message)
        {
            string sim = await GetSim(message.PIN, message.PAN, "");

            if (sim == null)
            {
                return(false);
            }

            if (!_simQueues.ContainsKey(sim))
            {
                _simQueues[sim] = new BufferBlock <SingleMessage>();
            }
            _simQueues[sim].Post(message);
            return(true);
        }
Ejemplo n.º 4
0
        private void DeletePendingMessage(SingleMessage message)
        {
            var TargetDir = Path.Combine(_settings.ConversationsPath, message.PIN.ToString(), message.PAN, "Pending");

            if (!Directory.Exists(TargetDir))
            {
                Directory.CreateDirectory(TargetDir);
            }

            var TargetPath = Path.Combine(TargetDir, message.Body.MessageId.ToString()) + ".utap";

            if (File.Exists(TargetPath))
            {
                File.Delete(TargetPath);
            }
        }
Ejemplo n.º 5
0
        public void StorePendingMessage(SingleMessage message)
        {
            var TargetDir = Path.Combine(_settings.ConversationsPath, message.PIN.ToString(), message.PAN, "Pending");

            if (!Directory.Exists(TargetDir))
            {
                Directory.CreateDirectory(TargetDir);
            }

            var TargetFile = Path.Combine(TargetDir, message.Body.MessageId + ".utap");

            if (!File.Exists(TargetFile))
            {
                message.Body.Status = MessageStatus.SEND_PENDING;
                var json = JsonConvert.SerializeObject(message.Body);
                File.WriteAllText(TargetFile, json + Environment.NewLine);
            }
        }
Ejemplo n.º 6
0
 public void SaveMessageToDisk(SingleMessage message)
 {
     try
     {
         message.Body.Status = MessageStatus.SEND_SUCCESSFUL;
         var jsonString = JsonConvert.SerializeObject(message.Body);
         var TargetPath = Path.Combine(Path.Combine(new string[] { Path.Combine(_settings.ConversationsPath, message.PIN.ToString(), message.PAN) }.Concat(message.Body.Timestamp.ToString("yyyy-MM-dd").Split('-')).ToArray()), ((DateTimeOffset)message.Body.Timestamp).ToUnixTimeMilliseconds() + ".utap");
         Directory.CreateDirectory(Path.GetDirectoryName(TargetPath));
         File.WriteAllText(TargetPath, jsonString);
         Log.Information("Message saved at: {path} - From: {PIN} To: {PAN}", TargetPath, message.PIN, message.PAN);
         _hubContext.Clients.All.SendAsync("ReceiveMessage", message.PIN, message.PAN, message.Body);
         DeletePendingMessage(message);
     }
     catch (Exception ex)
     {
         Log.Error("Exception on message save - From: {PIN} To: {PAN} Body: {Body} Ex: {ex}", message.PIN, message.PAN, message.Body.Text, ex.Message);
     }
 }
Ejemplo n.º 7
0
        public void StoreInboundMessage(SMS Message)
        {
            Log.Information("Message received - From: {PAN} To: {SIM} Body: {Body}", Message.From, Message.To, Message.Body);
            var PAN = _DBManager.PANList.FirstOrDefault(p => p.Digits == Message.From && p.SimNumber == Message.To);

            if (PAN == null)
            {
                Log.Warning("Unable to find PAN in StoreInboundMessages - From: {PAN} To: {SIM} Body: {Body}", Message.From, Message.To, Message.Body);
            }
            else
            {
                var PIN = PAN.PIN;
                if (ValidatePAN(PIN, Message.From))
                {
                    var sm = new SingleMessage
                    {
                        PIN  = PIN,
                        PAN  = Message.From,
                        Body = new Body
                        {
                            FromMe    = false,
                            Timestamp = Message.Timestamp,
                            Text      = Message.Body
                        }
                    };

                    var jsonString = JsonConvert.SerializeObject(sm.Body);
                    var TargetPath = Path.Combine(Path.Combine(new string[] { Path.Combine(_settings.ConversationsPath, PIN.ToString(), Message.From) }.Concat(Message.Timestamp.ToString("yyyy-MM-dd").Split('-')).ToArray()), ((DateTimeOffset)Message.Timestamp).ToUnixTimeMilliseconds() + ".utap");
                    int counter    = 1;
                    while (File.Exists(TargetPath))
                    {
                        TargetPath = Path.Combine(Path.Combine(new string[] { Path.Combine(_settings.ConversationsPath, PIN.ToString(), Message.From) }.Concat(Message.Timestamp.ToString("yyyy-MM-dd").Split('-')).ToArray()), ((DateTimeOffset)Message.Timestamp).ToUnixTimeMilliseconds() + "." + counter++.ToString() + ".utap");
                    }
                    Directory.CreateDirectory(Path.GetDirectoryName(TargetPath));
                    File.WriteAllText(TargetPath, jsonString + Environment.NewLine);
                    Log.Information("Message saved at: {path} - From: {PAN} To: {PIN}", TargetPath, Message.From, PIN);
                    _DBManager.Enqueue(string.Format("UPDATE allowed_phone_numbers SET ConversationRead = 0 WHERE PIN = {0} AND p_number = '{1}'", PIN, Message.From));
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", sm.PIN, sm.PAN, sm.Body);
                }
            }
        }