Ejemplo n.º 1
0
        private void MainThread()
        {
            Threads = new Dictionary <Int32, ThreadObj>();
            //eventLog1.BeginInit();
            STKTVMain.TVMain TvMain;
            int RequestInterval = 60000;

            DateTime TGKSendTime;
            DateTime AnalizerTime;

            AnalizerTime = DateTime.Now;
            TGKSendTime  = DateTime.Now;
            TGKSendTime  = TGKSendTime.AddMinutes(2);
            AnalizerTime = AnalizerTime.AddMinutes(10);

            DataRow dr;
            int     ModemCount = 0;
            bool    bLogged    = false;

            do
            {
                bLogged = false;
                TvMain  = new STKTVMain.TVMain();
                do
                {
                    // случилось команда на выход из сервиса
                    if (pStopServiceEvent.WaitOne(1, false))
                    {
                        try
                        {
                            InfoReport("Exiting working thread...");
                            StopDeviceThreads();
                            pStopApprovedEvent.Set();
                            return;
                        }
                        catch (Exception Ex)
                        {
                            ErrorReport("Exiting working thread error: " + Ex.Message);
                            return;
                        }
                    }



                    // пытаемся соединиться с базой данных
                    try
                    {
                        //InfoReport("Try DB login.");
                        if (TvMain.Init() == true)
                        {
                            bLogged         = true;
                            RequestInterval = TvMain.RequestInterval;
                        }
                        else
                        {
                            //pRefService.WarningReport("Unable to login, check credentials");
                            WarningReport("Unable to login, check credentials");
                            InfoReport(TvMain.GetEnvInfo());
                        }
                    }
                    catch (Exception Ex)
                    {
                        //pRefService.ErrorReport("Login failed, try again... " + Ex.Message);
                        ErrorReport("Login failed, try again... " + Ex.Message);
                        InfoReport(TvMain.GetEnvInfo());
                    }
                } while (!pStopServiceEvent.WaitOne(1000, false) && !bLogged);
                //pRefService.InfoReport("DB Initialization OK");
                //InfoReport("DB Initialization OK");



                // соединение прошло успешно
                if (bLogged)
                {
                    try
                    {
                        ModemCount = TvMain.GetStationModemCount();
                        System.Data.DataTable oRS;
                        ThreadObj             throbj = null;

                        oRS = null;
                        oRS = TvMain.GetDBDevicePlanListModem();


                        if (oRS != null)
                        {
                            if (oRS.Rows.Count > 0)
                            {
                                InfoReport("Checking " + oRS.Rows.Count + " active plan(s) at " + DateTime.Now.ToString());
                                for (int i = 0; i < oRS.Rows.Count && i < 10 && ActiveThreadsCount() <= ModemCount + 1; i++)
                                {
                                    try
                                    {
                                        dr    = oRS.Rows[i];
                                        id_bd = Convert.ToInt32(dr["id_bd"].ToString());
                                        if (!Threads.ContainsKey(id_bd))
                                        {
                                            // создаем и стартуем новый процесс для опроса устройства
                                            throbj       = new ThreadObj();
                                            throbj.DevID = id_bd;
                                            Process p = new Process();
                                            String  FileName;
                                            FileName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\STKDeviceThread.exe";
                                            String DirName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location);
                                            p.StartInfo.FileName         = FileName;
                                            p.StartInfo.Arguments        = "-D " + id_bd.ToString();
                                            p.StartInfo.WorkingDirectory = DirName;
                                            throbj.Process = p;
                                            Threads.Add(id_bd, throbj);
                                            throbj.Process.Start();
                                        }
                                        else
                                        {
                                            if (Threads[id_bd].Process.HasExited == true || Threads[id_bd].Process.StartTime.AddMinutes(15) <= DateTime.Now)
                                            {
                                                // инициализируем процесс еще раз
                                                // сначала убиваем процесс
                                                try
                                                {
                                                    Threads[id_bd].Process.Kill();
                                                }
                                                catch
                                                {
                                                }
                                                throbj       = new ThreadObj();
                                                throbj.DevID = id_bd;
                                                Process p = new Process();
                                                String  FileName;
                                                FileName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\STKDeviceThread.exe";
                                                String DirName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location);

                                                p.StartInfo.FileName         = FileName;
                                                p.StartInfo.Arguments        = "-D " + id_bd.ToString();
                                                p.StartInfo.WorkingDirectory = DirName;
                                                throbj.Process = p;
                                                Threads[id_bd] = throbj;
                                                throbj.Process.Start();
                                            }
                                        }
                                    }
                                    catch (Exception Ex)
                                    {
                                        ErrorReport("Thread " + id_bd.ToString() + " error:" + Ex.Message);
                                    }
                                    Thread.Sleep(8000);  // wait for modem pull locking
                                }
                            } // зершение цикла по активным устройствам

                            oRS    = null;
                            throbj = null;
                        }
                    }
                    catch
                    { }


                    //if (true)  //Analizer flag
                    //{
                    //    if (AnalizerTime <= DateTime.Now)
                    //    {
                    //        try{
                    //            System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\NCAnalizer.exe");
                    //            AnalizerTime = DateTime.Now.AddMinutes(13);
                    //        }catch {
                    //        }
                    //    }
                    //}

                    if (TvMain.SendToTGK)
                    {
                        if (TGKSendTime <= DateTime.Now)
                        {
                            try
                            {
                                System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\TGKSender.exe");
                                TGKSendTime = DateTime.Now.AddHours(6);
                            }
                            catch
                            {
                            }
                        }
                    }
                    try
                    {
                        TvMain.DeviceClose();
                        TvMain.CloseDBConnection();
                    }
                    catch { }
                }
                TvMain = null;
            } while (!pStopServiceEvent.WaitOne(RequestInterval, false));

            try
            {
                InfoReport("Closing STKServiceModem...");
                StopDeviceThreads();
                Threads.Clear();
                Threads = null;
                dr      = null;
                // base.OnStop();
                //eventLog1.Dispose();
                return;
            }
            catch (Exception Ex)
            {
                ErrorReport("Closing STKServiceModem error:" + Ex.Message);
            }
        }
Ejemplo n.º 2
0
        private void MainThread()
        {
            Threads = new Dictionary<Int32, ThreadObj>();
            eventLog1.BeginInit();
            STKTVMain.TVMain TvMain;
            int RequestInterval = 60000;

            DateTime TGKSendTime;
            DateTime AnalizerTime;
            AnalizerTime = DateTime.Now;
            TGKSendTime = DateTime.Now;
            TGKSendTime = TGKSendTime.AddMinutes(2);
            AnalizerTime = AnalizerTime.AddMinutes(10);

            DataRow dr;
            int ModemCount = 0;
            bool bLogged = false;
            do
            {
                bLogged = false;
                TvMain = new STKTVMain.TVMain();
                do
                {
                    // случилось команда на выход из сервиса
                    if (pStopServiceEvent.WaitOne(1, false))
                    {
                        try
                        {

                            InfoReport("Exiting working thread...");
                            StopDeviceThreads();
                            pStopApprovedEvent.Set();
                            return;
                        }
                        catch (Exception Ex)
                        {
                            ErrorReport("Exiting working thread error: " + Ex.Message);
                            return;
                        }
                    }



                    // пытаемся соединиться с базой данных
                    try
                    {

                        //InfoReport("Try DB login.");
                        if (TvMain.Init() == true)
                        {
                            bLogged = true;
                            RequestInterval = TvMain.RequestInterval;
                        }
                        else
                        {
                            //pRefService.WarningReport("Unable to login, check credentials");
                            WarningReport("Unable to login, check credentials");
                            InfoReport(TvMain.GetEnvInfo());
                        }
                    }
                    catch (Exception Ex)
                    {
                        //pRefService.ErrorReport("Login failed, try again... " + Ex.Message);
                        ErrorReport("Login failed, try again... " + Ex.Message);
                        InfoReport(TvMain.GetEnvInfo());
                    }
                } while (!pStopServiceEvent.WaitOne(1000, false) && !bLogged);
                //pRefService.InfoReport("DB Initialization OK");
                //InfoReport("DB Initialization OK");



                // соединение прошло успешно
                if (bLogged)
                {
                    try
                    {

                        ModemCount = TvMain.GetStationModemCount();
                        System.Data.DataTable oRS;
                        ThreadObj throbj = null;

                        oRS = null;
                        oRS = TvMain.GetDBDevicePlanListModem();


                        if (oRS != null)
                        {

                            if (oRS.Rows.Count > 0)
                            {
                                InfoReport("Checking " + oRS.Rows.Count + " active plan(s) at " + DateTime.Now.ToString());
                                for (int i = 0; i < oRS.Rows.Count && i < 10 && ActiveThreadsCount() < ModemCount;  i++)
                                {

                                    try
                                    {
                                        dr = oRS.Rows[i];
                                        id_bd = Convert.ToInt32(dr["id_bd"].ToString());
                                        if (!Threads.ContainsKey(id_bd))
                                        {

                                            // создаем и стартуем новый процесс для опроса устройства
                                            throbj = new ThreadObj();
                                            throbj.DevID = id_bd;
                                            Process p = new Process();
                                            String FileName;
                                            FileName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\STKDeviceThread.exe";
                                            String DirName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location);
                                            p.StartInfo.FileName = FileName;
                                            p.StartInfo.Arguments = "-D " + id_bd.ToString();
                                            p.StartInfo.WorkingDirectory = DirName;
                                            throbj.Process = p;
                                            Threads.Add(id_bd, throbj);
                                            throbj.Process.Start();


                                        }
                                        else
                                        {
                                            if (Threads[id_bd].Process.HasExited == true || Threads[id_bd].Process.StartTime.AddMinutes(30) <= DateTime.Now)
                                            {
                                                // инициализируем процесс еще раз
                                                // сначала убиваем процесс
                                                try
                                                {
                                                    Threads[id_bd].Process.Kill();
                                                }catch{

                                                }
                                                throbj = new ThreadObj();
                                                throbj.DevID = id_bd;
                                                Process p = new Process();
                                                String FileName;
                                                FileName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\STKDeviceThread.exe";
                                                String DirName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location);

                                                p.StartInfo.FileName = FileName;
                                                p.StartInfo.Arguments = "-D " + id_bd.ToString();
                                                p.StartInfo.WorkingDirectory = DirName;
                                                throbj.Process = p;
                                                Threads[id_bd] = throbj;
                                                throbj.Process.Start();

                                            }
                                          
                                           
                                        }

                                    }
                                    catch (Exception Ex)
                                    {
                                        ErrorReport("Thread " + id_bd.ToString() + " error:" + Ex.Message);
                                    }
                                    Thread.Sleep(8000);  // wait for modem pull locking
                                }
                            } // зершение цикла по активным устройствам 

                            oRS = null;
                            throbj = null;

                        }
                    }
                    catch 
                    { }


                    //if (true)  //Analizer flag
                    //{
                    //    if (AnalizerTime <= DateTime.Now)
                    //    {
                    //        try{
                    //            System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\NCAnalizer.exe");
                    //            AnalizerTime = DateTime.Now.AddMinutes(13);
                    //        }catch {
                    //        }
                    //    }
                    //}

                    if (TvMain.SendToTGK)
                    {

                        if (TGKSendTime <= DateTime.Now)
                        {
                            try{
                                System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\TGKSender.exe");
                                TGKSendTime = DateTime.Now.AddHours(6);
                            }
                            catch{
                            }
                        }
                    }
                    try
                    {
                        TvMain.DeviceClose();
                        TvMain.CloseDBConnection();
                    }
                    catch { }
                }
                TvMain = null;

               
            } while (!pStopServiceEvent.WaitOne(RequestInterval, false));

            try
            {
                InfoReport("Closing STKServiceModem...");
                StopDeviceThreads();
                Threads.Clear();
                Threads = null;
                dr = null;
                base.OnStop();
                eventLog1.Dispose();
                return;
            }
            catch (Exception Ex)
            {
                ErrorReport("Closing STKServiceModem error:" + Ex.Message);
            }
            
            
        }