Esempio n. 1
0
        private int ActiveThreadsCount()
        {
            int       Count = 0;
            ThreadObj tt;

            STKTVMain.TVMain TvMain = null;

            foreach (var pair in Threads)
            {
                tt = pair.Value;
                if (tt.Process.HasExited == true || tt.Process.StartTime.AddMinutes(30) <= DateTime.Now)
                {
                    try
                    {
                        tt.Process.Kill();
                    }
                    catch
                    {
                    }
                    if (TvMain == null)
                    {
                        TvMain = new STKTVMain.TVMain();
                    }
                    try
                    {
                        if (TvMain.Init())
                        {
                            TvMain.UnLockDevice(pair.Key);
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    Count++;
                }
            }

            return(Count);
        }
        private void MainThread()
        {
            eventLog1.BeginInit();

            DataRow dr      = null;
            bool    bLogged = false;

            TvMain = new STKTVMain.TVMain();

            try
            {
                if (TvMain.Init() == true)
                {
                    bLogged = true;
                }
                else
                {
                    WarningReport("Unable to login, check credentials");
                    InfoReport(TvMain.GetEnvInfo());
                    return;
                }
            }
            catch (Exception Ex)
            {
                ErrorReport("Login failed, try again... " + Ex.Message);
                InfoReport(TvMain.GetEnvInfo());
            }

            //InfoReport("DB Initialization OK");
            System.Data.DataTable oRS;
            if (bLogged)
            {
                oRS = null;
                String q;
                q = "select plancall.*,npip,nppassword,ipport,transport,sysdate ServerDate from bdevices join plancall on bdevices.id_bd=plancall.id_bd where  ( nplock is null or nplock <sysdate ) and npquery=1 " +
                    " and bdevices.id_bd=" + DivID.ToString() + " ";
                oRS = TvMain.QuerySelect(q);
                if (oRS != null)
                {
                    if (oRS.Rows.Count > 0)
                    {
                        try
                        {
                            dr = oRS.Rows[0];
                            DeviceThread(dr);
                        }
                        catch (Exception Ex)
                        {
                            ErrorReport("Прибор ID=  " + DivID.ToString() + " error:" + Ex.Message);
                            dr = null;
                        }
                    }
                    oRS = null;
                }



                try
                {
                    //InfoReport("Closing Device thread...");

                    //dr = null;

                    TvMain.ClearDuration();
                    // close transport
                    TvMain.DeviceClose();
                    TvMain.CloseDBConnection();
                    TvMain = null;
                    eventLog1.Dispose();
                    return;
                }
                catch (Exception Ex)
                {
                    ErrorReport("Closing DeviceThread error:" + Ex.Message);
                }
            }
        }
Esempio n. 3
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);
            }
        }
        private void Executor()
        {
            eventLog1.BeginInit();
            DataRow dr = null;
            bool bLogged = false;
            TvMain = new STKTVMain.TVMain();

            try
            {

                if (TvMain.Init() == true)
                {
                    bLogged = true;
                }
                else
                {
                    WarningReport("Unable to login, check credentials");
                    InfoReport(TvMain.GetEnvInfo());
                    return;
                }
            }
            catch (Exception Ex)
            {

                ErrorReport("Login failed, try again... " + Ex.Message);
                InfoReport(TvMain.GetEnvInfo());
            }

            //InfoReport("DB Initialization OK");
            System.Data.DataTable oRS;
            System.Data.DataTable devRS;
            if (bLogged)
            {

                devRS = TvMain.QuerySelect("select id_bd from bdevices where transport=5 and callerid='" + aSocket.callerID + "'");


                if (devRS.Rows.Count == 1)
                {
                    DivID = Convert.ToInt32(devRS.Rows[0]["id_bd"]);
                    oRS = null;


                    //if (TvMain.LockDevice(DivID, 6000, false))
                    {

                    oRS = TvMain.QuerySelect("select plancall.*,npip,nppassword,ipport,transport,sysdate ServerDate from bdevices join plancall on bdevices.id_bd=plancall.id_bd  where bdevices.id_bd=" + DivID.ToString());
                    if (oRS != null)
                    {

                        if (oRS.Rows.Count > 0)
                        {
                            try
                            {
                                dr = oRS.Rows[0];
                                ProcessPlan(dr);

                            }
                            catch (Exception Ex)
                            {
                                ErrorReport("Прибор ID=  " + DivID.ToString() + " error:" + Ex.Message);
                                dr = null;
                            }
                        }
                        oRS = null;

                    }

                    }


                    try
                    {
                        //InfoReport("Closing Device thread...");

                        //dr = null;

                        TvMain.ClearDuration();
                        // close transport
                        TvMain.DeviceClose();

                        if (dr != null)
                        {
                            AnalizeDevice(dr);
                        }
                        TvMain.CloseDBConnection();
                        TvMain = null;
                        eventLog1.Dispose();
                        return;
                    }
                    catch (Exception Ex)
                    {
                        ErrorReport("Closing ProcessPlan error:" + Ex.Message);
                    }

                }
                else
                {
                    if (devRS.Rows.Count ==0)
                        ErrorReport("Не обнаружен прибор для АССВ устройства с идентификатором: " + aSocket.callerID);
                    else
                        ErrorReport("Обнаружено " +devRS.Rows.Count.ToString() + " приборов для АССВ устройства с идентификатором: " + aSocket.callerID);
                }
            }


        }
Esempio n. 5
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
                    {

                       
                        System.Data.DataTable oRS;
                        ThreadObj throbj = null;

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


                        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 STKService...");
                StopDeviceThreads();
                Threads.Clear();
                Threads = null;
                dr = null;
               // base.OnStop();
                //eventLog1.Dispose();
                return;
            }
            catch (Exception Ex)
            {
                ErrorReport("Closing STKService error:" + Ex.Message);
            }


        }
Esempio n. 6
0
        private void MainThread()
        {
            NextPort = 0;
            Threads  = new Dictionary <Int32, Thread>();
            eventLog1.BeginInit();
            STKTVMain.TVMain TvMain;
            DataRow          dr;
            bool             bLogged = false;

            TvMain = new STKTVMain.TVMain();

            do
            {
                if (pStopServiceEvent.WaitOne(1, false))
                {
                    try
                    {
                        //TvMain.EndInit();
                        //pRefService.InfoReport("Exiting working thread...");
                        InfoReport("Exiting working thread...");
                        pStopApprovedEvent.Set();
                        return;
                    }
                    catch (Exception Ex)
                    {
                        ErrorReport("Exiting working thread error: " + Ex.Message);

                        return;
                    }
                }
                try
                {
                    //pRefService.InfoReport("Try DB login.");
                    InfoReport("Try DB login.");
                    if (TvMain.Init(true) == true)
                    {
                        bLogged = true;
                    }
                    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");
            //bool isgetplantable = false;

            //ƒл¤ проверки работы сервиса
            //Int16 a = 5;
            //while (a < 6)
            //{
            //    a = 5;
            //}

            do
            {
                System.Data.DataTable oRS;
                ThreadObj             throbj = null;

                oRS = null;

                if (DivID == 0)
                {
                    oRS = TvMain.GetDBDevicePlanList();
                }
                else
                {
                    oRS = TvMain.GetDBOneDevicePlanList(DivID);
                }

                if (oRS != null)
                {
                    //isgetplantable = true;
                    NextPort = 0;
                    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++)
                        {
                            try
                            {
                                dr    = oRS.Rows[i];
                                id_bd = Convert.ToInt32(dr["id_bd"].ToString());
                                //if (Threads[id_bd] == null)
                                if (!Threads.ContainsKey(id_bd))
                                {
                                    throbj = new ThreadObj();
                                    //throbj.dr = oRS.Rows[i];
                                    throbj.dr = null;
                                    throbj.dr = dr;

                                    //throbj.TvMain = TvMain;
                                    throbj.TvMain = new STKTVMain.TVMain();
                                    throbj.TvMain.Init(false);
                                    //throbj.TvMain.connect();

                                    Threads.Add(id_bd, new Thread(new ParameterizedThreadStart(this.CallThr)));
                                    Threads[id_bd].SetApartmentState(ApartmentState.MTA);
                                    Threads[id_bd].Name = "Service thread " + id_bd.ToString();
                                    //InfoReport("Starting  thread " + id_bd.ToString());
                                    Threads[id_bd].Start(throbj);
                                }
                                else
                                {
                                    if (Threads[id_bd].IsAlive == false)
                                    {
                                        throbj = new ThreadObj();
                                        //throbj.dr = oRS.Rows[i];
                                        throbj.dr = null;
                                        throbj.dr = dr;
                                        //throbj.TvMain = TvMain;
                                        throbj.TvMain = new STKTVMain.TVMain();
                                        throbj.TvMain.Init(false);
                                        Threads[id_bd] = new Thread(new ParameterizedThreadStart(this.CallThr));
                                        Threads[id_bd].SetApartmentState(ApartmentState.MTA);
                                        Threads[id_bd].Name = "Service thread " + id_bd.ToString();
                                        //InfoReport("Restarting  thread " + id_bd.ToString());
                                        Threads[id_bd].Start(throbj);
                                    }
                                }
                            }
                            catch (Exception Ex)
                            {
                                ErrorReport("Thread " + id_bd.ToString() + " error:" + Ex.Message);
                            }
                        }
                    }
                    oRS    = null;
                    throbj = null;
                }
            } while (!pStopServiceEvent.WaitOne(TvMain.RequestInterval, false));

            try
            {
                InfoReport("Closing STKService...");
                Threads.Clear();
                Threads = null;
                dr      = null;

                // close nport library
                TvMain.EndInit(true);
                TvMain.CloseDBConnection();
                TvMain = null;
                //eventLog1.Close();
                eventLog1.Dispose();
                return;
            }
            catch (Exception Ex)
            {
                ErrorReport("Closing STKService error:" + Ex.Message);
            }
        }
        private void MainThread()
        {

          
            eventLog1.BeginInit();
           
            DataRow dr=null;
            bool bLogged = false;
            TvMain = new STKTVMain.TVMain();
            
            try
            {
            
                if (TvMain.Init() == true)
                {
                    bLogged = true;
                }
                else
                {
                    WarningReport("Unable to login, check credentials");
                    InfoReport(TvMain.GetEnvInfo());
                    return;
                }
            }
            catch (Exception Ex)
            {
            
                ErrorReport("Login failed, try again... " + Ex.Message);
                InfoReport(TvMain.GetEnvInfo());
            }
      
            //InfoReport("DB Initialization OK");
            System.Data.DataTable oRS;
            if (bLogged)
            {
                oRS = null;
                oRS = TvMain.GetDBOneDevicePlanList(DivID);
                if (oRS != null)
                {

                    if (oRS.Rows.Count > 0)
                    {
                        try
                        {
                            dr = oRS.Rows[0];
                            DeviceThread(dr);

                        }
                        catch (Exception Ex)
                        {
                            ErrorReport("Прибор ID=  " + DivID.ToString() + " error:" + Ex.Message);
                            dr = null;
                        }
                    }
                    oRS = null;

                }




                try
                {
                    //InfoReport("Closing Device thread...");

                    //dr = null;

                    TvMain.ClearDuration(); 
                    // close transport
                    TvMain.DeviceClose();

                    if (dr != null)
                    {
                        AnalizeDevice(dr);
                    }
                    TvMain.CloseDBConnection();
                    TvMain = null;
                    eventLog1.Dispose();
                    return;
                }
                catch (Exception Ex)
                {
                    ErrorReport("Closing DeviceThread error:" + Ex.Message);
                }

                
            }
            
            
        }