예제 #1
0
        public override string OnPull(ParameterizedMap map)
        {
            StringBuilder responseJson  = new StringBuilder();
            bool          deviceRemoved = false;

            GSMCommunication.Feature.BasicInformation basic = map.TryGet <BasicInformation>("base");
            if (basic != null)
            {
                try
                {
                    basic.OnBeginExecuting();
                    List <BaseResult <SMSReadResult> > list = actions.Get <ISMS>().ReadAll(map);
                    actions.Get <ISMS>().DeleteAll(map);
                    responseJson = new StringBuilder(Newtonsoft.Json.JsonConvert.SerializeObject(list));
                }
                catch (System.IO.IOException ex)
                {
                    /// port closed
                    IErrorLogging log = ObjectPool.Instance.Resolve <IErrorLogging>();
                    log.Write(ex.Message + " - pull command (I/O)");
                    log.Write("plug the device...");
                    deviceRemoved = true;
                }
                catch (System.InvalidOperationException ioe)
                {
                    // port closed
                    IErrorLogging log = ObjectPool.Instance.Resolve <IErrorLogging>();
                    log.Write(ioe.Message + " - pull command (invalid operation)");
                    log.Write("plug the device...");
                    deviceRemoved = true;
                }
                finally
                {
                    basic.OnEndExecuting();
                }
            }
            if (deviceRemoved)
            {
                Thread.Sleep(1000);
                IServer server = ObjectPool.Instance.Resolve <IServer>();
                server.OnDeviceRemoved();
            }
            return(responseJson.ToString());
        }
예제 #2
0
        public override string OnPush(ParameterizedMap map)
        {
            dynamic       response      = null;
            bool          deviceRemoved = false;
            StringBuilder responseJson  = new StringBuilder();

            GSMCommunication.Feature.BasicInformation basic = map.TryGet <BasicInformation>("base");
            if (basic != null)
            {
                try
                {
                    basic.OnBeginExecuting();
                    List <string> command = map.TryGet <List <string> >("command");

                    if (command.Where(s => s.ToLower() == "help").Any())
                    { // if command contains 'help' word
                        ActionInvoker invoker = actions.ToList().Where(d => d.InterfaceType.Name.ToLower() == command[1].ToLower()).SingleOrDefault();
                        if (invoker != null)
                        {
                            responseJson = new StringBuilder(Newtonsoft.Json.JsonConvert.SerializeObject(invoker.Commands,
                                                                                                         Newtonsoft.Json.Formatting.None,
                                                                                                         new Newtonsoft.Json.JsonSerializerSettings()
                            {
                                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                            }));
                        }
                    }
                    else
                    {
                        ActionInvoker invoker = actions.ToList().Where(d => d.InterfaceType.Name.ToLower() == command[0].ToLower()).SingleOrDefault();
                        if (invoker != null)
                        {
                            response = invoker.TryInvoke(command[1], new object[] { map });
                            if (response != null)
                            {
                                responseJson = new StringBuilder(Newtonsoft.Json.JsonConvert.SerializeObject(response,
                                                                                                             Newtonsoft.Json.Formatting.None,
                                                                                                             new Newtonsoft.Json.JsonSerializerSettings()
                                {
                                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                                }));
                            }
                        }
                    }
                }
                catch (UnauthorizedAccessException uae)
                {
                    IErrorLogging log = ObjectPool.Instance.Resolve <IErrorLogging>();
                    log.Write(uae.Message + " - push command (unauthorized access)");
                    log.Write("plug the device...");
                    deviceRemoved = true;
                }
                catch (System.IO.IOException ioe)
                {
                    IErrorLogging log = ObjectPool.Instance.Resolve <IErrorLogging>();
                    log.Write(ioe.Message + " - push command (I/O)");
                    log.Write("plug the device...");
                    deviceRemoved = true;
                }
                finally
                {
                    basic.OnEndExecuting();
                }
            }
            if (deviceRemoved)
            {
                Thread.Sleep(1000);
                IServer server = ObjectPool.Instance.Resolve <IServer>();
                server.OnDeviceRemoved();
            }
            return(responseJson.ToString());
        }
예제 #3
0
파일: SMS.cs 프로젝트: sandalkuilang/texto
 public SMS(BasicInformation connector)  
 { 
     this.Connection = connector; 
 }
예제 #4
0
 public Call(BasicInformation connector)
 {
     this.Connection = connector;
 }
예제 #5
0
 public PhoneBook(BasicInformation connector) 
 {
     this.Connection = connector;
 }
예제 #6
0
 private void RaiseError(BasicInformation connection, Exception ex, string location)
 {
     portColletion.Add(connection);
     if (portColletion.Count > 0)
         Console.WriteLine("connection has been restored.");
     IErrorLogging log = ObjectPool.Instance.Resolve<IErrorLogging>();
     log.Write(string.Format("{0} - {1}", ex, location));
 }
예제 #7
0
        private void InitSerialPort(BasicInformation connection)
        {
            //// let the connection remains open until the server is closed/disposed
            connection.Connector.Open();

            connection.OnBeginExecuting();
 
            BaseResult<GenericTypeResult<string>> manufacturer = connection.GetManufacturer();
            if (!string.IsNullOrEmpty(manufacturer.Response.Result))
            {
                connection.GetServiceCenter();
                connection.SetErrorMessageFormat(1);
                connection.GetOperator();

                PhoneBook pb = new PhoneBook(connection);
                pb.SetPhoneBookMemory(MemoryStorage.SIMOwnNumber);
                pb.GetInfo();

                SMS sms = new SMS(connection);
                sms.SetMessageStorage(MemoryStorage.MobilePhonebook, MemoryStorage.MobilePhonebook, MemoryStorage.MobilePhonebook);
                sms.SetMessageFormat(connection.PDUMode);

                string prefixOwnNumber = string.Empty;
                if (string.IsNullOrEmpty(prefixOwnNumber))
                {
                    GSMServer.Configuration.IConfiguration configuration = ObjectPool.Instance.Resolve<GSMServer.Configuration.IConfiguration>();
                    prefixOwnNumber = ((ApplicationSettings)configuration).General.PrefixOwnNumber;
                }

                BaseResult<GenericTypeResult<List<PhoneNumberInfo>>> list = pb.ReadPhoneBook(MemoryStorage.SIMOwnNumber, 1, -1);
                if (list.Response.Result.Count > 0)
                {
                    foreach (PhoneNumberInfo info in list.Response.Result)
                    {
                        if (info.Name.Equals(prefixOwnNumber))
                        {
                            connection.OwnNumber = info.PhoneNumber;
                            break;
                        }
                    }
                }
                portColletion.Add(connection);
            }
            connection.OnEndExecuting();
        }
예제 #8
0
        private void Initialize(string ipAddress, int port)
        {
            /// set culture to US
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            /// register configuration & worker  
            ObjectPool.Instance.Register<IServer>().ImplementedBy(this);
            ObjectPool.Instance.Register<GSMServer.Configuration.IConfiguration>().ImplementedBy(new ApplicationSettings());
            ObjectPool.Instance.Register<Worker.IPipeline>().ImplementedBy(new DefaultPipeline(new ActionInvokerLookup(this)));

            /// register logging IInternalLogging
            ObjectPool.Instance.Register<IInternalLogging>().ImplementedBy(new BaseInternalLogging());
            ObjectPool.Instance.Register<ISMSLogging>().ImplementedBy(new ArchiveSMSLogging()); 

            /// initialize database
            InitializeDatabase();
             
            GSMServer.Configuration.IConfiguration settings = ObjectPool.Instance.Resolve<GSMServer.Configuration.IConfiguration>();

            portColletion = new RandomConnectionProvider(); 
            workerPoolManager = new WorkerPoolManager();
            workerPoolManager.AddPool(new DatabaseWorkerPool());
            workerPoolManager.AddPool(new MemoryWorkerPool());

            pluginMap = new PluginObserver();

            string countryCode = ((ApplicationSettings)settings).General.CountryCode;
            string IsNullintervalProcessQueue = ((ApplicationSettings)settings).General.IntervalWorkerQueue.ToString();
            string IsNullintervalReadMessage = ((ApplicationSettings)settings).General.IntervalReadMessage.ToString();

            int intervalProcessQueue;
            int intervalReadMessage = TimeConstant.READ_TIMEOUT;

            if (string.IsNullOrEmpty(IsNullintervalProcessQueue))
                throw new System.Configuration.ConfigurationErrorsException("Interval Queue cannot be null or zero.");
            else
            {
                intervalProcessQueue = Convert.ToInt32(IsNullintervalProcessQueue);
                if (intervalProcessQueue < 1000)
                {
                    throw new InvalidOperationException("Interval Queue may cause invalid operation while sending SMS because value is to fast.");
                }
            }

            BasicInformation connection;
            StringBuilder portname, sparity, sstop, shand;
            StringBuilder serviceCenter = new StringBuilder();
            bool pduMode = false; 
            int baudrate, databits;
            Parity parity;
            StopBits stopBits;
            Handshake handshake;

            /// for comparison is the configuration in accordance with the detected serial ports by OS?
            string[] ports = System.IO.Ports.SerialPort.GetPortNames(); 

            foreach (GSMServer.Configuration.SerialPort node in ((ApplicationSettings)settings).SerialPorts.Items)
            {
                portname = new StringBuilder(node.PortName);
                pduMode = false;
                pduMode = node.PDUMode; 
                if (!string.IsNullOrEmpty(node.ServiceCenter))
                {
                    serviceCenter = new StringBuilder(countryCode + node.ServiceCenter.Substring(1, node.ServiceCenter.Length - 1));
                } 

                baudrate = Convert.ToInt32(node.BaudRate);
                databits = Convert.ToInt32(node.DataBits);
                parity = new Parity();
                sparity = new StringBuilder(node.Parity.ToLower());
                if (sparity.ToString() == "even")
                    parity = Parity.Even;
                else
                    if (sparity.ToString() == "mark")
                        parity = Parity.Mark;
                    else
                        if (sparity.ToString() == "none")
                            parity = Parity.None;
                        else
                            if (sparity.ToString() == "odd")
                                parity = Parity.Odd;
                            else
                                if (sparity.ToString() == "space")
                                    parity = Parity.Space; 
                stopBits = new StopBits();
                sstop = new StringBuilder(node.StopBits.ToLower());
                if (sstop.ToString() == "none")
                    stopBits = StopBits.None;
                else
                    if (sstop.ToString() == "one")
                        stopBits = StopBits.One;
                    else
                        if (sstop.ToString() == "onepointfive")
                            stopBits = StopBits.OnePointFive;
                        else
                            if (sstop.ToString() == "two")
                                stopBits = StopBits.Two;
                handshake = new Handshake();
                shand = new StringBuilder(node.Handshake.ToLower());
                if (shand.ToString() == "none")
                    handshake = Handshake.None;
                else
                    if (shand.ToString() == "requesttosend")
                        handshake = Handshake.RequestToSend;
                    else
                        if (shand.ToString() == "RequestToSendXOnXOff")
                            handshake = Handshake.RequestToSendXOnXOff;
                        else
                            if (shand.ToString() == "xonxoff")
                                handshake = Handshake.XOnXOff;

                /// check whether port there is in collection?
                if (ports.Where(b => b.ToLower().Equals(portname.ToString().ToLower())).SingleOrDefault() != null)
                {
                    connection = new BasicInformation(portname.ToString(), baudrate, parity, stopBits, databits, handshake, serviceCenter.ToString(), pduMode);
                    InitSerialPort(connection);
                }
            }
                  
            GSMServer.Plugin.Plugin plugin;
            foreach (PluginElement pluginElement in ((ApplicationSettings)settings).Plugins.Items)
            {
                plugin = PluginActivator.Create(pluginElement.AssemblyFile, pluginElement.Type);
                if (plugin != null)
                {
                    pluginMap.Add(pluginElement.AssemblyFile, plugin);
                }
            } 

            availableConnections = portColletion.ToList();
            if (portColletion.Count > 0)
            { 
                intervalProcessQueue = (intervalProcessQueue / portColletion.Count) + TimeConstant.DEFAULT_INTERVAL_QUEUE;
            }
            else
            {
                intervalProcessQueue = (intervalProcessQueue) + TimeConstant.DEFAULT_INTERVAL_QUEUE;
            }

            timerReadQueue = new System.Timers.Timer(intervalReadMessage);
            timerReadQueue.Elapsed += timerReadQueue_Elapsed;

            timerProcessRequestQueue = new System.Timers.Timer(intervalProcessQueue);
            timerProcessRequestQueue.Elapsed += TimerProcessingRequest_Elapsed;

            if (portColletion.Count > 0)
            {
                if (!this.Active) 
                    this.BeginAcceptClient();
            }

            base.PacketReceived += OnPacketReceived;
            base.Connected += OnClientConnected;
            base.Disconnected += OnClientDisconnect;
            base.Closed += OnClosed;
            base.Open += OnOpen;
        }
예제 #9
0
 public PhoneBook(BasicInformation connector)
 {
     this.Connection = connector;
 }