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);
                }
            }
        }
        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);
                }
            }


        }
        private void DeviceThread(DataRow dr)
        {
            DateTime SrvDate;
            DateTime ddd;
            Boolean  DeviceOK;
            Int16    archType_moment = 1;
            Int16    ncall           = 0;
            Int16    nmaxcall        = 5;
            Int16    minrepeat       = 5;

            try
            {
                #region "init"
                ncall     = Convert.ToInt16(dr["ncall"]);
                nmaxcall  = Convert.ToInt16(dr["nmaxcall"]);
                minrepeat = Convert.ToInt16(dr["minrepeat"]);
                Int32 id_bdc;

                id_bdc = Convert.ToInt32(dr["id_bd"].ToString());

                SrvDate = DateTime.Now;
                try
                {
                    SrvDate = Convert.ToDateTime(dr["ServerDate"].ToString());
                }
                catch {
                }



                {
                    TvMain.ClearDuration();
                    if (TvMain.LockDevice(id_bdc, 60 * 40, false))
                    {
                        if (TvMain.DeviceInit(id_bdc))
                        {
                            DeviceOK = true;
                            //TvMain.SaveLog(id_bdc,0,"??",1,"Инициализация транспортного уровня:OK");
                        }
                        else
                        {
                            bool SkipErr = false;
                            if (TvMain.TVD != null)
                            {
                                if (TvMain.TVD.Transport == 0)
                                {
                                    if (TvMain.TVD.ComPort == "")
                                    {
                                        SkipErr = true;
                                    }
                                    if (TvMain.PortBusy)
                                    {
                                        SkipErr = true;
                                    }
                                }
                            }
                            if (!SkipErr)
                            {
                                string tError = "";
                                try
                                {
                                    tError = TvMain.ConnectStatus();
                                }
                                catch (Exception)
                                {
                                    tError = "";
                                }


                                if (tError != "")
                                {
                                    TvMain.WriteErrToDB(id_bdc, DateTime.Now, tError);
                                    TvMain.SaveLog(id_bdc, 0, "??", 1, tError);
                                }
                                else
                                {
                                    if (TvMain.TVD != null)
                                    {
                                        if (TvMain.TVD.DriverTransport != null)
                                        {
                                            tError = TvMain.TVD.DriverTransport.GetError;
                                        }
                                    }
                                    TvMain.WriteErrToDB(id_bdc, DateTime.Now, "Ошибка транспорта. " + tError);
                                    TvMain.SaveLog(id_bdc, 0, "??", 1, "Ошибка транспорта. " + tError);
                                }
                            }
                            DeviceOK = false;
                            TvMain.UnLockDevice(id_bdc);
                            TvMain.SaveLog(id_bdc, 0, "??", 1, "Снятие блокировки устройства");
                        }
                    }
                    else
                    {
                        TvMain.SaveLog(id_bdc, 0, "??", 1, "Тепловычислитель занят");
                        DeviceOK = false;
                    }
                    if (DeviceOK)
                    {
                        TvMain.connect();
                        if (TvMain.isConnected() == false)
                        {
                            string tError = "";
                            try
                            {
                                tError = TvMain.ConnectStatus();
                            }
                            catch (Exception)
                            {
                                tError = "";
                            }
                            if (tError != "")
                            {
                                TvMain.WriteErrToDB(id_bdc, DateTime.Now, tError);
                                TvMain.SaveLog(id_bdc, 0, "??", 1, tError);
                            }
                            else
                            {
                                TvMain.WriteErrToDB(id_bdc, DateTime.Now, "Ошибка соединения. " + tError);
                                TvMain.SaveLog(id_bdc, 0, "??", 1, "Ошибка соединения. " + tError);
                            }

                            if (ncall + 1 < nmaxcall)
                            {
                                TvMain.SetNCALLToPlanCall(id_bdc.ToString(), ncall + 1);
                                TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dlock", DateTime.Now);
                            }
                            else
                            {
                                TvMain.SetNCALLToPlanCall(id_bdc.ToString(), 0);

                                ddd = SrvDate;
                                try
                                {
                                    ddd = Convert.ToDateTime(dr["dnextcurr"].ToString());
                                }
                                catch (System.Exception ex)
                                {
                                    InfoReport("Прибор ID=  " + id_bdc.ToString() + " error converting dnextcurr :" + dr["dnextcurr"].ToString());
                                    TvMain.SaveLog(id_bdc, 3, "??", 0, "Ошибка преобразования даты (dnextcurr) :" + dr["dnextcurr"].ToString() + " " + ex.Message);
                                }
                                while (ddd < SrvDate)
                                {
                                    ddd = ddd.AddHours(1);
                                }
                                ddd = ddd.AddMinutes(-minrepeat);

                                TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dlock", ddd);
                            }

                            ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " Counter initialization Error!");
                            DeviceOK = false;
                        }
                        else
                        {
                            TvMain.SaveLog(id_bdc, 0, "??", 1, "Соединение с тепловычислителем:OK");
                        }
                    }
                    #endregion "init"
                    if (DeviceOK)
                    {
                        TvMain.SetNCALLToPlanCall(id_bdc.ToString(), 0);


                        #region "moment"


                        ddd = SrvDate;

                        if (TvMain.TVD.IsConnected())
                        {
                            InfoReport("Прибор ID=  " + id_bdc.ToString() + " чтение текущих архивов на " + ddd.ToString());
                            try
                            {
                                if (TvMain.LockDevice(id_bdc, 400, true))
                                {
                                    TvMain.HoldLine();
                                    String str;
                                    TvMain.ClearDuration();
                                    str = TvMain.readmarch();
                                    if (str.Length == 0)
                                    {
                                        TvMain.WriteErrToDB(id_bdc, SrvDate, "Ошибка чтения архива");
                                        TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Ошибка чтения текущего архива");
                                    }
                                    else
                                    {
                                        if (str.Substring(0, 6) != "Ошибка")
                                        {
                                            if (TvMain.TVD.isMArchToDBWrite)
                                            {
                                                TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Текущий архив" + ":OK");
                                                TvMain.WritemArchToDB();
                                            }
                                            //);


                                            TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dnextcurr", SrvDate.AddMilliseconds(500));
                                        }
                                        else
                                        {
                                            TvMain.WriteErrToDB(id_bdc, SrvDate, str);
                                            TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Ошибка чтения текущего архива " + str);



                                            TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dnextcurr", SrvDate.AddMilliseconds(500));
                                        }
                                    }
                                    //TvMain.UnLockDevice(id_bdc);
                                }
                            }//try
                            catch (Exception Ex)
                            {
                                TvMain.WriteErrToDB(id_bdc, SrvDate, "Ошибка чтения архива:" + Ex.Message);
                                ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " read arch failed, " + Ex.Message);
                            }
                        }//if (ccurr)



                        #endregion "moment"



                        #region "commands"
                        if (TvMain.TVD.IsConnected())
                        {
                            InfoReport("Прибор ID=  " + id_bdc.ToString() + " Отработка очереди комманд ");
                            try
                            {
                                if (TvMain.LockDevice(id_bdc, 2000, true))
                                {
                                    TvMain.HoldLine();
                                    TvMain.ClearDuration();
                                    if (TvMain.TVD.ProcessComands() > 0)
                                    {
                                        TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Отработка очереди комманд: OK");
                                    }
                                }
                            }//try
                            catch (Exception Ex)
                            {
                                TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Отработка очереди комманд:" + Ex.Message);
                                ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " commands processing error, " + Ex.Message);
                            }
                        }
                        #endregion

                        TvMain.UnLockDevice(id_bdc);
                        string transpname = "";
                        if (TvMain.TVD != null)
                        {
                            if (TvMain.TVD.Transport == 0)
                            {
                                transpname = TvMain.TVD.ComPort;
                            }
                        }
                        TvMain.SaveLog(id_bdc, 0, "??", 1, "Закрытие канала. " + transpname);
                    }
                    else
                    {
                        ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " transport initialization Error!");
                    }
                }
            }
            catch (System.Exception threadEx)
            {
                ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " thread failed, " + threadEx.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);
                }

                
            }
            
            
        }