public bool SendSMS(TextMessage TextMessage) { if (SimCards.Exists(s => s.PhoneNumber == TextMessage.PAN)) { Log.Information("!!Attempt to send to UTAP SIM!! - PIN: {PIN} SIM: {SIM}", TextMessage.PIN, TextMessage.PAN); return(false); } var sim = SimCards.Find(s => s.PhoneNumber == TextMessage.SIM); var success = false; try { lock (sim.locker) { SendCommand(sim.COMPort, ATCommands._CONFIRM_AT_DEVICE, 3000); SendCommand(sim.COMPort, ATCommands._SET_MESSAGE_FORMAT, 3000); SendCommand(sim.COMPort, ATCommands._SET_REPORTING_STATUS, 3000); var Messages = UTAPEncoding.EncodePDU(TextMessage.PIN, TextMessage.PAN, TextMessage.Message, TextMessage.RefNumber); foreach (var message in Messages) { SendCommand(sim.COMPort, string.Format(ATCommands._START_SEND_MESSAGE, (message.Length - 2) / 2), 3000); SendCommand(sim.COMPort, message + char.ConvertFromUtf32(26) + "\r", 10000); } success = true; } } catch (Exception ex) { Log.Error("Exception in SendSMS - From: {PIN} To: {PAN} Ex: {ex}", TextMessage.PIN, TextMessage.PAN, ex.Message); } return(success); }
private async Task InboundMessageReader() { while (true) { try { var messages = new List <SMS>(); foreach (SimCard sim in SimCards) { if (sim.Connected) { try { lock (sim.locker) { var allMessages = SendCommand(sim.COMPort, ATCommands._GET_ALL_MESSAGES, 3000); Regex r = new Regex(RegexStrings._MATCH_MESSAGES); Match m = r.Match(allMessages); var indexes = new List <string>(); while (m.Success) { indexes.Add(m.Groups["Index"].Value); m = m.NextMatch(); } foreach (string index in indexes) { var message = SendCommand(sim.COMPort, string.Format(ATCommands._GET_MESSAGE, index), 3000); Regex messageMatcher = new Regex(RegexStrings._MATCH_MESSAGE); Match messageMatch = messageMatcher.Match(message); while (messageMatch.Success) { try { SMS msg = new SMS { From = UTAPEncoding.DecodePDUSender(messageMatch.Groups["Body"].Value) }; if (msg.From.StartsWith("44")) { msg.From = "0" + msg.From.Remove(0, 2); } else if (msg.From.StartsWith("+44")) { msg.From = "0" + msg.From.Remove(0, 3); } msg.To = sim.PhoneNumber; msg.Timestamp = DateTime.UtcNow; msg.Body = UTAPEncoding.DecodePDUMessage(messageMatch.Groups["Body"].Value); if (msg.Body != null) { _queue.Post(msg); } } catch (Exception ex) { Log.Information("Exception on decode message - To: {To} Ex: {ex}", sim.PhoneNumber, ex.Message); } SendCommand(sim.COMPort, string.Format(ATCommands._DELETE_MESSAGE, index), 3000); messageMatch = m.NextMatch(); } } } } catch (Exception ex) { Log.Error("Exception in InboundMessageReader on message parse - SimNumber: {SimNumber} Port: {Port} Ex: {ex}", sim.PhoneNumber, sim.COMPort.PortName, ex.Message); } } } } catch (Exception ex) { Log.Error("Exception in InboundMessageReader: {ex}", ex.Message); } finally { await Task.Delay(10000); } } }