311 lines
9.8 KiB
C#
311 lines
9.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.Remoting;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
using MTConnect.Clients;
|
|
using MTConnect.Observations;
|
|
using MTConnect.Streams;
|
|
using YamlDotNet.Serialization.ObjectGraphVisitors;
|
|
|
|
namespace MTConnect
|
|
{
|
|
internal class Program
|
|
{
|
|
#region Private Fields
|
|
|
|
private static MTConnectHttpClient client;
|
|
|
|
// Get the current directory
|
|
private static string currentDirectory = Directory.GetCurrentDirectory();
|
|
|
|
private static string filePath = Path.Combine(currentDirectory, "file.log");
|
|
private static string sep = "------------------------";
|
|
private static Stopwatch sw = new Stopwatch();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
private static void Client_AssetsReceived(object sender, Assets.IAssetsResponseDocument document)
|
|
{
|
|
doLog(sep);
|
|
doLog("ASSET");
|
|
doLog(sep);
|
|
foreach (var asset in document.Assets)
|
|
{
|
|
// Print AssetId to the Console
|
|
doLog($"Asset: {asset.AssetId}");
|
|
}
|
|
doLog(sep);
|
|
doLog();
|
|
}
|
|
|
|
private static void Client_ClientStarted(object sender, EventArgs e)
|
|
{
|
|
doLog(sep);
|
|
doLog($"Client Started! | {e}");
|
|
doLog(sep);
|
|
doLog("Press ESC to exit");
|
|
}
|
|
|
|
private static void Client_ClientStopped(object sender, EventArgs e)
|
|
{
|
|
doLog(sep);
|
|
doLog($"Client Stopped! | {e}");
|
|
doLog(sep);
|
|
doLog("Press ESC to exit");
|
|
}
|
|
|
|
private static void Client_ConnectionError(object sender, Exception exc)
|
|
{
|
|
doLog(sep);
|
|
doLog($"CONNECTION ERROR returned!");
|
|
doLog($"{exc}");
|
|
doLog(sep);
|
|
doLog("Press ESC to exit");
|
|
}
|
|
|
|
private static void Client_CurrentReceived(object sender, Streams.IStreamsResponseDocument document)
|
|
{
|
|
doLog(sep);
|
|
doLog("CURRENT");
|
|
doLog(sep);
|
|
foreach (var deviceStream in document.Streams)
|
|
{
|
|
// DeviceStream
|
|
doLog($"Stream: {deviceStream.Name}");
|
|
|
|
// Component Streams
|
|
foreach (var componentStream in deviceStream.ComponentStreams)
|
|
{
|
|
processData(componentStream);
|
|
}
|
|
}
|
|
doLog(sep);
|
|
doLog();
|
|
}
|
|
|
|
private static void Client_ObservationReceived(object sender, IObservation observ)
|
|
{
|
|
doLog(sep);
|
|
doLog("OBSERVATION");
|
|
doLog(sep);
|
|
processObservation(observ);
|
|
}
|
|
|
|
private static void Client_ProbeReceived(object sender, Devices.IDevicesResponseDocument document)
|
|
{
|
|
doLog(sep);
|
|
doLog("PROBE returned");
|
|
doLog(sep);
|
|
foreach (var device in document.Devices)
|
|
{
|
|
// Device
|
|
doLog($"DEV: {device.Id}");
|
|
|
|
// All DataItems (traverse the entire Device model)
|
|
if (device.DataItems.Count() > 0)
|
|
{
|
|
foreach (var dataItem in device.GetDataItems())
|
|
{
|
|
doLog($" DItm | {dataItem.IdPath}");
|
|
}
|
|
}
|
|
|
|
// All Components (traverse the entire Device model)
|
|
if (device.Components.Count() > 0)
|
|
{
|
|
foreach (var component in device.GetComponents())
|
|
{
|
|
doLog($" Comp | {component.IdPath}");
|
|
}
|
|
}
|
|
|
|
// All Compositions (traverse the entire Device model)
|
|
if (device.Compositions.Count() > 0)
|
|
{
|
|
foreach (var composition in device.GetCompositions())
|
|
{
|
|
doLog($" Cpst | {composition.IdPath}");
|
|
}
|
|
}
|
|
}
|
|
doLog(sep);
|
|
doLog();
|
|
}
|
|
|
|
private static void Client_SampleReceived(object sender, Streams.IStreamsResponseDocument document)
|
|
{
|
|
doLog(sep);
|
|
doLog("SAMPLE");
|
|
doLog(sep);
|
|
foreach (var deviceStream in document.Streams)
|
|
{
|
|
// DeviceStream
|
|
doLog($"Stream: {deviceStream.Name}");
|
|
// Component Streams
|
|
foreach (var componentStream in deviceStream.ComponentStreams)
|
|
{
|
|
processData(componentStream);
|
|
}
|
|
}
|
|
doLog(sep);
|
|
doLog();
|
|
}
|
|
|
|
private static void doLog(string msg = "")
|
|
{
|
|
string dtMessage = $"{DateTime.Now:HH:mm:ss.fff} | {msg}";
|
|
File.AppendAllText(filePath, $"{dtMessage}{Environment.NewLine}");
|
|
Console.WriteLine(dtMessage);
|
|
}
|
|
|
|
private static void doPreliminaryProbe(string baseUrl)
|
|
{
|
|
doLog();
|
|
doLog("Call Probe");
|
|
|
|
var prbClient = new MTConnectHttpProbeClient(baseUrl);
|
|
prbClient.Timeout = 20000;
|
|
var prbDoc = prbClient.Get();
|
|
if (prbDoc == null)
|
|
{
|
|
doLog("Preliminari Probe | No answer, continue...");
|
|
}
|
|
else
|
|
{
|
|
doLog(sep);
|
|
doLog("Preliminary PROBE");
|
|
doLog(sep);
|
|
foreach (var device in prbDoc.Devices)
|
|
{
|
|
// Device
|
|
doLog($"DEV: {device.Id}");
|
|
|
|
// All DataItems (traverse the entire Device model)
|
|
if (device.DataItems.Count() > 0)
|
|
{
|
|
foreach (var dataItem in device.GetDataItems())
|
|
{
|
|
doLog($" DItm | {dataItem.IdPath}");
|
|
}
|
|
}
|
|
|
|
// All Components (traverse the entire Device model)
|
|
if (device.Components.Count() > 0)
|
|
{
|
|
foreach (var component in device.GetComponents())
|
|
{
|
|
doLog($" Comp | {component.IdPath}");
|
|
}
|
|
}
|
|
|
|
// All Compositions (traverse the entire Device model)
|
|
if (device.Compositions.Count() > 0)
|
|
{
|
|
foreach (var composition in device.GetCompositions())
|
|
{
|
|
doLog($" Cpst | {composition.IdPath}");
|
|
}
|
|
}
|
|
}
|
|
doLog(sep);
|
|
doLog();
|
|
}
|
|
}
|
|
|
|
private static void Main(string[] args)
|
|
{
|
|
// setup oggetti
|
|
//var baseUrl = "127.0.0.1:5000";
|
|
var baseUrl = "192.168.1.27:5000";
|
|
|
|
// inizio ciclo
|
|
doLog(sep);
|
|
doLog("Rest Client test application");
|
|
doLog(sep);
|
|
|
|
doPreliminaryProbe(baseUrl);
|
|
|
|
doLog("Call connection");
|
|
client = new MTConnectHttpClient(baseUrl);
|
|
// sample interval
|
|
client.Interval = 500;
|
|
// timeout x richieste
|
|
client.Timeout = 4000;
|
|
// timeout riconnessione
|
|
client.ReconnectionInterval = 5000;
|
|
|
|
// aggancio eventi
|
|
client.ClientStarted += Client_ClientStarted;
|
|
client.ClientStopped += Client_ClientStopped;
|
|
client.ConnectionError += Client_ConnectionError;
|
|
client.ProbeReceived += Client_ProbeReceived;
|
|
client.AssetsReceived += Client_AssetsReceived;
|
|
client.CurrentReceived += Client_CurrentReceived;
|
|
client.SampleReceived += Client_SampleReceived;
|
|
// conviene usare sample x avere la stessa cosa ma con altre info...
|
|
//client.ObservationReceived += Client_ObservationReceived;
|
|
|
|
// vero avvio
|
|
client.Start();
|
|
|
|
Task.Delay(5000);
|
|
doLog();
|
|
ConsoleKeyInfo answ;
|
|
doLog("Press ESC to exit");
|
|
answ = Console.ReadKey();
|
|
while (answ == null || answ.Key != ConsoleKey.Escape)
|
|
{
|
|
doLog("Press ESC to exit");
|
|
Task.Delay(1000);
|
|
answ = Console.ReadKey();
|
|
}
|
|
doLog("...closing Now!");
|
|
}
|
|
|
|
private static void processData(IComponentStream dataStream)
|
|
{
|
|
doLog($"Component {dataStream.Name} -->");
|
|
// DataItems (Samples, Events, and Conditions)
|
|
foreach (var observation in dataStream.Observations)
|
|
{
|
|
processObservation(observation);
|
|
}
|
|
}
|
|
|
|
private static void processObservation(IObservation observation)
|
|
{
|
|
string sMsg = $" - {observation.DataItemId} | {observation.Category}";
|
|
switch (observation.Category)
|
|
{
|
|
case Devices.DataItemCategory.CONDITION:
|
|
break;
|
|
|
|
case Devices.DataItemCategory.EVENT:
|
|
break;
|
|
|
|
case Devices.DataItemCategory.SAMPLE:
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
if (observation.Values != null && observation.Values.Count() > 0)
|
|
{
|
|
foreach (var item in observation.Values)
|
|
{
|
|
sMsg += $" | {item.Key}: {item.Value}";
|
|
}
|
|
}
|
|
doLog(sMsg);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |