diff --git a/MTC_Adapter/MTC-ADB/AdapterGeneric.cs b/MTC_Adapter/MTC-ADB/AdapterGeneric.cs index a8080c1..42025a0 100644 --- a/MTC_Adapter/MTC-ADB/AdapterGeneric.cs +++ b/MTC_Adapter/MTC-ADB/AdapterGeneric.cs @@ -31,20 +31,16 @@ namespace MTC_ADB /// /// valore booleano di check se sia stato AVVIATO l'adapter (Running) /// - public bool adpRunning = false; + public bool adpRunning; /// /// valore booleano di check se l'adapter STIA SALVANDO /// - public bool adpSaving = false; + public bool adpSaving; /// /// valore booleano (richiesta di riavvio automatico) /// public bool adpTryRestart; /// - /// porta x adapter (x restart) - /// - protected int adpPortNum; - /// /// ultimo ID letto /// protected uint lastId = 0; @@ -166,15 +162,18 @@ namespace MTC_ADB // salvo al form chiamante parentForm = caller; + adpRunning = false; + adpSaving = false; + // item base mAdapter.AddDataItem(mAvail); mAdapter.AddDataItem(mStatus); mAdapter.AddDataItem(mClock); mAdapter.AddDataItem(mMessage); - mAvail.Value = ""; - mStatus.Value = ""; - mClock.Value = ""; - mMessage.Value = ""; + mAvail.Value = "ND"; + mStatus.Value = "ND"; + mClock.Value = string.Format("{0:yyyy-MM-dd} {0:HH:mm:ss}", DateTime.Now); + mMessage.Value = "INIT"; createDbConn(); @@ -204,12 +203,16 @@ namespace MTC_ADB /// public void loadServerConf() { - ServerType = utils.CRS("ServerType"); - ServerAddress = utils.CRS("ServerAddress"); - DbName = utils.CRS("DbName"); - DbUser = utils.CRS("DbUser"); - DbPwd = utils.CRS("DbPwd"); - DbConn.ConnectionString = string.Format("SERVER={0};database={1};uid={2};password={3}", ServerAddress, DbName, DbUser, DbPwd); + // disconnetto e riconnetto SE fosse giĆ  connesso... + if (!connectionOk) + { + ServerType = utils.CRS("ServerType"); + ServerAddress = utils.CRS("ServerAddress"); + DbName = utils.CRS("DbName"); + DbUser = utils.CRS("DbUser"); + DbPwd = utils.CRS("DbPwd"); + DbConn.ConnectionString = string.Format("SERVER={0};database={1};uid={2};password={3}", ServerAddress, DbName, DbUser, DbPwd); + } } /// @@ -242,7 +245,6 @@ namespace MTC_ADB // salvo porta! mAdapter.Port = port; - adpPortNum = port; // avvio! mAdapter.Start(); @@ -286,7 +288,6 @@ namespace MTC_ADB lg.Error(exc, "Eccezione in chiusura Adapter"); } dtStopAdp = DateTime.Now; - adpPortNum = mAdapter.Port; adpTryRestart = tryRestart; adpRunning = false; // chiudo! @@ -355,7 +356,7 @@ namespace MTC_ADB catch (Exception exc) { // segnalo eccezione e indico disconnesso... - lg.Error(exc, "Errore in gestione ciclo principale ADP, fermo adapter"); + lg.Error(exc, string.Format("Errore in gestione ciclo principale ADP, fermo adapter{0}{1}", Environment.NewLine, exc)); parentForm.fermaAdapter(true); } // tolgo flag running @@ -443,15 +444,22 @@ namespace MTC_ADB public virtual void tryConnect() { mMessage.Code = "DB-CONN"; - try + if (!connectionOk) { - DbConn.Open(); - mMessage.Value = "ADP - DB Connesso"; + try + { + DbConn.Open(); + mMessage.Value = "ADP - DB Connesso"; + } + catch (Exception exc) + { + lg.Error(exc, string.Format("Errore apertura connessione DB: {0}{1}", Environment.NewLine, exc)); + mMessage.Value = "ADP - Errore apertura connessione DB"; + } } - catch (Exception exc) + else { - lg.Error(exc, string.Format("Errore apertura connessione DB: {0}{1}", Environment.NewLine, exc)); - mMessage.Value = "ADP - Errore apertura connessione DB"; + lg.Info("Exit tryConnect (ADP connected)"); } } /// @@ -496,18 +504,23 @@ namespace MTC_ADB DbCommand qrycmd = DbConn.CreateCommand(); qrycmd.CommandText = Query; qrycmd.Connection = DbConn; - DbDataReader reader = qrycmd.ExecuteReader(); - try + using (DbDataReader reader = qrycmd.ExecuteReader()) { - while (reader.Read()) + try { - answ = reader["num"].ToString(); + while (reader.Read()) + { + answ = reader["num"].ToString(); + } + // chiudo reader! + reader.Close(); + + } + catch (Exception exc) + { + lg.Error(exc, string.Format("Errore in recupero conteggio eventi: {0}{1}", Environment.NewLine, exc)); + answ = "ERR"; } - } - catch (Exception exc) - { - lg.Error(exc, string.Format("Errore in recupero conteggio eventi: {0}{1}", Environment.NewLine, exc)); - answ = "ERR"; } } return answ; diff --git a/MTC_Adapter/MTC-ADB/App.config b/MTC_Adapter/MTC-ADB/App.config index b4ad8e2..42d8605 100644 --- a/MTC_Adapter/MTC-ADB/App.config +++ b/MTC_Adapter/MTC-ADB/App.config @@ -15,6 +15,8 @@ + + diff --git a/MTC_Adapter/MTC-ADB/MainForm.cs b/MTC_Adapter/MTC-ADB/MainForm.cs index b0456ba..98bebad 100644 --- a/MTC_Adapter/MTC-ADB/MainForm.cs +++ b/MTC_Adapter/MTC-ADB/MainForm.cs @@ -43,23 +43,23 @@ namespace MTC_ADB /// /// timer base (base moltiplica) /// - protected int timerIntMs = 100; // di norma 100 msec x refresh... + protected int timerIntMs = 100; // di norma 100 msec x timer base... /// /// contatore veloce /// - protected int fastCount = 10; // di norma 1 sec x refresh... + protected int fastCount = 1; // 100ms primo refresh... /// /// contatore normale /// - protected int normCount = 300; // di norma 30 sec x refresh... + protected int normCount = 2; // 200ms primo refresh... /// /// contatore slow /// - protected int slowCount = 600; // di norma 60 sec x refresh... + protected int slowCount = 3; // 300ms primo refresh... /// /// contatore lentissimo /// - protected int verySlowCount = 1200; // di norma 2 minuti + protected int verySlowCount = 4; // 400ms primo refresh... /// /// ultimo tentativo riavvio... /// @@ -155,6 +155,7 @@ namespace MTC_ADB avviaAdapter(); } checkStarted(); + checkConnected(); } /// @@ -168,10 +169,29 @@ namespace MTC_ADB checkConnected(); } /// + /// Tentativo connessione al DB + /// + private void connectAdapter() + { + displayTaskAndWait("...CONNECTIONG DB..."); + agObj.tryConnect(); + checkConnected(); + } + /// + /// Tentativo disconnessione al DB + /// + private void disconnectAdapter() + { + displayTaskAndWait("...DISCONNECTIONG DB..."); + agObj.tryDisconnect(); + checkConnected(); + } + /// /// Verifica se il server sia connesso... /// private void checkConnected() { + lblLastData.Text = DateTime.Now.ToString("yy-MM-dd HH:mm:ss"); if (agObj.connectionOk) { lblStatus.Text = "CONNECTED"; @@ -179,6 +199,7 @@ namespace MTC_ADB lblStatus.ForeColor = Color.Yellow; btnConnect.Text = "close"; lblServer.ForeColor = Color.Black; + lblServer.Text = string.Format("{0} | {1}", agObj.ServerType, agObj.ServerAddress); // recupero conteggio degli eventi... advProgBar(); lblNumData.Text = string.Format("{0} new data", agObj.numRecAvail); @@ -385,14 +406,16 @@ namespace MTC_ADB { // avvio adapter vero e proprio... displayTaskAndWait("Adapter starting"); - - // inizio con la connessione al DB - loadServerConf(); - advProgBar(); - agObj = new AdapterGeneric(this); + // inizio con la connessione al DB + if (!agObj.connectionOk) + { + loadServerConf(); + connectAdapter(); + advProgBar(); + } agObj.startAdapter(portMTC); - displayTaskAndWait("Adapter started!"); + //displayTaskAndWait("Adapter started!"); btnOpenDump.Enabled = true; if (utils.CRB("openDumpOnStart")) { @@ -406,6 +429,7 @@ namespace MTC_ADB fastCount = utils.CRI("fastCount"); normCount = utils.CRI("normCount"); slowCount = utils.CRI("slowCount"); + verySlowCount = utils.CRI("verySlowCount"); displayTaskAndWait("Adapter Running..."); } @@ -460,6 +484,7 @@ namespace MTC_ADB lastStartTry = DateTime.Now; agObj.tryConnect(); agObj.loadPersData(); + checkStarted(); checkConnected(); } } @@ -493,7 +518,6 @@ namespace MTC_ADB if (slowCount <= 0) { slowCount = utils.CRI("slowCount"); - // avvio fase raccolta dati e invio con adapter agObj.gaterAndSend(gatherCycle.LF); } } @@ -503,7 +527,6 @@ namespace MTC_ADB if (verySlowCount <= 0) { verySlowCount = utils.CRI("verySlowCount"); - // avvio fase raccolta dati e invio con adapter agObj.gaterAndSend(gatherCycle.VLF); // effettua salvataggio del file di conf con i valori ATTUALI dei parametri ove applicabile/aggiornati (es ore utilizzo, KM assi percorsi...) --> sia file corrente che file "history" persistData(); @@ -558,8 +581,7 @@ namespace MTC_ADB if (normCount <= 0) { normCount = utils.CRI("normCount"); - // avvio fase raccolta dati e invio con adapter - agObj.gaterAndSend(gatherCycle.MF); + agObj.gaterAndSend(gatherCycle.MF); } } /// @@ -585,9 +607,8 @@ namespace MTC_ADB if (fastCount <= 0) { fastCount = utils.CRI("fastCount"); - // avvio fase raccolta dati e invio con adapter agObj.gaterAndSend(gatherCycle.HF); - } + } } @@ -730,12 +751,16 @@ namespace MTC_ADB lblStatus.BackColor = Color.Goldenrod; displayTaskAndWait("Closing DB Connection"); // CHIUDO! - agObj.tryDisconnect(); + disconnectAdapter(); } else { + lblStatus.Text = "...CONNECTING..."; + lblStatus.BackColor = Color.Goldenrod; + displayTaskAndWait("Opening DB Connection"); // APRO! loadServerConf(); + connectAdapter(); } // sblocco button... btnConnect.Enabled = true; @@ -752,17 +777,15 @@ namespace MTC_ADB // processo if (agObj.adpRunning) { - displayTaskAndWait("Stopping Adapter"); // CHIUDO! fermaTutto(true, false); } else { avviaAdapter(); - agObj.loadPersData(); - displayTaskAndWait("Adapter Started"); } checkStarted(); + checkConnected(); // sblocco button... btnStart.Enabled = true; btnStart.Refresh();