diff --git a/MTC_Adapter/EsaCncLib/thdNcEsaGvKvara.cs b/MTC_Adapter/EsaCncLib/thdNcEsaGvKvara.cs index 599bb61..70e17ac 100644 --- a/MTC_Adapter/EsaCncLib/thdNcEsaGvKvara.cs +++ b/MTC_Adapter/EsaCncLib/thdNcEsaGvKvara.cs @@ -119,6 +119,7 @@ namespace EsaCncLib public EsaStringMem SCMA_MManCMS_Release; public EsaStringMem SCMA_MManCMS_SerialNumber; + public EsaStringMem[] SCMA_MManCMS_Strings; public EsaDWordMem SCMA_MMan_ACK; public EsaDWordMem SCMA_MMan_STROBE; public EsaDWordMem SCMA_MMan_ALM; @@ -164,13 +165,13 @@ namespace EsaCncLib private static extern int ConvComunicationChannel([MarshalAs(UnmanagedType.LPStr)] string sChannelType); [DllImport("KvCom3x", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_init_board@8")] - private static extern int init_board([MarshalAs(UnmanagedType.LPStr)]string defcn_name, int ChannelType); + private static extern int init_board([MarshalAs(UnmanagedType.LPStr)] string defcn_name, int ChannelType); [DllImport("KvCom3x", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_exit_board@0")] private static extern int exit_board(); [DllImport("KvCom3x", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_get_reg_by_name@8")] - private static extern int get_reg_by_name([MarshalAs(UnmanagedType.LPStr)]string name, ref TS_REG pReg); + private static extern int get_reg_by_name([MarshalAs(UnmanagedType.LPStr)] string name, ref TS_REG pReg); [DllImport("KvCom3x", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_read_regbuffer@16")] private static extern int read_regbuffer(ref TS_REG pReg, UInt16 Offset, UInt16 Count, [MarshalAs(UnmanagedType.LPArray)] UInt32[] pDst); @@ -242,6 +243,7 @@ namespace EsaCncLib { SCMA_MManCMS_Release = new EsaStringMem(); SCMA_MManCMS_SerialNumber = new EsaStringMem(); + SCMA_MManCMS_Strings = new EsaStringMem[20]; SCMA_MMan_ACK = new EsaDWordMem(); SCMA_MMan_STROBE = new EsaDWordMem(); SCMA_MMan_WD = new EsaWordMem(); @@ -260,6 +262,11 @@ namespace EsaCncLib // aree FIXED secondo necessità: indicazione dimensione x impiego // rif aree reali ESA reale (commento successivo) SCMA_MManCMS_Release.MemorySize = 20; SCMA_MManCMS_SerialNumber.MemorySize = 50; + for (int i = 0; i < 20; i++) + { + SCMA_MManCMS_Strings[i] = new EsaStringMem(); + SCMA_MManCMS_Strings[i].MemorySize = 50; + } SCMA_MMan_ACK.MemorySize = 4; // 32 SCMA_MMan_STROBE.MemorySize = 4; // 32 SCMA_MMan_WD.MemorySize = 1; // 1 @@ -279,6 +286,10 @@ namespace EsaCncLib // indirizzo area ESA... SCMA_MManCMS_Release.AddressName = "CMSretvariables.Release"; SCMA_MManCMS_SerialNumber.AddressName = "CMSretvariables.SerialNumber"; + for (int i = 0; i < 20; i++) + { + SCMA_MManCMS_Strings[i].AddressName = $"scma.scmastring[{i}].tag"; + } SCMA_MMan_ACK.AddressName = "scma.ACK"; //fIni.ReadString("NC", "SCMA_AreaAddress_ACK", "scma.ACK"); SCMA_MMan_STROBE.AddressName = "scma.STROBE"; SCMA_MMan_WD.AddressName = "scma.WD"; @@ -298,6 +309,10 @@ namespace EsaCncLib // Creazione buffers letture memoria SCMA_MManCMS_Release.data = "Release"; SCMA_MManCMS_SerialNumber.data = "SerialNumber"; + for (int i = 0; i < 20; i++) + { + SCMA_MManCMS_Strings[i].data = ""; + } SCMA_MMan_ACK.data = new UInt32[SCMA_MMan_ACK.MemorySize]; SCMA_MMan_STROBE.data = new UInt32[SCMA_MMan_STROBE.MemorySize]; SCMA_MMan_ALM.data = new UInt32[SCMA_MMan_ALM.MemorySize]; @@ -412,15 +427,18 @@ namespace EsaCncLib string valString = ""; - for (int i = 0; i < currData.MemorySize; i++) + if (currData.tempData != null) { - try + for (int i = 0; i < currData.MemorySize; i++) { - valString += Convert.ToChar(currData.tempData[i]).ToString().Replace("\0", " "); - } - catch (Exception exc) - { - lg.Error(exc, $"Errore in decodifica {currData.AddressName}"); + try + { + valString += Convert.ToChar(currData.tempData[i]).ToString().Replace("\0", " "); + } + catch (Exception exc) + { + lg.Error(exc, $"Errore in decodifica {currData.AddressName}"); + } } } // trimmo! @@ -438,6 +456,11 @@ namespace EsaCncLib { readConvString(SCMA_MManCMS_Release); readConvString(SCMA_MManCMS_SerialNumber); + // leggo le 20 variabili stringa... + for (int i = 0; i < 20; i++) + { + readConvString(SCMA_MManCMS_Strings[i]); + } } /// /// Leggo il buffer di memorie DWORD diff --git a/MTC_Adapter/SCMA/AdapterPLC/CMS/EsaKosmos.cs b/MTC_Adapter/SCMA/AdapterPLC/CMS/EsaKosmos.cs index 24824c3..15c0b87 100644 --- a/MTC_Adapter/SCMA/AdapterPLC/CMS/EsaKosmos.cs +++ b/MTC_Adapter/SCMA/AdapterPLC/CMS/EsaKosmos.cs @@ -54,6 +54,7 @@ namespace SCMA.AdapterPLC.CMS loadMaintData("CNT", 0, 1); loadStatusData("MNT", 0, 1); loadAnalogData("ANALOG", 0, 1); + loadStringData("STR", 0, 1); loadAllarmiCNC(); } @@ -597,6 +598,42 @@ namespace SCMA.AdapterPLC.CMS } return fatto; } + + /// + /// Gestione lettura dati stringa + /// + public override bool getStringDataFromPlc() + { + bool fatto = false; + try + { + // leggo e salvo var analogiche ANALOG + if (stringDataMaxNum > 0) + { + // recupero dati + inizio = DateTime.Now; + ncDevice.ReadStringData(); + if (utils.CRB("recTime")) + { + TimingData.addResult("R-StringData", DateTime.Now.Subtract(inizio).Ticks); + } + string[] tabDatiString = new string[stringDataMaxNum]; + for (int i = 0; i < stringDataMaxNum; i++) + { + tabDatiString[i] = ncDevice.SCMA_MManCMS_Strings[i].data; + } + // decodifico aree memoria secondo tab configurazione + processStringData(tabDatiString, stringData); + } + fatto = true; + } + catch (Exception exc) + { + lg.Error(string.Format("{0}", exc)); + } + return fatto; + } + /// /// Gestione lettura dati status da PLC /// diff --git a/MTC_Adapter/SCMA/AdapterPLC/Generic.cs b/MTC_Adapter/SCMA/AdapterPLC/Generic.cs index 457806d..5223b3f 100644 --- a/MTC_Adapter/SCMA/AdapterPLC/Generic.cs +++ b/MTC_Adapter/SCMA/AdapterPLC/Generic.cs @@ -4,12 +4,12 @@ using System.Globalization; using System.IO; using System.Linq; using System.Text; +using NLog; namespace SCMA.AdapterPLC { using AdapterCom; using MTC; - using NLog; using OPC_UA_REDIS; #region macro oggetti da istanziare a blocchi da configurazione XML @@ -754,7 +754,7 @@ namespace SCMA.AdapterPLC currGateway.addItemNodeAndSet(vettUnOp[i].loadKey, itemType.Sample, availStatus.UNAVAILABLE.ToString().ToLower()); currGateway.addItemNodeAndSet(vettUnOp[i].accTimeKey, itemType.Sample, availStatus.UNAVAILABLE.ToString().ToLower()); currGateway.addItemNodeAndSet(vettUnOp[i].countKey, itemType.Sample, availStatus.UNAVAILABLE.ToString().ToLower()); - + // SOLO SE NON sono SOUR... if (currGateway.protocollo != gwProtocol.SOURS) { @@ -794,7 +794,7 @@ namespace SCMA.AdapterPLC // allarmi "base" currGateway.addAlarmNodes(); - //2019.07.22 NON RIPORTO USER_ACTION x SOUR + // 2019.07.22 NON RIPORTO USER_ACTION x SOUR if (currGateway.protocollo != gwProtocol.SOURS) { // azioni utente @@ -1093,6 +1093,11 @@ namespace SCMA.AdapterPLC /// public otherData[] analogData; + /// + /// Contenuto valori variabili stringa da PLC + /// + public otherData[] stringData; + /// /// Contenuto valori status data da PLC /// @@ -1351,6 +1356,11 @@ namespace SCMA.AdapterPLC /// public List elAnalogData; + /// + /// Vettore string data (SAMPLE) + /// + public List elStringData; + #endregion Gestione contatori & obj specifici (Events & Samples) #region oggetti complessi/completi @@ -1889,6 +1899,76 @@ namespace SCMA.AdapterPLC } } + /// + /// Lettura file gestione dati stringa da PLC + /// + /// + /// + /// + public void loadStringData(string memPre, int baseAddr, int memSize) + { + // carico dati x Maintenance + if (utils.CRB("verbose")) + { + lg.Info("Inizio caricamento vettore variabili stringa gestite"); + } + + int totRighe = 0; + string fileName = utils.StringListFile; + if (currGateway.protocollo == gwProtocol.SOURS) + { + fileName = utils.StringListFileSOUR; + } + string linea; + totRighe = File.ReadLines(fileName).Count(); + // creo un vettore della dimensione corretta... conta anche commenti tanto poi riduco... + stringData = new otherData[File.ReadLines(fileName).Count()]; + // carica da file... + StreamReader file = new StreamReader(fileName); + // leggo 1 linea alla volta... + int numRiga = 0; + while ((linea = file.ReadLine()) != null) + { + // se NON VUOTA... + if (linea != "") + { + // SE non è un commento... + if (linea.Substring(0, 1) != "#") + { + stringData[numRiga] = decodeOtherData(linea, utils.CRC("testCharSep"), memPre, baseAddr, memSize); + numRiga++; + } + } + } + // chiudo file + file.Close(); + // ora trimmo vettore al solo numero VERO dei valori caricati... + Array.Resize(ref stringData, numRiga); + + if (utils.CRB("verbose")) + { + lg.Info(string.Format("Fine caricamento vettore di {0} variabili stringa gestite", numRiga)); + } + + // ora inizializzo l'insieme dei Samples delle var analogiche + elStringData = new List(); + string varName = ""; + for (int i = 0; i < numRiga; i++) + { + varName = stringData[i].varName; + if (currGateway.protocollo != gwProtocol.SOURS) + { + varName = string.Format("STR_" + stringData[i].varName); + } + elStringData.Add(varName); + currGateway.addItemNodeByType(varName, itemType.Sample); + } + if (utils.CRB("verbose")) + { + lg.Info(string.Format("inizializzato vettore di {0} MTC.SAMPLES delle variabili string", numRiga)); + } + } + /// /// caricamento allarmi da file /// @@ -2736,6 +2816,15 @@ namespace SCMA.AdapterPLC { lg.Error($"Eccezione in getAnalogDataFromPlc:{Environment.NewLine}{exc}"); } + // gestisco lettura dati stringa da PLC... + try + { + getStringDataFromPlc(); + } + catch (Exception exc) + { + lg.Error($"Eccezione in getStringDataFromPlc:{Environment.NewLine}{exc}"); + } } /// @@ -2783,6 +2872,15 @@ namespace SCMA.AdapterPLC return fatto; } + /// + /// Gestione lettura dati stringa + /// + public virtual bool getStringDataFromPlc() + { + bool fatto = false; + return fatto; + } + /// /// Gestione lettura dati status da PLC /// @@ -4605,6 +4703,36 @@ namespace SCMA.AdapterPLC return _analogDataMaxNum; } } + + /// + /// Numero massimo (indice) valori string (privato) + /// + protected int _stringDataMaxNum = 0; + /// + /// Numero massimo (indice) valori string (calcolato) + /// + public int stringDataMaxNum + { + get + { + if (_stringDataMaxNum == 0) + { + if (stringData != null) + { + // prima stima + int idx = stringData.Length; + // ora ciclo... + foreach (var item in stringData) + { + int.TryParse(item.codNum, out idx); + _stringDataMaxNum = idx > _stringDataMaxNum ? idx : _stringDataMaxNum; + } + } + } + return _stringDataMaxNum; + } + } + /// /// Processa la tabella di memoria dei dati analogici /// @@ -4664,7 +4792,7 @@ namespace SCMA.AdapterPLC // gestisco in modalità "lazy" qualsiasi variabile (dividendo x opportuno fattore conversione...) try { - analogVal = (double) tabDatiAnag[idxTab] / (double) utils.CRI("fattDecVA"); + analogVal = (double)tabDatiAnag[idxTab] / (double)utils.CRI("fattDecVA"); } catch (Exception exc) { @@ -4679,6 +4807,33 @@ namespace SCMA.AdapterPLC // aggiungo i vari analogici rilevati parentForm.dataMonitor_3 += sb.ToString(); } + /// + /// Processa la tabella di memoria dei dati stringa + /// + /// + /// + public void processStringData(string[] tabDatiString, otherData[] stringData) + { + // uno ad uno vado a inserirli nella mappa dei dati dell'adapter... + StringBuilder sb = new StringBuilder(); + double analogVal = 0; + int idxTab = 0; + string origName = ""; + for (int i = 0; i < stringData.Length; i++) + { + // calcolo idx a indice 0... + int.TryParse(stringData[i].codNum, out idxTab); + idxTab--; + origName = getOrigName(stringData[i].varName); + + // salvo vettore Eventi... + currGateway.updateItemNodeValue(elStringData[idxTab], tabDatiString[idxTab]); + // accodo ultimi codici in visualizzazione... + sb.AppendLine(currGateway.getItemNode(elStringData[idxTab]).ToString().Replace("|", " | ")); + } + // aggiungo i vari analogici rilevati + parentForm.dataMonitor_3 += sb.ToString(); + } /// /// processa dataLayer e se necessario salva/mostra @@ -4711,7 +4866,7 @@ namespace SCMA.AdapterPLC needSave = procPzProd(needSave); needSave = procGiriTotUnOp(needSave); needSave = procNumCU(needSave); - needSave = procRepetitions(needSave); + needSave = procRepetitions(needSave); needSave = procCounters(needSave); needSave = procMovTotAssi(needSave); needSave = procAccTimeAssi(needSave); @@ -5783,7 +5938,7 @@ namespace SCMA.AdapterPLC parentForm.dataMonitor_2 += string.Format("{0}{1}", outString.Substring(0, outString.Length - 3), Environment.NewLine); return needSave; } - + /// /// Processing delle variabili sul totale m percorsi dagli assi diff --git a/MTC_Adapter/SCMA/App.config b/MTC_Adapter/SCMA/App.config index eb3bad9..330d731 100644 --- a/MTC_Adapter/SCMA/App.config +++ b/MTC_Adapter/SCMA/App.config @@ -1,197 +1,198 @@  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MTC_Adapter/SCMA/DATA/CONF/DataModel.xml b/MTC_Adapter/SCMA/DATA/CONF/DataModel.xml index a4c07d0..9470387 100644 --- a/MTC_Adapter/SCMA/DATA/CONF/DataModel.xml +++ b/MTC_Adapter/SCMA/DATA/CONF/DataModel.xml @@ -260,7 +260,7 @@ - + diff --git a/MTC_Adapter/SCMA/MainForm.cs b/MTC_Adapter/SCMA/MainForm.cs index 8f62385..305563c 100644 --- a/MTC_Adapter/SCMA/MainForm.cs +++ b/MTC_Adapter/SCMA/MainForm.cs @@ -22,2280 +22,2303 @@ using System.Windows.Forms; namespace SCMA { - public partial class MainForm : Form - { - #region inizializzazione contatori - - /// - /// contatore veloce - /// - protected int fastCount; - /// - /// contatore normale - /// - protected int normCount; - /// - /// contatore lento - /// - protected int slowCount; - /// - /// contatore sync allarmi - /// - protected int alarmSyncCount; - /// - /// timer base in avvio - /// - protected int startTimerMs = 250; - /// - /// ultimo tentativo riavvio... - /// - protected DateTime lastStartTry; - - #endregion - - #region inizializzazione oggetti base - - /// - /// oggetto logging - /// - public static Logger lg; - - /// - /// Oggetto x gestione dell'adapter GENERICO (x poter usare metodi di ognuno...) - /// - AdapterPLC.Generic agObj; - /// - /// configurazione caricata - /// - AdapterConf adpConf; - /// - /// DataModel caricato - /// - DataModel currDataModel; - /// - /// tipo di adapter prescelto... - /// - public tipoAdapter tipoScelto = tipoAdapter.DEMO; - - //public Gateway protocollo - - #endregion - - - #region utils ed helpers - - public static void EnableTab(TabPage page, bool enable) + public partial class MainForm : Form { - EnableControls(page.Controls, enable); - } - private static void EnableControls(Control.ControlCollection ctls, bool enable) - { - foreach (Control ctl in ctls) - { - ctl.Enabled = enable; - EnableControls(ctl.Controls, enable); - } - } - private class Item - { - public string Name; - public int Value; - public Item(string name, int value) - { - Name = name; Value = value; - } - } - /// - /// mostra un testo sulla status bar ed attende startTimerMs - /// - /// - public void displayTaskAndWait(string txt2show) - { - lblStatus.Text = txt2show; - lg.Info(txt2show); - Thread.Sleep(startTimerMs); - } - /// - /// Mostra update delle statistiche di comunicazione (numero chiamate, tempo medio...) - /// - /// - public void updateComStats(string txt2show) - { - lblComStats.Text = string.Format("{0} | ", txt2show); - } + #region inizializzazione contatori - #endregion + /// + /// contatore veloce + /// + protected int fastCount; + /// + /// contatore normale + /// + protected int normCount; + /// + /// contatore lento + /// + protected int slowCount; + /// + /// contatore sync allarmi + /// + protected int alarmSyncCount; + /// + /// timer base in avvio + /// + protected int startTimerMs = 250; + /// + /// ultimo tentativo riavvio... + /// + protected DateTime lastStartTry; - #region gestione form e visibilità + #endregion - /// - /// Gestione form principale - /// - public MainForm() - { - InitializeComponent(); - lblStatus.Text = "Loading"; + #region inizializzazione oggetti base - lastStartTry = DateTime.Now; + /// + /// oggetto logging + /// + public static Logger lg; - // fix icon! - notifyIcon1.Text = string.Format("SCMA | {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version); - Icon = Icon.ExtractAssociatedIcon(defIconFilePath); - notifyIcon1.Icon = Icon.ExtractAssociatedIcon(defIconFilePath); + /// + /// Oggetto x gestione dell'adapter GENERICO (x poter usare metodi di ognuno...) + /// + AdapterPLC.Generic agObj; + /// + /// configurazione caricata + /// + AdapterConf adpConf; + /// + /// DataModel caricato + /// + DataModel currDataModel; + /// + /// tipo di adapter prescelto... + /// + public tipoAdapter tipoScelto = tipoAdapter.DEMO; - // fix versione! - lblApp.Text = string.Format("{0}", ConfigurationManager.AppSettings.Get("appName")); - lblVers.Text = string.Format(" v.{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version); + //public Gateway protocollo - startTimerMs = utils.CRI("startTimerMs"); + #endregion + + + #region utils ed helpers + + public static void EnableTab(TabPage page, bool enable) + { + EnableControls(page.Controls, enable); + } + private static void EnableControls(Control.ControlCollection ctls, bool enable) + { + foreach (Control ctl in ctls) + { + ctl.Enabled = enable; + EnableControls(ctl.Controls, enable); + } + } + private class Item + { + public string Name; + public int Value; + public Item(string name, int value) + { + Name = name; Value = value; + } + } + /// + /// mostra un testo sulla status bar ed attende startTimerMs + /// + /// + public void displayTaskAndWait(string txt2show) + { + lblStatus.Text = txt2show; + lg.Info(txt2show); + Thread.Sleep(startTimerMs); + } + /// + /// Mostra update delle statistiche di comunicazione (numero chiamate, tempo medio...) + /// + /// + public void updateComStats(string txt2show) + { + lblComStats.Text = string.Format("{0} | ", txt2show); + } + + #endregion + + #region gestione form e visibilità + + /// + /// Gestione form principale + /// + public MainForm() + { + InitializeComponent(); + lblStatus.Text = "Loading"; + + lastStartTry = DateTime.Now; + + // fix icon! + notifyIcon1.Text = string.Format("SCMA | {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version); + Icon = Icon.ExtractAssociatedIcon(defIconFilePath); + notifyIcon1.Icon = Icon.ExtractAssociatedIcon(defIconFilePath); + + // fix versione! + lblApp.Text = string.Format("{0}", ConfigurationManager.AppSettings.Get("appName")); + lblVers.Text = string.Format(" v.{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version); + + startTimerMs = utils.CRI("startTimerMs"); #if DEBUG - // Setup the logging view for Sentinel - http://sentinel.codeplex.com - var sentinalTarget = new NLogViewerTarget() - { - Name = "sentinal", - Address = "udp://127.0.0.1:9999", - IncludeNLogData = false - }; - var sentinalRule = new LoggingRule("*", LogLevel.Trace, sentinalTarget); - LogManager.Configuration.AddTarget("sentinal", sentinalTarget); - LogManager.Configuration.LoggingRules.Add(sentinalRule); + // Setup the logging view for Sentinel - http://sentinel.codeplex.com + var sentinalTarget = new NLogViewerTarget() + { + Name = "sentinal", + Address = "udp://127.0.0.1:9999", + IncludeNLogData = false + }; + var sentinalRule = new LoggingRule("*", LogLevel.Trace, sentinalTarget); + LogManager.Configuration.AddTarget("sentinal", sentinalTarget); + LogManager.Configuration.LoggingRules.Add(sentinalRule); #endif - LogManager.ReconfigExistingLoggers(); + LogManager.ReconfigExistingLoggers(); - lg = LogManager.GetCurrentClassLogger(); - displayTaskAndWait("Starting MainForm"); + lg = LogManager.GetCurrentClassLogger(); + displayTaskAndWait("Starting MainForm"); - // inizio con tab control disabilitati - EnableTab(tabCtrlMain.TabPages[1], false); - EnableTab(tabCtrlMain.TabPages[2], false); + // inizio con tab control disabilitati + EnableTab(tabCtrlMain.TabPages[1], false); + EnableTab(tabCtrlMain.TabPages[2], false); - // se abilitato autoload conf leggo file corretto... - if (utils.CRB("autoLoadConf")) - { - loadXmlFile(defConfFilePath); - lg.Info("XML LOADED"); - loadPersistLayer(defPersLayerFile); - lg.Info("PersLayerFile READ"); - agObj.loadPersData(); - lg.Info("PersLayerFile LOADED"); - } - else - { - // definisco e avvio tipo adapter generico - tipoScelto = tipoAdapter.ND; - adpConf = new AdapterConf(); - loadAdapterType(); - displayTaskAndWait("Waiting for config file selection"); - } + // se abilitato autoload conf leggo file corretto... + if (utils.CRB("autoLoadConf")) + { + loadXmlFile(defConfFilePath); + lg.Info("XML LOADED"); + loadPersistLayer(defPersLayerFile); + lg.Info("PersLayerFile READ"); + agObj.loadPersData(); + lg.Info("PersLayerFile LOADED"); + } + else + { + // definisco e avvio tipo adapter generico + tipoScelto = tipoAdapter.ND; + adpConf = new AdapterConf(); + loadAdapterType(); + displayTaskAndWait("Waiting for config file selection"); + } - // Start timer periodico - gather.Interval = utils.CRI("timerIntMs"); - gather.Enabled = true; + // Start timer periodico + gather.Interval = utils.CRI("timerIntMs"); + gather.Enabled = true; - displayTaskAndWait("Program Running"); - createTrayMenu(); - displayTaskAndWait("Tray Menu OK"); + displayTaskAndWait("Program Running"); + createTrayMenu(); + displayTaskAndWait("Tray Menu OK"); - // avvio minimizzato se richiesto - if (utils.CRB("startMinimized")) - { - // imposto minimized se necessario! - if (WindowState != FormWindowState.Minimized) - { - WindowState = FormWindowState.Minimized; + // avvio minimizzato se richiesto + if (utils.CRB("startMinimized")) + { + // imposto minimized se necessario! + if (WindowState != FormWindowState.Minimized) + { + WindowState = FormWindowState.Minimized; + } + displayTaskAndWait("Minimized"); + } + + displayTaskAndWait("Main Form OK"); } - displayTaskAndWait("Minimized"); - } - - displayTaskAndWait("Main Form OK"); - } - /// - /// Verifica finale a fine show... - /// - /// - /// - private void MainForm_Shown(object sender, EventArgs e) - { - // avvio minimizzato se richiesto - if (utils.CRB("startMinimized")) - { - // controllo e mando a tray... - sendToTray(); - } - displayTaskAndWait("Main Form SHOWN"); - } - /// - /// crea menù tray x applicazione - /// - private void createTrayMenu() - { - // Fix testi menù tray... - trayMenu.Items.Clear(); - // SE permessa massimizzazione... - if (utils.CRB("windowCanMax")) - { - trayMenu.Items.Add("Show SCMA"); - } - // se è permesso tray close... - if (utils.CRB("trayClose")) - { - trayMenu.Items.Add("Close SCMA"); - } - } - - /// - /// doppio click su tray icon - /// - /// - /// - private void notifyIcon1_DoubleClick(object sender, EventArgs e) - { - // SOLO SE PERMESSO mostrare full... - if (utils.CRB("windowCanMax")) - { - Show(); - WindowState = FormWindowState.Normal; - } - } - /// - /// evento resize - /// - /// - /// - private void MainForm_Resize(object sender, EventArgs e) - { - checkFormVisibility(); - } - /// - /// Verifica stato windows (minimized/normal) e visibilità con tray... - /// - private void checkFormVisibility() - { - // se non può massimizzare imposto COMUNQUE a minimized... - if (!utils.CRB("windowCanMax")) - { - WindowState = FormWindowState.Minimized; - } - // controllo cosa devo mostrare... - if (WindowState == FormWindowState.Minimized) - { - notifyIcon1.Visible = false; - sendToTray(); - } - else - { - notifyIcon1.Visible = false; - } - - } - /// - /// Gestisce "andata nel tray" della form - /// - private void sendToTray() - { - if (!notifyIcon1.Visible) - { - notifyIcon1.BalloonTipTitle = utils.CRS("appName"); - notifyIcon1.BalloonTipText = string.Format("{0} running on tray", utils.CRS("appName")); - notifyIcon1.Visible = true; - notifyIcon1.ShowBalloonTip(100); - } - Hide(); - } - /// - /// click su menù contestuale in tray - /// - /// - /// - private void trayMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) - { - if (e.ClickedItem.Text.StartsWith("Close")) - { - // stop adapter... - closeAdapter(); - // chiudo! - Close(); - } - else if (e.ClickedItem.Text.StartsWith("Show")) - { - if (utils.CRB("windowCanMax")) + /// + /// Verifica finale a fine show... + /// + /// + /// + private void MainForm_Shown(object sender, EventArgs e) { - Show(); - WindowState = FormWindowState.Normal; + // avvio minimizzato se richiesto + if (utils.CRB("startMinimized")) + { + // controllo e mando a tray... + sendToTray(); + } + displayTaskAndWait("Main Form SHOWN"); } - } - - } - - #endregion - - protected string defConfFilePath - { - get - { - return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("defaultConfFile")); - } - } - /// - /// File persistenza generale - /// - protected string defPersLayerFile - { - get - { - return string.Format(@"{0}\{1}", utils.dataDatDir, utils.CRS("defaultPersLayerFile")); - } - } - protected string defIconFilePath - { - get - { - return string.Format(@"{0}\Scma.ico", utils.resxDir); - } - } - - /// - /// carica adapter richiesto - /// - private void loadAdapterType() - { - Gateway currGateway; - switch (utils.CRS("adpProto")) - { - case "MTC": - currGateway = new GatewayMTC(); - break; - case "SOURS": - currGateway = new GatewaySOURS(); - break; - case "": - default: - currGateway = new Gateway(); - break; - } - // ira inizializzo VERO adapter x tipo controllo e protocollo... - switch (tipoScelto) - { - case tipoAdapter.DEMO: - agObj = new AdapterPLC.Demo(this, adpConf, currGateway); - EnableTab(tabCtrlMain.TabPages[1], true); - EnableTab(tabCtrlMain.TabPages[2], true); - start.Enabled = true; - break; - case tipoAdapter.ESAGVCMS: - agObj = new AdapterPLC.CMS.EsaKosmos(this, adpConf, currGateway); - EnableTab(tabCtrlMain.TabPages[1], true); - EnableTab(tabCtrlMain.TabPages[2], false); - start.Enabled = true; - break; - case tipoAdapter.ESAGVSCM: - agObj = new AdapterPLC.SCM.EsaKvara(this, adpConf, currGateway); - EnableTab(tabCtrlMain.TabPages[1], true); - EnableTab(tabCtrlMain.TabPages[2], false); - start.Enabled = true; - break; - case tipoAdapter.FANUC: - agObj = new AdapterPLC.CMS.Fanuc(this, adpConf, currGateway); - EnableTab(tabCtrlMain.TabPages[1], true); - EnableTab(tabCtrlMain.TabPages[2], false); - start.Enabled = true; - break; - case tipoAdapter.OSAI: - agObj = new AdapterPLC.CMS.Osai(this, adpConf, currGateway); - EnableTab(tabCtrlMain.TabPages[1], true); - EnableTab(tabCtrlMain.TabPages[2], false); - start.Enabled = true; - break; - case tipoAdapter.SIEMENS: - agObj = new AdapterPLC.CMS.Siemens(this, adpConf, currGateway); - EnableTab(tabCtrlMain.TabPages[1], true); - EnableTab(tabCtrlMain.TabPages[2], false); - start.Enabled = true; - break; - case tipoAdapter.ND: - default: - agObj = new AdapterPLC.Demo(this, adpConf, currGateway); - EnableTab(tabCtrlMain.TabPages[1], false); - EnableTab(tabCtrlMain.TabPages[2], false); - start.Enabled = false; - break; - } - lblCurrAdapt.Text = string.Format("Adapter loaded: {0}", tipoScelto.ToString().ToUpper()); - - // carico i default values su interfaccia - setDefaults(); - - displayTaskAndWait(string.Format("Caricata conf per adapter {0}", tipoScelto)); - } - - /// - /// impostazione valori defaults - /// - private void setDefaults() - { - stop.Enabled = false; - dump.Enabled = false; - - D1_NAME.Text = "DEMO MACHINE"; // utils.CRS("D1_NAME"); - D1_ID.Text = "DEMO ID"; //utils.CRS("D1_ID"); - D1_UUID.Text = "DEMO UUID"; // utils.CRS("D1_UUID"); - - program.Text = "DEMO PRG"; // utils.CRS("D1_PROGRAM"); - PROG_ROW_NUM.Text = "0"; - partID.Text = "DEMO PART-ID"; // utils.CRS("PartID"); - - pzOk = 0; - pzKo = 0; - Power = 1000; - AccTime = 1440 * 365; - - OPERATOR_ID.Text = "OPxxxx"; - - STATUS_STRB_DW0.Text = utils.binaryForm(0); - STATUS_STRB_DW1.Text = utils.binaryForm(0); - STATUS_STRB_DW2.Text = utils.binaryForm(0); - STATUS_STRB_DW3.Text = utils.binaryForm(0); - - STATUS_ACK_DW0.Text = utils.binaryForm(0); - STATUS_ACK_DW1.Text = utils.binaryForm(0); - STATUS_ACK_DW2.Text = utils.binaryForm(0); - STATUS_ACK_DW3.Text = utils.binaryForm(0); - - MainProgrBar.Minimum = 0; - MainProgrBar.Maximum = 1000; - MainProgrBar.Value = 0; - MainProgrBar.Step = 1; - - // compilo combobox causali fermo... - string[] contenuto = File.ReadAllLines(utils.confDir + "\\HaltTypeList.map"); - - System.Collections.Generic.Dictionary comboSource = new Dictionary(); - foreach (var line in contenuto) - { - // se la linea non è commento e non è vuota... - if (line.Length > 0 && line[0] != '#') + /// + /// crea menù tray x applicazione + /// + private void createTrayMenu() { - string[] tokens = line.Split(':'); - comboSource.Add(tokens[0], tokens[1]); - } - } - functionalMode.DataSource = new BindingSource(comboSource, null); - functionalMode.DisplayMember = "Value"; - functionalMode.ValueMember = "Key"; - - - string[] row = { "0", "0", "0", "0", "0", "0" }; - PosAct.Rows.Add(row); - - // fix componenti vettoriali PATH, UNOP, ASSI... - for (int i = 0; i < adpConf.nPath; i++) - { - cbPathSel.Items.Insert(i, adpConf.Path[i].ident); - } - if (adpConf.nPath > 0) - { - cbPathSel.SelectedIndex = 0; - } - - for (int i = 0; i < adpConf.nUnOp; i++) - { - cbUnOpSel.Items.Insert(i, adpConf.UnOp[i].ident); - } - if (adpConf.nUnOp > 0) - { - cbUnOpSel.SelectedIndex = 0; - } - - for (int i = 0; i < adpConf.nAxis; i++) - { - cbAxNum.Items.Insert(i, adpConf.Axis[i].ident); - } - if (adpConf.nAxis > 0) - { - cbAxNum.SelectedIndex = 0; - } - } - - /// - /// Avvio dell'adapter - /// - /// - /// - private void start_Click(object sender, EventArgs e) - { - avviaAdapter(); - agObj.loadPersData(); - // salvo che ho avviato adapter - lg.Info("Completato LOAD Adapter"); - } - - public void avviaAdapter() - { - displayTaskAndWait("Adapter starting"); - // se NON sta girando... - if (!agObj.adpRunning) - { - agObj.startAdapter(); - displayTaskAndWait("Adapter started!"); - - // fix buttons start/stop/dump - start.Enabled = false; - stop.Enabled = true; - dump.Enabled = true; - if (utils.CRB("openDumpOnStart")) - { - displayTaskAndWait("Dump Window starting"); - apriDumpAgent(); - displayTaskAndWait("Dump Windows OK"); + // Fix testi menù tray... + trayMenu.Items.Clear(); + // SE permessa massimizzazione... + if (utils.CRB("windowCanMax")) + { + trayMenu.Items.Add("Show SCMA"); + } + // se è permesso tray close... + if (utils.CRB("trayClose")) + { + trayMenu.Items.Add("Close SCMA"); + } } - displayTaskAndWait("Start Timers"); - // inizializzo contatori fast/mid/slow - fastCount = utils.CRI("fastCount"); - normCount = utils.CRI("normCount"); - slowCount = utils.CRI("slowCount"); - alarmSyncCount = utils.CRI("alarmSyncCount"); - displayTaskAndWait("Adapter Running..."); - // forzo check allarmi.. - agObj.forceAlarmCheck(); - } - else - { - displayTaskAndWait("Adapter STILL Running..."); - } - } - - /// - /// fermata dell'adapter - /// - /// - /// - private void stop_Click(object sender, EventArgs e) - { - fermaAdapter(false); - // salvo che ho fermato adapter - lg.Info("UNLOAD Adapter"); - } - /// - /// Ferma l'adapter - /// - /// determina se si debba tentare riavvio automatico (per caduta connessione) - public void fermaAdapter(bool tryRestart) - { - fermaTutto(false, tryRestart); - } - - private void gather_Tick(object sender, EventArgs e) - { - // eseguo cicli attivi SOLO se adapter è in EFFETTIVO running... - if (agObj.adpRunning) - { - // inizio a riportare che sto eseguendo.. - advProgBar(); - if (agObj.connectionOk) + /// + /// doppio click su tray icon + /// + /// + /// + private void notifyIcon1_DoubleClick(object sender, EventArgs e) { - // check esecuzione FastTask - checkFastTask(); - // check esecuzione NormTask - checkNormTask(); - // check esecuzione SlowTask - checkSlowTask(); - // check esecuzione AlarmSync - checkAlarmSync(); - // check esecuzione SendTask (VHF) - checkSendTask(); - if (utils.CRI("waitEndCycle") > 0) - { - Thread.Sleep(utils.CRI("waitEndCycle")); - } + // SOLO SE PERMESSO mostrare full... + if (utils.CRB("windowCanMax")) + { + Show(); + WindowState = FormWindowState.Normal; + } } - else + /// + /// evento resize + /// + /// + /// + private void MainForm_Resize(object sender, EventArgs e) { - double currWait = DateTime.Now.Subtract(lastStartTry).TotalMilliseconds; - if (agObj.adpTryRestart && currWait > utils.CRI("waitRecMSec")) - { - lastStartTry = DateTime.Now; - agObj.tryConnect(); + checkFormVisibility(); + } + /// + /// Verifica stato windows (minimized/normal) e visibilità con tray... + /// + private void checkFormVisibility() + { + // se non può massimizzare imposto COMUNQUE a minimized... + if (!utils.CRB("windowCanMax")) + { + WindowState = FormWindowState.Minimized; + } + // controllo cosa devo mostrare... + if (WindowState == FormWindowState.Minimized) + { + notifyIcon1.Visible = false; + sendToTray(); + } + else + { + notifyIcon1.Visible = false; + } + + } + /// + /// Gestisce "andata nel tray" della form + /// + private void sendToTray() + { + if (!notifyIcon1.Visible) + { + notifyIcon1.BalloonTipTitle = utils.CRS("appName"); + notifyIcon1.BalloonTipText = string.Format("{0} running on tray", utils.CRS("appName")); + notifyIcon1.Visible = true; + notifyIcon1.ShowBalloonTip(100); + } + Hide(); + } + /// + /// click su menù contestuale in tray + /// + /// + /// + private void trayMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) + { + if (e.ClickedItem.Text.StartsWith("Close")) + { + // stop adapter... + closeAdapter(); + // chiudo! + Close(); + } + else if (e.ClickedItem.Text.StartsWith("Show")) + { + if (utils.CRB("windowCanMax")) + { + Show(); + WindowState = FormWindowState.Normal; + } + } + + } + + #endregion + + protected string defConfFilePath + { + get + { + return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("defaultConfFile")); + } + } + /// + /// File persistenza generale + /// + protected string defPersLayerFile + { + get + { + return string.Format(@"{0}\{1}", utils.dataDatDir, utils.CRS("defaultPersLayerFile")); + } + } + protected string defIconFilePath + { + get + { + return string.Format(@"{0}\Scma.ico", utils.resxDir); + } + } + + /// + /// carica adapter richiesto + /// + private void loadAdapterType() + { + Gateway currGateway; + switch (utils.CRS("adpProto")) + { + case "MTC": + currGateway = new GatewayMTC(); + break; + case "SOURS": + currGateway = new GatewaySOURS(); + break; + case "": + default: + currGateway = new Gateway(); + break; + } + // ira inizializzo VERO adapter x tipo controllo e protocollo... + switch (tipoScelto) + { + case tipoAdapter.DEMO: + agObj = new AdapterPLC.Demo(this, adpConf, currGateway); + EnableTab(tabCtrlMain.TabPages[1], true); + EnableTab(tabCtrlMain.TabPages[2], true); + start.Enabled = true; + break; + case tipoAdapter.ESAGVCMS: + agObj = new AdapterPLC.CMS.EsaKosmos(this, adpConf, currGateway); + EnableTab(tabCtrlMain.TabPages[1], true); + EnableTab(tabCtrlMain.TabPages[2], false); + start.Enabled = true; + break; + case tipoAdapter.ESAGVSCM: + agObj = new AdapterPLC.SCM.EsaKvara(this, adpConf, currGateway); + EnableTab(tabCtrlMain.TabPages[1], true); + EnableTab(tabCtrlMain.TabPages[2], false); + start.Enabled = true; + break; + case tipoAdapter.FANUC: + agObj = new AdapterPLC.CMS.Fanuc(this, adpConf, currGateway); + EnableTab(tabCtrlMain.TabPages[1], true); + EnableTab(tabCtrlMain.TabPages[2], false); + start.Enabled = true; + break; + case tipoAdapter.OSAI: + agObj = new AdapterPLC.CMS.Osai(this, adpConf, currGateway); + EnableTab(tabCtrlMain.TabPages[1], true); + EnableTab(tabCtrlMain.TabPages[2], false); + start.Enabled = true; + break; + case tipoAdapter.SIEMENS: + agObj = new AdapterPLC.CMS.Siemens(this, adpConf, currGateway); + EnableTab(tabCtrlMain.TabPages[1], true); + EnableTab(tabCtrlMain.TabPages[2], false); + start.Enabled = true; + break; + case tipoAdapter.ND: + default: + agObj = new AdapterPLC.Demo(this, adpConf, currGateway); + EnableTab(tabCtrlMain.TabPages[1], false); + EnableTab(tabCtrlMain.TabPages[2], false); + start.Enabled = false; + break; + } + lblCurrAdapt.Text = string.Format("Adapter loaded: {0}", tipoScelto.ToString().ToUpper()); + + // carico i default values su interfaccia + setDefaults(); + + displayTaskAndWait(string.Format("Caricata conf per adapter {0}", tipoScelto)); + } + + /// + /// impostazione valori defaults + /// + private void setDefaults() + { + stop.Enabled = false; + dump.Enabled = false; + + D1_NAME.Text = "DEMO MACHINE"; // utils.CRS("D1_NAME"); + D1_ID.Text = "DEMO ID"; //utils.CRS("D1_ID"); + D1_UUID.Text = "DEMO UUID"; // utils.CRS("D1_UUID"); + + program.Text = "DEMO PRG"; // utils.CRS("D1_PROGRAM"); + PROG_ROW_NUM.Text = "0"; + partID.Text = "DEMO PART-ID"; // utils.CRS("PartID"); + + pzOk = 0; + pzKo = 0; + Power = 1000; + AccTime = 1440 * 365; + + OPERATOR_ID.Text = "OPxxxx"; + + STATUS_STRB_DW0.Text = utils.binaryForm(0); + STATUS_STRB_DW1.Text = utils.binaryForm(0); + STATUS_STRB_DW2.Text = utils.binaryForm(0); + STATUS_STRB_DW3.Text = utils.binaryForm(0); + + STATUS_ACK_DW0.Text = utils.binaryForm(0); + STATUS_ACK_DW1.Text = utils.binaryForm(0); + STATUS_ACK_DW2.Text = utils.binaryForm(0); + STATUS_ACK_DW3.Text = utils.binaryForm(0); + + MainProgrBar.Minimum = 0; + MainProgrBar.Maximum = 1000; + MainProgrBar.Value = 0; + MainProgrBar.Step = 1; + + // compilo combobox causali fermo... + string[] contenuto = File.ReadAllLines(utils.confDir + "\\HaltTypeList.map"); + + System.Collections.Generic.Dictionary comboSource = new Dictionary(); + foreach (var line in contenuto) + { + // se la linea non è commento e non è vuota... + if (line.Length > 0 && line[0] != '#') + { + string[] tokens = line.Split(':'); + comboSource.Add(tokens[0], tokens[1]); + } + } + functionalMode.DataSource = new BindingSource(comboSource, null); + functionalMode.DisplayMember = "Value"; + functionalMode.ValueMember = "Key"; + + + string[] row = { "0", "0", "0", "0", "0", "0" }; + PosAct.Rows.Add(row); + + // fix componenti vettoriali PATH, UNOP, ASSI... + for (int i = 0; i < adpConf.nPath; i++) + { + cbPathSel.Items.Insert(i, adpConf.Path[i].ident); + } + if (adpConf.nPath > 0) + { + cbPathSel.SelectedIndex = 0; + } + + for (int i = 0; i < adpConf.nUnOp; i++) + { + cbUnOpSel.Items.Insert(i, adpConf.UnOp[i].ident); + } + if (adpConf.nUnOp > 0) + { + cbUnOpSel.SelectedIndex = 0; + } + + for (int i = 0; i < adpConf.nAxis; i++) + { + cbAxNum.Items.Insert(i, adpConf.Axis[i].ident); + } + if (adpConf.nAxis > 0) + { + cbAxNum.SelectedIndex = 0; + } + } + + /// + /// Avvio dell'adapter + /// + /// + /// + private void start_Click(object sender, EventArgs e) + { + avviaAdapter(); agObj.loadPersData(); - } - } - // se è arrivato a MAX resetto... - if (MainProgrBar.Value >= MainProgrBar.Maximum) - { - MainProgrBar.Value = 0; - } - } - else - { - // verifico SE debba tentare il riavvio, ovvero NON running ma tryReconn e non ho riprovato x oltre waitRecMSec - double currWait = DateTime.Now.Subtract(lastStartTry).TotalMilliseconds; - if (agObj.adpTryRestart && currWait > utils.CRI("waitRecMSec")) - { - lastStartTry = DateTime.Now; - avviaAdapter(); - agObj.loadPersData(); - } - } - } - - public void resetProgBar() - { - MainProgrBar.Value = 0; - } - /// - /// Visualizzazione stato di comunicazione attiva con PLC - /// - public bool commPlcActive - { - set - { - // se true --> comunica/verde, altrimenti grigio - lblApp.ForeColor = value ? Color.SeaGreen : Color.Black; - lblVers.ForeColor = value ? Color.SeaGreen : Color.DarkSlateGray; - statusStrip1.Refresh(); - } - } - - private void checkAlarmSync() - { - - alarmSyncCount--; - if (alarmSyncCount <= 0) - { - alarmSyncCount = utils.CRI("alarmSyncCount"); - - // avvio fase raccolta dati e invio con adapter - agObj.gaterAndSend(gatherCycle.VLF); - } - } - private void checkSlowTask() - { - slowCount--; - if (slowCount <= 0) - { - slowCount = utils.CRI("slowCount"); - - // avvio fase raccolta dati e invio con adapter - agObj.gaterAndSend(gatherCycle.LF); - - // refresh stringhe code M/S/T - refreshCodeMST(); - - // 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(); - } - } - - /// - /// file persistenza generale - /// - public string histPersLayerFile - { - get - { - return string.Format(@"{0}\{1:yyyy}\{1:yyyy-MM-dd}.mtc", utils.dataDatDir, DateTime.Now); - } - } - /// - /// file persistenza generale data attuale ANTICIPATA di xx giorni - /// - /// num DD di anticipo - /// - public string prevPersLayerFile(int numDD) - { - return string.Format(@"{0}\{1:yyyy}\{1:yyyy-MM-dd}.mtc", utils.dataDatDir, DateTime.Now.AddDays(-numDD)); - } - /// - /// salva i valori attuali del file di conf sia in file corrente che in cartella valori storici - /// - public void persistData() - { - // salvo ogni "autoSaveSec" secondi dall'ultimo salvataggio... - TimeSpan tempoMod = new TimeSpan(0); - if (File.Exists(defPersLayerFile)) - { - DateTime adesso = DateTime.Now; - tempoMod = DateTime.Now.Subtract(File.GetLastWriteTime(defPersLayerFile)); - } - if (tempoMod.TotalSeconds > utils.CRI("autoSaveSec")) - { - savePersistLayer(defPersLayerFile); - savePersistLayer(histPersLayerFile); - } - } - - private void checkNormTask() - { - - // decremento... - normCount--; - // se il counter è a zero eseguo... - if (normCount <= 0) - { - normCount = utils.CRI("normCount"); - - // avvio fase raccolta dati e invio con adapter - agObj.gaterAndSend(gatherCycle.MF); - } - } - /// - /// Avanza la barra di stato... - /// - public void advProgBar() - { - try - { - MainProgrBar.PerformStep(); - MainProgrBar.Invalidate(); - } - catch - { } - } - - private void checkSendTask() - { - // avvio fase invio con adapter (code MST) - agObj.gaterAndSend(gatherCycle.VHF); - } - private void checkFastTask() - { - // decremento... - fastCount--; - // se il counter è a zero eseguo... - if (fastCount <= 0) - { - fastCount = utils.CRI("fastCount"); - - // avvio fase raccolta dati e invio con adapter - agObj.gaterAndSend(gatherCycle.HF); - - refreshVisualStrobes(); - } - } - - private void message_Leave(object sender, EventArgs e) - { - } - - // apro eseguibile dump - private void dump_Click(object sender, EventArgs e) - { - apriDumpAgent(); - } - /// - /// apre agent di dump in CMD - /// - private static void apriDumpAgent() - { - // solo se sono in modalità MTC... - if (utils.CRS("adpProto") == "MTC") - { - string path = Application.StartupPath; - try - { - Process.Start(string.Format(@"{0}\..\..\dump\dump.exe", path)); - } - catch - { - Process.Start(string.Format(@"{0}\dump\dump.exe", path)); - } - } - } - - #region classi x simulazione valori vari - - /// - /// simula alcuni dati generando ad esempio numeri casuali... SE abilitato e SE adapter è DEMO - /// - private void simulateData() - { - if (enableDataSim.Checked && tipoScelto == tipoAdapter.DEMO) - { - DateTime adesso = DateTime.Now; - Random rnd = new Random(); - // 10% casi cambia riga - if (rnd.Next(0, 100) > 90) - { - PROG_ROW_NUM.Text = rnd.Next(1, 10000).ToString(); + // salvo che ho avviato adapter + lg.Info("Completato LOAD Adapter"); } - - // cambio posizioni 6% casi: aggiungo codici M - if (rnd.Next(0, 100) > 95) + public void avviaAdapter() { - agObj.appendCodeMST("M", rnd.Next(1, 30).ToString(), 0); - } - - // cambio posizioni 3% casi: aggiungo codici S - if (rnd.Next(0, 100) > 97) - { - agObj.appendCodeMST("S", rnd.Next(100, 10000).ToString(), 0); - } - // cambio posizioni 3% casi: aggiungo codici T - if (rnd.Next(0, 100) > 97) - { - agObj.appendCodeMST("T", rnd.Next(1, 100).ToString(), 0); - } - - - // aggiungo 1 pz al totale nel 0.3% dei casi (-->TC circa 333 x tick)... - if (rnd.Next(0, 1000) > 997) - { - pzOk++; - agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.PZ_OK; - } - - // cambio posizioni XYZ/ijk nel 20% dei casi x ogni terna... - if (rnd.Next(0, 100) > 80) - { - PosAct.Rows[0].Cells[0].Value = rnd.Next(0, 100); - PosAct.Rows[0].Cells[1].Value = rnd.Next(0, 100); - PosAct.Rows[0].Cells[2].Value = rnd.Next(0, 100); - agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.POS_ACT; - } - if (rnd.Next(0, 100) > 80) - { - PosAct.Rows[0].Cells[3].Value = rnd.Next(0, 100); - PosAct.Rows[0].Cells[4].Value = rnd.Next(0, 100); - PosAct.Rows[0].Cells[5].Value = rnd.Next(0, 100); - agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.POS_ACT; - } - - - // cambio power nel 15% dei casi... - if (rnd.Next(0, 100) > 85) - { - Power += rnd.Next(-10, 10); - } - // aggiungo 1 min AccTime nel 0.1% dei casi - if (rnd.Next(0, 1000) > 999) - { - AccTime++; - } - - // toggle 1 allarme sys nello 0.5% dei casi - if (rnd.Next(0, 1000) > 995) - { - int codAlarm = rnd.Next(1, 3); - switch (codAlarm) - { - case 1: - SysErr_01.Checked = !SysErr_01.Checked; - break; - case 2: - SysErr_02.Checked = !SysErr_02.Checked; - break; - default: - SysErr_03.Checked = !SysErr_03.Checked; - break; - } - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.AL01; - } - // toggle 1 allarme PLC nello 0.4% dei casi - if (rnd.Next(0, 1000) > 996) - { - int codAlarm = rnd.Next(1, 3); - switch (codAlarm) - { - case 1: - PlcErr_01.Checked = !PlcErr_01.Checked; - break; - case 2: - PlcErr_02.Checked = !PlcErr_02.Checked; - break; - default: - PlcErr_03.Checked = !PlcErr_03.Checked; - break; - } - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.AL02; - } - // toggle 1 allarme CNC nello 0.3% dei casi - if (rnd.Next(0, 1000) > 997) - { - int codAlarm = rnd.Next(1, 3); - switch (codAlarm) - { - case 1: - CncErr_01.Checked = !CncErr_01.Checked; - break; - case 2: - CncErr_02.Checked = !CncErr_02.Checked; - break; - default: - CncErr_03.Checked = !CncErr_03.Checked; - break; - } - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.AL03; - } - - // nel 2% dei casi imposto un RUN MODE diverso da RUN - if (rnd.Next(0, 100) > 98) - { - int codSel = rnd.Next(1, 3); - switch (codSel) - { - case 1: - manual.Checked = true; - break; - case 2: - mdi.Checked = true; - break; - default: - edit.Checked = true; - break; - } - } - // nel 3% degli altri casi riporto RUN MODE a RUN - else - { - if (rnd.Next(0, 100) > 97) - { - automatic.Checked = true; - } - } - - - // nel 2% dei casi imposto un EXE MODE diverso da AUTO - if (rnd.Next(0, 100) > 98) - { - int codSel = rnd.Next(1, 3); - switch (codSel) - { - case 1: - ready.Checked = true; - break; - case 2: - stopped.Checked = true; - break; - default: - feedhold.Checked = true; - break; - } - } - // nel 3% degli altri casi riporto EXE MODE a AUTO - else - { - if (rnd.Next(0, 100) > 97) - { - running.Checked = true; - } - } - - - - // controllo se ci sia il flag di lettura di un evento M/S/T nel qual caso lo abbasso... - if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.M_CODE)) - { - if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.M_CODE)) - { - agObj.STROBE_PLC -= Strobe.M_CODE; - } - } - if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.S_CODE)) - { - if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.S_CODE)) - { - agObj.STROBE_PLC -= Strobe.S_CODE; - } - } - if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.T_CODE)) - { - if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.T_CODE)) - { - agObj.STROBE_PLC -= Strobe.T_CODE; - } - } - - // controllo se ci sia il flag di lettura di un evento pz ok/ko nel qual caso lo abbasso... - if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.PZ_OK)) - { - if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.PZ_OK)) - { - agObj.STROBE_PLC -= Strobe.PZ_OK; - } - } - if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.PZ_KO)) - { - if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.PZ_KO)) - { - agObj.STROBE_PLC -= Strobe.PZ_KO; - } - } - - // controllo se ci sia il flag di lettura di un evento FEED_SPEED nel qual caso lo abbasso... - if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.FEED_SPEED)) - { - if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.FEED_SPEED)) - { - agObj.STROBE_PLC -= Strobe.FEED_SPEED; - } - } - - // controllo se ci sia il flag di lettura di un evento POS_ACT nel qual caso lo abbasso... - if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.POS_ACT)) - { - if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.POS_ACT)) - { - agObj.STROBE_PLC -= Strobe.POS_ACT; - } - } - - refreshVisualStrobes(); - refreshCodeMST(); - } - } - - private void refreshVisualStrobes() - { - // aggiorno visualizzazione strobe! - STATUS_STRB_DW0.Text = utils.binaryForm((int)agObj.STRB_DW0); - STATUS_STRB_DW1.Text = utils.binaryForm((int)agObj.STRB_DW1); - STATUS_STRB_DW2.Text = utils.binaryForm((int)agObj.STRB_DW2); - STATUS_STRB_DW3.Text = utils.binaryForm((int)agObj.STRB_DW3); - - STATUS_ACK_DW0.Text = utils.binaryForm((int)agObj.ACK_DW0); - STATUS_ACK_DW1.Text = utils.binaryForm((int)agObj.ACK_DW1); - STATUS_ACK_DW2.Text = utils.binaryForm((int)agObj.ACK_DW2); - STATUS_ACK_DW3.Text = utils.binaryForm((int)agObj.ACK_DW3); - } - - - - /// - /// aggiorna visualizzazione code... - /// - private void refreshCodeMST() - { - lblCodaM.Text = string.Join(",", agObj.codaM[0].ToArray()); - lblCodaT.Text = string.Join(",", agObj.codaT[0].ToArray()); - lblCodaS.Text = string.Join(",", agObj.codaS[0].ToArray()); - } - - private void accodaCodM() - { - if (addCodM.Text.Trim() != "") - { - agObj.appendCodeMST("M", addCodM.Text.Trim(), 0); - addCodM.Text = ""; - } - refreshCodeMST(); - } - private void accodaCodS() - { - if (addCodS.Text.Trim() != "") - { - agObj.appendCodeMST("S", addCodS.Text.Trim(), 0); - addCodS.Text = ""; - } - refreshCodeMST(); - } - private void accodaCodT() - { - if (addCodT.Text.Trim() != "") - { - agObj.appendCodeMST("T", addCodT.Text.Trim(), 0); - addCodT.Text = ""; - } - refreshCodeMST(); - } - - private void addCodM_KeyDown(object sender, KeyEventArgs e) - { - if (e.KeyCode == Keys.Enter) - { - // accodo testo - accodaCodM(); - } - else if (e.KeyCode == Keys.Escape) - { - // svuoto! - addCodM.Text = ""; - } - } - private void addCodM_Leave(object sender, EventArgs e) - { - accodaCodM(); - } - private void addCodS_KeyDown(object sender, KeyEventArgs e) - { - - if (e.KeyCode == Keys.Enter) - { - // accodo testo - accodaCodS(); - } - else if (e.KeyCode == Keys.Escape) - { - // svuoto! - addCodS.Text = ""; - } - } - private void addCodS_Leave(object sender, EventArgs e) - { - accodaCodS(); - } - private void addCodT_KeyDown(object sender, KeyEventArgs e) - { - - if (e.KeyCode == Keys.Enter) - { - // accodo testo - accodaCodT(); - } - else if (e.KeyCode == Keys.Escape) - { - // svuoto! - addCodT.Text = ""; - } - } - private void addCodT_Leave(object sender, EventArgs e) - { - accodaCodT(); - } - - - - #endregion - - private void txtPathFeed_TextChanged(object sender, EventArgs e) - { - hsPathFeed.Value = Convert.ToInt32(txtPathFeed.Text); - } - - private void txtPathFeedOver_TextChanged(object sender, EventArgs e) - { - hsPathFeedOver.Value = Convert.ToInt32(txtPathFeedOver.Text); - } - - private void txtPathSpeedOver_TextChanged(object sender, EventArgs e) - { - hsPathSpeedOver.Value = Convert.ToInt32(txtPathSpeedOver.Text); - } - - private void raiseFlag_FEED_SPEED() - { - // se non c'è flag lo alzo... - if (!utils.IsSetAll(agObj.STROBE_PLC, Strobe.FEED_SPEED)) - { - agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.FEED_SPEED; - } - } - - private void hsPathFeed_ValueChanged(object sender, EventArgs e) - { - txtPathFeed.Text = hsPathFeed.Value.ToString(); - raiseFlag_FEED_SPEED(); - } - - private void hsPathFeedOver_ValueChanged(object sender, EventArgs e) - { - txtPathFeedOver.Text = hsPathFeedOver.Value.ToString(); - raiseFlag_FEED_SPEED(); - } - - private void hsPathSpeedOver_ValueChanged(object sender, EventArgs e) - { - txtPathSpeedOver.Text = hsPathSpeedOver.Value.ToString(); - raiseFlag_FEED_SPEED(); - } - - #region gestione load conf - - private void mConfGen_Click(object sender, EventArgs e) - { - SetupAdapter setupWIndow = new SetupAdapter(); - setupWIndow.Show(this); - } - - private void mLoadAdaptConf_Click(object sender, EventArgs e) - { - // mostro selettore file x leggere adapter.. - OpenFileDialog openFileDial = new OpenFileDialog(); - - // directory iniziale - openFileDial.InitialDirectory = utils.confDir; // string.Format(@"{0}\{1}", Application.StartupPath, utils.CRS("dataConfPath")); - // Set filter options and filter index. - openFileDial.Filter = "XML Files (.xml)|*.xml|All Files (*.*)|*.*"; - openFileDial.FilterIndex = 1; - // altre opzioni - openFileDial.Multiselect = false; - - // Call the ShowDialog method to show the dialog box. - DialogResult userClickedOK = openFileDial.ShowDialog(); - - // Process input if the user clicked OK. - if (userClickedOK == DialogResult.OK) - { - string XmlConfFile = openFileDial.FileName; - loadXmlFile(XmlConfFile); - lg.Info("XML LOADED"); - loadPersistLayer(defPersLayerFile); - lg.Info("PersLayerFile READ"); - agObj.loadPersData(); - lg.Info("PersLayerFile LOADED"); - } - } - /// - /// Carica file XML della configurazione richiesta - /// - /// - private void loadXmlFile(string XmlConfFile) - { - displayTaskAndWait(string.Format("Loading XML: {0}", XmlConfFile)); - - // 2019.07.13: cambiata procedura SE ci trociamo nel caso OPC-UA / SOUR che parte da DataModel.xml... - if (utils.CRS("adpProto") == "SOURS" && XmlConfFile.EndsWith("DataModel.xml")) - { - - if (utils.CRB("verbose")) - { - lg.Info("Inizio caricamento DataModel per OPC-UA REDIS server"); - } - StreamReader file; - // carica da file... - file = new StreamReader(XmlConfFile); - // leggo file... - string rawDataModel = file.ReadToEnd(); - // faccio un primo passaggio di pulizia commenti... - string sanitizedXml = utils.xmlSanitize(rawDataModel); - // chiudo file - file.Close(); - // effettuo deserializzazione... - DataModel dm = new DataModel(); - currDataModel = dm.Deserialize(sanitizedXml); - // avvio oggetto conf - tipoScelto = tipoAdapter.ND; - adpConf = new AdapterConf(); - // salvo i dati "specifici" in base a quanto recuperato da DataModel... - // cerco da plc/Name il tipo di PLC... - string plcName = ""; - var trovatoCnc = currDataModel.Properties.Find(x => x.BrowseName.Contains("Machine:Cnc:Name")); - var trovatoPlc = currDataModel.Properties.Find(x => x.BrowseName.Contains("Machine:Plc:Name")); - if (trovatoCnc != null) - { - plcName = trovatoCnc.Value; - } - else if (trovatoPlc != null) - { - plcName = trovatoPlc.Value; - } - Enum.TryParse(plcName, true, out tipoScelto); - adpConf.NomeAdapt = plcName; - adpConf.TipoAdapt = tipoScelto; - adpConf.Version = 2; - - // preparo file temp x generazione regole - StreamWriter fileOut; - // apro in scrittura il file x mettere le regole... - fileOut = new StreamWriter(utils.nameRepRoleFileSOUR); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine("# AUTO GENERATED Replacement Conf File"); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine(""); - - // altre variabili - element newElement; - int numRules = 0; - string replString = ""; - - // cerco ASSI come componenti... - List> dataRefList; - int numAx = 0; - List listaAx = new List(); - List selAxes = currDataModel.Components.FindAll(x => x.BrowseName.Contains("Machine:Axes:")); - string axBaseName = ""; - string axType = "LINEAR"; - foreach (var item in selAxes) - { - numAx++; - axBaseName = $"Axis_{numAx.ToString("00")}"; - // preparo la datarefList x TIPO assi e gruppo... - dataRefList = new List>(); - // calcolo TIPO DI ASSE - DataModel.Property tipoAsse = currDataModel.Properties.Find(x => x.BrowseName.Contains($"{item.BrowseName}:Type")); - if (tipoAsse != null) - { - axType = tipoAsse.Value; - } - else - { - axType = "LINEAR"; - } - - // aggiungo conf specifiche... - dataRefList.Add(new DataRefItem($"Axis_{ numAx.ToString("00") }_Grp", "00")); - dataRefList.Add(new DataRefItem($"Axis_{ numAx.ToString("00") }_Type", axType)); - - newElement = new element() - { - ident = axBaseName, - dataRefList = dataRefList - }; - listaAx.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_PosAct|{replString}:CurrentPos"); - fileOut.WriteLine($"A|I|{newElement.ident}_PosTgt|{replString}:TargetPos"); - fileOut.WriteLine($"A|I|{newElement.ident}_InvDDone|{replString}:Inversions"); - fileOut.WriteLine($"A|I|{newElement.ident}_DistDone|{replString}:Distance"); - fileOut.WriteLine($"A|I|{newElement.ident}_FeedAct|{replString}:FeedRate"); - fileOut.WriteLine($"A|I|{newElement.ident}_FeedOver|{replString}:FeedOverride"); - fileOut.WriteLine($"A|I|{newElement.ident}_MainProc|{replString}:ParentProc"); - fileOut.WriteLine($"A|I|{newElement.ident}_Descr|{replString}:Name"); - fileOut.WriteLine($"A|I|AV_{newElement.ident}_Load|{replString}:Load"); - fileOut.WriteLine($"A|I|{newElement.ident}_AccTime|{replString}:ActiveTime"); - fileOut.WriteLine($"A|I|{newElement.ident}_Dir|{replString}:Direction"); - fileOut.WriteLine($"A|I|{newElement.ident}_MastId|{replString}:MasterId"); - fileOut.WriteLine($"A|I|{newElement.ident}_Type|{replString}:Type"); - numRules += 13; - } - fileOut.WriteLine(""); - - // cerco PATH/Processi come componenti... - int numProc = 0; - List listaProc = new List(); - List selProc = currDataModel.Components.FindAll(x => x.BrowseName.Contains("Machine:Cnc:CncProcesses:")); - string procBaseName = ""; - foreach (var item in selProc) - { - numProc++; - procBaseName = $"Path_{numProc.ToString("00")}"; - newElement = new element() - { - ident = procBaseName - }; - listaProc.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_CodG_Act|{replString}:CodG"); - fileOut.WriteLine($"A|I|{newElement.ident}_Cod_M|{replString}:CodM"); - fileOut.WriteLine($"A|I|{newElement.ident}_Cod_S|{replString}:CodS"); - fileOut.WriteLine($"A|I|{newElement.ident}_Cod_T|{replString}:CodT"); - fileOut.WriteLine($"A|I|{newElement.ident}_EXE_MODE|{replString}:Status"); - fileOut.WriteLine($"A|I|{newElement.ident}_RUN_MODE|{replString}:Mode"); - fileOut.WriteLine($"A|I|{newElement.ident}_RapidOverr|{replString}:RapidOverride"); - fileOut.WriteLine($"A|I|{newElement.ident}_FeedOverr|{replString}:FeedOverride"); - - numRules += 8; - } - fileOut.WriteLine(""); - - // cerco UnOp / Gruppi operatori come componenti... - int numUnOp = 0; - List listaUnOp = new List(); - List selUnOp = currDataModel.Components.FindAll(x => x.BrowseName.Contains("Machine:OperatingGroups:")); - string procUnOp = ""; - foreach (var item in selUnOp) - { - numUnOp++; - procUnOp = $"UnOp_{numUnOp.ToString("00")}"; - newElement = new element() - { - ident = procUnOp - }; - listaUnOp.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Speed|{replString}:SpeedRate"); - fileOut.WriteLine($"A|I|{newElement.ident}_KRev|{replString}:Distance"); - fileOut.WriteLine($"A|I|{newElement.ident}_AccTime|{replString}:ActiveTime"); - fileOut.WriteLine($"A|I|{newElement.ident}_ToolId|{replString}:ToolId"); - fileOut.WriteLine($"A|I|{newElement.ident}_NumCU|{replString}:ToolChanges"); - fileOut.WriteLine($"A|I|{newElement.ident}_Load|{replString}:Load"); - fileOut.WriteLine($"A|I|{newElement.ident}_SpeedOverr|{replString}:SpeedOverride"); - fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); - fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); - - numRules += 9; - } - fileOut.WriteLine(""); - - // cerco nei gruppi ausiliari... - // VacuumPump... - int numVP = 0; - List listaVP = new List(); - // VacuumAct - int numVA = 0; - List listaVA = new List(); - // SlittaMag - int numSlMag = 0; - List listaSlMag = new List(); - // ProtMag - int numProtMag = 0; - List listaProtMag = new List(); - // SlittaTast - int numSlTas = 0; - List listaSlTas = new List(); - // Cooler... - int numCool = 0; - List listaCool = new List(); - // Lubro... - int numLub = 0; - List listaLub = new List(); - // Press... - int numPres = 0; - List listaPress = new List(); - // Temperature... - int numTemp = 0; - List listaTemp = new List(); - - // ricerca gruppi AUX - List selAux = currDataModel.Properties.FindAll(x => x.BrowseName.Contains("Machine:AuxiliaryGroups")); - foreach (var item in selAux) - { - if (item.Value == "COOLER") - { - numCool++; - newElement = new element() + displayTaskAndWait("Adapter starting"); + // se NON sta girando... + if (!agObj.adpRunning) { - ident = $"Cooler_{numCool.ToString("00")}" - }; - listaCool.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); - numRules++; - } - if (item.Value == "LUBRO") - { - numLub++; - newElement = new element() + agObj.startAdapter(); + displayTaskAndWait("Adapter started!"); + + // fix buttons start/stop/dump + start.Enabled = false; + stop.Enabled = true; + dump.Enabled = true; + if (utils.CRB("openDumpOnStart")) + { + displayTaskAndWait("Dump Window starting"); + apriDumpAgent(); + displayTaskAndWait("Dump Windows OK"); + } + + displayTaskAndWait("Start Timers"); + // inizializzo contatori fast/mid/slow + fastCount = utils.CRI("fastCount"); + normCount = utils.CRI("normCount"); + slowCount = utils.CRI("slowCount"); + alarmSyncCount = utils.CRI("alarmSyncCount"); + displayTaskAndWait("Adapter Running..."); + // forzo check allarmi.. + agObj.forceAlarmCheck(); + } + else { - ident = $"Lubro_{numLub.ToString("00")}" - }; - listaLub.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); - numRules++; - } - if (item.Value == "VACUUM_PUMP") - { - numVP++; - newElement = new element() + displayTaskAndWait("Adapter STILL Running..."); + } + } + + /// + /// fermata dell'adapter + /// + /// + /// + private void stop_Click(object sender, EventArgs e) + { + fermaAdapter(false); + // salvo che ho fermato adapter + lg.Info("UNLOAD Adapter"); + } + /// + /// Ferma l'adapter + /// + /// determina se si debba tentare riavvio automatico (per caduta connessione) + public void fermaAdapter(bool tryRestart) + { + fermaTutto(false, tryRestart); + } + + private void gather_Tick(object sender, EventArgs e) + { + // eseguo cicli attivi SOLO se adapter è in EFFETTIVO running... + if (agObj.adpRunning) { - ident = $"VacPump_{numVP.ToString("00")}" - }; - listaVP.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); - fileOut.WriteLine($"A|I|{newElement.ident}_WrkTime|{replString}:ActiveTime"); - numRules += 2; - } - if (item.Value == "VACUUM_ACT") - { - numVA++; - newElement = new element() + // inizio a riportare che sto eseguendo.. + advProgBar(); + if (agObj.connectionOk) + { + // check esecuzione FastTask + checkFastTask(); + // check esecuzione NormTask + checkNormTask(); + // check esecuzione SlowTask + checkSlowTask(); + // check esecuzione AlarmSync + checkAlarmSync(); + // check esecuzione SendTask (VHF) + checkSendTask(); + if (utils.CRI("waitEndCycle") > 0) + { + Thread.Sleep(utils.CRI("waitEndCycle")); + } + } + else + { + double currWait = DateTime.Now.Subtract(lastStartTry).TotalMilliseconds; + if (agObj.adpTryRestart && currWait > utils.CRI("waitRecMSec")) + { + lastStartTry = DateTime.Now; + agObj.tryConnect(); + agObj.loadPersData(); + } + } + // se è arrivato a MAX resetto... + if (MainProgrBar.Value >= MainProgrBar.Maximum) + { + MainProgrBar.Value = 0; + } + } + else { - ident = $"VacAct_{numVA.ToString("00")}" - }; - listaVA.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); - fileOut.WriteLine($"A|I|{newElement.ident}_Value|{replString}:Value"); - numRules++; - } - if (item.Value == "SLITTA_MAG") - { - numSlMag++; - newElement = new element() + // verifico SE debba tentare il riavvio, ovvero NON running ma tryReconn e non ho riprovato x oltre waitRecMSec + double currWait = DateTime.Now.Subtract(lastStartTry).TotalMilliseconds; + if (agObj.adpTryRestart && currWait > utils.CRI("waitRecMSec")) + { + lastStartTry = DateTime.Now; + avviaAdapter(); + agObj.loadPersData(); + } + } + } + + public void resetProgBar() + { + MainProgrBar.Value = 0; + } + /// + /// Visualizzazione stato di comunicazione attiva con PLC + /// + public bool commPlcActive + { + set { - ident = $"SlittaMagazzino_{numSlMag.ToString("00")}" - }; - listaSlMag.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); - numRules++; - } - if (item.Value == "PROT_MAG") - { - numProtMag++; - newElement = new element() + // se true --> comunica/verde, altrimenti grigio + lblApp.ForeColor = value ? Color.SeaGreen : Color.Black; + lblVers.ForeColor = value ? Color.SeaGreen : Color.DarkSlateGray; + statusStrip1.Refresh(); + } + } + + private void checkAlarmSync() + { + + alarmSyncCount--; + if (alarmSyncCount <= 0) { - ident = $"ProtezioneMagazzino_{numProtMag.ToString("00")}" - }; - listaProtMag.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); - numRules++; - } - if (item.Value == "SLITTA_TAST") - { - numSlTas++; - newElement = new element() + alarmSyncCount = utils.CRI("alarmSyncCount"); + + // avvio fase raccolta dati e invio con adapter + agObj.gaterAndSend(gatherCycle.VLF); + } + } + private void checkSlowTask() + { + slowCount--; + if (slowCount <= 0) { - ident = $"SlittaTastatore_{numPres.ToString("00")}" - }; - listaSlTas.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); - numRules++; - } - if (item.Value == "PRESSURE") - { - numPres++; - newElement = new element() + slowCount = utils.CRI("slowCount"); + + // avvio fase raccolta dati e invio con adapter + agObj.gaterAndSend(gatherCycle.LF); + + // refresh stringhe code M/S/T + refreshCodeMST(); + + // 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(); + } + } + + /// + /// file persistenza generale + /// + public string histPersLayerFile + { + get { - ident = $"Press_{numPres.ToString("00")}" - }; - listaPress.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Value|{replString}:Value"); - fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); - fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); - numRules += 1; - } - if (item.Value == "TEMPERATURE") - { - numTemp++; - newElement = new element() + return string.Format(@"{0}\{1:yyyy}\{1:yyyy-MM-dd}.mtc", utils.dataDatDir, DateTime.Now); + } + } + /// + /// file persistenza generale data attuale ANTICIPATA di xx giorni + /// + /// num DD di anticipo + /// + public string prevPersLayerFile(int numDD) + { + return string.Format(@"{0}\{1:yyyy}\{1:yyyy-MM-dd}.mtc", utils.dataDatDir, DateTime.Now.AddDays(-numDD)); + } + /// + /// salva i valori attuali del file di conf sia in file corrente che in cartella valori storici + /// + public void persistData() + { + // salvo ogni "autoSaveSec" secondi dall'ultimo salvataggio... + TimeSpan tempoMod = new TimeSpan(0); + if (File.Exists(defPersLayerFile)) { - ident = $"Temp_{numTemp.ToString("00")}" - }; - listaTemp.Add(newElement); - // aggiungo su file... - replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); - fileOut.WriteLine($"A|I|{newElement.ident}_Value|{replString}:Value"); - numRules += 1; - } + DateTime adesso = DateTime.Now; + tempoMod = DateTime.Now.Subtract(File.GetLastWriteTime(defPersLayerFile)); + } + if (tempoMod.TotalSeconds > utils.CRI("autoSaveSec")) + { + savePersistLayer(defPersLayerFile); + savePersistLayer(histPersLayerFile); + } } - // aggiungo elementi trovati... - if (numAx > 0) + private void checkNormTask() { - element[] elencoAx = listaAx.ToArray(); - adpConf.Axis = elencoAx; + + // decremento... + normCount--; + // se il counter è a zero eseguo... + if (normCount <= 0) + { + normCount = utils.CRI("normCount"); + + // avvio fase raccolta dati e invio con adapter + agObj.gaterAndSend(gatherCycle.MF); + } } - if (numProc > 0) + /// + /// Avanza la barra di stato... + /// + public void advProgBar() { - element[] elencoProc = listaProc.ToArray(); - adpConf.Path = elencoProc; - } - if (numUnOp > 0) - { - element[] elencoUnOp = listaUnOp.ToArray(); - adpConf.UnOp = elencoUnOp; - } - if (numCool > 0) - { - element[] elencoCool = listaCool.ToArray(); - adpConf.Cooler = elencoCool; - } - if (numLub > 0) - { - element[] elencoLub = listaLub.ToArray(); - adpConf.Lubro = elencoLub; - } - if (numVP > 0) - { - element[] elencoVP = listaVP.ToArray(); - adpConf.VacuumPump = elencoVP; - } - if (numVA > 0) - { - element[] elencoVA = listaVA.ToArray(); - adpConf.VacuumAct = elencoVA; - } - if (numSlMag > 0) - { - element[] elencoSlMag = listaSlMag.ToArray(); - adpConf.SlittaMag = elencoSlMag; - } - if (numSlTas > 0) - { - element[] elencoSlTas = listaSlTas.ToArray(); - adpConf.SlittaTas = elencoSlTas; - } - if (numPres > 0) - { - element[] elencoPress = listaPress.ToArray(); - adpConf.Press = elencoPress; - } - if (numTemp > 0) - { - element[] elencoTemp = listaTemp.ToArray(); - adpConf.Temp = elencoTemp; + try + { + MainProgrBar.PerformStep(); + MainProgrBar.Invalidate(); + } + catch + { } } - fileOut.WriteLine(""); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine($"# EOF: {numRules} rules added"); - fileOut.WriteLine("#------------------------------------------------------------"); - // chiudo file regole! - fileOut.Close(); - - // compilo il file StatusList.SOUR.map - fileOut = new StreamWriter(utils.StatusListFileSOUR); - int numStatus = 0; - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine("# AUTO GENERATED StatusList File"); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine(""); - // ricerca in variabili - List selStatus = currDataModel.Variables.FindAll(x => x.CmsDataType.Equals("StatusList")); - foreach (var item in selStatus) + private void checkSendTask() { - // compilo il file di conf... - fileOut.WriteLine($"{item.CmsDataIndex}|{item.BrowseName.Replace("Machine:", "")}|{item.CmsDataOpt}"); - numStatus++; + // avvio fase invio con adapter (code MST) + agObj.gaterAndSend(gatherCycle.VHF); } - fileOut.WriteLine(""); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine($"# EOF: {numStatus} status added"); - fileOut.WriteLine("#------------------------------------------------------------"); - // chiudo file regole! - fileOut.Close(); - - // compilo il file CounterList.SOUR.map - fileOut = new StreamWriter(utils.CounterListFileSOUR); - int numCounter = 0; - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine("# AUTO GENERATED CounterList File"); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine(""); - // ricerca in variabili - List selCounter = currDataModel.Variables.FindAll(x => x.CmsDataType.Equals("CounterList")); - foreach (var item in selCounter) + private void checkFastTask() { - // compilo il file di conf... se è RTV aggiungo la parte RTV.. - fileOut.WriteLine($"{item.CmsDataIndex}|{item.BrowseName.Replace("Machine:", "")}|{item.CmsDataOpt}|{item.CmsDataScale}"); - numCounter++; - } - fileOut.WriteLine(""); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine($"# EOF: {numCounter} counters added"); - fileOut.WriteLine("#------------------------------------------------------------"); - // chiudo file regole! - fileOut.Close(); + // decremento... + fastCount--; + // se il counter è a zero eseguo... + if (fastCount <= 0) + { + fastCount = utils.CRI("fastCount"); - // compilo il file StatusList.SOUR.map - fileOut = new StreamWriter(utils.AnalogListFileSOUR); - int numAnalog = 0; - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine("# AUTO GENERATED AnalogData File"); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine(""); - // ricerca in variabili - List selAnalog = currDataModel.Variables.FindAll(x => x.CmsDataType.Equals("AnalogData")); - foreach (var item in selAnalog) + // avvio fase raccolta dati e invio con adapter + agObj.gaterAndSend(gatherCycle.HF); + + refreshVisualStrobes(); + } + } + + private void message_Leave(object sender, EventArgs e) { - // compilo il file di conf... - fileOut.WriteLine($"{item.CmsDataIndex}|{item.BrowseName.Replace("Machine:", "")}|{item.CmsDataOpt}"); - numAnalog++; } - fileOut.WriteLine(""); - fileOut.WriteLine("#------------------------------------------------------------"); - fileOut.WriteLine($"# EOF: {numAnalog} analog added"); - fileOut.WriteLine("#------------------------------------------------------------"); - // chiudo file regole! - fileOut.Close(); - } - else - { - // legge obj configurazione "legacy" - adpConf = AdapterConf.Deserialize(XmlConfFile); - } + + // apro eseguibile dump + private void dump_Click(object sender, EventArgs e) + { + apriDumpAgent(); + } + /// + /// apre agent di dump in CMD + /// + private static void apriDumpAgent() + { + // solo se sono in modalità MTC... + if (utils.CRS("adpProto") == "MTC") + { + string path = Application.StartupPath; + try + { + Process.Start(string.Format(@"{0}\..\..\dump\dump.exe", path)); + } + catch + { + Process.Start(string.Format(@"{0}\dump\dump.exe", path)); + } + } + } + + #region classi x simulazione valori vari + + /// + /// simula alcuni dati generando ad esempio numeri casuali... SE abilitato e SE adapter è DEMO + /// + private void simulateData() + { + if (enableDataSim.Checked && tipoScelto == tipoAdapter.DEMO) + { + DateTime adesso = DateTime.Now; + Random rnd = new Random(); + // 10% casi cambia riga + if (rnd.Next(0, 100) > 90) + { + PROG_ROW_NUM.Text = rnd.Next(1, 10000).ToString(); + } - // indico quale sia il tipo di adapter - tipoScelto = adpConf.TipoAdapt; - loadAdapterType(); - // carico file XML in web browser... - wbXmlConf.DocumentText = AdapterConf.rawXml(XmlConfFile); - displayTaskAndWait("XML loaded"); + // cambio posizioni 6% casi: aggiungo codici M + if (rnd.Next(0, 100) > 95) + { + agObj.appendCodeMST("M", rnd.Next(1, 30).ToString(), 0); + } + + // cambio posizioni 3% casi: aggiungo codici S + if (rnd.Next(0, 100) > 97) + { + agObj.appendCodeMST("S", rnd.Next(100, 10000).ToString(), 0); + } + // cambio posizioni 3% casi: aggiungo codici T + if (rnd.Next(0, 100) > 97) + { + agObj.appendCodeMST("T", rnd.Next(1, 100).ToString(), 0); + } + + + // aggiungo 1 pz al totale nel 0.3% dei casi (-->TC circa 333 x tick)... + if (rnd.Next(0, 1000) > 997) + { + pzOk++; + agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.PZ_OK; + } + + // cambio posizioni XYZ/ijk nel 20% dei casi x ogni terna... + if (rnd.Next(0, 100) > 80) + { + PosAct.Rows[0].Cells[0].Value = rnd.Next(0, 100); + PosAct.Rows[0].Cells[1].Value = rnd.Next(0, 100); + PosAct.Rows[0].Cells[2].Value = rnd.Next(0, 100); + agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.POS_ACT; + } + if (rnd.Next(0, 100) > 80) + { + PosAct.Rows[0].Cells[3].Value = rnd.Next(0, 100); + PosAct.Rows[0].Cells[4].Value = rnd.Next(0, 100); + PosAct.Rows[0].Cells[5].Value = rnd.Next(0, 100); + agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.POS_ACT; + } + + + // cambio power nel 15% dei casi... + if (rnd.Next(0, 100) > 85) + { + Power += rnd.Next(-10, 10); + } + // aggiungo 1 min AccTime nel 0.1% dei casi + if (rnd.Next(0, 1000) > 999) + { + AccTime++; + } + + // toggle 1 allarme sys nello 0.5% dei casi + if (rnd.Next(0, 1000) > 995) + { + int codAlarm = rnd.Next(1, 3); + switch (codAlarm) + { + case 1: + SysErr_01.Checked = !SysErr_01.Checked; + break; + case 2: + SysErr_02.Checked = !SysErr_02.Checked; + break; + default: + SysErr_03.Checked = !SysErr_03.Checked; + break; + } + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.AL01; + } + // toggle 1 allarme PLC nello 0.4% dei casi + if (rnd.Next(0, 1000) > 996) + { + int codAlarm = rnd.Next(1, 3); + switch (codAlarm) + { + case 1: + PlcErr_01.Checked = !PlcErr_01.Checked; + break; + case 2: + PlcErr_02.Checked = !PlcErr_02.Checked; + break; + default: + PlcErr_03.Checked = !PlcErr_03.Checked; + break; + } + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.AL02; + } + // toggle 1 allarme CNC nello 0.3% dei casi + if (rnd.Next(0, 1000) > 997) + { + int codAlarm = rnd.Next(1, 3); + switch (codAlarm) + { + case 1: + CncErr_01.Checked = !CncErr_01.Checked; + break; + case 2: + CncErr_02.Checked = !CncErr_02.Checked; + break; + default: + CncErr_03.Checked = !CncErr_03.Checked; + break; + } + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.AL03; + } + + // nel 2% dei casi imposto un RUN MODE diverso da RUN + if (rnd.Next(0, 100) > 98) + { + int codSel = rnd.Next(1, 3); + switch (codSel) + { + case 1: + manual.Checked = true; + break; + case 2: + mdi.Checked = true; + break; + default: + edit.Checked = true; + break; + } + } + // nel 3% degli altri casi riporto RUN MODE a RUN + else + { + if (rnd.Next(0, 100) > 97) + { + automatic.Checked = true; + } + } + + + // nel 2% dei casi imposto un EXE MODE diverso da AUTO + if (rnd.Next(0, 100) > 98) + { + int codSel = rnd.Next(1, 3); + switch (codSel) + { + case 1: + ready.Checked = true; + break; + case 2: + stopped.Checked = true; + break; + default: + feedhold.Checked = true; + break; + } + } + // nel 3% degli altri casi riporto EXE MODE a AUTO + else + { + if (rnd.Next(0, 100) > 97) + { + running.Checked = true; + } + } + + + + // controllo se ci sia il flag di lettura di un evento M/S/T nel qual caso lo abbasso... + if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.M_CODE)) + { + if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.M_CODE)) + { + agObj.STROBE_PLC -= Strobe.M_CODE; + } + } + if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.S_CODE)) + { + if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.S_CODE)) + { + agObj.STROBE_PLC -= Strobe.S_CODE; + } + } + if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.T_CODE)) + { + if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.T_CODE)) + { + agObj.STROBE_PLC -= Strobe.T_CODE; + } + } + + // controllo se ci sia il flag di lettura di un evento pz ok/ko nel qual caso lo abbasso... + if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.PZ_OK)) + { + if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.PZ_OK)) + { + agObj.STROBE_PLC -= Strobe.PZ_OK; + } + } + if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.PZ_KO)) + { + if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.PZ_KO)) + { + agObj.STROBE_PLC -= Strobe.PZ_KO; + } + } + + // controllo se ci sia il flag di lettura di un evento FEED_SPEED nel qual caso lo abbasso... + if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.FEED_SPEED)) + { + if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.FEED_SPEED)) + { + agObj.STROBE_PLC -= Strobe.FEED_SPEED; + } + } + + // controllo se ci sia il flag di lettura di un evento POS_ACT nel qual caso lo abbasso... + if (utils.IsSetAll(agObj.STROBE_ADP, Strobe.POS_ACT)) + { + if (utils.IsSetAll(agObj.STROBE_PLC, Strobe.POS_ACT)) + { + agObj.STROBE_PLC -= Strobe.POS_ACT; + } + } + + refreshVisualStrobes(); + refreshCodeMST(); + } + } + + private void refreshVisualStrobes() + { + // aggiorno visualizzazione strobe! + STATUS_STRB_DW0.Text = utils.binaryForm((int)agObj.STRB_DW0); + STATUS_STRB_DW1.Text = utils.binaryForm((int)agObj.STRB_DW1); + STATUS_STRB_DW2.Text = utils.binaryForm((int)agObj.STRB_DW2); + STATUS_STRB_DW3.Text = utils.binaryForm((int)agObj.STRB_DW3); + + STATUS_ACK_DW0.Text = utils.binaryForm((int)agObj.ACK_DW0); + STATUS_ACK_DW1.Text = utils.binaryForm((int)agObj.ACK_DW1); + STATUS_ACK_DW2.Text = utils.binaryForm((int)agObj.ACK_DW2); + STATUS_ACK_DW3.Text = utils.binaryForm((int)agObj.ACK_DW3); + } + + + + /// + /// aggiorna visualizzazione code... + /// + private void refreshCodeMST() + { + lblCodaM.Text = string.Join(",", agObj.codaM[0].ToArray()); + lblCodaT.Text = string.Join(",", agObj.codaT[0].ToArray()); + lblCodaS.Text = string.Join(",", agObj.codaS[0].ToArray()); + } + + private void accodaCodM() + { + if (addCodM.Text.Trim() != "") + { + agObj.appendCodeMST("M", addCodM.Text.Trim(), 0); + addCodM.Text = ""; + } + refreshCodeMST(); + } + private void accodaCodS() + { + if (addCodS.Text.Trim() != "") + { + agObj.appendCodeMST("S", addCodS.Text.Trim(), 0); + addCodS.Text = ""; + } + refreshCodeMST(); + } + private void accodaCodT() + { + if (addCodT.Text.Trim() != "") + { + agObj.appendCodeMST("T", addCodT.Text.Trim(), 0); + addCodT.Text = ""; + } + refreshCodeMST(); + } + + private void addCodM_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + // accodo testo + accodaCodM(); + } + else if (e.KeyCode == Keys.Escape) + { + // svuoto! + addCodM.Text = ""; + } + } + private void addCodM_Leave(object sender, EventArgs e) + { + accodaCodM(); + } + private void addCodS_KeyDown(object sender, KeyEventArgs e) + { + + if (e.KeyCode == Keys.Enter) + { + // accodo testo + accodaCodS(); + } + else if (e.KeyCode == Keys.Escape) + { + // svuoto! + addCodS.Text = ""; + } + } + private void addCodS_Leave(object sender, EventArgs e) + { + accodaCodS(); + } + private void addCodT_KeyDown(object sender, KeyEventArgs e) + { + + if (e.KeyCode == Keys.Enter) + { + // accodo testo + accodaCodT(); + } + else if (e.KeyCode == Keys.Escape) + { + // svuoto! + addCodT.Text = ""; + } + } + private void addCodT_Leave(object sender, EventArgs e) + { + accodaCodT(); + } + + + + #endregion + + private void txtPathFeed_TextChanged(object sender, EventArgs e) + { + hsPathFeed.Value = Convert.ToInt32(txtPathFeed.Text); + } + + private void txtPathFeedOver_TextChanged(object sender, EventArgs e) + { + hsPathFeedOver.Value = Convert.ToInt32(txtPathFeedOver.Text); + } + + private void txtPathSpeedOver_TextChanged(object sender, EventArgs e) + { + hsPathSpeedOver.Value = Convert.ToInt32(txtPathSpeedOver.Text); + } + + private void raiseFlag_FEED_SPEED() + { + // se non c'è flag lo alzo... + if (!utils.IsSetAll(agObj.STROBE_PLC, Strobe.FEED_SPEED)) + { + agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.FEED_SPEED; + } + } + + private void hsPathFeed_ValueChanged(object sender, EventArgs e) + { + txtPathFeed.Text = hsPathFeed.Value.ToString(); + raiseFlag_FEED_SPEED(); + } + + private void hsPathFeedOver_ValueChanged(object sender, EventArgs e) + { + txtPathFeedOver.Text = hsPathFeedOver.Value.ToString(); + raiseFlag_FEED_SPEED(); + } + + private void hsPathSpeedOver_ValueChanged(object sender, EventArgs e) + { + txtPathSpeedOver.Text = hsPathSpeedOver.Value.ToString(); + raiseFlag_FEED_SPEED(); + } + + #region gestione load conf + + private void mConfGen_Click(object sender, EventArgs e) + { + SetupAdapter setupWIndow = new SetupAdapter(); + setupWIndow.Show(this); + } + + private void mLoadAdaptConf_Click(object sender, EventArgs e) + { + // mostro selettore file x leggere adapter.. + OpenFileDialog openFileDial = new OpenFileDialog(); + + // directory iniziale + openFileDial.InitialDirectory = utils.confDir; // string.Format(@"{0}\{1}", Application.StartupPath, utils.CRS("dataConfPath")); + // Set filter options and filter index. + openFileDial.Filter = "XML Files (.xml)|*.xml|All Files (*.*)|*.*"; + openFileDial.FilterIndex = 1; + // altre opzioni + openFileDial.Multiselect = false; + + // Call the ShowDialog method to show the dialog box. + DialogResult userClickedOK = openFileDial.ShowDialog(); + + // Process input if the user clicked OK. + if (userClickedOK == DialogResult.OK) + { + string XmlConfFile = openFileDial.FileName; + loadXmlFile(XmlConfFile); + lg.Info("XML LOADED"); + loadPersistLayer(defPersLayerFile); + lg.Info("PersLayerFile READ"); + agObj.loadPersData(); + lg.Info("PersLayerFile LOADED"); + } + } + /// + /// Carica file XML della configurazione richiesta + /// + /// + private void loadXmlFile(string XmlConfFile) + { + displayTaskAndWait(string.Format("Loading XML: {0}", XmlConfFile)); + + // 2019.07.13: cambiata procedura SE ci trociamo nel caso OPC-UA / SOUR che parte da DataModel.xml... + if (utils.CRS("adpProto") == "SOURS" && XmlConfFile.EndsWith("DataModel.xml")) + { + + if (utils.CRB("verbose")) + { + lg.Info("Inizio caricamento DataModel per OPC-UA REDIS server"); + } + StreamReader file; + // carica da file... + file = new StreamReader(XmlConfFile); + // leggo file... + string rawDataModel = file.ReadToEnd(); + // faccio un primo passaggio di pulizia commenti... + string sanitizedXml = utils.xmlSanitize(rawDataModel); + // chiudo file + file.Close(); + // effettuo deserializzazione... + DataModel dm = new DataModel(); + currDataModel = dm.Deserialize(sanitizedXml); + // avvio oggetto conf + tipoScelto = tipoAdapter.ND; + adpConf = new AdapterConf(); + // salvo i dati "specifici" in base a quanto recuperato da DataModel... + // cerco da plc/Name il tipo di PLC... + string plcName = ""; + var trovatoCnc = currDataModel.Properties.Find(x => x.BrowseName.Contains("Machine:Cnc:Name")); + var trovatoPlc = currDataModel.Properties.Find(x => x.BrowseName.Contains("Machine:Plc:Name")); + if (trovatoCnc != null) + { + plcName = trovatoCnc.Value; + } + else if (trovatoPlc != null) + { + plcName = trovatoPlc.Value; + } + Enum.TryParse(plcName, true, out tipoScelto); + adpConf.NomeAdapt = plcName; + adpConf.TipoAdapt = tipoScelto; + adpConf.Version = 2; + + // preparo file temp x generazione regole + StreamWriter fileOut; + // apro in scrittura il file x mettere le regole... + fileOut = new StreamWriter(utils.nameRepRoleFileSOUR); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine("# AUTO GENERATED Replacement Conf File"); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine(""); + + // altre variabili + element newElement; + int numRules = 0; + string replString = ""; + + // cerco ASSI come componenti... + List> dataRefList; + int numAx = 0; + List listaAx = new List(); + List selAxes = currDataModel.Components.FindAll(x => x.BrowseName.Contains("Machine:Axes:")); + string axBaseName = ""; + string axType = "LINEAR"; + foreach (var item in selAxes) + { + numAx++; + axBaseName = $"Axis_{numAx.ToString("00")}"; + // preparo la datarefList x TIPO assi e gruppo... + dataRefList = new List>(); + // calcolo TIPO DI ASSE + DataModel.Property tipoAsse = currDataModel.Properties.Find(x => x.BrowseName.Contains($"{item.BrowseName}:Type")); + if (tipoAsse != null) + { + axType = tipoAsse.Value; + } + else + { + axType = "LINEAR"; + } + + // aggiungo conf specifiche... + dataRefList.Add(new DataRefItem($"Axis_{ numAx.ToString("00") }_Grp", "00")); + dataRefList.Add(new DataRefItem($"Axis_{ numAx.ToString("00") }_Type", axType)); + + newElement = new element() + { + ident = axBaseName, + dataRefList = dataRefList + }; + listaAx.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_PosAct|{replString}:CurrentPos"); + fileOut.WriteLine($"A|I|{newElement.ident}_PosTgt|{replString}:TargetPos"); + fileOut.WriteLine($"A|I|{newElement.ident}_InvDDone|{replString}:Inversions"); + fileOut.WriteLine($"A|I|{newElement.ident}_DistDone|{replString}:Distance"); + fileOut.WriteLine($"A|I|{newElement.ident}_FeedAct|{replString}:FeedRate"); + fileOut.WriteLine($"A|I|{newElement.ident}_FeedOver|{replString}:FeedOverride"); + fileOut.WriteLine($"A|I|{newElement.ident}_MainProc|{replString}:ParentProc"); + fileOut.WriteLine($"A|I|{newElement.ident}_Descr|{replString}:Name"); + fileOut.WriteLine($"A|I|AV_{newElement.ident}_Load|{replString}:Load"); + fileOut.WriteLine($"A|I|{newElement.ident}_AccTime|{replString}:ActiveTime"); + fileOut.WriteLine($"A|I|{newElement.ident}_Dir|{replString}:Direction"); + fileOut.WriteLine($"A|I|{newElement.ident}_MastId|{replString}:MasterId"); + fileOut.WriteLine($"A|I|{newElement.ident}_Type|{replString}:Type"); + numRules += 13; + } + fileOut.WriteLine(""); + + // cerco PATH/Processi come componenti... + int numProc = 0; + List listaProc = new List(); + List selProc = currDataModel.Components.FindAll(x => x.BrowseName.Contains("Machine:Cnc:CncProcesses:")); + string procBaseName = ""; + foreach (var item in selProc) + { + numProc++; + procBaseName = $"Path_{numProc.ToString("00")}"; + newElement = new element() + { + ident = procBaseName + }; + listaProc.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_CodG_Act|{replString}:CodG"); + fileOut.WriteLine($"A|I|{newElement.ident}_Cod_M|{replString}:CodM"); + fileOut.WriteLine($"A|I|{newElement.ident}_Cod_S|{replString}:CodS"); + fileOut.WriteLine($"A|I|{newElement.ident}_Cod_T|{replString}:CodT"); + fileOut.WriteLine($"A|I|{newElement.ident}_EXE_MODE|{replString}:Status"); + fileOut.WriteLine($"A|I|{newElement.ident}_RUN_MODE|{replString}:Mode"); + fileOut.WriteLine($"A|I|{newElement.ident}_RapidOverr|{replString}:RapidOverride"); + fileOut.WriteLine($"A|I|{newElement.ident}_FeedOverr|{replString}:FeedOverride"); + + numRules += 8; + } + fileOut.WriteLine(""); + + // cerco UnOp / Gruppi operatori come componenti... + int numUnOp = 0; + List listaUnOp = new List(); + List selUnOp = currDataModel.Components.FindAll(x => x.BrowseName.Contains("Machine:OperatingGroups:")); + string procUnOp = ""; + foreach (var item in selUnOp) + { + numUnOp++; + procUnOp = $"UnOp_{numUnOp.ToString("00")}"; + newElement = new element() + { + ident = procUnOp + }; + listaUnOp.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Speed|{replString}:SpeedRate"); + fileOut.WriteLine($"A|I|{newElement.ident}_KRev|{replString}:Distance"); + fileOut.WriteLine($"A|I|{newElement.ident}_AccTime|{replString}:ActiveTime"); + fileOut.WriteLine($"A|I|{newElement.ident}_ToolId|{replString}:ToolId"); + fileOut.WriteLine($"A|I|{newElement.ident}_NumCU|{replString}:ToolChanges"); + fileOut.WriteLine($"A|I|{newElement.ident}_Load|{replString}:Load"); + fileOut.WriteLine($"A|I|{newElement.ident}_SpeedOverr|{replString}:SpeedOverride"); + fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); + fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); + + numRules += 9; + } + fileOut.WriteLine(""); + + // cerco nei gruppi ausiliari... + // VacuumPump... + int numVP = 0; + List listaVP = new List(); + // VacuumAct + int numVA = 0; + List listaVA = new List(); + // SlittaMag + int numSlMag = 0; + List listaSlMag = new List(); + // ProtMag + int numProtMag = 0; + List listaProtMag = new List(); + // SlittaTast + int numSlTas = 0; + List listaSlTas = new List(); + // Cooler... + int numCool = 0; + List listaCool = new List(); + // Lubro... + int numLub = 0; + List listaLub = new List(); + // Press... + int numPres = 0; + List listaPress = new List(); + // Temperature... + int numTemp = 0; + List listaTemp = new List(); + + // ricerca gruppi AUX + List selAux = currDataModel.Properties.FindAll(x => x.BrowseName.Contains("Machine:AuxiliaryGroups")); + foreach (var item in selAux) + { + if (item.Value == "COOLER") + { + numCool++; + newElement = new element() + { + ident = $"Cooler_{numCool.ToString("00")}" + }; + listaCool.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); + numRules++; + } + if (item.Value == "LUBRO") + { + numLub++; + newElement = new element() + { + ident = $"Lubro_{numLub.ToString("00")}" + }; + listaLub.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); + numRules++; + } + if (item.Value == "VACUUM_PUMP") + { + numVP++; + newElement = new element() + { + ident = $"VacPump_{numVP.ToString("00")}" + }; + listaVP.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); + fileOut.WriteLine($"A|I|{newElement.ident}_WrkTime|{replString}:ActiveTime"); + numRules += 2; + } + if (item.Value == "VACUUM_ACT") + { + numVA++; + newElement = new element() + { + ident = $"VacAct_{numVA.ToString("00")}" + }; + listaVA.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); + fileOut.WriteLine($"A|I|{newElement.ident}_Value|{replString}:Value"); + numRules++; + } + if (item.Value == "SLITTA_MAG") + { + numSlMag++; + newElement = new element() + { + ident = $"SlittaMagazzino_{numSlMag.ToString("00")}" + }; + listaSlMag.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); + numRules++; + } + if (item.Value == "PROT_MAG") + { + numProtMag++; + newElement = new element() + { + ident = $"ProtezioneMagazzino_{numProtMag.ToString("00")}" + }; + listaProtMag.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); + numRules++; + } + if (item.Value == "SLITTA_TAST") + { + numSlTas++; + newElement = new element() + { + ident = $"SlittaTastatore_{numPres.ToString("00")}" + }; + listaSlTas.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); + numRules++; + } + if (item.Value == "PRESSURE") + { + numPres++; + newElement = new element() + { + ident = $"Press_{numPres.ToString("00")}" + }; + listaPress.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Value|{replString}:Value"); + fileOut.WriteLine($"A|I|{newElement.ident}_Status|{replString}:Status"); + fileOut.WriteLine($"A|I|{newElement.ident}_Count|{replString}:Repetitions"); + numRules += 1; + } + if (item.Value == "TEMPERATURE") + { + numTemp++; + newElement = new element() + { + ident = $"Temp_{numTemp.ToString("00")}" + }; + listaTemp.Add(newElement); + // aggiungo su file... + replString = item.BrowseName.Replace("Machine:", "").Replace(":Type", ""); + fileOut.WriteLine($"A|I|{newElement.ident}_Value|{replString}:Value"); + numRules += 1; + } + } + + // aggiungo elementi trovati... + if (numAx > 0) + { + element[] elencoAx = listaAx.ToArray(); + adpConf.Axis = elencoAx; + } + if (numProc > 0) + { + element[] elencoProc = listaProc.ToArray(); + adpConf.Path = elencoProc; + } + if (numUnOp > 0) + { + element[] elencoUnOp = listaUnOp.ToArray(); + adpConf.UnOp = elencoUnOp; + } + if (numCool > 0) + { + element[] elencoCool = listaCool.ToArray(); + adpConf.Cooler = elencoCool; + } + if (numLub > 0) + { + element[] elencoLub = listaLub.ToArray(); + adpConf.Lubro = elencoLub; + } + if (numVP > 0) + { + element[] elencoVP = listaVP.ToArray(); + adpConf.VacuumPump = elencoVP; + } + if (numVA > 0) + { + element[] elencoVA = listaVA.ToArray(); + adpConf.VacuumAct = elencoVA; + } + if (numSlMag > 0) + { + element[] elencoSlMag = listaSlMag.ToArray(); + adpConf.SlittaMag = elencoSlMag; + } + if (numSlTas > 0) + { + element[] elencoSlTas = listaSlTas.ToArray(); + adpConf.SlittaTas = elencoSlTas; + } + if (numPres > 0) + { + element[] elencoPress = listaPress.ToArray(); + adpConf.Press = elencoPress; + } + if (numTemp > 0) + { + element[] elencoTemp = listaTemp.ToArray(); + adpConf.Temp = elencoTemp; + } + + fileOut.WriteLine(""); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine($"# EOF: {numRules} rules added"); + fileOut.WriteLine("#------------------------------------------------------------"); + // chiudo file regole! + fileOut.Close(); + + // compilo il file StatusList.SOUR.map + fileOut = new StreamWriter(utils.StatusListFileSOUR); + int numStatus = 0; + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine("# AUTO GENERATED StatusList File"); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine(""); + // ricerca in variabili + List selStatus = currDataModel.Variables.FindAll(x => x.CmsDataType.Equals("StatusList")); + foreach (var item in selStatus) + { + // compilo il file di conf... + fileOut.WriteLine($"{item.CmsDataIndex}|{item.BrowseName.Replace("Machine:", "")}|{item.CmsDataOpt}"); + numStatus++; + } + fileOut.WriteLine(""); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine($"# EOF: {numStatus} status added"); + fileOut.WriteLine("#------------------------------------------------------------"); + // chiudo file regole! + fileOut.Close(); + + // compilo il file CounterList.SOUR.map + fileOut = new StreamWriter(utils.CounterListFileSOUR); + int numCounter = 0; + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine("# AUTO GENERATED CounterList File"); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine(""); + // ricerca in variabili + List selCounter = currDataModel.Variables.FindAll(x => x.CmsDataType.Equals("CounterList")); + foreach (var item in selCounter) + { + // compilo il file di conf... se è RTV aggiungo la parte RTV.. + fileOut.WriteLine($"{item.CmsDataIndex}|{item.BrowseName.Replace("Machine:", "")}|{item.CmsDataOpt}|{item.CmsDataScale}"); + numCounter++; + } + fileOut.WriteLine(""); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine($"# EOF: {numCounter} counters added"); + fileOut.WriteLine("#------------------------------------------------------------"); + // chiudo file regole! + fileOut.Close(); + + // compilo il file AnalogList.SOUR.map + fileOut = new StreamWriter(utils.AnalogListFileSOUR); + int numAnalog = 0; + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine("# AUTO GENERATED AnalogList File"); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine(""); + // ricerca in variabili + List selAnalog = currDataModel.Variables.FindAll(x => x.CmsDataType.Equals("AnalogData")); + foreach (var item in selAnalog) + { + // compilo il file di conf... + fileOut.WriteLine($"{item.CmsDataIndex}|{item.BrowseName.Replace("Machine:", "")}|{item.CmsDataOpt}"); + numAnalog++; + } + fileOut.WriteLine(""); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine($"# EOF: {numAnalog} analog added"); + fileOut.WriteLine("#------------------------------------------------------------"); + // chiudo file regole! + fileOut.Close(); + + + // compilo il file StringList.SOUR.map + fileOut = new StreamWriter(utils.StringListFileSOUR); + int numString = 0; + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine("# AUTO GENERATED StringList File"); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine(""); + // ricerca in variabili + List selString = currDataModel.Variables.FindAll(x => x.CmsDataType.Equals("StringData")); + foreach (var item in selString) + { + // compilo il file di conf... + fileOut.WriteLine($"{item.CmsDataIndex}|{item.BrowseName.Replace("Machine:", "")}|{item.CmsDataOpt}"); + numString++; + } + fileOut.WriteLine(""); + fileOut.WriteLine("#------------------------------------------------------------"); + fileOut.WriteLine($"# EOF: {numString} string added"); + fileOut.WriteLine("#------------------------------------------------------------"); + // chiudo file regole! + fileOut.Close(); + } + else + { + // legge obj configurazione "legacy" + adpConf = AdapterConf.Deserialize(XmlConfFile); + } + + + // indico quale sia il tipo di adapter + tipoScelto = adpConf.TipoAdapt; + loadAdapterType(); + // carico file XML in web browser... + wbXmlConf.DocumentText = AdapterConf.rawXml(XmlConfFile); + displayTaskAndWait("XML loaded"); + + // avvio macchina con adapter specificato... + if (utils.CRB("autoStartOnLoad")) + { + displayTaskAndWait("Auto Starting..."); + // avvio! + avviaAdapter(); + displayTaskAndWait("Auto Started!"); + } + } + /// + /// salva su file l'oggetto AdapterConf + /// + /// + public void saveXmlFile(string XmlConfFile) + { + AdapterConf.Serialize(XmlConfFile, agObj.currAdpConf); + } + /// + /// Salva su file l'oggetto di persistenza dati + /// + /// + public void savePersistLayer(string filePath) + { + // in primis check semaforo salvataggio... + if (!agObj.adpSaving) + { + // alzo semaforo salvataggio + agObj.adpSaving = true; + // se HO dei dati... + if (agObj.persistenceLayer != null) + { + try + { + utils.WritePlain(agObj.persistenceLayer, filePath); + } + catch (Exception exc) + { + lg.Error(string.Format("Errore salvataggio file{0}{1}", Environment.NewLine, exc)); + } + } + else + { + lg.Info("persistenceLayer null, non salvato..."); + } + // abbasso semaforo salvataggio + agObj.adpSaving = false; + } + } + /// + /// Carica da file l'oggetto di persistenza dati + /// + /// + public void loadPersistLayer(string filePath) + { + // inizializzo prima di leggere... + agObj.persistenceLayer = new Dictionary(); + agObj.persistenceLayer = utils.ReadPlain(filePath); + + // 2017.03.23 check problema files corrotti... + if (agObj.persistenceLayer.Count == 0) + { + // se avesse letto un valore NON coerente (senza righe) PROVA a leggere a ritroso vecchi files... da histPersLayerFile e precedenti... + int numDD = 0; + int maxNumDD = utils.CRI("maxNumDD"); + Dictionary lastRead = new Dictionary(); + // continuo fino a che non leggo almeno 1 riga valida e non ho raggiunto maxDD + while (numDD < maxNumDD && lastRead.Count == 0) + { + // leggo il file storico alla data anticipata... (ci provo...) + try + { + lastRead = utils.ReadPlain(prevPersLayerFile(numDD)); + } + catch + { } + numDD++; + } + // se sono uscito PROVO a passare il file storico letto buono (oppure vuoto...) + agObj.persistenceLayer = lastRead; + } + } + + #endregion + + #region accesso dati produzione + + /// + /// Dati di produzione (su form) + /// + public prodData datiProd + { + get + { + prodData answ = new prodData(); + // carico da form + answ.Operator = OPERATOR_ID.Text; + + // variabili lette da + controlli + answ.Status = stop.Enabled; + answ.Power = Power; + answ.AccTime = AccTime; + answ.EmrStop = estop.Checked; + + answ.FuncMode = ((KeyValuePair)functionalMode.SelectedItem).Key; + + answ.MessageCode = messageCode.Text; + answ.MessageText = messageText.Text; + + // parto vuoto con StatusBitMap... + agObj.STATUS_FLAG = 0; + // aggiorno status flag in base a valori... + if (answ.EmrStop) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.ESTOP; + } + + if (automatic.Checked) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_AUTO; + } + else if (mdi.Checked) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_MDI; + } + else if (edit.Checked) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_EDIT; + } + else + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_MANUAL; + } + + if (running.Checked) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_RUN; + } + else if (feedhold.Checked) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_FEEDHOLD; + } + else if (stopped.Checked) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_STOP; + } + else if (ready.Checked) + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_READY; + } + + if (answ.FuncMode != "1") + { + agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.HM; + } + + // ritorno oggetto! + return answ; + } + } + + private void pzKo_TextChanged(object sender, EventArgs e) + { + agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.PZ_KO; + txtPzTot.Text = pzTot.ToString(); + } + protected int Power + { + get + { + int answ = 0; + try + { + answ = Convert.ToInt32(txtPower.Text.Trim()); + } + catch + { } + return answ; + } + set + { + txtPower.Text = value.ToString(); + } + } + protected int AccTime + { + get + { + int answ = 0; + try + { + answ = Convert.ToInt32(txtAccTime.Text.Trim()); + } + catch + { } + return answ; + } + set + { + txtAccTime.Text = value.ToString(); + } + } + protected int pzOk + { + get + { + int answ = 0; + try + { + answ = Convert.ToInt32(txtPzOk.Text.Trim()); + } + catch + { } + return answ; + } + set + { + txtPzOk.Text = value.ToString(); + txtPzTot.Text = pzTot.ToString(); + } + } + protected int pzKo + { + get + { + int answ = 0; + try + { + answ = Convert.ToInt32(txtPzKo.Text.Trim()); + } + catch + { } + return answ; + } + set + { + txtPzKo.Text = value.ToString(); + txtPzTot.Text = pzTot.ToString(); + } + } + protected int pzTot + { + get + { + return pzOk + pzKo; + } + } + + #endregion + + #region gestione Path + + + private void cbPathSel_SelectedIndexChanged(object sender, EventArgs e) + { + hsPathFeed.Value = 50; + hsPathFeedOver.Value = 50; + hsPathSpeedOver.Value = 50; + Random rnd = new Random(); + PosAct.Rows[0].Cells[0].Value = rnd.Next(0, 1000); + PosAct.Rows[0].Cells[1].Value = rnd.Next(0, 1000); + PosAct.Rows[0].Cells[2].Value = rnd.Next(0, 1000); + PosAct.Rows[0].Cells[3].Value = rnd.Next(0, 360); + PosAct.Rows[0].Cells[4].Value = rnd.Next(0, 360); + PosAct.Rows[0].Cells[5].Value = rnd.Next(0, 360); + } + + /// + /// Dati PATH + /// + public PathData CurrPath + { + get + { + PathData answ = new PathData(); + + // modifiche dati portati da macchina a path... + answ.ProgramName = program.Text; + answ.ProgrRow = PROG_ROW_NUM.Text; + answ.PartId = partID.Text; + answ.PathType = "LAVORO"; + // pezzi + answ.pzTot = pzOk + pzKo; + + if (automatic.Checked) + { + answ.RunMode = "AUTOMATIC"; + } + else if (mdi.Checked) + { + answ.RunMode = "MANUAL_DATA_INPUT"; + } + else if (edit.Checked) + { + answ.RunMode = "EDIT"; + } + else + { + answ.RunMode = "MANUAL"; + } + + if (running.Checked) + { + answ.ExeMode = "ACTIVE"; + } + else if (feedhold.Checked) + { + answ.ExeMode = "FEED_HOLD"; + } + else if (stopped.Checked) + { + answ.ExeMode = "STOPPED"; + } + else if (ready.Checked) + { + answ.ExeMode = "READY"; + } + + answ.ActiveAxes = "1 2 3 4 5 6"; // lascio sempre tutti attivi... + + answ.PathSel = cbPathSel.SelectedIndex; + // FEED/SPEED + answ.PathFeedrate = hsPathFeed.Value; + answ.PathFeedrateOver = hsPathFeedOver.Value; + answ.PathRapidOver = hsPathSpeedOver.Value; + + // posizione attuale + position posAct = new position(); + DataGridViewCellCollection cells = PosAct.Rows[0].Cells; + float.TryParse(cells[0].Value.ToString(), out posAct.x); + float.TryParse(cells[1].Value.ToString(), out posAct.y); + float.TryParse(cells[2].Value.ToString(), out posAct.z); + float.TryParse(cells[3].Value.ToString(), out posAct.i); + float.TryParse(cells[4].Value.ToString(), out posAct.j); + float.TryParse(cells[5].Value.ToString(), out posAct.k); + answ.PathPosAct = posAct; + + // aggiungo altri oggetti... + answ.CodG_Act = "G01 G10 G34 G60"; + answ.SubMode = string.Format("# Asse selezionato {0} #", cbAxNum.SelectedIndex); + + // ritorno oggetto! + return answ; + } + } + + #endregion + + #region gestione UnOp + + + private void cbUnOpSel_SelectedIndexChanged(object sender, EventArgs e) + { + uoToolId.Text = "1"; + uoNumCU.Text = "2"; + uoStatus.Text = "RUN"; + uoVitaRes.Text = "100"; + uoSpeed.Text = "80"; + uoLoad.Text = "80"; + uoAccTime.Text = "0"; + } + + /// + /// Dati UnOp + /// + public UnOpData CurrUnOp + { + get + { + UnOpData answ = new UnOpData(); + + answ.UnOpSel = cbUnOpSel.SelectedIndex; + + int.TryParse(uoToolId.Text, out answ.UnOpToolId); + int.TryParse(uoNumCU.Text, out answ.UnOpNumCU); + answ.UnOpStatus = uoStatus.Text; + int.TryParse(uoVitaRes.Text, out answ.UnOpVitaRes); + answ.UnOpSpeed = uoSpeed.Value; + answ.UnOpLoad = uoLoad.Value; + int.TryParse(uoAccTime.Text, out answ.UnOpAccTime); + + // ritorno oggetto! + return answ; + } + } + + private void uoLoad_Scroll(object sender, ScrollEventArgs e) + { + uoLoadVal.Text = uoLoad.Value.ToString(); + } + + private void uoSpeed_Scroll(object sender, ScrollEventArgs e) + { + uoSpeedVal.Text = uoSpeed.Value.ToString(); + } + + #endregion + + #region gestione assi + + private void cbAxNum_SelectedIndexChanged(object sender, EventArgs e) + { + // aggiorna visualizzazione asse... imposto valori standard x controlli + AxMainProc.Text = "1"; + AxIsMaster.Checked = false; + AxMastId.Text = "0"; + cbAxType.SelectedIndex = 0; + AxDir.Text = "1"; + AxLoad.Value = 50; + AxPosition.Value = 50; + AxFeedAct.Value = 50; + AxFeedOver.Value = 100; + AxAccelAct.Text = "0"; + AxAccTime.Text = "0"; + AxBatt.Text = "100"; + } + + /// + /// Dati di produzione (su form) + /// + public AxisData CurrAxis + { + get + { + Random rnd = new Random(); + AxisData answ = new AxisData(); + answ.AxisSel = cbAxNum.SelectedIndex; + answ.AxisMainProc = AxMainProc.Text; + answ.AxisIsMaster = AxIsMaster.Checked; + answ.AxisMastId = AxMastId.Text; + answ.AxisType = AxisType; + answ.AxisDir = AxDir.Text; + answ.AxisLoad = AxLoad.Value; + answ.AxisPosAct = AxPosition.Value;// + rnd.Next(-2, 2); + answ.AxisPosTgt = AxPosition.Value; + answ.AxisFeedAct = AxFeedAct.Value; + answ.AxisFeedOver = AxFeedOver.Value; + answ.AxisAccel = AxAccelAct.Text; + answ.AxisAccTime = AxAccTime.Text; + answ.AxisBattery = AxBatt.Text; + + // restituisco oggetto + return answ; + } + } + + protected string AxisType + { + get + { + string answ = ""; + try + { + answ = cbAxType.SelectedItem.ToString(); ; + } + catch + { } + return answ; + } + } + + private void xLoad_Scroll(object sender, ScrollEventArgs e) + { + AxLoadValue.Text = AxLoad.Value.ToString(); + } + + private void xPosition_Scroll(object sender, ScrollEventArgs e) + { + + AxPositionValue.Text = AxPosition.Value.ToString(); + } + + private void cLoad_Scroll(object sender, ScrollEventArgs e) + { + AxFeedActValue.Text = AxFeedAct.Value.ToString(); + } + + private void cSpeed_Scroll(object sender, ScrollEventArgs e) + { + AxFeedOverValue.Text = AxFeedOver.Value.ToString(); + } + + #endregion + + + /// + /// Stringa dati monitoraggio mostrata (1 SX)... + /// + public string dataMonitor_1 + { + get + { + return lblOutMessage.Text; + } + set + { + lblOutMessage.Text = value; + } + } + /// + /// Stringa dati monitoraggio mostrata (2 centro)... + /// + public string dataMonitor_2 + { + get + { + return lblOutMessage2.Text; + } + set + { + lblOutMessage2.Text = value; + } + } + /// + /// Stringa dati monitoraggio mostrata (3 dx)... + /// + public string dataMonitor_3 + { + get + { + return lblOutMessage3.Text; + } + set + { + lblOutMessage3.Text = value; + } + } + private void SysErr_01_CheckedChanged(object sender, EventArgs e) + { + // aggiorno il vettore allarmi (da 128 byte) sulla base della selezione dei checkbox (per i primi 9...) - ultimi 3 selezionano 8 allarmi ciascuno... + BitArray bitArray = new BitArray(1024); + + bitArray[7] = SysErr_01.Checked; + bitArray[6] = SysErr_02.Checked; + bitArray[5] = SysErr_03.Checked; + bitArray[4] = PlcErr_01.Checked; + bitArray[3] = PlcErr_02.Checked; + bitArray[2] = PlcErr_03.Checked; + bitArray[1] = CncErr_01.Checked; + bitArray[0] = CncErr_02.Checked; + bitArray[15] = CncErr_03.Checked; + + // ne assegno 8 alla volta... + for (int i = 0; i < 8; i++) + { + bitArray[16 + i] = ErrRT_01.Checked; + bitArray[24 + i] = ErrRT_02.Checked; + bitArray[32 + i] = ErrRT_03.Checked; + } + byte[] newAlarm = utils.ToByteArray(bitArray); + Buffer.BlockCopy(newAlarm, 0, agObj.currGateway.AlarmFlags, 0, 32); + } + + private void MainForm_FormClosing(object sender, FormClosingEventArgs e) + { + closeAdapter(); + } + + private void closeAdapter() + { + fermaTutto(true, false); + } + /// + /// Ferma tutti i componenti adapter + update buttons + /// + /// determina se fermare il timer (gather) principale (solo se non si chiude) + /// determina se tentare di riconnettersi + private void fermaTutto(bool stopTimer, bool tryRestart) + { + agObj.stopAdapter(tryRestart); + // salvo! + savePersistLayer(defPersLayerFile); + savePersistLayer(histPersLayerFile); + + stop.Enabled = false; + dump.Enabled = false; + start.Enabled = true; + + if (stopTimer) + { + gather.Enabled = false; + agObj.tryDisconnect(); + } + } - // avvio macchina con adapter specificato... - if (utils.CRB("autoStartOnLoad")) - { - displayTaskAndWait("Auto Starting..."); - // avvio! - avviaAdapter(); - displayTaskAndWait("Auto Started!"); - } } - /// - /// salva su file l'oggetto AdapterConf - /// - /// - public void saveXmlFile(string XmlConfFile) - { - AdapterConf.Serialize(XmlConfFile, agObj.currAdpConf); - } - /// - /// Salva su file l'oggetto di persistenza dati - /// - /// - public void savePersistLayer(string filePath) - { - // in primis check semaforo salvataggio... - if (!agObj.adpSaving) - { - // alzo semaforo salvataggio - agObj.adpSaving = true; - // se HO dei dati... - if (agObj.persistenceLayer != null) - { - try - { - utils.WritePlain(agObj.persistenceLayer, filePath); - } - catch (Exception exc) - { - lg.Error(string.Format("Errore salvataggio file{0}{1}", Environment.NewLine, exc)); - } - } - else - { - lg.Info("persistenceLayer null, non salvato..."); - } - // abbasso semaforo salvataggio - agObj.adpSaving = false; - } - } - /// - /// Carica da file l'oggetto di persistenza dati - /// - /// - public void loadPersistLayer(string filePath) - { - // inizializzo prima di leggere... - agObj.persistenceLayer = new Dictionary(); - agObj.persistenceLayer = utils.ReadPlain(filePath); - - // 2017.03.23 check problema files corrotti... - if (agObj.persistenceLayer.Count == 0) - { - // se avesse letto un valore NON coerente (senza righe) PROVA a leggere a ritroso vecchi files... da histPersLayerFile e precedenti... - int numDD = 0; - int maxNumDD = utils.CRI("maxNumDD"); - Dictionary lastRead = new Dictionary(); - // continuo fino a che non leggo almeno 1 riga valida e non ho raggiunto maxDD - while (numDD < maxNumDD && lastRead.Count == 0) - { - // leggo il file storico alla data anticipata... (ci provo...) - try - { - lastRead = utils.ReadPlain(prevPersLayerFile(numDD)); - } - catch - { } - numDD++; - } - // se sono uscito PROVO a passare il file storico letto buono (oppure vuoto...) - agObj.persistenceLayer = lastRead; - } - } - - #endregion - - #region accesso dati produzione - - /// - /// Dati di produzione (su form) - /// - public prodData datiProd - { - get - { - prodData answ = new prodData(); - // carico da form - answ.Operator = OPERATOR_ID.Text; - - // variabili lette da + controlli - answ.Status = stop.Enabled; - answ.Power = Power; - answ.AccTime = AccTime; - answ.EmrStop = estop.Checked; - - answ.FuncMode = ((KeyValuePair)functionalMode.SelectedItem).Key; - - answ.MessageCode = messageCode.Text; - answ.MessageText = messageText.Text; - - // parto vuoto con StatusBitMap... - agObj.STATUS_FLAG = 0; - // aggiorno status flag in base a valori... - if (answ.EmrStop) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.ESTOP; - } - - if (automatic.Checked) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_AUTO; - } - else if (mdi.Checked) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_MDI; - } - else if (edit.Checked) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_EDIT; - } - else - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.RM_MANUAL; - } - - if (running.Checked) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_RUN; - } - else if (feedhold.Checked) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_FEEDHOLD; - } - else if (stopped.Checked) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_STOP; - } - else if (ready.Checked) - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.EM_READY; - } - - if (answ.FuncMode != "1") - { - agObj.STATUS_FLAG = agObj.STATUS_FLAG | StatusBitMap.HM; - } - - // ritorno oggetto! - return answ; - } - } - - private void pzKo_TextChanged(object sender, EventArgs e) - { - agObj.STROBE_PLC = agObj.STROBE_PLC | Strobe.PZ_KO; - txtPzTot.Text = pzTot.ToString(); - } - protected int Power - { - get - { - int answ = 0; - try - { - answ = Convert.ToInt32(txtPower.Text.Trim()); - } - catch - { } - return answ; - } - set - { - txtPower.Text = value.ToString(); - } - } - protected int AccTime - { - get - { - int answ = 0; - try - { - answ = Convert.ToInt32(txtAccTime.Text.Trim()); - } - catch - { } - return answ; - } - set - { - txtAccTime.Text = value.ToString(); - } - } - protected int pzOk - { - get - { - int answ = 0; - try - { - answ = Convert.ToInt32(txtPzOk.Text.Trim()); - } - catch - { } - return answ; - } - set - { - txtPzOk.Text = value.ToString(); - txtPzTot.Text = pzTot.ToString(); - } - } - protected int pzKo - { - get - { - int answ = 0; - try - { - answ = Convert.ToInt32(txtPzKo.Text.Trim()); - } - catch - { } - return answ; - } - set - { - txtPzKo.Text = value.ToString(); - txtPzTot.Text = pzTot.ToString(); - } - } - protected int pzTot - { - get - { - return pzOk + pzKo; - } - } - - #endregion - - #region gestione Path - - - private void cbPathSel_SelectedIndexChanged(object sender, EventArgs e) - { - hsPathFeed.Value = 50; - hsPathFeedOver.Value = 50; - hsPathSpeedOver.Value = 50; - Random rnd = new Random(); - PosAct.Rows[0].Cells[0].Value = rnd.Next(0, 1000); - PosAct.Rows[0].Cells[1].Value = rnd.Next(0, 1000); - PosAct.Rows[0].Cells[2].Value = rnd.Next(0, 1000); - PosAct.Rows[0].Cells[3].Value = rnd.Next(0, 360); - PosAct.Rows[0].Cells[4].Value = rnd.Next(0, 360); - PosAct.Rows[0].Cells[5].Value = rnd.Next(0, 360); - } - - /// - /// Dati PATH - /// - public PathData CurrPath - { - get - { - PathData answ = new PathData(); - - // modifiche dati portati da macchina a path... - answ.ProgramName = program.Text; - answ.ProgrRow = PROG_ROW_NUM.Text; - answ.PartId = partID.Text; - answ.PathType = "LAVORO"; - // pezzi - answ.pzTot = pzOk + pzKo; - - if (automatic.Checked) - { - answ.RunMode = "AUTOMATIC"; - } - else if (mdi.Checked) - { - answ.RunMode = "MANUAL_DATA_INPUT"; - } - else if (edit.Checked) - { - answ.RunMode = "EDIT"; - } - else - { - answ.RunMode = "MANUAL"; - } - - if (running.Checked) - { - answ.ExeMode = "ACTIVE"; - } - else if (feedhold.Checked) - { - answ.ExeMode = "FEED_HOLD"; - } - else if (stopped.Checked) - { - answ.ExeMode = "STOPPED"; - } - else if (ready.Checked) - { - answ.ExeMode = "READY"; - } - - answ.ActiveAxes = "1 2 3 4 5 6"; // lascio sempre tutti attivi... - - answ.PathSel = cbPathSel.SelectedIndex; - // FEED/SPEED - answ.PathFeedrate = hsPathFeed.Value; - answ.PathFeedrateOver = hsPathFeedOver.Value; - answ.PathRapidOver = hsPathSpeedOver.Value; - - // posizione attuale - position posAct = new position(); - DataGridViewCellCollection cells = PosAct.Rows[0].Cells; - float.TryParse(cells[0].Value.ToString(), out posAct.x); - float.TryParse(cells[1].Value.ToString(), out posAct.y); - float.TryParse(cells[2].Value.ToString(), out posAct.z); - float.TryParse(cells[3].Value.ToString(), out posAct.i); - float.TryParse(cells[4].Value.ToString(), out posAct.j); - float.TryParse(cells[5].Value.ToString(), out posAct.k); - answ.PathPosAct = posAct; - - // aggiungo altri oggetti... - answ.CodG_Act = "G01 G10 G34 G60"; - answ.SubMode = string.Format("# Asse selezionato {0} #", cbAxNum.SelectedIndex); - - // ritorno oggetto! - return answ; - } - } - - #endregion - - #region gestione UnOp - - - private void cbUnOpSel_SelectedIndexChanged(object sender, EventArgs e) - { - uoToolId.Text = "1"; - uoNumCU.Text = "2"; - uoStatus.Text = "RUN"; - uoVitaRes.Text = "100"; - uoSpeed.Text = "80"; - uoLoad.Text = "80"; - uoAccTime.Text = "0"; - } - - /// - /// Dati UnOp - /// - public UnOpData CurrUnOp - { - get - { - UnOpData answ = new UnOpData(); - - answ.UnOpSel = cbUnOpSel.SelectedIndex; - - int.TryParse(uoToolId.Text, out answ.UnOpToolId); - int.TryParse(uoNumCU.Text, out answ.UnOpNumCU); - answ.UnOpStatus = uoStatus.Text; - int.TryParse(uoVitaRes.Text, out answ.UnOpVitaRes); - answ.UnOpSpeed = uoSpeed.Value; - answ.UnOpLoad = uoLoad.Value; - int.TryParse(uoAccTime.Text, out answ.UnOpAccTime); - - // ritorno oggetto! - return answ; - } - } - - private void uoLoad_Scroll(object sender, ScrollEventArgs e) - { - uoLoadVal.Text = uoLoad.Value.ToString(); - } - - private void uoSpeed_Scroll(object sender, ScrollEventArgs e) - { - uoSpeedVal.Text = uoSpeed.Value.ToString(); - } - - #endregion - - #region gestione assi - - private void cbAxNum_SelectedIndexChanged(object sender, EventArgs e) - { - // aggiorna visualizzazione asse... imposto valori standard x controlli - AxMainProc.Text = "1"; - AxIsMaster.Checked = false; - AxMastId.Text = "0"; - cbAxType.SelectedIndex = 0; - AxDir.Text = "1"; - AxLoad.Value = 50; - AxPosition.Value = 50; - AxFeedAct.Value = 50; - AxFeedOver.Value = 100; - AxAccelAct.Text = "0"; - AxAccTime.Text = "0"; - AxBatt.Text = "100"; - } - - /// - /// Dati di produzione (su form) - /// - public AxisData CurrAxis - { - get - { - Random rnd = new Random(); - AxisData answ = new AxisData(); - answ.AxisSel = cbAxNum.SelectedIndex; - answ.AxisMainProc = AxMainProc.Text; - answ.AxisIsMaster = AxIsMaster.Checked; - answ.AxisMastId = AxMastId.Text; - answ.AxisType = AxisType; - answ.AxisDir = AxDir.Text; - answ.AxisLoad = AxLoad.Value; - answ.AxisPosAct = AxPosition.Value;// + rnd.Next(-2, 2); - answ.AxisPosTgt = AxPosition.Value; - answ.AxisFeedAct = AxFeedAct.Value; - answ.AxisFeedOver = AxFeedOver.Value; - answ.AxisAccel = AxAccelAct.Text; - answ.AxisAccTime = AxAccTime.Text; - answ.AxisBattery = AxBatt.Text; - - // restituisco oggetto - return answ; - } - } - - protected string AxisType - { - get - { - string answ = ""; - try - { - answ = cbAxType.SelectedItem.ToString(); ; - } - catch - { } - return answ; - } - } - - private void xLoad_Scroll(object sender, ScrollEventArgs e) - { - AxLoadValue.Text = AxLoad.Value.ToString(); - } - - private void xPosition_Scroll(object sender, ScrollEventArgs e) - { - - AxPositionValue.Text = AxPosition.Value.ToString(); - } - - private void cLoad_Scroll(object sender, ScrollEventArgs e) - { - AxFeedActValue.Text = AxFeedAct.Value.ToString(); - } - - private void cSpeed_Scroll(object sender, ScrollEventArgs e) - { - AxFeedOverValue.Text = AxFeedOver.Value.ToString(); - } - - #endregion - - - /// - /// Stringa dati monitoraggio mostrata (1 SX)... - /// - public string dataMonitor_1 - { - get - { - return lblOutMessage.Text; - } - set - { - lblOutMessage.Text = value; - } - } - /// - /// Stringa dati monitoraggio mostrata (2 centro)... - /// - public string dataMonitor_2 - { - get - { - return lblOutMessage2.Text; - } - set - { - lblOutMessage2.Text = value; - } - } - /// - /// Stringa dati monitoraggio mostrata (3 dx)... - /// - public string dataMonitor_3 - { - get - { - return lblOutMessage3.Text; - } - set - { - lblOutMessage3.Text = value; - } - } - private void SysErr_01_CheckedChanged(object sender, EventArgs e) - { - // aggiorno il vettore allarmi (da 128 byte) sulla base della selezione dei checkbox (per i primi 9...) - ultimi 3 selezionano 8 allarmi ciascuno... - BitArray bitArray = new BitArray(1024); - - bitArray[7] = SysErr_01.Checked; - bitArray[6] = SysErr_02.Checked; - bitArray[5] = SysErr_03.Checked; - bitArray[4] = PlcErr_01.Checked; - bitArray[3] = PlcErr_02.Checked; - bitArray[2] = PlcErr_03.Checked; - bitArray[1] = CncErr_01.Checked; - bitArray[0] = CncErr_02.Checked; - bitArray[15] = CncErr_03.Checked; - - // ne assegno 8 alla volta... - for (int i = 0; i < 8; i++) - { - bitArray[16 + i] = ErrRT_01.Checked; - bitArray[24 + i] = ErrRT_02.Checked; - bitArray[32 + i] = ErrRT_03.Checked; - } - byte[] newAlarm = utils.ToByteArray(bitArray); - Buffer.BlockCopy(newAlarm, 0, agObj.currGateway.AlarmFlags, 0, 32); - } - - private void MainForm_FormClosing(object sender, FormClosingEventArgs e) - { - closeAdapter(); - } - - private void closeAdapter() - { - fermaTutto(true, false); - } - /// - /// Ferma tutti i componenti adapter + update buttons - /// - /// determina se fermare il timer (gather) principale (solo se non si chiude) - /// determina se tentare di riconnettersi - private void fermaTutto(bool stopTimer, bool tryRestart) - { - agObj.stopAdapter(tryRestart); - // salvo! - savePersistLayer(defPersLayerFile); - savePersistLayer(histPersLayerFile); - - stop.Enabled = false; - dump.Enabled = false; - start.Enabled = true; - - if (stopTimer) - { - gather.Enabled = false; - agObj.tryDisconnect(); - } - } - - } } diff --git a/MTC_Adapter/SCMA/Resources/CMS/DataModel.Esa.xml b/MTC_Adapter/SCMA/Resources/CMS/DataModel.Esa.xml index a4c07d0..de43f37 100644 --- a/MTC_Adapter/SCMA/Resources/CMS/DataModel.Esa.xml +++ b/MTC_Adapter/SCMA/Resources/CMS/DataModel.Esa.xml @@ -260,7 +260,7 @@ - + diff --git a/MTC_Adapter/SCMA/SCMA.csproj b/MTC_Adapter/SCMA/SCMA.csproj index 1c5da03..8f65f92 100644 --- a/MTC_Adapter/SCMA/SCMA.csproj +++ b/MTC_Adapter/SCMA/SCMA.csproj @@ -266,6 +266,9 @@ App.config true + + Always + Always diff --git a/MTC_Adapter/SCMA/utils.cs b/MTC_Adapter/SCMA/utils.cs index 73dd522..614de94 100644 --- a/MTC_Adapter/SCMA/utils.cs +++ b/MTC_Adapter/SCMA/utils.cs @@ -4,165 +4,187 @@ using System.Xml; namespace SCMA { - public class utils : MTC.baseUtils - { - /// - /// folder archiviazione dati configurazione (DATA\CONF) - /// - public static string resxDir + public class utils : MTC.baseUtils { - get - { - return string.Format(@"{0}\{1}", Application.StartupPath, CRS("resxPath")); - } - } - /// - /// folder archiviazione dati configurazione (DATA\CONF) - /// - public static string confDir - { - get - { - return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataConfPath")); - } - } - /// - /// folder archiviazione dati storici giornalieri (DATA\DAT) - /// - public static string dataDatDir - { - get - { - return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataDatPath")); - } - } - /// - /// folder archiviazione dati (DATA) - /// - public static string dataDir - { - get - { - return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataPath")); - } - } - /// - /// File x regole di replacement USER DEFINED - /// - public static string nameRepRoleFile - { - get - { - return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("NameRepRolesList")); - } - } - /// - /// File x regole di replacement generato ad hoc x SOUR / OPC-UA - /// - public static string nameRepRoleFileSOUR - { - get - { - return nameRepRoleFile.Replace(".map", ".SOUR.map"); - } - } - /// - /// File StatusList - /// - public static string StatusListFile - { - get - { - return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("StatusListFilePath")); - } - } - /// - /// File StatusList generato ad hoc x SOUR / OPC-UA - /// - /// - public static string StatusListFileSOUR - { - get - { - return StatusListFile.Replace(".map", ".SOUR.map"); - } - } - /// - /// File CounterList - /// - public static string CounterListFile - { - get - { - return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("CounterListFilePath")); - } - } - /// - /// File CounterList generato ad hoc x SOUR / OPC-UA - /// - /// - public static string CounterListFileSOUR - { - get - { - return CounterListFile.Replace(".map", ".SOUR.map"); - } - } - /// - /// File AnalogList - /// - public static string AnalogListFile - { - get - { - return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("AnalogDataFilePath")); - } - } - /// - /// File AnalogList generato ad hoc x SOUR / OPC-UA - /// - /// - public static string AnalogListFileSOUR - { - get - { - return AnalogListFile.Replace(".map", ".SOUR.map"); - } - } - - /// - /// Metodo di sanitizzazione XML - /// - elimina commenti - /// - formatta - /// - /// - /// - public static string xmlSanitize(string xmlOrig) - { - string sXml = xmlOrig; - bool xmlSanitize = utils.CRB("xmlSanitize"); - // se richeisto faccio sanitize xml (pulizia commenti...) - if (xmlSanitize) - { - // primo step: converto stringa in dox XML - XmlDocument xmlDoc = new XmlDocument(); - xmlDoc.PreserveWhitespace = false; - xmlDoc.LoadXml(sXml); - // ora lo parso come lista eliminando i commenti - XmlNodeList list = xmlDoc.SelectNodes("//comment()"); - foreach (XmlNode node in list) + /// + /// folder archiviazione dati configurazione (DATA\CONF) + /// + public static string resxDir { - node.ParentNode.RemoveChild(node); + get + { + return string.Format(@"{0}\{1}", Application.StartupPath, CRS("resxPath")); + } + } + /// + /// folder archiviazione dati configurazione (DATA\CONF) + /// + public static string confDir + { + get + { + return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataConfPath")); + } + } + /// + /// folder archiviazione dati storici giornalieri (DATA\DAT) + /// + public static string dataDatDir + { + get + { + return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataDatPath")); + } + } + /// + /// folder archiviazione dati (DATA) + /// + public static string dataDir + { + get + { + return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataPath")); + } + } + /// + /// File x regole di replacement USER DEFINED + /// + public static string nameRepRoleFile + { + get + { + return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("NameRepRolesList")); + } + } + /// + /// File x regole di replacement generato ad hoc x SOUR / OPC-UA + /// + public static string nameRepRoleFileSOUR + { + get + { + return nameRepRoleFile.Replace(".map", ".SOUR.map"); + } + } + /// + /// File StatusList + /// + public static string StatusListFile + { + get + { + return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("StatusListFilePath")); + } + } + /// + /// File StatusList generato ad hoc x SOUR / OPC-UA + /// + /// + public static string StatusListFileSOUR + { + get + { + return StatusListFile.Replace(".map", ".SOUR.map"); + } + } + /// + /// File CounterList + /// + public static string CounterListFile + { + get + { + return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("CounterListFilePath")); + } + } + /// + /// File CounterList generato ad hoc x SOUR / OPC-UA + /// + /// + public static string CounterListFileSOUR + { + get + { + return CounterListFile.Replace(".map", ".SOUR.map"); + } + } + /// + /// File AnalogList + /// + public static string AnalogListFile + { + get + { + return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("AnalogDataFilePath")); + } + } + /// + /// File AnalogList generato ad hoc x SOUR / OPC-UA + /// + /// + public static string AnalogListFileSOUR + { + get + { + return AnalogListFile.Replace(".map", ".SOUR.map"); + } + } + + /// + /// File AnalogList + /// + public static string StringListFile + { + get + { + return string.Format(@"{0}\{1}", utils.confDir, utils.CRS("StringDataFilePath")); + } + } + /// + /// File AnalogList generato ad hoc x SOUR / OPC-UA + /// + /// + public static string StringListFileSOUR + { + get + { + return StringListFile.Replace(".map", ".SOUR.map"); + } + } + + /// + /// Metodo di sanitizzazione XML + /// - elimina commenti + /// - formatta + /// + /// + /// + public static string xmlSanitize(string xmlOrig) + { + string sXml = xmlOrig; + bool xmlSanitize = utils.CRB("xmlSanitize"); + // se richeisto faccio sanitize xml (pulizia commenti...) + if (xmlSanitize) + { + // primo step: converto stringa in dox XML + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.PreserveWhitespace = false; + xmlDoc.LoadXml(sXml); + // ora lo parso come lista eliminando i commenti + XmlNodeList list = xmlDoc.SelectNodes("//comment()"); + foreach (XmlNode node in list) + { + node.ParentNode.RemoveChild(node); + } + // fix formattazione "riscrivendo" indentazione... + StringWriter string_writer = new StringWriter(); + XmlTextWriter xml_text_writer = new XmlTextWriter(string_writer); + xml_text_writer.Formatting = Formatting.Indented; + xmlDoc.WriteTo(xml_text_writer); + sXml = string_writer.ToString(); + } + // restituisco + return sXml; } - // fix formattazione "riscrivendo" indentazione... - StringWriter string_writer = new StringWriter(); - XmlTextWriter xml_text_writer = new XmlTextWriter(string_writer); - xml_text_writer.Formatting = Formatting.Indented; - xmlDoc.WriteTo(xml_text_writer); - sXml = string_writer.ToString(); - } - // restituisco - return sXml; } - } }