From f595f6e57930d92113753e4fdef7678f959bfb56 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Thu, 2 Jul 2020 18:24:28 +0200 Subject: [PATCH] Namespace fix + resource decoding --- IOB-WIN-FILE-EXP/Constants.cs | 17 - IOB-WIN-FILE-EXP/IOB-Exporter.cs | 531 ----------------- IOB.WIN.Adapters.sln | 2 +- .../App.config | 0 .../Config/setup-MHT.json | 0 .../Config/setup-Steamware.json | 0 .../Config/setup.json | 0 .../IOB-Exporter.Designer.cs | 2 +- IOB.WIN.FileExp/IOB-Exporter.cs | 545 ++++++++++++++++++ .../IOB-Exporter.resx | 0 .../IOB.WIN.FileExp.csproj | 5 +- .../NLog.config | 0 .../NLog.xsd | 0 .../Program.cs | 2 +- .../Properties/AssemblyInfo.cs | 0 .../Properties/Resources.Designer.cs | 4 +- .../Properties/Resources.resx | 0 .../Properties/Settings.Designer.cs | 4 +- .../Properties/Settings.settings | 0 .../Resources/SteamWare.ico | Bin .../Resources/logoSteamware.png | Bin .../SteamWare.ico | Bin .../connParam.cs | 2 +- .../dataProxy.cs | 2 +- .../logs/.placeholder.txt | 0 .../memAddress.cs | 2 +- .../packages.config | 0 .../utils.cs | 2 +- 28 files changed, 558 insertions(+), 562 deletions(-) delete mode 100644 IOB-WIN-FILE-EXP/Constants.cs delete mode 100644 IOB-WIN-FILE-EXP/IOB-Exporter.cs rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/App.config (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Config/setup-MHT.json (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Config/setup-Steamware.json (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Config/setup.json (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/IOB-Exporter.Designer.cs (99%) create mode 100644 IOB.WIN.FileExp/IOB-Exporter.cs rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/IOB-Exporter.resx (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/IOB.WIN.FileExp.csproj (97%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/NLog.config (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/NLog.xsd (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Program.cs (95%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Properties/AssemblyInfo.cs (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Properties/Resources.Designer.cs (94%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Properties/Resources.resx (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Properties/Settings.Designer.cs (94%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Properties/Settings.settings (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Resources/SteamWare.ico (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/Resources/logoSteamware.png (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/SteamWare.ico (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/connParam.cs (92%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/dataProxy.cs (98%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/logs/.placeholder.txt (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/memAddress.cs (97%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/packages.config (100%) rename {IOB-WIN-FILE-EXP => IOB.WIN.FileExp}/utils.cs (99%) diff --git a/IOB-WIN-FILE-EXP/Constants.cs b/IOB-WIN-FILE-EXP/Constants.cs deleted file mode 100644 index 0a31790..0000000 --- a/IOB-WIN-FILE-EXP/Constants.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace MHT_Siemens -{ - public static class Constants - { - #region memorie configurate staticamente - - public const string RESOURCE_DIRECTORY = @"IOB.WIN.FileExp.Config."; - public const string CONFIG_PATH = RESOURCE_DIRECTORY + @"setup.json"; - - #endregion - } -} diff --git a/IOB-WIN-FILE-EXP/IOB-Exporter.cs b/IOB-WIN-FILE-EXP/IOB-Exporter.cs deleted file mode 100644 index 9e3b574..0000000 --- a/IOB-WIN-FILE-EXP/IOB-Exporter.cs +++ /dev/null @@ -1,531 +0,0 @@ -using Newtonsoft.Json; -using NLog; -using S7.Net; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.NetworkInformation; -using System.Windows.Forms; - -namespace MHT_Siemens -{ - public partial class IOB_Exporter : Form - { - #region oggetti di base - - /// - /// configurazione principale proxy - /// - protected dataProxy currDataProxy; - /// - /// Oggetto PLC da ri-utilizzare... - /// - protected Plc currPLC; - /// - /// parametri di connessione - /// - protected connParam parametri; - /// - /// oggetto logging - /// - public static Logger lg; - /// - /// oggetto uiTimer x gestione refresh UI - /// - protected Timer uiTimer = new Timer(); - /// - /// oggetto uiTimer x sampling file testuale - /// - protected Timer sampleTimer = new Timer(); - /// - /// oggetto uiTimer x verifiche - /// - protected Timer checkTimer = new Timer(); - /// - /// Contatore per decidere quando loggare la lettura del file - /// - protected int loaderLogCounter = 30; - /// - /// file delle conf attivo - /// - protected string setupFile = "setup.json"; - /// - /// Ultimi valori registrati da file - /// - protected Dictionary prevValues; - /// - /// valori correntemente letti dal file - /// - protected Dictionary currValues; - #endregion - - #region inizializazione - - public IOB_Exporter() - { - InitializeComponent(); - myInit(); - } - - private void myInit() - { - lg = LogManager.GetCurrentClassLogger(); - tsslApp.Text = $"{utils.CRS("appName")}"; - fixVisibility(); - - - prevValues = new Dictionary(); - currValues = new Dictionary(); - loadMemConf(); - loadPlc(); - startUiTimer(); - startSampleTimer(); - startCheckTimer(); - } - - private void fixVisibility() - { - bool showParams = false; -#if DEBUG - showParams = true; -#endif - lblPlcIp.Visible = showParams; - lblPlcCpu.Visible = showParams; - lblPlcRack.Visible = showParams; - lblPlcSlot.Visible = showParams; - txtIP.Visible = showParams; - txtCpuType.Visible = showParams; - txtRack.Visible = showParams; - txtSlot.Visible = showParams; - } - - private void startUiTimer() - { - uiTimer.Interval = 20; - uiTimer.Tick += UiTimer_Tick; - uiTimer.Start(); - } - private void startSampleTimer() - { - int sampleTimerMs = utils.CRI("sampleTimerMs"); - sampleTimerMs = sampleTimerMs < 100 ? 100 : sampleTimerMs; - sampleTimer.Interval = sampleTimerMs; - sampleTimer.Tick += SampleTimer_Tick; - sampleTimer.Start(); - } - private void startCheckTimer() - { - // ogni 5 minuti watchdog - checkTimer.Interval = sampleTimer.Interval * 60; - checkTimer.Tick += CheckTimer_Tick; - checkTimer.Start(); - } - /// - /// init PLC - /// - private void loadPlc() - { - lgInfo("Refreshing connection..."); - parametri = currDataProxy.confPLC; - // ora tento avvio PLC... SE PING OK... - if (testPing() == IPStatus.Success) - { - try - { - lgInfo($"PLC parameters: CPU {parametri.tipoCpu} | IP: {parametri.ipAdrr} | R/S: {parametri.rack}/{parametri.slot}"); - currPLC = new Plc(parametri.tipoCpu, parametri.ipAdrr, parametri.rack, parametri.slot); - currPLC.Open(); - if (currPLC.IsConnected) - { - lgInfo("PLC Connected!"); - } - else - { - lgInfo("Connection error!"); - } - } - catch (Exception exc) - { - lgError(exc, "Errore in INIT PLC"); - } - } - } - /// - /// Init conf memoria - /// - protected void loadMemConf() - { - lgInfo("Starting loadMemConf"); - if (File.Exists(setupFile)) - { - lgInfo("Setup File found!"); - try - { - StreamReader reader = new StreamReader(setupFile); - string jsonData = reader.ReadToEnd(); - if (!string.IsNullOrEmpty(jsonData)) - { - currDataProxy = JsonConvert.DeserializeObject(jsonData); - lgInfo("Completed data deserialization"); - } - reader.Close(); - } - catch - { - currDataProxy = null; - } - } - // se non esistesse creo un nuovo file default - if (currDataProxy == null) - { - lgInfo($"File NOT found: {setupFile} - creating new"); - dataConf newParam = new dataConf() - { - Column = "Valore assoluto", - Index = 8, - MemConf = "DB701.DBD142", - DataType = "real" - }; - Dictionary paramList = new Dictionary(); - paramList.Add(newParam.MemConf, newParam); - // creo nuovo obj... - currDataProxy = new dataProxy() - { - csvFilePath = @"c:\zz\prova1.csv", - csvFileEnableWrite = true, - csvSeparator = ';', - confPLC = new connParam() - { - ipAdrr = "192.168.0.102", - tipoCpu = CpuType.S71500, - rack = 0, - slot = 1 - }, - parametersList = paramList - }; - // salvo! - string json = JsonConvert.SerializeObject(currDataProxy); - - //write string to file - File.WriteAllText(setupFile, json); - lgInfo($"Setup File saved: {setupFile}"); - } - - // adesso valorizzo tutti i parametri - txtCsvPath.Text = currDataProxy.csvFilePath; - txtIP.Text = currDataProxy.confPLC.ipAdrr; - txtCpuType.Text = $"{currDataProxy.confPLC.tipoCpu}"; - txtRack.Text = $"{currDataProxy.confPLC.rack}"; - txtSlot.Text = $"{currDataProxy.confPLC.slot}"; - } - -#endregion - - -#region metodi ricorrenti - - - private void UiTimer_Tick(object sender, EventArgs e) - { - toolStripProgressBar1.ProgressBar.Value++; - if (toolStripProgressBar1.ProgressBar.Value >= toolStripProgressBar1.ProgressBar.Maximum) - { - toolStripProgressBar1.ProgressBar.Value = 0; - } - } - private void CheckTimer_Tick(object sender, EventArgs e) - { - // loggo! - lgInfo("Program Alive control..."); - //verifico PLC - if (currPLC.IsConnected) - { - lgInfo("PLC Connected!"); - } - else - { - lgInfo("Connection error!"); - } - // loggo che COMUNQUE forzo scrittura su PLC! - lgInfo("Forced PLC write"); - // invio a PLC - saveToPLC(); - } - private void SampleTimer_Tick(object sender, EventArgs e) - { - // rileggo il file - reloadFromFile(); - - // verifico valore confrontando con i precedenti... - if (dataChanged()) - { - // loggo! - lgInfo("Data Change on File"); - - // invio a PLC - saveToPLC(); - } - } - - - -#endregion - -#region helper methods - - /// - /// salva i dati sul PLC - /// - private void saveToPLC() - { - // invio TUTTI i dati al PLC secondo configurazione... - if (testCncConn()) - { - // decodifico memoria... - memAddress memoria = null; - double valore = -999; - foreach (var item in currValues) - { - memoria = new memAddress(item.Key); - double.TryParse(item.Value, out valore); - // hard coded REAL!!! - byte[] DB_Byte = new byte[4]; - S7.Net.Types.Double.ToByteArray(valore).CopyTo(DB_Byte, 0); - currPLC.WriteBytes(DataType.DataBlock, memoria.DbNum, memoria.indiceMem, DB_Byte); - // loggo invio al PLC! - lgInfo($"Value sent to PLC: {item.Key} | {valore}"); - } - } - } - /// - /// Effettua comparazioen dati vecchi/nuovi - /// - /// - private bool dataChanged() - { - bool answ = false; - // se i 2 vettori sono diversi - if (currValues.Count != prevValues.Count) - { - resetPrevVal(); - return true; - } - // verifico ogni singolo valore... - foreach (var item in currValues) - { - // cerco... - if (prevValues.ContainsKey(item.Key)) - { - // verifico se diversi --> trovato cambio! - answ = currValues[item.Key] != prevValues[item.Key]; - } - else - { - answ = true; - } - // se ho cambiamenti --> esco! - if (answ) - { - // salvo ed esco subito - resetPrevVal(); - return true; - } - } - return answ; - } - /// - /// Refresh oggetto valori precedenti - /// - private void resetPrevVal() - { - // salvo ed esco subito - prevValues = new Dictionary(); - foreach (var item in currValues) - { - prevValues.Add(item.Key, item.Value); - } - } - - /// - /// Effettua rilettura da file - /// - protected void reloadFromFile() - { - if (File.Exists(currDataProxy.csvFilePath)) - { - // leggo... - using (StreamReader sr = new StreamReader(currDataProxy.csvFilePath)) - { - string currentLine; - //se ha header salto la prima riga... - if (currDataProxy.csvFileEnableWrite) - { - if ((currentLine = sr.ReadLine()) == null) - { - // vuoto: loggo ed esco! - lgError("Errore intestazione vuota!"); - return; - } - } - // ora leggo la riga successiva... SE !=null - - if ((currentLine = sr.ReadLine()) != null) - { - // splitto riga e cerco valore... - var valori = currentLine.Split(currDataProxy.csvSeparator); - // cerco parametri! - try - { - foreach (var item in currDataProxy.parametersList) - { - // cerco! - if (valori.Count() >= item.Value.Index) - { - upsertValue(item.Key, valori[item.Value.Index]); - } - } - } - catch (Exception exc) - { - lgError($"Eccezione i decodifica riga misura: {Environment.NewLine}{exc}"); - } - loaderLogCounter--; - if (loaderLogCounter < 0) - { - // loggo che ho letto file e che contiene una certa riga... - lgInfo($"Verifica periodica riga file, contenuto:{Environment.NewLine}{currentLine}"); - loaderLogCounter = 30; - } - } - else - { - // vuoto: loggo ed esco! - lgError("Errore riga vuota!"); - return; - } - loaderLogCounter--; - if (loaderLogCounter < 0) - { - // loggo che ho letto file e che contiene una certa riga... - lgInfo("Check periodico file presente OK"); - loaderLogCounter = 30; - } - } - } - else - { - lgError($"Errore! file non trovato: {currDataProxy.csvFilePath}"); - } - } - - protected void upsertValue(string key, string value) - { - // cerco se ci sia... aggiorno! - if (currValues.ContainsKey(key)) - { - currValues[key] = value; - } - // altrimenti aggiungo - else - { - currValues.Add(key, value); - } - - } - - /// - /// test ping all'indirizzo impostato nei parametri - /// - /// - private IPStatus testPing() - { - IPStatus answ = IPStatus.Unknown; ; - IPAddress address; - PingReply reply; - Ping pingSender = new Ping(); - address = IPAddress.Loopback; - IPAddress.TryParse(parametri.ipAdrr, out address); - reply = pingSender.Send(address, 100); - answ = reply.Status; - return answ; - } - /// - /// Test connessione CNC - /// - /// - private bool testCncConn() - { - bool answ = false; - IPStatus pingStatus = testPing(); - // se passa il ping faccio il resto... - if (pingStatus != IPStatus.Success) - { - lgError("Errore ping"); - } - else - { - if (!currPLC.IsConnected) - { - currPLC.Open(); - } - - if (!currPLC.IsConnected) - { - lgError("Errore connessione"); - } - else - { - answ = true; - } - } - return answ; - } - private void appenRTLog(string contenuto) - { - // se troppe righe trimmo... - string fullLog = limitLine2show($"{contenuto}{Environment.NewLine}{txtLog.Text}"); - // update - txtLog.Text = fullLog; - } - /// - /// Effettua un trim della stringa al numero max di linee da mostrare a video - /// - /// - /// - public string limitLine2show(string newString) - { - if (!string.IsNullOrEmpty(newString)) - { - // se num righe superiore a limite trimmo... - if (newString.Split('\n').Length > 50) - { - //int idx = newString.LastIndexOf('\r'); - int idx = newString.LastIndexOf(Environment.NewLine); - newString = newString.Substring(0, idx); - } - } - return newString; - } - protected void lgInfo(string contenuto) - { - lg.Info(contenuto); - appenRTLog(contenuto); - } - - - protected void lgError(string contenuto) - { - lg.Error(contenuto); - appenRTLog(contenuto); - } - protected void lgError(Exception exc, string contenuto) - { - lg.Error(exc, contenuto); - appenRTLog(contenuto); - } - -#endregion - } -} diff --git a/IOB.WIN.Adapters.sln b/IOB.WIN.Adapters.sln index 3ddfcfc..d5739e5 100644 --- a/IOB.WIN.Adapters.sln +++ b/IOB.WIN.Adapters.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30002.166 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IOB.WIN.FileExp", "IOB-WIN-FILE-EXP\IOB.WIN.FileExp.csproj", "{958CB938-3860-49D3-8B16-15ED0E9FE2FB}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IOB.WIN.FileExp", "IOB.WIN.FileExp\IOB.WIN.FileExp.csproj", "{958CB938-3860-49D3-8B16-15ED0E9FE2FB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/IOB-WIN-FILE-EXP/App.config b/IOB.WIN.FileExp/App.config similarity index 100% rename from IOB-WIN-FILE-EXP/App.config rename to IOB.WIN.FileExp/App.config diff --git a/IOB-WIN-FILE-EXP/Config/setup-MHT.json b/IOB.WIN.FileExp/Config/setup-MHT.json similarity index 100% rename from IOB-WIN-FILE-EXP/Config/setup-MHT.json rename to IOB.WIN.FileExp/Config/setup-MHT.json diff --git a/IOB-WIN-FILE-EXP/Config/setup-Steamware.json b/IOB.WIN.FileExp/Config/setup-Steamware.json similarity index 100% rename from IOB-WIN-FILE-EXP/Config/setup-Steamware.json rename to IOB.WIN.FileExp/Config/setup-Steamware.json diff --git a/IOB-WIN-FILE-EXP/Config/setup.json b/IOB.WIN.FileExp/Config/setup.json similarity index 100% rename from IOB-WIN-FILE-EXP/Config/setup.json rename to IOB.WIN.FileExp/Config/setup.json diff --git a/IOB-WIN-FILE-EXP/IOB-Exporter.Designer.cs b/IOB.WIN.FileExp/IOB-Exporter.Designer.cs similarity index 99% rename from IOB-WIN-FILE-EXP/IOB-Exporter.Designer.cs rename to IOB.WIN.FileExp/IOB-Exporter.Designer.cs index 2468ee2..b01dc17 100644 --- a/IOB-WIN-FILE-EXP/IOB-Exporter.Designer.cs +++ b/IOB.WIN.FileExp/IOB-Exporter.Designer.cs @@ -1,4 +1,4 @@ -namespace MHT_Siemens +namespace IOB.WIN.FileExp { partial class IOB_Exporter { diff --git a/IOB.WIN.FileExp/IOB-Exporter.cs b/IOB.WIN.FileExp/IOB-Exporter.cs new file mode 100644 index 0000000..2c32768 --- /dev/null +++ b/IOB.WIN.FileExp/IOB-Exporter.cs @@ -0,0 +1,545 @@ +using Newtonsoft.Json; +using NLog; +using S7.Net; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Reflection; +using System.Windows.Forms; + +namespace IOB.WIN.FileExp +{ + public partial class IOB_Exporter : Form + { + #region oggetti di base + + /// + /// configurazione principale proxy + /// + protected dataProxy currDataProxy; + /// + /// Oggetto PLC da ri-utilizzare... + /// + protected Plc currPLC; + /// + /// parametri di connessione + /// + protected connParam parametri; + /// + /// oggetto logging + /// + public static Logger lg; + /// + /// oggetto uiTimer x gestione refresh UI + /// + protected Timer uiTimer = new Timer(); + /// + /// oggetto uiTimer x sampling file testuale + /// + protected Timer sampleTimer = new Timer(); + /// + /// oggetto uiTimer x verifiche + /// + protected Timer checkTimer = new Timer(); + /// + /// Contatore per decidere quando loggare la lettura del file + /// + protected int loaderLogCounter = 30; + /// + /// Directory base delle risorse (virtuale) + /// + protected const string RESOURCE_DIRECTORY = @"IOB.WIN.FileExp.Config."; + /// + /// file delle conf attivo + /// + protected const string setupFile = RESOURCE_DIRECTORY + @"setup.json"; + /// + /// Ultimi valori registrati da file + /// + protected Dictionary prevValues; + /// + /// valori correntemente letti dal file + /// + protected Dictionary currValues; + #endregion + + #region inizializazione + + public IOB_Exporter() + { + InitializeComponent(); + myInit(); + } + + private void myInit() + { + lg = LogManager.GetCurrentClassLogger(); + tsslApp.Text = $"{utils.CRS("appName")}"; + fixVisibility(); + prevValues = new Dictionary(); + currValues = new Dictionary(); + loadMemConf(); +#if false + loadPlc(); + startUiTimer(); + startSampleTimer(); + startCheckTimer(); +#endif + } + + private void fixVisibility() + { + bool showParams = false; +#if DEBUG + showParams = true; +#endif + lblPlcIp.Visible = showParams; + lblPlcCpu.Visible = showParams; + lblPlcRack.Visible = showParams; + lblPlcSlot.Visible = showParams; + txtIP.Visible = showParams; + txtCpuType.Visible = showParams; + txtRack.Visible = showParams; + txtSlot.Visible = showParams; + } + + private void startUiTimer() + { + uiTimer.Interval = 20; + uiTimer.Tick += UiTimer_Tick; + uiTimer.Start(); + } + private void startSampleTimer() + { + int sampleTimerMs = utils.CRI("sampleTimerMs"); + sampleTimerMs = sampleTimerMs < 100 ? 100 : sampleTimerMs; + sampleTimer.Interval = sampleTimerMs; + sampleTimer.Tick += SampleTimer_Tick; + sampleTimer.Start(); + } + private void startCheckTimer() + { + // ogni 5 minuti watchdog + checkTimer.Interval = sampleTimer.Interval * 60; + checkTimer.Tick += CheckTimer_Tick; + checkTimer.Start(); + } + /// + /// init PLC + /// + private void loadPlc() + { + lgInfo("Refreshing connection..."); + parametri = currDataProxy.confPLC; + // ora tento avvio PLC... SE PING OK... + if (testPing() == IPStatus.Success) + { + try + { + lgInfo($"PLC parameters: CPU {parametri.tipoCpu} | IP: {parametri.ipAdrr} | R/S: {parametri.rack}/{parametri.slot}"); + currPLC = new Plc(parametri.tipoCpu, parametri.ipAdrr, parametri.rack, parametri.slot); + currPLC.Open(); + if (currPLC.IsConnected) + { + lgInfo("PLC Connected!"); + } + else + { + lgInfo("Connection error!"); + } + } + catch (Exception exc) + { + lgError(exc, "Errore in INIT PLC"); + } + } + } + /// + /// Init conf memoria + /// + protected void loadMemConf() + { + lgInfo("Starting loadMemConf"); + + // recupero conf da resources + Assembly myAssembly = Assembly.GetExecutingAssembly(); + //var resName = myAssembly.GetManifestResourceNames(); + //var resInfo = myAssembly.GetManifestResourceInfo(setupFile); + using (Stream confStream = myAssembly.GetManifestResourceStream(setupFile)) + { + using (StreamReader fileReader = new StreamReader(confStream)) + { + string jsonData = fileReader.ReadToEnd(); + if (!string.IsNullOrEmpty(jsonData)) + { + lgInfo("Setup File found!"); + try + { + currDataProxy = JsonConvert.DeserializeObject(jsonData); + // verifica num max variabili (MAX 8...) + if (currDataProxy.parametersList.Count > 8) + { + lgInfo("Invalid config, reduce params list"); + while (currDataProxy.parametersList.Count > 8) + { + currDataProxy.parametersList.Remove(currDataProxy.parametersList.LastOrDefault().Key); + } + } + + lgInfo("Completed data deserialization"); + } + catch + { + currDataProxy = null; + } + } + } + } + + if (currDataProxy == null) + { + lgInfo($"File NOT found: {setupFile} - creating new"); + dataConf newParam = new dataConf() + { + Column = "Valore assoluto", + Index = 8, + MemConf = "DB701.DBD142", + DataType = "real" + }; + Dictionary paramList = new Dictionary(); + paramList.Add(newParam.MemConf, newParam); + // creo nuovo obj... + currDataProxy = new dataProxy() + { + TriggerMemConf = "DB700.DBB0", + confPLC = new connParam() + { + ipAdrr = "192.168.0.102", + tipoCpu = CpuType.S71500, + rack = 0, + slot = 1 + }, + parametersList = paramList + }; + } + + // adesso valorizzo tutti i parametri + txtCsvPath.Text = utils.CRS("csvFilePath"); + txtIP.Text = currDataProxy.confPLC.ipAdrr; + txtCpuType.Text = $"{currDataProxy.confPLC.tipoCpu}"; + txtRack.Text = $"{currDataProxy.confPLC.rack}"; + txtSlot.Text = $"{currDataProxy.confPLC.slot}"; + } + + #endregion + + #region metodi ricorrenti + + + private void UiTimer_Tick(object sender, EventArgs e) + { + toolStripProgressBar1.ProgressBar.Value++; + if (toolStripProgressBar1.ProgressBar.Value >= toolStripProgressBar1.ProgressBar.Maximum) + { + toolStripProgressBar1.ProgressBar.Value = 0; + } + } + private void CheckTimer_Tick(object sender, EventArgs e) + { + // loggo! + lgInfo("Program Alive control..."); + //verifico PLC + if (currPLC.IsConnected) + { + lgInfo("PLC Connected!"); + } + else + { + lgInfo("Connection error!"); + } + // loggo che COMUNQUE forzo scrittura su PLC! + lgInfo("Forced PLC write"); + // invio a PLC + saveToPLC(); + } + private void SampleTimer_Tick(object sender, EventArgs e) + { + // rileggo il file + reloadFromFile(); + + // verifico valore confrontando con i precedenti... + if (dataChanged()) + { + // loggo! + lgInfo("Data Change on File"); + + // invio a PLC + saveToPLC(); + } + } + + + + #endregion + + #region helper methods + + /// + /// salva i dati sul PLC + /// + private void saveToPLC() + { + // invio TUTTI i dati al PLC secondo configurazione... + if (testCncConn()) + { + // decodifico memoria... + memAddress memoria = null; + double valore = -999; + foreach (var item in currValues) + { + memoria = new memAddress(item.Key); + double.TryParse(item.Value, out valore); + // hard coded REAL!!! + byte[] DB_Byte = new byte[4]; + S7.Net.Types.Double.ToByteArray(valore).CopyTo(DB_Byte, 0); + currPLC.WriteBytes(DataType.DataBlock, memoria.DbNum, memoria.indiceMem, DB_Byte); + // loggo invio al PLC! + lgInfo($"Value sent to PLC: {item.Key} | {valore}"); + } + } + } + /// + /// Effettua comparazioen dati vecchi/nuovi + /// + /// + private bool dataChanged() + { + bool answ = false; + // se i 2 vettori sono diversi + if (currValues.Count != prevValues.Count) + { + resetPrevVal(); + return true; + } + // verifico ogni singolo valore... + foreach (var item in currValues) + { + // cerco... + if (prevValues.ContainsKey(item.Key)) + { + // verifico se diversi --> trovato cambio! + answ = currValues[item.Key] != prevValues[item.Key]; + } + else + { + answ = true; + } + // se ho cambiamenti --> esco! + if (answ) + { + // salvo ed esco subito + resetPrevVal(); + return true; + } + } + return answ; + } + /// + /// Refresh oggetto valori precedenti + /// + private void resetPrevVal() + { + // salvo ed esco subito + prevValues = new Dictionary(); + foreach (var item in currValues) + { + prevValues.Add(item.Key, item.Value); + } + } + + /// + /// Effettua rilettura da file + /// + protected void reloadFromFile() + { +#if false + if (File.Exists(currDataProxy.csvFilePath)) + { + // leggo... + using (StreamReader sr = new StreamReader(currDataProxy.csvFilePath)) + { + string currentLine; + //se ha header salto la prima riga... + if (currDataProxy.csvFileEnableWrite) + { + if ((currentLine = sr.ReadLine()) == null) + { + // vuoto: loggo ed esco! + lgError("Errore intestazione vuota!"); + return; + } + } + // ora leggo la riga successiva... SE !=null + + if ((currentLine = sr.ReadLine()) != null) + { + // splitto riga e cerco valore... + var valori = currentLine.Split(currDataProxy.csvSeparator); + // cerco parametri! + try + { + foreach (var item in currDataProxy.parametersList) + { + // cerco! + if (valori.Count() >= item.Value.Index) + { + upsertValue(item.Key, valori[item.Value.Index]); + } + } + } + catch (Exception exc) + { + lgError($"Eccezione i decodifica riga misura: {Environment.NewLine}{exc}"); + } + loaderLogCounter--; + if (loaderLogCounter < 0) + { + // loggo che ho letto file e che contiene una certa riga... + lgInfo($"Verifica periodica riga file, contenuto:{Environment.NewLine}{currentLine}"); + loaderLogCounter = 30; + } + } + else + { + // vuoto: loggo ed esco! + lgError("Errore riga vuota!"); + return; + } + loaderLogCounter--; + if (loaderLogCounter < 0) + { + // loggo che ho letto file e che contiene una certa riga... + lgInfo("Check periodico file presente OK"); + loaderLogCounter = 30; + } + } + } + else + { + lgError($"Errore! file non trovato: {currDataProxy.csvFilePath}"); + } +#endif + } + + protected void upsertValue(string key, string value) + { + // cerco se ci sia... aggiorno! + if (currValues.ContainsKey(key)) + { + currValues[key] = value; + } + // altrimenti aggiungo + else + { + currValues.Add(key, value); + } + + } + + /// + /// test ping all'indirizzo impostato nei parametri + /// + /// + private IPStatus testPing() + { + IPStatus answ = IPStatus.Unknown; ; + IPAddress address; + PingReply reply; + Ping pingSender = new Ping(); + address = IPAddress.Loopback; + IPAddress.TryParse(parametri.ipAdrr, out address); + reply = pingSender.Send(address, 100); + answ = reply.Status; + return answ; + } + /// + /// Test connessione CNC + /// + /// + private bool testCncConn() + { + bool answ = false; + IPStatus pingStatus = testPing(); + // se passa il ping faccio il resto... + if (pingStatus != IPStatus.Success) + { + lgError("Errore ping"); + } + else + { + if (!currPLC.IsConnected) + { + currPLC.Open(); + } + + if (!currPLC.IsConnected) + { + lgError("Errore connessione"); + } + else + { + answ = true; + } + } + return answ; + } + private void appenRTLog(string contenuto) + { + // se troppe righe trimmo... + string fullLog = limitLine2show($"{contenuto}{Environment.NewLine}{txtLog.Text}"); + // update + txtLog.Text = fullLog; + } + /// + /// Effettua un trim della stringa al numero max di linee da mostrare a video + /// + /// + /// + public string limitLine2show(string newString) + { + if (!string.IsNullOrEmpty(newString)) + { + // se num righe superiore a limite trimmo... + if (newString.Split('\n').Length > 50) + { + //int idx = newString.LastIndexOf('\r'); + int idx = newString.LastIndexOf(Environment.NewLine); + newString = newString.Substring(0, idx); + } + } + return newString; + } + protected void lgInfo(string contenuto) + { + lg.Info(contenuto); + appenRTLog(contenuto); + } + + + protected void lgError(string contenuto) + { + lg.Error(contenuto); + appenRTLog(contenuto); + } + protected void lgError(Exception exc, string contenuto) + { + lg.Error(exc, contenuto); + appenRTLog(contenuto); + } + + #endregion + } +} diff --git a/IOB-WIN-FILE-EXP/IOB-Exporter.resx b/IOB.WIN.FileExp/IOB-Exporter.resx similarity index 100% rename from IOB-WIN-FILE-EXP/IOB-Exporter.resx rename to IOB.WIN.FileExp/IOB-Exporter.resx diff --git a/IOB-WIN-FILE-EXP/IOB.WIN.FileExp.csproj b/IOB.WIN.FileExp/IOB.WIN.FileExp.csproj similarity index 97% rename from IOB-WIN-FILE-EXP/IOB.WIN.FileExp.csproj rename to IOB.WIN.FileExp/IOB.WIN.FileExp.csproj index 534ce29..f886da2 100644 --- a/IOB-WIN-FILE-EXP/IOB.WIN.FileExp.csproj +++ b/IOB.WIN.FileExp/IOB.WIN.FileExp.csproj @@ -6,8 +6,8 @@ AnyCPU {958CB938-3860-49D3-8B16-15ED0E9FE2FB} WinExe - MHT_Siemens - MHT-Siemens + IOB.WIN.FileExp + IOB.WIN.FileExp v4.0 512 true @@ -65,7 +65,6 @@ - diff --git a/IOB-WIN-FILE-EXP/NLog.config b/IOB.WIN.FileExp/NLog.config similarity index 100% rename from IOB-WIN-FILE-EXP/NLog.config rename to IOB.WIN.FileExp/NLog.config diff --git a/IOB-WIN-FILE-EXP/NLog.xsd b/IOB.WIN.FileExp/NLog.xsd similarity index 100% rename from IOB-WIN-FILE-EXP/NLog.xsd rename to IOB.WIN.FileExp/NLog.xsd diff --git a/IOB-WIN-FILE-EXP/Program.cs b/IOB.WIN.FileExp/Program.cs similarity index 95% rename from IOB-WIN-FILE-EXP/Program.cs rename to IOB.WIN.FileExp/Program.cs index 85fd6c7..cd7d103 100644 --- a/IOB-WIN-FILE-EXP/Program.cs +++ b/IOB.WIN.FileExp/Program.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; -namespace MHT_Siemens +namespace IOB.WIN.FileExp { static class Program { diff --git a/IOB-WIN-FILE-EXP/Properties/AssemblyInfo.cs b/IOB.WIN.FileExp/Properties/AssemblyInfo.cs similarity index 100% rename from IOB-WIN-FILE-EXP/Properties/AssemblyInfo.cs rename to IOB.WIN.FileExp/Properties/AssemblyInfo.cs diff --git a/IOB-WIN-FILE-EXP/Properties/Resources.Designer.cs b/IOB.WIN.FileExp/Properties/Resources.Designer.cs similarity index 94% rename from IOB-WIN-FILE-EXP/Properties/Resources.Designer.cs rename to IOB.WIN.FileExp/Properties/Resources.Designer.cs index 8b8ee52..8f9409b 100644 --- a/IOB-WIN-FILE-EXP/Properties/Resources.Designer.cs +++ b/IOB.WIN.FileExp/Properties/Resources.Designer.cs @@ -8,7 +8,7 @@ // //------------------------------------------------------------------------------ -namespace MHT_Siemens.Properties { +namespace IOB.WIN.FileExp.Properties { using System; @@ -39,7 +39,7 @@ namespace MHT_Siemens.Properties { internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MHT_Siemens.Properties.Resources", typeof(Resources).Assembly); + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("IOB.WIN.FileExp.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; diff --git a/IOB-WIN-FILE-EXP/Properties/Resources.resx b/IOB.WIN.FileExp/Properties/Resources.resx similarity index 100% rename from IOB-WIN-FILE-EXP/Properties/Resources.resx rename to IOB.WIN.FileExp/Properties/Resources.resx diff --git a/IOB-WIN-FILE-EXP/Properties/Settings.Designer.cs b/IOB.WIN.FileExp/Properties/Settings.Designer.cs similarity index 94% rename from IOB-WIN-FILE-EXP/Properties/Settings.Designer.cs rename to IOB.WIN.FileExp/Properties/Settings.Designer.cs index 901bdbc..29a78fd 100644 --- a/IOB-WIN-FILE-EXP/Properties/Settings.Designer.cs +++ b/IOB.WIN.FileExp/Properties/Settings.Designer.cs @@ -8,11 +8,11 @@ // //------------------------------------------------------------------------------ -namespace MHT_Siemens.Properties { +namespace IOB.WIN.FileExp.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.5.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.6.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/IOB-WIN-FILE-EXP/Properties/Settings.settings b/IOB.WIN.FileExp/Properties/Settings.settings similarity index 100% rename from IOB-WIN-FILE-EXP/Properties/Settings.settings rename to IOB.WIN.FileExp/Properties/Settings.settings diff --git a/IOB-WIN-FILE-EXP/Resources/SteamWare.ico b/IOB.WIN.FileExp/Resources/SteamWare.ico similarity index 100% rename from IOB-WIN-FILE-EXP/Resources/SteamWare.ico rename to IOB.WIN.FileExp/Resources/SteamWare.ico diff --git a/IOB-WIN-FILE-EXP/Resources/logoSteamware.png b/IOB.WIN.FileExp/Resources/logoSteamware.png similarity index 100% rename from IOB-WIN-FILE-EXP/Resources/logoSteamware.png rename to IOB.WIN.FileExp/Resources/logoSteamware.png diff --git a/IOB-WIN-FILE-EXP/SteamWare.ico b/IOB.WIN.FileExp/SteamWare.ico similarity index 100% rename from IOB-WIN-FILE-EXP/SteamWare.ico rename to IOB.WIN.FileExp/SteamWare.ico diff --git a/IOB-WIN-FILE-EXP/connParam.cs b/IOB.WIN.FileExp/connParam.cs similarity index 92% rename from IOB-WIN-FILE-EXP/connParam.cs rename to IOB.WIN.FileExp/connParam.cs index a96c21e..e351b0c 100644 --- a/IOB-WIN-FILE-EXP/connParam.cs +++ b/IOB.WIN.FileExp/connParam.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MHT_Siemens +namespace IOB.WIN.FileExp { public class connParam { diff --git a/IOB-WIN-FILE-EXP/dataProxy.cs b/IOB.WIN.FileExp/dataProxy.cs similarity index 98% rename from IOB-WIN-FILE-EXP/dataProxy.cs rename to IOB.WIN.FileExp/dataProxy.cs index 8db5694..13d0806 100644 --- a/IOB-WIN-FILE-EXP/dataProxy.cs +++ b/IOB.WIN.FileExp/dataProxy.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MHT_Siemens +namespace IOB.WIN.FileExp { /// /// classe costruzione dataproxy diff --git a/IOB-WIN-FILE-EXP/logs/.placeholder.txt b/IOB.WIN.FileExp/logs/.placeholder.txt similarity index 100% rename from IOB-WIN-FILE-EXP/logs/.placeholder.txt rename to IOB.WIN.FileExp/logs/.placeholder.txt diff --git a/IOB-WIN-FILE-EXP/memAddress.cs b/IOB.WIN.FileExp/memAddress.cs similarity index 97% rename from IOB-WIN-FILE-EXP/memAddress.cs rename to IOB.WIN.FileExp/memAddress.cs index ebd2988..64b0294 100644 --- a/IOB-WIN-FILE-EXP/memAddress.cs +++ b/IOB.WIN.FileExp/memAddress.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MHT_Siemens +namespace IOB.WIN.FileExp { public class memAddress { diff --git a/IOB-WIN-FILE-EXP/packages.config b/IOB.WIN.FileExp/packages.config similarity index 100% rename from IOB-WIN-FILE-EXP/packages.config rename to IOB.WIN.FileExp/packages.config diff --git a/IOB-WIN-FILE-EXP/utils.cs b/IOB.WIN.FileExp/utils.cs similarity index 99% rename from IOB-WIN-FILE-EXP/utils.cs rename to IOB.WIN.FileExp/utils.cs index 8b7aa4e..9aa0e96 100644 --- a/IOB-WIN-FILE-EXP/utils.cs +++ b/IOB.WIN.FileExp/utils.cs @@ -8,7 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace MHT_Siemens +namespace IOB.WIN.FileExp { public class utils {