string ResolveNameByNumber(string number) { IDbCommand cmd; IDataReader reader; try { cmd = DatabaseService.TryCreateCommand(); cmd.CommandText = String.Format("SELECT name FROM persons WHERE number='{0}'", number); reader = cmd.ExecuteReader(); } catch (Exception ex) { Log.AddException("SMS", ex); return(number); } Log.Add(LogLevel.Verbose, "SMS", cmd.CommandText); string ret; if (reader.Read()) { ret = (string)reader["name"]; } else { Log.Add(LogLevel.Warning, "SMS", String.Format("Number {0} not found in db.", number)); ret = number; } reader.Close(); return(ret); }
void SendMessages() { Log.Add(LogLevel.Verbose, "SMS", "Sending SMS..."); List <TriggerMessage> msgs = new List <TriggerMessage>(); for (int i = 0; i < _messagesPerTimerTick && _messagesToSend.Count > 0; i++) { msgs.Add(_messagesToSend.Dequeue()); } foreach (TriggerMessage msg in msgs) { Log.Add(LogLevel.Info, "SMS", string.Format("Sending Message to {0}", msg.Destination)); Log.Add(LogLevel.Verbose, "SMS", string.Format("Message: {0} ({2}): {1}", msg.Destination, msg.Text, msg.FlashMessage)); SmsSubmitPdu[] pdus = SmartMessageFactory.CreateConcatTextMessage(msg.Text, msg.Destination); if (msg.FlashMessage) { foreach (var pdu in pdus) { pdu.DataCodingScheme = DataCodingScheme.Class0_7Bit; } } _gsm.SendMessages(pdus); } }
public void StopReceiving() { if (_rcvTimer.Enabled) { Log.Add(LogLevel.Info, "SMS", "SMS receiving stopped."); _rcvTimer.Stop(); } }
public override void Start() { try { Log.Add(LogLevel.Info, "SMS", "Opening device to port."); _gsm.Open(); StartReceiving(); } catch (Exception ex) { Log.AddException("SMS", ex); } }
protected override void InitRoutine() { string port = GetConfigString("Port"); int baud = GetConfigInteger("Baud"); _messagesPerTimerTick = GetConfigInteger("MessagesPerTick"); NormalizeNumbers = true; _resolveNumbers = true; _gsm = new GsmCommMain(port, baud); Log.Add(LogLevel.Debug, "SMS", String.Format("Initialising on port {0}:{1}baud", port, baud)); }
public void StartReceiving(int interval) { if (_gsm.IsConnected()) { _rcvTimer = new Timer(interval); _rcvTimer.AutoReset = true; _rcvTimer.Elapsed += _rcvTimer_Elapsed; _rcvTimer.Start(); Log.Add(LogLevel.Info, "SMS", "SMS receiving started."); } else { Log.Add(LogLevel.Error, "SMS", "Cannot start receiving SMS, not connected to Phone."); } }
public void HandleTriggerMessage(object sender, TriggerMessage message) { Log.Add(LogLevel.Debug, "SMS", string.Format("Enqueing Message to {0}", message.Destination)); _messagesToSend.Enqueue(message); }
void ReceiveMessages() { Log.Add(LogLevel.Verbose, "SMS", "Reading SMS messages from device..."); Stopwatch sw = new Stopwatch(); sw.Start(); DecodedShortMessage[] msgs = _gsm.ReadMessages(PhoneMessageStatus.All, "MT"); Log.Add(LogLevel.Verbose, "SMS", String.Format("{0} messages in storage", msgs.Length)); foreach (DecodedShortMessage msg in msgs) { string from = string.Empty; string text = string.Empty; DateTime received = DateTime.Now; bool _fullMessageReceived = false; SmsDeliverPdu pdu = (SmsDeliverPdu)msg.Data; if (SmartMessageDecoder.IsPartOfConcatMessage(pdu)) { IConcatenationInfo info = SmartMessageDecoder.GetConcatenationInfo(pdu); Log.Add(LogLevel.Debug, "SMS", string.Format("Received multi-part message {0}: {1}/{2}", info.ReferenceNumber, info.CurrentNumber, info.TotalMessages)); if (_concatPdus.ContainsKey(info.ReferenceNumber)) { _concatPdus[info.ReferenceNumber].Add(pdu); } else { _concatPdus.Add(info.ReferenceNumber, new List <SmsPdu>() { pdu }); } if (SmartMessageDecoder.AreAllConcatPartsPresent(_concatPdus[info.ReferenceNumber])) { _fullMessageReceived = true; from = pdu.OriginatingAddress; received = pdu.SCTimestamp.ToDateTime(); text = SmartMessageDecoder.CombineConcatMessageText(_concatPdus[info.ReferenceNumber]); } } else { Log.Add(LogLevel.Debug, "SMS", "Received single-part SMS."); _fullMessageReceived = true; from = String.Format("{0}", pdu.OriginatingAddress); received = pdu.SCTimestamp.ToDateTime(); text = pdu.UserDataText; } if (_fullMessageReceived) { Log.Add(LogLevel.Info, "SMS", String.Format("Incoming SMS from {0}", from)); if (NormalizeNumbers) { from = NormalizeNumber(from); } if (_resolveNumbers) { from = ResolveNameByNumber(from); } Log.Add(LogLevel.Debug, "SMS", String.Format("Message from {0} at {1}: {2}", from, received, text)); if (OnMessageReceived != null) { OnMessageReceived(this, new Message(from, received, text)); } } _gsm.DeleteMessage(msg.Index, msg.Storage); } sw.Stop(); Log.Add(LogLevel.Verbose, "SMS", String.Format("Reading took {0}ms", sw.Elapsed.TotalMilliseconds)); }