Files
Mapo-IOB-WIN/IOB-WIN-NEXT/IobOpcUaMBHCimolai.cs
T
2022-10-06 12:19:47 +02:00

407 lines
14 KiB
C#

using IOB_UT_NEXT;
using MapoSDK;
using Opc.Ua;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
namespace IOB_WIN_NEXT
{
public class IobOpcUaMBHCimolai : IobOpcUaMBH
{
#region Public Constructors
/// <summary>
/// Estende l'init della classe base, impiegando il pacchetto Nuget OPC-UA foundation con la
/// gestione specifica per MBH (es Cimolai, Baglietto) https://github.com/OPCFoundation/UA-.NETStandard
/// </summary>
/// <param name="caller"></param>
/// <param name="IOBConf"></param>
public IobOpcUaMBHCimolai(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
{
lgInfo("Init OpcUa MBH versione Cimolai (Baglietto)");
// inizializzo classe base...
if (!string.IsNullOrEmpty(getOptPar("CHANGE_ODL_MODE")))
{
CHANGE_ODL_MODE = getOptPar("CHANGE_ODL_MODE");
}
sendKeyRichiesta = true;
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Processo i task richiesti e li elimino dalla coda 2:2
/// </summary>
/// <param name="task2exe"></param>
public override Dictionary<string, string> executeTasks(Dictionary<string, string> task2exe)
{
// uso metodo base x salvare esito scrittura
var writeResult = base.executeTasks(task2exe);
// aggiungo comportamento custom: se ho impostato nome ricetta (programma) --> imposto
// richiesta caricamento se ho richiesto reset o fine lavoro --> imposto azzeramento
// esco restituendo risutlato scrittura iniziali
if (task2exe != null)
{
// controllo se memMap != null...
if (memMap != null)
{
bool taskOk = false;
string taskVal = "";
// cerco task specifici x OMP
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.setProg:
// recupero dati da memMap...
if (memMap != null && memMap.mMapWrite != null)
{
if (memMap.mMapWrite.ContainsKey(item.Key))
{
dataConf currMem = memMap.mMapWrite[item.Key];
string addr = currMem.memAddr;
taskVal = $"SET task: {item.Key} --> {item.Value} | mem: {currMem.memAddr} - {currMem.size} byte";
// salvo il nuovo valore nella memoria... così prox invio lo trasmetterà
memMap.mMapWrite[item.Key].value = item.Value;
}
else
{
taskVal = $"NO DATA MEM, SET task: {item.Key} --> {item.Value}";
}
}
else
{
taskVal = $"NO MemMap found, SET task: {item.Key} --> {item.Value}";
}
// salvo in currProd..
saveProdData(new KeyValuePair<string, string>(item.Key, item.Value));
break;
case taskType.startSetup:
setFineLotto();
break;
case taskType.stopSetup:
setInizioProd();
break;
case taskType.syncDbData:
iobGetDataFromServer();
iobWriteLocalCSV();
iobSendFTP("");
break;
default:
taskVal = $"taskReq: {tName} | key: {item.Key} | val: {item.Value} | SKIPPED | NO EXEC";
lgInfo($"Chiamata senza processing: taskOk: {taskOk} | taskVal: {taskVal}");
break;
}
}
}
else
{
lgError($"Attenzione! memMap è nullo, non posso eseguire task2exe!");
}
}
return writeResult;
}
/// <summary>
/// Effettua vero processing contapezzi
/// </summary>
public override void processContapezzi()
{
// non fa nulal (non ha contapezzi)
}
public override bool resetcontapezziPLC()
{
bool answ = false;
try
{
List<WriteValue> nodes2Write = new List<WriteValue>();
foreach (var item in opcUaParams.actResetCounter)
{
WriteValue commWriteVal = new WriteValue();
commWriteVal.NodeId = new NodeId(item.Key);
commWriteVal.AttributeId = Attributes.Value;
commWriteVal.Value = new DataValue();
commWriteVal.Value.Value = item.Value;
nodes2Write.Add(commWriteVal);
}
// vera scrittura
UA_ref.WriteNodes(nodes2Write);
answ = true;
}
catch
{ }
return answ;
}
#endregion Public Methods
#region Protected Properties
protected List<ArtRow> ListaArticoli { get; set; } = new List<ArtRow>();
protected List<JobRow> ListaJobs { get; set; } = new List<JobRow>();
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Recupera da server set di dati specifici x IOB : qui per compilare files CSV
/// </summary>
/// <returns></returns>
protected override bool iobGetDataFromServer()
{
bool answ = false;
// recupera dati da server tramite chiamate REST a MP/IO/IOB...
// FIXME TODO !!!
Random rnd = new Random();
int firstId = rnd.Next(1000);
simArticoli(firstId);
simJobs(firstId);
answ = true;
return answ;
}
/// <summary>
/// Effettua upload verso server FTP della macchina dei files nella folder indicata
/// </summary>
/// <param name="folderDir"></param>
/// <returns></returns>
protected override bool iobSendFTP(string folderDir)
{
// FIXME TODO !!!
bool answ = false;
return answ;
}
/// <summary>
/// Prepara files CSV da inviare alla macchina
/// </summary>
protected override bool iobWriteLocalCSV()
{
bool answ = false;
// salvo articoli
string basePath = Directory.GetCurrentDirectory();
string filePath = Path.Combine(basePath, "temp/articoli.csv");
answ = DataExport.SaveToCsv(ListaArticoli, filePath);
if (answ)
{
// salvo PODL
filePath = Path.Combine(basePath, $"temp/{DateTime.Now:dd-MM-yyyy}.csv");
answ = DataExport.SaveToCsv(ListaJobs, filePath);
}
return answ;
}
#endregion Protected Methods
#region Protected Classes
protected class ArtRow
{
#region Public Properties
[Description("Articolo")]
public string Articolo { get; set; } = "";
[Description("Identificazione")]
public string Descrizione { get; set; } = "";
[Description("Limitazione_velocita_sollevamento[%]")]
public int LimiteVel { get; set; } = 100;
[Description("Matricola_articolo")]
public string Matricola { get; set; } = "";
[Description("Peso_teorico_linea1[ton]")]
public int Peso_01 { get; set; } = 0;
[Description("Peso_teorico_linea2[ton]")]
public int Peso_02 { get; set; } = 0;
[Description("Peso_teorico_linea3[ton]")]
public int Peso_03 { get; set; } = 0;
[Description("Peso_teorico_linea4[ton]")]
public int Peso_04 { get; set; } = 0;
[Description("Pos. Carrello 1[mm]")]
public int PosizCarrello_01 { get; set; } = 0;
[Description("Pos. Carrello 2[mm]")]
public int PosizCarrello_02 { get; set; } = 0;
[Description("Pos. Carrello 3[mm]")]
public int PosizCarrello_03 { get; set; } = 0;
[Description("Pos. Carrello 4[mm]")]
public int PosizCarrello_04 { get; set; } = 0;
#endregion Public Properties
}
protected class JobRow
{
#region Public Properties
[Description("Articolo")]
public string Articolo { get; set; } = "";
[Description("Commessa")]
public string Commessa { get; set; } = "";
[Description("Data inserimento")]
public string DataIns { get; set; } = "";
[Description("Identificazione")]
public string Descrizione { get; set; } = "";
[Description("Lavorazione")]
public string Lavorazione { get; set; } = "";
[Description("Matricola")]
public string Matricola { get; set; } = "";
[Description("Ora inserimento")]
public string OraIns { get; set; } = "";
#endregion Public Properties
}
#endregion Protected Classes
#region Private Methods
/// <summary>
/// Azioni specifiche x indicare fine lotto di produzione
/// </summary>
/// <returns></returns>
private bool setFineLotto()
{
bool answ = false;
try
{
List<WriteValue> nodes2Write = new List<WriteValue>();
foreach (var item in opcUaParams.actStopProd)
{
WriteValue commWriteVal = new WriteValue();
commWriteVal.NodeId = new NodeId(item.Key);
commWriteVal.AttributeId = Attributes.Value;
commWriteVal.Value = new DataValue();
commWriteVal.Value.Value = item.Value;
nodes2Write.Add(commWriteVal);
}
// vera scrittura
UA_ref.WriteNodes(nodes2Write);
answ = true;
}
catch
{ }
return answ;
}
/// <summary>
/// Azioni specifiche x iniziare produzione (impostazione ricetta)
/// </summary>
/// <returns></returns>
private bool setInizioProd()
{
bool answ = false;
try
{
List<WriteValue> nodes2Write = new List<WriteValue>();
foreach (var item in opcUaParams.actSetRecipe)
{
WriteValue commWriteVal = new WriteValue();
commWriteVal.NodeId = new NodeId(item.Key);
commWriteVal.AttributeId = Attributes.Value;
commWriteVal.Value = new DataValue();
commWriteVal.Value.Value = item.Value;
nodes2Write.Add(commWriteVal);
}
// vera scrittura
UA_ref.WriteNodes(nodes2Write);
answ = true;
}
catch
{ }
return answ;
}
private void simArticoli(int firstId)
{
// FAKE: crea finti dati...
Random rnd = new Random();
ListaArticoli = new List<ArtRow>();
for (int i = firstId; i <= firstId + 5; i++)
{
ArtRow artRow = new ArtRow()
{
Matricola = $"{i}",
Articolo = $"Articolo_{i:000}",
Descrizione = $"Descrizione_{i:000}",
Peso_01 = rnd.Next(100),
Peso_02 = rnd.Next(100),
Peso_03 = rnd.Next(100),
Peso_04 = rnd.Next(100),
PosizCarrello_01 = rnd.Next(0, 2000),
PosizCarrello_02 = rnd.Next(2100, 5000),
PosizCarrello_03 = rnd.Next(6000, 15000),
PosizCarrello_04 = rnd.Next(16000, 25000)
};
ListaArticoli.Add(artRow);
}
}
private void simJobs(int firstId)
{
// FAKE: crea finti dati...
Random rnd = new Random();
ListaJobs = new List<JobRow>();
for (int i = firstId; i <= firstId + 5; i++)
{
DateTime adesso = DateTime.Now;
JobRow jobRow = new JobRow()
{
Matricola = $"{i}",
Commessa = $"ODL000{i:0000}",
Articolo = $"Articolo_{i:000}",
Descrizione = $"Descrizione_{i:000}",
DataIns = $"{adesso:dd/MM/yyyy}",
OraIns = $"{adesso.AddMinutes(-i):HH:mm}",
Lavorazione = "TEST LAV"
};
ListaJobs.Add(jobRow);
}
}
#endregion Private Methods
}
}