209 lines
6.7 KiB
C#
209 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
using MTConnect.Clients;
|
|
|
|
namespace MTConnect
|
|
{
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
// setup oggetti
|
|
var baseUrl = "192.168.1.27:5000";
|
|
string sep = "------------------------";
|
|
|
|
// inizio ciclo
|
|
doLog(sep);
|
|
doLog("Rest Client test application");
|
|
doLog(sep);
|
|
|
|
doLog();
|
|
doLog("Call Probe");
|
|
|
|
|
|
var prbClient = new MTConnectHttpProbeClient(baseUrl);
|
|
var prbDoc = prbClient.Get();
|
|
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();
|
|
|
|
doLog("Call connection");
|
|
client = new MTConnectHttpClient(baseUrl);
|
|
client.Interval = 500;
|
|
|
|
client.ProbeReceived += (sender, 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();
|
|
};
|
|
|
|
client.CurrentReceived += (sender, 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)
|
|
{
|
|
doLog($"Component {componentStream.Name} -->");
|
|
|
|
// DataItems (Samples, Events, and Conditions)
|
|
foreach (var observation in componentStream.Observations)
|
|
{
|
|
doLog(observation.DataItemId);
|
|
}
|
|
}
|
|
}
|
|
doLog(sep);
|
|
doLog();
|
|
};
|
|
|
|
client.SampleReceived += (sender, 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)
|
|
{
|
|
doLog($"Component {componentStream.Name} -->");
|
|
|
|
// DataItems (Samples, Events, and Conditions)
|
|
foreach (var observation in componentStream.Observations)
|
|
{
|
|
doLog($" - {observation.DataItemId}");
|
|
}
|
|
}
|
|
}
|
|
doLog(sep);
|
|
doLog();
|
|
};
|
|
|
|
client.AssetsReceived += (sender, 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();
|
|
};
|
|
|
|
client.Start();
|
|
Task.Delay(5000);
|
|
|
|
doLog();
|
|
ConsoleKeyInfo answ;
|
|
doLog("Press ESC to exit");
|
|
answ = Console.ReadKey();
|
|
while (answ == null || answ.Key != ConsoleKey.Escape)
|
|
{
|
|
Task.Delay(1000);
|
|
answ = Console.ReadKey();
|
|
}
|
|
doLog("...closing Now!");
|
|
}
|
|
|
|
private static void doLog(string msg = "")
|
|
{
|
|
File.AppendAllText(filePath, $"{DateTime.Now:HH:mm:ss.fff} | {msg}{Environment.NewLine}");
|
|
Console.WriteLine(msg);
|
|
}
|
|
|
|
private static Stopwatch sw = new Stopwatch();
|
|
private static MTConnectHttpClient client;
|
|
// Get the current directory
|
|
private static string currentDirectory = Directory.GetCurrentDirectory();
|
|
private static string filePath = Path.Combine(currentDirectory, "file.log");
|
|
}
|
|
}
|