395 lines
12 KiB
C#
395 lines
12 KiB
C#
using CncLib.OPENcontrol;
|
|
using IOB_UT;
|
|
using MapoSDK;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace IOB_WIN
|
|
{
|
|
public class IobFileEurom63 : IobFile
|
|
{
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// step di comunicazione attivo
|
|
/// </summary>
|
|
protected Eurom63.ComLevel actLevel = Eurom63.ComLevel.None;
|
|
|
|
/// <summary>
|
|
/// DIrectory eseguibile corrente
|
|
/// </summary>
|
|
protected string appPath = Directory.GetCurrentDirectory();
|
|
|
|
/// <summary>
|
|
/// Oggetti decodificati da pagina
|
|
/// </summary>
|
|
protected Eurom63.ProtoConf confE63;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Estende l'init della classe base...
|
|
/// </summary>
|
|
/// <param name="caller"></param>
|
|
/// <param name="adpConf"></param>
|
|
public IobFileEurom63(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
|
|
{
|
|
lgInfo("INIT IobFileEurom63");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Processa una sessione
|
|
/// - andando a verificare l'esistenza della REQ + se esito positivo pulizia
|
|
/// - andando a richeidere di nuovo risposta
|
|
/// </summary>
|
|
/// <param name="nextLevel"></param>
|
|
/// <param name="connectSession"></param>
|
|
private void processSession(Eurom63.ComLevel nextLevel, Eurom63.Session connectSession)
|
|
{
|
|
// controllo esistenza directory --> segno connected...
|
|
connectionOk = Directory.Exists(BaseDir);
|
|
if (connectionOk)
|
|
{
|
|
// verifico se ci sia risp CONNECT
|
|
if (checkResp(connectSession))
|
|
{
|
|
// aggiorno livello
|
|
actLevel = nextLevel;
|
|
// elimino file sessione
|
|
cleanupSession(connectSession);
|
|
}
|
|
// richiedo SE non ci fosse i dati CONNECT...
|
|
else
|
|
{
|
|
processSession(connectSession);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// aspetto prima di riprovare...
|
|
Thread.Sleep(50);
|
|
}
|
|
}
|
|
|
|
private void requestSampleData()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Protected Methods
|
|
|
|
protected void abortPrevJob()
|
|
{
|
|
var nextLevel = Eurom63.ComLevel.ChannelOk;
|
|
var connectSession = confE63.ActiveSessions[4];
|
|
processSession(nextLevel, connectSession);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifica una sessione configurata (ovvero la comunicazione su TUTTI i file associati)
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <returns></returns>
|
|
protected bool checkResp(Eurom63.Session session)
|
|
{
|
|
bool answ = false;
|
|
string fileName = "";
|
|
if (session != null)
|
|
{
|
|
fileName = $"{BaseDir}\\{session.SessionName}.RSP";
|
|
if (File.Exists(fileName))
|
|
{
|
|
// verifico contenuto
|
|
string rawData = File.ReadAllText(fileName);
|
|
answ = rawData.Contains(session.RespOk);
|
|
if (!answ)
|
|
{
|
|
// se NON ok faccio pulizia...
|
|
cleanupSession(session);
|
|
}
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina tutti i file della sessione indicata
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <returns></returns>
|
|
protected bool cleanupSession(Eurom63.Session session)
|
|
{
|
|
bool answ = false;
|
|
if (session != null)
|
|
{
|
|
string[] file2del = Directory.GetFiles(BaseDir, $"{session.SessionName}.*");
|
|
foreach (var file in file2del)
|
|
{
|
|
File.Delete(file);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processa i file della sessione indicata (copy + transform)
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <returns></returns>
|
|
protected bool processSession(Eurom63.Session session)
|
|
{
|
|
bool answ = false;
|
|
if (session != null)
|
|
{
|
|
string fileFrom = "";
|
|
string fileTo = "";
|
|
// processo OGNI file sessione x farne copia
|
|
foreach (var file2Proc in session.FileList)
|
|
{
|
|
fileFrom = $"{appPath}\\{file2Proc.Path}";
|
|
fileTo = $"{BaseDir}\\{Path.GetFileName(file2Proc.Path)}";
|
|
File.Copy(fileFrom, fileTo, true);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua richiesta info x macchina (validare startup process)
|
|
/// </summary>
|
|
protected void requestInfo()
|
|
{
|
|
var nextLevel = Eurom63.ComLevel.HasInfo;
|
|
var connectSession = confE63.ActiveSessions[1];
|
|
processSession(nextLevel, connectSession);
|
|
}
|
|
|
|
protected void setMachineTime()
|
|
{
|
|
var nextLevel = Eurom63.ComLevel.TimeSet;
|
|
var connectSession = confE63.ActiveSessions[3];
|
|
processSession(nextLevel, connectSession);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Internal Methods
|
|
|
|
/// <summary>
|
|
/// Metodi preliminari x comunicazione:
|
|
/// - richiesta connessione
|
|
/// - richiesta stato attivo
|
|
/// </summary>
|
|
internal void checkCommStatus()
|
|
{
|
|
// init obj display
|
|
newDisplayData currDispData = new newDisplayData();
|
|
|
|
switch (actLevel)
|
|
{
|
|
case Eurom63.ComLevel.None:
|
|
tryConnect();
|
|
break;
|
|
|
|
case Eurom63.ComLevel.IsConnected:
|
|
requestInfo();
|
|
break;
|
|
|
|
case Eurom63.ComLevel.HasInfo:
|
|
setMachineTime();
|
|
break;
|
|
|
|
case Eurom63.ComLevel.TimeSet:
|
|
abortPrevJob();
|
|
break;
|
|
|
|
case Eurom63.ComLevel.ChannelOk:
|
|
requestSampleData();
|
|
break;
|
|
|
|
case Eurom63.ComLevel.DataRequested:
|
|
break;
|
|
|
|
case Eurom63.ComLevel.Sampling:
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
lgInfo("DONE checkCommStatus");
|
|
raiseRefresh(currDispData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia preliminare folder comunicazione
|
|
/// </summary>
|
|
internal override void cleanupFolder()
|
|
{
|
|
// elimino OGNI file per tipo configurato
|
|
foreach (var cleanExt in confE63.cleanupExt)
|
|
{
|
|
string[] file2del = Directory.GetFiles(BaseDir, cleanExt);
|
|
foreach (var file in file2del)
|
|
{
|
|
File.Delete(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ricarica conf adapter...
|
|
/// </summary>
|
|
internal override void reloadAdapterConf()
|
|
{
|
|
// 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
|
|
{
|
|
confE63 = JsonConvert.DeserializeObject<Eurom63.ProtoConf>(jsonData);
|
|
|
|
// salvo baseUri
|
|
BaseDir = confE63.BaseDir;
|
|
lgInfo($"baseDir = {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);
|
|
}
|
|
|
|
#endregion Internal Methods
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Effettua lettura semafori principale
|
|
/// <paramref name="currDispData">Parametri da aggiornare x display in form</paramref>
|
|
/// </summary>
|
|
public override void readSemafori(ref newDisplayData currDispData)
|
|
{
|
|
// in primis controllo status...
|
|
checkCommStatus();
|
|
|
|
// init a zero...
|
|
B_input = 0;
|
|
|
|
// se sono OLTRE i primi 2 step --> accesa...
|
|
if (actLevel > Eurom63.ComLevel.None)
|
|
{
|
|
B_input += (1 << 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Connessione
|
|
/// </summary>
|
|
public override void tryConnect()
|
|
{
|
|
var nextLevel = Eurom63.ComLevel.IsConnected;
|
|
var connectSession = confE63.ActiveSessions[0];
|
|
processSession(nextLevel, connectSession);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disconnessione
|
|
/// </summary>
|
|
public override void tryDisconnect()
|
|
{
|
|
connectionOk = false;
|
|
try
|
|
{
|
|
cleanupFolder();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError(exc, "Eccezione in tryDisconnect");
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |