Files
Mapo-IOB-WIN/IOB-WIN-NEXT/IobIcoelDb.cs
T
2022-05-30 12:22:23 +02:00

374 lines
14 KiB
C#

using EgwProxy.Icoel;
using IOB_UT_NEXT;
using MapoSDK;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
namespace IOB_WIN_NEXT
{
/// <summary>
/// Adapter specializzato per ICOEL e le chiamate tramite DB per i dati
/// - accettazione lotti prodotto (frontiera / entrata ciliegie)
/// - export (totale per prodotti)
/// - tracciabilità (confezioni
/// </summary>
public class IobIcoelDb : IobGeneric
{
#region Public Constructors
/// <summary>
/// Costruttore dell'IOB Icoel DB
/// </summary>
/// <param name="caller">AdapterForm chiamante</param>
/// <param name="IOBConf">Configurazione IOB per avvio</param>
public IobIcoelDb(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
{
/* --------------------------------------
* todo's
* --------------------------------------
* - init obj comunicazione da conf e nuget
* - lanciare sync e verifica stato sync
*/
string SyncStateDb = getOptPar("SyncStateDb");
string SyncStateUser = getOptPar("SyncStateUser");
string SyncStatePwd = getOptPar("SyncStatePwd");
string connSyncState = $"data source={SyncStateDb};initial catalog=MoonPro_ISF;persist security info=True;user id={SyncStateUser};password={SyncStatePwd};MultipleActiveResultSets=True;App=IOB-WIN-NEXT";
// eccezione: NON TROVA EntityFramework 6.0.0 o successivo... why?!?
dbProxy = new DbProxy(connSyncState);
lastPING = DateTime.Now.AddHours(-1);
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Implementazione custom esecuzione task specifici
/// </summary>
/// <param name="task2exe"></param>
/// <returns></returns>
public override Dictionary<string, string> executeTasks(Dictionary<string, string> task2exe)
{
// unico task ammissibile: fare un SYNC forzato...
Dictionary<string, string> taskDone = new Dictionary<string, string>();
if (task2exe != null)
{
// controllo se memMap != null...
if (memMap != null)
{
bool taskOk = false;
string taskVal = "";
// cerco task specifici: se ho startSetup --> imposto bit DBB701.DBB0.4
foreach (var item in task2exe)
{
taskOk = false;
taskVal = "";
// converto richiesta in enum...
taskType tName = taskType.nihil;
Enum.TryParse(item.Key, out tName);
// controllo sulla KEY...
switch (tName)
{
case taskType.syncDbData:
break;
default:
taskVal = $"IobIcoelDb | taskReq: {tName} | key: {item.Key} | val: {item.Value} | SKIPPED | NO EXEC";
lgInfo($"IobIcoelDb | chiamata senza processing: taskOk: {taskOk} | taskVal: {taskVal}");
break;
}
}
}
}
return taskDone;
}
/// <summary>
/// Recupero dati dinamici...
/// </summary>
public override Dictionary<string, string> getDynData()
{
// valore non presente in vers default... se gestito fare override
Dictionary<string, string> outVal = new Dictionary<string, string>();
// recupero da variabile appoggio dati del DB locale conteggio record trasferiti
#if false
// recupero da libreria il dizionario dei performance parameters...
var perfParamDict = IcoelSizer.GetPerfMeters();
foreach (var item in perfParamDict)
{
saveValue(ref outVal, item.Value, item.Key);
}
#endif
lastReadPLC = DateTime.Now;
return outVal;
}
/// <summary>
/// Effettua processing CUSTOM x Icoel:
/// - recupera elenco batch delle 2 linee
/// - invia al sistema
/// </summary>
public override void processCustomTaskLF()
{
lgInfo($"Richiesto processCustomTaskLF");
var elencoSyncStatePre = dbProxy.DataController.SyncStateGetAll();
Stopwatch sw = new Stopwatch();
sw.Start();
var elencoSyncState = dbProxy.DataController.SyncStateDoImportAll();
sw.Stop();
lgInfo($"DB: esecuzione task dbProxy.DataController.SyncStateGetAll() in {sw.ElapsedMilliseconds} ms");
if (elencoSyncState != null)
{
foreach (var item in elencoSyncState)
{
lgTrace($"TAB: {item.TableName} | IdxIN / IdxLocal {item.LastIdxIn} / {item.LastIdx} | NumIn / NumLocal {item.NumRecIn} / {item.NumRec}");
// verifico se serva redo..
}
}
// fare processing DB secondo il seguente schema:
/*
*
bool needRedo = true;
var elencoSyncState = dbProxy.DataController.SyncStateGetAll();
while (needRedo)
{
Console.WriteLine(" --- PRE --- ");
if (elencoSyncState != null)
{
foreach (var item in elencoSyncState)
{
Console.WriteLine($"TAB: {item.TableName} | IdxIN / IdxLocal {item.LastIdxIn} / {item.LastIdx} | NumIn / NumLocal {item.NumRecIn} / {item.NumRec}");
// verifico se serva redo..
}
}
Console.WriteLine();
Console.WriteLine("Premere un tasto x continuare...");
Console.ReadKey();
Stopwatch sw = new Stopwatch();
sw.Start();
elencoSyncState = dbProxy.DataController.SyncStateDoImportAll();
sw.Stop();
Console.WriteLine($" process time: {sw.ElapsedMilliseconds} ms ");
Console.WriteLine(" --- POST --- ");
if (elencoSyncState != null)
{
foreach (var item in elencoSyncState)
{
Console.WriteLine($"TAB: {item.TableName} | IdxIN / IdxLocal {item.LastIdxIn} / {item.LastIdx} | NumIn / NumLocal {item.NumRecIn} / {item.NumRec}");
// verifico se serva redo..
}
}
// processo
needRedo = testNeedRedo(elencoSyncState);
}
*
*
* */
#if false
var currBatch = IcoelSizer.GetCurrentBatch();
if (currBatch != null)
{
// verifico se i batch siano variati... e quindi da inviare...
bool doSend = (currBatchList == null || currBatchList.Count == 0);
if (currBatch.Count > 0 && !doSend)
{
foreach (var item in currBatch)
{
// se variato ID è cambiato
doSend = doSend || (currBatchList[item.Key].Id != item.Value.Id);
}
}
// se devo inviare impacchetto dati
if (doSend)
{
accodaRawData(IOB_UT_NEXT.rawTransfType.IcoelBatch, currBatch);
currBatchList = currBatch;
}
}
#endif
lastReadPLC = DateTime.Now;
}
/// <summary>
/// Effettua lettura semafori principale <paramref name="currDispData">Parametri da
/// aggiornare x display in form</paramref>
/// </summary>
public override void readSemafori(ref newDisplayData currDispData)
{
if (connectionOk)
{
B_input = 1;
currDispData.semIn = Semaforo.SV;
if (dbProxy != null)
{
B_input = 3;
}
#if false
if (currBatchList == null)
{
// se nullo --> provo a leggere!
var currBatch = IcoelSizer.GetCurrentBatch();
if (currBatch != null)
{
currBatchList = currBatch;
// accodo invio!
string rawBatch = JsonConvert.SerializeObject(currBatch);
accodaRawData(IOB_UT_NEXT.rawTransfType.IcoelBatch, currBatch);
}
}
else
{
// se ho batch NON chiusi (data = minValue) allora lavora
if (currBatchList[1].EndTime == DateTime.MinValue || currBatchList[1].EndTime == DateTime.MinValue)
{
B_input = 3;
}
}
#endif
}
else
{
B_input = 0;
currDispData.semIn = Semaforo.SR;
}
}
/// <summary>
/// Override connessione
/// </summary>
public override void tryConnect()
{
if (!connectionOk)
{
// controllo che il ping sia stato tentato almeno pingTestSec fa...
if (DateTime.Now.Subtract(lastPING).TotalSeconds > utils.CRI("pingTestSec"))
{
if (verboseLog || periodicLog)
{
lgInfo("IcoelSoap: ConnKO - tryConnect");
}
// in primis salvo data ping...
lastPING = DateTime.Now;
// se passa il ping faccio il resto...
if (testPingMachine == IPStatus.Success)
{
string szStatusConnection = "";
try
{
// ora provo connessione...
parentForm.commPlcActive = true;
if (dbProxy != null)
{
connectionOk = true;
}
#if false
// recupero elenco batch... se != vuoto -_> connesso!
var batchList = IcoelSizer.GetCurrentBatch();
if (batchList != null && batchList.Count > 0)
{
lgInfo($"szStatusConnection IcoelSoap, recuperato elenco di {batchList.Count} batch");
parentForm.commPlcActive = false;
connectionOk = true;
}
#endif
// refresh stato connessione!!!
if (connectionOk)
{
if (adpRunning)
{
lgInfo("Connessione OK");
}
}
else
{
lgError("Impossibile procedere, connessione mancante...");
}
}
catch (Exception exc)
{
lgFatal($"Errore nella connessione all'adapter IcoelSoap: {szStatusConnection}{Environment.NewLine}{exc}");
connectionOk = false;
lgInfo($"Eccezione in TryConnect, Adapter IcoelSoap NON running, pausa di {utils.CRI("waitRecMSec")} msec prima di ulteriori tentativi di riconnessione");
}
}
else
{
// loggo no risposta ping ...
connectionOk = false;
if (verboseLog || periodicLog)
{
lgInfo($"Attenzione: IcoelSoap controllo PING fallito per IP {cIobConf.cncIpAddr}");
}
}
}
}
else
{
needRefresh = true;
}
}
public override void tryDisconnect()
{
// registro solo che è disconnesso
connectionOk = false;
}
#endregion Public Methods
#if false
/// <summary>
/// elenco dei BAtch correntemente caricati x testing variazione
/// </summary>
protected Dictionary<int, Batch> currBatchList { get; set; }
#endif
#region Protected Properties
protected EgwProxy.Icoel.DbProxy dbProxy { get; set; }
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// recupera le variety ed i rispettivi layout e li invia al sistema MP/IO
/// </summary>
protected void refreshVarietyData()
{
#if false
///determina se recuperare SOLO varietà attive o tutte
bool soloAttive = false;
var varList = IcoelSizer.GetVarietyList(soloAttive);
if (varList != null && varList.Length > 0)
{
var varietyData = IcoelSizer.GetLayoutForVarietyList(varList);
// invio dai al server IO
}
#endif
lastReadPLC = DateTime.Now;
}
#endregion Protected Methods
}
}