396 lines
14 KiB
C#
396 lines
14 KiB
C#
using IOB_UT_NEXT;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using static IOB_UT_NEXT.CustomObj;
|
|
|
|
namespace IOB_WIN_NEXT
|
|
{
|
|
/* --------------------------------------------------------------------------------
|
|
* Controlli ModBusTCP FIMAT
|
|
* - protocollo ModBus TCP
|
|
* - gestione stato via Modbus
|
|
* - resto gestioen via file
|
|
*
|
|
* G:\Drive condivisi\30_Clienti\Tenditalia\Macchina Fimat
|
|
*
|
|
* -------------------------------------------------------------------------------- */
|
|
|
|
public class IobModbusTCPFimat : IobModbusTCP
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Classe base con i metodi x ModBusTCP
|
|
/// </summary>
|
|
/// <param name="caller"></param>
|
|
/// <param name="IOBConf"></param>
|
|
public IobModbusTCPFimat(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
|
|
{
|
|
lgInfo("NEW IOB ModBus TCP FIMAT");
|
|
|
|
setupSpecialParams();
|
|
// provo lettura una prima volta i dati DYN
|
|
if (currPLC != null && currPLC.Connected)
|
|
{
|
|
try
|
|
{
|
|
processDynData();
|
|
if (EnableTest)
|
|
{
|
|
testReadExt();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError($"Eccezione in processDynData iniziale x ModBus TCP FIMAT:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
if (EnableTest)
|
|
{
|
|
processDataSync();
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// Quantità massima PODL prima di fare split ordine (default 999'999)
|
|
/// </summary>
|
|
protected int maxPodlQty = 999999;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
/// <summary>
|
|
/// Restituisce controllo IN ALLARME
|
|
/// </summary>
|
|
protected bool AlarmState
|
|
{
|
|
get
|
|
{
|
|
return testIntCondition("AlarmIntCond");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce status di ESTOP triggered (triggered = premuta, altrimenti armed)
|
|
/// </summary>
|
|
protected bool EStopTriggered
|
|
{
|
|
get
|
|
{
|
|
return testBitCondition("EStopBitCond");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce status di Manuale, hard coded
|
|
/// </summary>
|
|
protected bool ManualState
|
|
{
|
|
get
|
|
{
|
|
return testBitCondition("ManualBitCond");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce status di WORK (auto + lavora), hard coded
|
|
/// </summary>
|
|
protected bool WorkState
|
|
{
|
|
get
|
|
{
|
|
return testIntCondition("WorkIntCond");
|
|
}
|
|
}
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Effettua decodifica aree memoria alla bitmap usata x MAPO
|
|
/// </summary>
|
|
protected override void decodeToBaseBitmap()
|
|
{
|
|
// init a zero...
|
|
B_input = 0;
|
|
|
|
/* -----------------------------------------------------
|
|
* bitmap MAPO STANDARD 60
|
|
* B0: POWER_ON
|
|
* B1: RUN
|
|
* B2: pzCount
|
|
* B3: allarme
|
|
* B4: manuale
|
|
* B5: slowTC
|
|
* B6: WarmUpCoolDown
|
|
* B7: EmergArmata
|
|
*
|
|
----------------------------------------------------- */
|
|
|
|
var MemInt = new byte[2];
|
|
|
|
int byteSignals = 0;
|
|
// bit 0 (poweron) imposto a 1 SE connected...
|
|
if (currPLC.Connected)
|
|
{
|
|
byteSignals += (1 << 0);
|
|
}
|
|
// se ho qualcosa nella holding register...
|
|
if (HoldingRegisterLUT != null && HoldingRegisterLUT.Count > 0)
|
|
{
|
|
// se emergenza NON premuta (triggered) indico OK (armata...) FARE !!! togliere true
|
|
if (!EStopTriggered)
|
|
{
|
|
byteSignals += (1 << 7);
|
|
}
|
|
|
|
// impiego controlli da setup IntConditions... processo dagli stati + gravi...
|
|
if (AlarmState || hasAlarms())
|
|
{
|
|
byteSignals += (1 << 3);
|
|
}
|
|
else if (ManualState)
|
|
{
|
|
byteSignals += (1 << 4);
|
|
}
|
|
else if (WorkState)
|
|
{
|
|
byteSignals += (1 << 1);
|
|
}
|
|
|
|
checkTranslateBit();
|
|
checkTranslateInt();
|
|
}
|
|
else
|
|
{
|
|
lgInfo("HoldingRegisterLUT vuoto!");
|
|
}
|
|
|
|
// salvo!
|
|
B_input = byteSignals;
|
|
}
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// Init parametri speciali, tipicamente KVP opzionali da json
|
|
/// </summary>
|
|
protected override void setupSpecialParams()
|
|
{
|
|
string sCond = "";
|
|
// per prima cosa inizializzo lista PODL inviati
|
|
POdlSentList = POdlSentFileArch;
|
|
|
|
// verifico se usare ricette locali/http...
|
|
var sLocRec = getOptJsonKVP("useLocalRecipe");
|
|
bool.TryParse(sLocRec, out useLocalRecipe);
|
|
|
|
// recupero quantitativo massimo KG x PODL
|
|
var sMaxQty = getOptJsonKVP("maxPodlQty");
|
|
int.TryParse(sMaxQty, out maxQtyPerFile);
|
|
|
|
// recupero le folder specifiche x IN/OUT ricette filtrando direttamente l'area di memoria...
|
|
sCond = "path";
|
|
var dirList = memMap.optKVP
|
|
.Where(x => x.Key.StartsWith(sCond))
|
|
.ToList();
|
|
pathList = dirList.ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
// gestione replace in file ricette...
|
|
sCond = "replace-";
|
|
var ruleList = memMap.optKVP
|
|
.Where(x => x.Key.StartsWith(sCond))
|
|
.ToList();
|
|
RecipeReplRules = ruleList.ToDictionary(x => x.Key.Replace(sCond, ""), x => x.Value);
|
|
}
|
|
#endif
|
|
|
|
#if false
|
|
protected bool useLocalRecipe = true;
|
|
protected int maxQtyPerFile = 0;
|
|
#endif
|
|
#if false
|
|
/// <summary>
|
|
/// Prepara la ricetta indicata partendo dai dati della ricetta template + dati ODL di riferimento
|
|
/// </summary>
|
|
/// <param name="recFilePath">Path del file template della ricetta</param>
|
|
/// <param name="podlReq">Record del PODL da inviare</param>
|
|
/// <returns>Path della ricetta da inviare</returns>
|
|
protected override bool recipePrepare(string tempPath, string recFilePath, PODLModel podlReq)
|
|
{
|
|
bool fatto = false;
|
|
// leggo il file template ricetta
|
|
var rawData = File.ReadAllText(recFilePath);
|
|
string fixContent = rawData;
|
|
// eseguo sostituzioni
|
|
foreach (var rule in ReplRules)
|
|
{
|
|
string replVal = rule.Value;
|
|
// calcolo la sostituzione per i valori SPECIFICI...
|
|
if (replVal.Contains("{{PODL}}"))
|
|
{
|
|
replVal.Replace("{{PODL}}", $"PODL{podlReq.IdxPromessa:00000000}");
|
|
}
|
|
if (replVal.Contains("{{Qty}}"))
|
|
{
|
|
replVal.Replace("{{Qty}}", $"{podlReq.NumPezzi}");
|
|
}
|
|
if (replVal.Contains("{{Note}}"))
|
|
{
|
|
replVal.Replace("{{Note}}", $"{podlReq.Note}");
|
|
}
|
|
fixContent = fixContent.Replace(rule.Key, replVal);
|
|
}
|
|
// scrivo file (secondo quantità...)!
|
|
string destPath = Path.Combine(tempPath, $"PODL{podlReq.IdxPromessa:00000000}.xml");
|
|
if (podlReq.NumPezzi <= maxQtyPerFile)
|
|
{
|
|
File.WriteAllText(destPath, fixContent);
|
|
}
|
|
else
|
|
{
|
|
int numReq = (int)Math.Ceiling((double)podlReq.NumPezzi / maxQtyPerFile);
|
|
// splitto e scrivo...
|
|
for (int i = 1; i <= numReq; i++)
|
|
{
|
|
destPath = Path.Combine(tempPath, $"PODL{podlReq.IdxPromessa:00000000}-{i}.xml");
|
|
File.WriteAllText(destPath, fixContent);
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
#endif
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// Prepara le ricette in folder locale
|
|
/// </summary>
|
|
/// <param name="tempPath"></param>
|
|
/// <param name="useLocal"></param>
|
|
/// <returns></returns>
|
|
protected override bool recipeReqWriteLocal(string tempPath, bool useLocal)
|
|
{
|
|
bool fatto = false;
|
|
|
|
List<PODLModel> reqPOdlList = new List<PODLModel>();
|
|
// per prima cosa recupero elenco PODL da gestire....
|
|
var rawListPODL = callUrl(urlGetCurrPODL, false);
|
|
if (!string.IsNullOrEmpty(rawListPODL))
|
|
{
|
|
try
|
|
{
|
|
reqPOdlList = JsonConvert.DeserializeObject<List<PODLModel>>(rawListPODL);
|
|
fatto = reqPOdlList.Count > 0;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error($"Errore: chiamata elenco PODL ha restituito errore{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lg.Error($"Errore: chiamata elenco PODL ({urlGetCurrPODL}) ha restituito valore vuoto");
|
|
}
|
|
// proseguo se ne ho trovati...
|
|
if (reqPOdlList.Count > 0)
|
|
{
|
|
// in questo elenco devo considerare solo PODL ATTIVI...
|
|
List<PODLModel> actPOdlList = reqPOdlList.Where(x => x.Attivabile).ToList();
|
|
// da questo elenco,se è rimasto qualcosa, ciclo x ricette da inviare
|
|
foreach (var actPodlReq in actPOdlList)
|
|
{
|
|
// devo escludere i PODL già gestiti
|
|
if (!POdlSentList.ContainsKey(actPodlReq.IdxPromessa))
|
|
{
|
|
string recFilePath = "";
|
|
try
|
|
{
|
|
// calcolo path ricetta...
|
|
recFilePath = Path.Combine(pathList["path-locBase"], pathList["path-00-Arch"], actPodlReq.Recipe);
|
|
// preparo file ricetta x PODL
|
|
bool fileOk = recipePrepare(tempPath, recFilePath, actPodlReq);
|
|
if (fileOk)
|
|
{
|
|
// aggiungo PODL ad elenco dei processati
|
|
POdlSentList.Add(actPodlReq.IdxPromessa, actPodlReq);
|
|
// indico fatto (almeno per 1 è ok...)
|
|
fatto = true;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError($"recipeReqWriteLocal | Errore durante ciclo verifica ricetta | idxPODL {actPodlReq.IdxPromessa} | recFilePath {recFilePath}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
}
|
|
// salvo su file elenco PODL gestiti/inviati
|
|
POdlSentFileArch = POdlSentList;
|
|
}
|
|
return fatto;
|
|
}
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Effettua sync dati x PODL attivi
|
|
/// </summary>
|
|
protected override void processDataSync()
|
|
{
|
|
string tempPath = Path.Combine(pathList["path-locBase"], pathList["path-01-Temp"]);
|
|
baseUtils.checkDir(tempPath);
|
|
// recupero elenco PODL da processare, confronta con elenco dei PODL già inviati, scrive
|
|
// copia locale ricette
|
|
bool create = recipeReqWriteLocal(tempPath, useLocalRecipe);
|
|
if (create)
|
|
{
|
|
// invio ricette a impianto
|
|
bool trasmitted = RecipeSend("");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cerca di recuperare i file generati dall'impianto in merito al processing degli ordini
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected override bool processFileImport()
|
|
{
|
|
bool fatto = false;
|
|
// cerca e sposta i NUOVI file
|
|
|
|
// per ogni file ricevuto controlla se sia corrispondente ad un file inviato (cerca PODL)
|
|
|
|
// se trova PODL prova ad avviare/chiudere PODL relativo (eventualmente duplicandolo)
|
|
|
|
return fatto;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Verifica l'elenco dei PODL (Attivi) per la macchina e verifica se vadano inviati
|
|
/// (controllando archivio PODL inviati)
|
|
/// </summary>
|
|
/// <param name="actPOdlList"></param>
|
|
/// <returns></returns>
|
|
protected bool trySendPOdl(List<PODLModel> actPOdlList)
|
|
{
|
|
bool answ = false;
|
|
foreach (var item in actPOdlList)
|
|
{
|
|
// verifico se sia nella folder di quelli inviati
|
|
|
|
// se non presente preparo, leggendo conf del NUMERO ricetta (e configurandola come 10'000+num???)
|
|
|
|
// infine invio
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
}
|
|
} |