Exemplo n.º 1
0
        private static void ModemPollingLoop()
        {
            //Find a port where the modem is at
            _logger.Info("Searching for a modem...");
            for (int i = 1; i <= 256; i++)
            {
                if (TestConnection($"COM{i}"))
                {
                    break;
                }
            }

            if (_comm.IsOpen() && _comm.IsConnected())
            {
                _logger.Info("Modem found");
                _logger.Info("Setting modem settings...");
                _comm.SelectCharacterSet("ucs2"); //Use UTF-16 encoding when getting data from modem

                //Hook debugging event if user wants to debug modem commands
                if (_config.GetValue("Debug/ModemCommands", false))
                {
                    _comm.LoglineAdded += comm_LoglineAdded;
                }

                try
                {
                    _comm.SetSmscAddress(_config.GetValue("General/SMSCenter", ""));
                }
                catch (Exception)
                {
                    _logger.Warn("Invalid SMS Center number or this feature is not supported by current modem");
                }

                _logger.Info("Listening for incoming messages...");
                while (_comm.IsOpen())
                {
                    if (_comm.IsConnected())
                    {
                        try
                        {
                            //Get all messages saved on SIM
                            DecodedShortMessage[] messages = _comm.ReadMessages(PhoneMessageStatus.All,
                                                                                PhoneStorageType.Sim);

                            foreach (DecodedShortMessage message in messages)
                            {
                                SmsPdu pdu = message.Data;
                                if (pdu is SmsDeliverPdu) // Check if it's SMS and not some kind of a service message
                                {
                                    SmsDeliverPdu data = (SmsDeliverPdu)pdu;
                                    _logger.Debug($"Received message from {data.OriginatingAddress}: " +
                                                  data.UserDataText + $" [TIMESTAMP: {data.SCTimestamp.ToSortableString()}]");

                                    ChangePassword(data.OriginatingAddress, data.UserDataText);

                                    //Delete message so it won't be parsed again
                                    //and so we don't have a problem with free space on SIM
                                    //in the future
                                    _comm.DeleteMessage(message.Index, message.Storage);
                                }
                                else
                                {
                                    _logger.Warn($"Received unknown message type: {pdu.GetType()}");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Warn($"Cannot read messages: {ex.Message} ({ex.GetType()})");
                            break;
                        }

                        //Sleep 5 seconds between message checks
                        Thread.Sleep(5000);
                    }
                    else
                    {
                        _logger.Error("Lost cellular network connection");
                        break;
                    }
                }
            }
            else
            {
                _logger.Fatal("No modem found");
            }

            //Close modem connection
            try
            {
                _comm.Close();
            }
            catch (Exception) { }
        }