357 lines
9.5 KiB
C#
357 lines
9.5 KiB
C#
using MTC;
|
|
using MTConnect;
|
|
using System;
|
|
|
|
namespace SCMA.AdapterCom
|
|
{
|
|
public class GatewayMTC : Gateway
|
|
{
|
|
/// <summary>
|
|
/// adapter globale MTC
|
|
/// </summary>
|
|
/// <summary>
|
|
public Adapter mAdapter = new Adapter();
|
|
|
|
#region gestione globale oggetto
|
|
|
|
/// <summary>
|
|
/// wrapper init
|
|
/// </summary>
|
|
public GatewayMTC()
|
|
{
|
|
protocollo = gwProtocol.MTC;
|
|
}
|
|
/// <summary>
|
|
/// Wrapper metodo Begin
|
|
/// </summary>
|
|
public override void beginDataCollect()
|
|
{
|
|
mAdapter.Begin();
|
|
}
|
|
/// <summary>
|
|
/// Wrapper metodo SendChanged
|
|
/// </summary>
|
|
public override void sendChanged()
|
|
{
|
|
mAdapter.SendChanged();
|
|
}
|
|
/// <summary>
|
|
/// Wrapper metodo imposta tutto a UNAVAILABLE
|
|
/// </summary>
|
|
public override void setAllUnavailable()
|
|
{
|
|
mAdapter.Unavailable();
|
|
}
|
|
/// <summary>
|
|
/// Wrapper metodo START
|
|
/// </summary>
|
|
public override void start()
|
|
{
|
|
mAdapter.Port = port;
|
|
mAdapter.Start();
|
|
}
|
|
/// <summary>
|
|
/// Wrapper metodo STOP
|
|
/// </summary>
|
|
public override void stop()
|
|
{
|
|
mAdapter.Stop();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region gestione controllo stato catena applicativi e messaggi da piattaforma
|
|
|
|
/// <summary>
|
|
/// Data-Ora ultima risposta AGENT x testing PROBE
|
|
/// </summary>
|
|
protected DateTime lastCurrProbe;
|
|
/// <summary>
|
|
/// Valore testuale ultima risposta agent (currentProbe)
|
|
/// </summary>
|
|
protected string currProbe;
|
|
/// <summary>
|
|
/// Valore testuale ultima risposta agent (StatusCurrent)
|
|
/// </summary>
|
|
protected string currStatus;
|
|
|
|
/// <summary>
|
|
/// Verifica lo stato di un servizio indicato per via numerica:
|
|
/// 1: verifica che l'AGENT sia attivo (risponde a porta 5000 con qualcosa)
|
|
/// 2: verifica risposta AGENT: deve essere XML e deve contenere stringa "MTConnectDevices"
|
|
/// 3: verifica che lo status (localhost:5000/current) contenga qualcosa
|
|
/// </summary>
|
|
/// <param name="numServ"></param>
|
|
/// <returns></returns>
|
|
public override bool checkStatus(int numServ)
|
|
{
|
|
// in primis verifico se ho una risposta recente (< 2 sec) dall'agent...
|
|
if (lastCurrProbe == null || lastCurrProbe.AddSeconds(2) < DateTime.Now)
|
|
{
|
|
// altrimenti carico la risposta
|
|
refreshAgentResp();
|
|
}
|
|
// ora so x certo che ho caricato dati recenti...
|
|
bool answ = false;
|
|
switch (numServ)
|
|
{
|
|
case 1:
|
|
answ = (currProbe != "");
|
|
break;
|
|
case 2:
|
|
answ = (currProbe.IndexOf("MTConnectDevices") >= 0 && currProbe.IndexOf("xml") >= 0);
|
|
break;
|
|
case 3:
|
|
answ = (currStatus != "");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Effettua una chiamata all'agent e salva valore e data chiamata...
|
|
/// </summary>
|
|
private void refreshAgentResp()
|
|
{
|
|
// controllo catena AGENT: cerco di scaricare il PROBE alla porta 5000
|
|
try
|
|
{
|
|
currProbe = callUrl("http://localhost:5000");
|
|
}
|
|
catch
|
|
{
|
|
currProbe = "";
|
|
}
|
|
// controllo catena AGENT x CURRENT STATUS: cerco di scaricare /current alla porta 5000
|
|
try
|
|
{
|
|
currStatus = callUrl("http://localhost:5000/current");
|
|
}
|
|
catch
|
|
{
|
|
currStatus = "";
|
|
}
|
|
lastCurrProbe = DateTime.Now;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region gestione nodi (dataItems, conditions)
|
|
|
|
public override void addItemNodeByType(string key, itemType tipo)
|
|
{
|
|
if (!itemNodes.ContainsKey(key))
|
|
{
|
|
// in base al TIPO genero l'oggetto corretto...
|
|
DataItem currObj;
|
|
switch (tipo)
|
|
{
|
|
case itemType.Condition:
|
|
currObj = new Condition(key);
|
|
break;
|
|
case itemType.Event:
|
|
currObj = new Event(key);
|
|
break;
|
|
case itemType.Message:
|
|
currObj = new Message(key);
|
|
break;
|
|
case itemType.Sample:
|
|
currObj = new Sample(key);
|
|
break;
|
|
default:
|
|
currObj = new DataItem(key);
|
|
break;
|
|
}
|
|
mAdapter.AddDataItem(currObj);
|
|
if (tipo == itemType.Condition)
|
|
{
|
|
addConditionNode(key, currObj);
|
|
addItemNode(key, currObj);
|
|
}
|
|
else
|
|
{
|
|
addItemNode(key, currObj);
|
|
}
|
|
}
|
|
}
|
|
/// Effettua chiamata VERA su adapter MTConnect...
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
public override void addItemNode(string key, object value)
|
|
{
|
|
if (!itemNodes.ContainsKey(key))
|
|
{
|
|
base.addItemNode(key, value);
|
|
// aggiungo ad adapter
|
|
mAdapter.RemoveDataItem((DataItem)value);
|
|
mAdapter.AddDataItem((DataItem)value);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Effettua chiamata VERA su adapter MTConnect...
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
public override void addItemNode(object value)
|
|
{
|
|
DataItem currObj = (DataItem)value;
|
|
string key = value.ToString().Replace("|", "");
|
|
try
|
|
{
|
|
key = key.Replace(currObj.Value.ToString(), "");
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
if (!itemNodes.ContainsKey(key))
|
|
{
|
|
base.addItemNode(key, value);
|
|
// aggiungo ad adapter
|
|
mAdapter.AddDataItem((DataItem)value);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Wrapper metodo rimozione di TUTTI gli ITEMS
|
|
/// </summary>
|
|
public override void removeAllItemNodes()
|
|
{
|
|
foreach (var item in itemNodes)
|
|
{
|
|
// rimuovo da MTC
|
|
mAdapter.RemoveDataItem((DataItem)item.Value.cObject);
|
|
}
|
|
base.removeAllItemNodes();
|
|
}
|
|
/// <summary>
|
|
/// Wrapper metodo rimozione ITEMS
|
|
/// </summary>
|
|
/// <param name="anItem"></param>
|
|
public override void removeItemNode(string key)
|
|
{
|
|
// rimuovo da MTC
|
|
mAdapter.RemoveDataItem((DataItem)itemNodes[key].cObject);
|
|
// rimuovo da vett interno
|
|
base.removeItemNode(key);
|
|
}
|
|
public override void updateItemNodeValue(string key, object value)
|
|
{
|
|
// aggiorno SE cambiato...
|
|
DataItem currDataItem = (DataItem)getItemNode(key);
|
|
if (!currDataItem.Value.Equals(value))
|
|
{
|
|
((DataItem)getItemNode(key)).Value = value;
|
|
}
|
|
}
|
|
public override void updateItemNodeCodeValue(string key, string code, object value)
|
|
{
|
|
((Message)getItemNode(key)).Code = code;
|
|
((Message)getItemNode(key)).Value = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// wrapper aggiunta condizione
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
public override void addConditionNode(string key, object value)
|
|
{
|
|
base.addConditionNode(key, value);
|
|
|
|
// aggiungo ad adapter
|
|
mAdapter.RemoveDataItem((Condition)value);
|
|
mAdapter.AddDataItem((Condition)value);
|
|
}
|
|
public override void removeAllConditionNodes()
|
|
{
|
|
foreach (var item in conditionNodes)
|
|
{
|
|
// rimuovo da MTC
|
|
mAdapter.RemoveDataItem((Condition)item.Value.cObject);
|
|
}
|
|
base.removeAllConditionNodes();
|
|
}
|
|
public override void removeConditionNode(string key)
|
|
{
|
|
// rimuovo da MTC
|
|
mAdapter.RemoveDataItem((Condition)conditionNodes[key].cObject);
|
|
// rimuovo da vett interno
|
|
base.removeConditionNode(key);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region gestione allarmi
|
|
|
|
/// <summary>
|
|
/// Specifica implementazione x formattazione ALLARMI
|
|
/// </summary>
|
|
/// <param name="currAllarm"></param>
|
|
public override void addAlarm(allarme currAllarm)
|
|
{
|
|
// implemento chiamata specifica...
|
|
if (conditionNodes.ContainsKey(currAllarm.gruppo))
|
|
{
|
|
itemNode nodo = conditionNodes[currAllarm.gruppo];
|
|
addAlarm(nodo, currAllarm);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Specifica implementazione x formattazione ALLARMI
|
|
/// </summary>
|
|
/// <param name="currAllarm"></param>
|
|
/// <param name="alarmNode">Generico nodo di tipo condition</param>
|
|
public override void addAlarm(itemNode alarmNode, allarme currAllarm)
|
|
{
|
|
// in base al tipo di allarme decodifico condizione...
|
|
Condition.Level livello = Condition.Level.NORMAL;
|
|
switch (currAllarm.livello)
|
|
{
|
|
case "WARNING":
|
|
livello = Condition.Level.WARNING;
|
|
break;
|
|
case "FAULT":
|
|
default:
|
|
livello = Condition.Level.FAULT;
|
|
break;
|
|
}
|
|
Condition mAlarm = (Condition)alarmNode.cObject;
|
|
mAlarm.Add(livello, currAllarm.descrizione, currAllarm.codNum, "", "");
|
|
}
|
|
/// <summary>
|
|
/// Wrapper setup obj allarmi
|
|
/// </summary>
|
|
public override void addAlarmNodes()
|
|
{
|
|
// registro
|
|
Condition newCondNode;
|
|
newCondNode = new Condition(nCncConditions);
|
|
addConditionNode(nCncConditions, newCondNode);
|
|
newCondNode = new Condition(nPlcConditions);
|
|
addConditionNode(nPlcConditions, newCondNode);
|
|
}
|
|
/// <summary>
|
|
/// Wrapper inizializzazione allarmi
|
|
/// </summary>
|
|
public override void initAlarms()
|
|
{
|
|
// trasformo i nodi in elenchi conditions e li aggiungo...
|
|
foreach (var item in conditionNodes)
|
|
{
|
|
((Condition)item.Value.cObject).Normal();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// inizializazione allarme a normal
|
|
/// </summary>
|
|
/// <param name="alarmNode"></param>
|
|
public override void initAlarm(itemNode alarmNode)
|
|
{
|
|
Condition mAlarm = (Condition)alarmNode.cObject;
|
|
mAlarm.Normal();
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|