224 lines
5.5 KiB
C#
224 lines
5.5 KiB
C#
using System.Collections;
|
|
|
|
namespace SCMA.AdapterCom
|
|
{
|
|
/// <summary>
|
|
/// Gateway di comunicazione secondo diversi standard, partendo da oggetti BASE MTC
|
|
/// </summary>
|
|
public class Gateway
|
|
{
|
|
/// <summary>
|
|
/// Protocollo attivo per la comunicazione dell'adapter
|
|
/// </summary>
|
|
public gwProtocol protocollo;
|
|
/// <summary>
|
|
/// init classe come output su LOGFILE
|
|
/// </summary>
|
|
public Gateway()
|
|
{
|
|
port = 0;
|
|
connConfig = "";
|
|
protocollo = gwProtocol.LOGFILE;
|
|
}
|
|
/// <summary>
|
|
/// Porta comunicazione standard oggetto
|
|
/// </summary>
|
|
public int port;
|
|
/// <summary>
|
|
/// Stringa di configurazione globale
|
|
/// </summary>
|
|
public string connConfig;
|
|
/// <summary>
|
|
/// Flag per indicare se l'obj stia ancora girando
|
|
/// </summary>
|
|
private bool myRunning = false;
|
|
/// <summary>
|
|
/// Indicatore public di oggetto running running.
|
|
/// </summary>
|
|
public bool Running { get { return myRunning; } }
|
|
/// <summary>
|
|
/// la parte di "mark & sweep" (segnala ed invia) è iniziata e stiamo tracciando le conditions.
|
|
/// </summary>
|
|
bool myBegun = false;
|
|
/// <summary>
|
|
/// Elenco di TUTTI gli oggetti gestiti dal gateway...
|
|
/// </summary>
|
|
public ArrayList trackItems = new ArrayList();
|
|
/// <summary>
|
|
/// Aggiunge un generico item all'elenco di quelli tracciati (NON STRONGLY TYPED!!! occhio!!!)
|
|
/// </summary>
|
|
/// <param name="newItem"></param>
|
|
public virtual void addItem(object newItem)
|
|
{
|
|
trackItems.Add(newItem);
|
|
}
|
|
/// <summary>
|
|
/// Rimuove TUTTI i data items tracciati
|
|
/// </summary>
|
|
public virtual void removeAllItems()
|
|
{
|
|
trackItems.Clear();
|
|
}
|
|
/// <summary>
|
|
/// Rimuove un item dall'elenco di quelli tracciati
|
|
/// </summary>
|
|
/// <param name="anItem"></param>
|
|
public virtual void removeItem(object anItem)
|
|
{
|
|
int ind = trackItems.IndexOf(anItem);
|
|
if (ind >= 0)
|
|
trackItems.RemoveAt(ind);
|
|
}
|
|
/// <summary>
|
|
/// Inizia la raccolta dati per confronto modifica da precedente...
|
|
/// </summary>
|
|
public virtual void beginDataCollect()
|
|
{
|
|
myBegun = true;
|
|
foreach (object di in trackItems)
|
|
{
|
|
// inizializza ogni oggetto (in particolare di tipo alarm/conditions x check variazione)
|
|
//di.Begin();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Fa la verifica di cosa sia cambiato ed invia
|
|
/// </summary>
|
|
public virtual void sendChanged()
|
|
{
|
|
if (myBegun)
|
|
{
|
|
foreach (object di in trackItems)
|
|
{
|
|
//di.Prepare();
|
|
}
|
|
}
|
|
|
|
#if false
|
|
// Separate out the data items into those that are on one line and those
|
|
// need separate lines.
|
|
List<SimpleDataItem> together = new List<SimpleDataItem>();
|
|
List<SimpleDataItem> separate = new List<SimpleDataItem>();
|
|
foreach (object di in trackItems)
|
|
{
|
|
List<SimpleDataItem> list = di.ItemList();
|
|
if (di.NewLine)
|
|
separate.AddRange(list);
|
|
else
|
|
together.AddRange(list);
|
|
}
|
|
|
|
// Compone all the same line data items onto one line.
|
|
string line;
|
|
if (timestamp == null)
|
|
{
|
|
DateTime now = DateTime.UtcNow;
|
|
timestamp = now.ToString("yyyy-MM-dd\\THH:mm:ss.fffffffK");
|
|
}
|
|
if (together.Count > 0)
|
|
{
|
|
line = timestamp;
|
|
foreach (SimpleDataItem di in together)
|
|
line += "|" + di.ToString();
|
|
line += "\n";
|
|
|
|
SendToAll(line);
|
|
}
|
|
|
|
// Now write out all the separate lines
|
|
if (separate.Count > 0)
|
|
{
|
|
foreach (SimpleDataItem di in separate)
|
|
{
|
|
line = timestamp;
|
|
line += "|" + di.ToString() + "\n";
|
|
SendToAll(line);
|
|
}
|
|
}
|
|
|
|
// Flush the output
|
|
FlushAll();
|
|
#endif
|
|
|
|
// Cleanup
|
|
foreach (object di in trackItems)
|
|
{
|
|
// pulizia oggetti
|
|
//di.Cleanup();
|
|
}
|
|
myBegun = false;
|
|
}
|
|
/// <summary>
|
|
/// Imposta TUTTI i data items unavailable
|
|
/// </summary>
|
|
public virtual void setAllUnavailable()
|
|
{
|
|
foreach (object di in trackItems)
|
|
{
|
|
//// imposta a unavailable
|
|
//di.Unavailable();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// AVVIA processo lettura CNC e invio dati a client
|
|
/// </summary>
|
|
public virtual void start()
|
|
{
|
|
#if false
|
|
if (!mRunning)
|
|
{
|
|
mListener = new TcpListener(IPAddress.Any, mPort);
|
|
mListener.Start();
|
|
mListenThread = new Thread(new ThreadStart(ListenForClients));
|
|
mListenThread.Start();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// FERMA processo lettura CNC e invio dati a client
|
|
/// </summary>
|
|
public virtual void stop()
|
|
{
|
|
#if false
|
|
if (mRunning)
|
|
{
|
|
mRunning = false;
|
|
mListener.Stop();
|
|
foreach (Object obj in mClients)
|
|
{
|
|
Stream client = (Stream)obj;
|
|
client.Close();
|
|
}
|
|
mClients.Clear();
|
|
|
|
// Wait 5 seconds for the thread to exit.
|
|
mListenThread.Join(2000);
|
|
|
|
// Wait for all client threads to exit.
|
|
mActiveClients.Wait(2000);
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Tipologia protocolli di comunicazione ammessi
|
|
/// </summary>
|
|
public enum gwProtocol
|
|
{
|
|
/// <summary>
|
|
/// NESSUN protocollo reale --> FILE DUMP sul log...
|
|
/// </summary>
|
|
LOGFILE,
|
|
/// <summary>
|
|
/// Protocollo di comunicazione MTConnect
|
|
/// </summary>
|
|
MTC,
|
|
/// <summary>
|
|
/// Protocollo di comunicazione SCM.OPC.UA.REDIS.SERVER
|
|
/// </summary>
|
|
SOURS
|
|
}
|
|
}
|