361 lines
12 KiB
C#
361 lines
12 KiB
C#
using IOB_UT;
|
|
using MapoSDK;
|
|
using Newtonsoft.Json;
|
|
using OpenQA.Selenium;
|
|
using OpenQA.Selenium.Chrome;
|
|
using OpenQA.Selenium.Firefox;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net.NetworkInformation;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace IOB_WIN
|
|
{
|
|
/// <summary>
|
|
/// Generica classe per implementare IOB basato su scambio FILE
|
|
/// </summary>
|
|
public class IobFile : IobGeneric, IDisposable
|
|
{
|
|
/* --------------------------------------------------------------------------------
|
|
* Controlli dotati di GENERICA funzionalità scambio info tramite file
|
|
*
|
|
* -------------------------------------------------------------------------------- */
|
|
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// Cartella di base per interscambio
|
|
/// </summary>
|
|
protected string BaseDir = @"C:\Steamware";
|
|
|
|
/// <summary>
|
|
/// Array di configurazione degli oggetti da cercare x decodifica e recupero info
|
|
/// </summary>
|
|
protected Dictionary<string, string> dataLocatorLUT;
|
|
|
|
/// <summary>
|
|
/// Vettore della frequenza di ogni status trovato... invio ogni 100 rilevazioni (modulo 100, resto == 1)
|
|
/// </summary>
|
|
protected Dictionary<string, int> freqUnknStatus = new Dictionary<string, int>();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Estende l'init della classe base...
|
|
/// </summary>
|
|
/// <param name="caller"></param>
|
|
/// <param name="adpConf"></param>
|
|
public IobFile(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
|
|
{
|
|
lgInfo("INIT IobFile");
|
|
//reloadAdapterConf();
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Processo stati unknown...
|
|
/// </summary>
|
|
/// <param name="cKey"></param>
|
|
private void processUnknStatus(string cKey)
|
|
{
|
|
// cerco se avevo già una key nella dictionary...
|
|
if (freqUnknStatus.ContainsKey(cKey))
|
|
{
|
|
freqUnknStatus[cKey]++;
|
|
// se è 1 ogni 100 (%100, resto ==1) --> loggo...
|
|
if (freqUnknStatus[cKey] % 100 == 1)
|
|
{
|
|
lgInfo($"Errore in decodifica status: MAPPING non trovato per {cKey} | freq: {freqUnknStatus[cKey]}");
|
|
// accodo come invio di tipo FLOG...
|
|
string sVal = string.Format("[UnknStatus] {0}, freq: {1}", cKey, freqUnknStatus);
|
|
// chiamo accodamento...
|
|
accodaFLog(sVal, qEncodeFLog("UnknStatus", sVal));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// creo chiave con freq = 1
|
|
freqUnknStatus.Add(cKey, 1);
|
|
// log iniziale
|
|
lgInfo($"Errore in decodifica status: MAPPING non trovato per {cKey}");
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Internal Methods
|
|
|
|
/// <summary>
|
|
/// Pulizia preliminare folder comunicazione
|
|
/// </summary>
|
|
internal virtual void cleanupFolder()
|
|
{
|
|
//
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ricarica conf adapter...
|
|
/// </summary>
|
|
internal virtual void reloadAdapterConf()
|
|
{
|
|
#if false
|
|
// init obj display
|
|
newDisplayData currDispData = new newDisplayData();
|
|
lgInfo("BEGIN reloadAdapterConf");
|
|
// inizializzo LUT decodifica
|
|
string jsonConf = getOptPar("LUT_CONF");
|
|
if (!string.IsNullOrEmpty(jsonConf))
|
|
{
|
|
string jsonFullPath = $"{Application.StartupPath}/DATA/CONF/{jsonConf}";
|
|
lgInfo($"Apertura file {jsonFullPath}");
|
|
StreamReader reader = new StreamReader(jsonFullPath);
|
|
string jsonData = reader.ReadToEnd();
|
|
if (!string.IsNullOrEmpty(jsonData))
|
|
{
|
|
try
|
|
{
|
|
monitoredItems = JsonConvert.DeserializeObject<MonitoredItemsConf>(jsonData);
|
|
// salvo baseUri
|
|
baseDir = monitoredItems.SrvData.baseUri;
|
|
lgInfo($"baseUri = {baseDir}");
|
|
// imposto a zero la bitmap x riavvio!
|
|
B_input = 0;
|
|
// FORZO invio dati...
|
|
accodaSigIN(ref currDispData);
|
|
// loggo!
|
|
lgInfo($"init input bitmap to zero: {B_input}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError(exc, "Eccezione in decodifica conf json");
|
|
}
|
|
}
|
|
reader.Dispose();
|
|
}
|
|
lgInfo("DONE reloadAdapterConf");
|
|
raiseRefresh(currDispData);
|
|
#endif
|
|
}
|
|
|
|
#endregion Internal Methods
|
|
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero dati dinamici in formato dictionary
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override Dictionary<string, string> getDynData()
|
|
{
|
|
lgInfo("Chiamata getDynData x IOB FILE!");
|
|
Dictionary<string, string> outVal = new Dictionary<string, string>();
|
|
#if false
|
|
try
|
|
{
|
|
/* ----------------------------------------------------------
|
|
* Recupero dalla TUTTE le chiavi richieste...
|
|
* */
|
|
|
|
string cKey = "";
|
|
string cVal = "";
|
|
// processo tutti i DynData...
|
|
foreach (var item in monitoredItems.DynData)
|
|
{
|
|
// cerco elemento indicato
|
|
element = driver.FindElement(By.Id(item.val));
|
|
cVal = element.Text;
|
|
// verifico nome o key...
|
|
if (!string.IsNullOrEmpty(item.name))
|
|
{
|
|
cKey = item.name;
|
|
}
|
|
else
|
|
{
|
|
element = driver.FindElement(By.Id(item.key));
|
|
cKey = element.Text;
|
|
}
|
|
// controllo se devo inviare (per tipo di dato, x scadenza)
|
|
if (monItem2Send(cVal, item))
|
|
{
|
|
item.actVal = cVal;
|
|
item.DTScad = DateTime.Now.AddSeconds(item.sPeriod);
|
|
// accodo!
|
|
outVal.Add(cKey, cVal);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError(exc, "Errore in getDynData x IOB FILE");
|
|
}
|
|
#endif
|
|
return outVal;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua processing del recupero delle OVERRIDE (spindle, feedrate, rapid)
|
|
/// </summary>
|
|
public override void processOverride()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua lettura semafori principale
|
|
/// <paramref name="currDispData">Parametri da aggiornare x display in form</paramref>
|
|
/// </summary>
|
|
public override void readSemafori(ref newDisplayData currDispData)
|
|
{
|
|
// init a zero...
|
|
B_input = 0;
|
|
// ciclo!
|
|
try
|
|
{
|
|
#if false
|
|
// controllo SE il driver SIA attivo...
|
|
if (driver != null)
|
|
{
|
|
string cKey = "";
|
|
string cVal = "";
|
|
// IPOTESI: un UNICO oggetto decodifica status
|
|
if (monitoredItems.Status.Count == 1)
|
|
{
|
|
var item = monitoredItems.Status[0];
|
|
// cerco elemento indicato
|
|
element = driver.FindElement(By.Id(item.val));
|
|
cKey = element.Text;
|
|
// verifico se mancasse il mapping...
|
|
if (!item.codeMapping.ContainsKey(cKey))
|
|
{
|
|
processUnknStatus(cKey);
|
|
}
|
|
else
|
|
{
|
|
// ora decodifico da variabile status a valore secondo impostazione "codeMapping"
|
|
cVal = item.codeMapping[cKey];
|
|
B_input = int.Parse(cVal, System.Globalization.NumberStyles.HexNumber);
|
|
if (currDispData != null)
|
|
{
|
|
currDispData.semIn = Semaforo.SV;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lgError("Errore: driver non pronto (null)");
|
|
}
|
|
#endif
|
|
// riporto bitmap...
|
|
reportRawInput(ref currDispData);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError(exc, "Errore in readSemafori x IOB FILE");
|
|
if (currDispData != null)
|
|
currDispData.semIn = Semaforo.SR;
|
|
}
|
|
}
|
|
|
|
public override void startAdapter(bool resetQueue)
|
|
{
|
|
// in primis RICARICO conf specifica...
|
|
reloadAdapterConf();
|
|
// pulizia preliminare folder
|
|
cleanupFolder();
|
|
// continuo con start...
|
|
base.startAdapter(resetQueue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Override x chiusura driver...
|
|
/// </summary>
|
|
/// <param name="tryRestart"></param>
|
|
/// <param name="forceDequeue"></param>
|
|
public override void stopAdapter(bool tryRestart, bool forceDequeue)
|
|
{
|
|
try
|
|
{
|
|
#if false
|
|
// in primis chiudo driver...
|
|
if (driver != null)
|
|
{
|
|
driver.Quit();
|
|
}
|
|
#endif
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError(exc, "Eccezione in tryDisconnect");
|
|
}
|
|
// continuo
|
|
base.stopAdapter(tryRestart, forceDequeue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Connessione
|
|
/// </summary>
|
|
public override void tryConnect()
|
|
{
|
|
// controllo ping --> segno connected...
|
|
connectionOk = Directory.Exists(BaseDir);
|
|
if (connectionOk)
|
|
{
|
|
#if false
|
|
try
|
|
{
|
|
// modalità sincrona
|
|
startDriver();
|
|
lgInfo("Completato start driver");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError(exc, "Eccezione in tryConnect");
|
|
}
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
// aspetto prima di riprovare...
|
|
Thread.Sleep(200);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disconnessione
|
|
/// </summary>
|
|
public override void tryDisconnect()
|
|
{
|
|
connectionOk = false;
|
|
try
|
|
{
|
|
#if false
|
|
// in primis chiudo driver...
|
|
if (driver != null)
|
|
//if (driver != null && driver.WindowHandles.Count > 0)
|
|
{
|
|
driver.Quit();
|
|
}
|
|
#endif
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError(exc, "Eccezione in tryDisconnect");
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |