Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 27, 2023
1 parent e48bf3c commit 925bb70
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 269 deletions.
103 changes: 100 additions & 3 deletions adapter/MTConnect.NET-Adapter/DataSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using MTConnect.Input;
using MTConnect.Observations.Events;
using NLog;
using MTConnect.Observations;
using System;
using System.Collections.Generic;
using System.Threading;

namespace MTConnect.Applications
{
Expand All @@ -13,7 +15,7 @@ namespace MTConnect.Applications

internal class DataSource : MTConnectDataSource
{
protected readonly Logger _engineLogger = LogManager.GetLogger("engine-logger");
//protected readonly Logger _engineLogger = LogManager.GetLogger("engine-logger");
//private PlcSimulator _dataSource;
//private CuttingToolAsset asset = Examples.CuttingTool();

Expand All @@ -37,6 +39,101 @@ protected override void OnStop()

protected override void OnRead()
{
var x = 0;
var i = 0;
var j = 0d;

while (true)
{
//Console.ReadLine();

var rnd = new Random();
var ts = DateTime.Now;

AddObservation("Xload", x, ts);
AddObservation("Yload", x, ts);
AddObservation("Zload", x, ts);


var datasetEntries = new List<IDataSetEntry>();
for (var e = 0; e < 100; e++)
{
datasetEntries.Add(new DataSetEntry($"E{e.ToString("D3")}", rnd.NextDouble() * 100));
}
var dataset = new DataSetObservationInput("testDataSet", datasetEntries);
AddObservation(dataset);


var tableEntries = new List<ITableEntry>();

var tableCells1 = new List<ITableCell>();
tableCells1.Add(new TableCell("Length", 102.310));
tableCells1.Add(new TableCell("Diameter", 12.493));
tableEntries.Add(new TableEntry("T1", tableCells1));

var tableCells2 = new List<ITableCell>();
tableCells2.Add(new TableCell("Length", 128.942));
tableCells2.Add(new TableCell("Diameter", 6.500));
tableEntries.Add(new TableEntry("T2", tableCells2));

var table = new TableObservationInput("testTable", tableEntries);
AddObservation(table);


var samples = new List<double>();
samples.Add(1);
samples.Add(2);
samples.Add(3);
samples.Add(4);
samples.Add(5);
var timeSeries = new TimeSeriesObservationInput("testTimeSeries", samples, 100);
AddObservation(timeSeries);


//app.DataSource.AddAsset(CuttingTool(device.Uuid, j));
j += 23.3455;




//Console.ReadLine();

//var device2 = XmlDevice.FromXml(System.IO.File.ReadAllBytes(@"D:\TrakHound\Source-Code\MTConnect.NET\agent\MTConnect.NET-Agent\bin\Debug\net8.0\devices\device-okuma.xml"));
//app.DataSource.AddDevice(device2);


//switch (i)
//{
// case 0:
// var warning = new ConditionFaultStateObservationInput("L2p1system", Observations.ConditionLevel.WARNING, ts);
// warning.NativeCode = "404";
// warning.Message = "Not Found";
// warning.Qualifier = Observations.ConditionQualifier.LOW;
// app.DataSource.AddObservation(warning);
// break;

// case 1:
// var fault = new ConditionFaultStateObservationInput("L2p1system", Observations.ConditionLevel.FAULT, ts);
// fault.NativeCode = "400";
// fault.Message = "Bad Request";
// app.DataSource.AddObservation(fault);
// break;

// case 2:
// var condition = new ConditionFaultStateObservationInput("L2p1system", Observations.ConditionLevel.NORMAL, ts);
// app.DataSource.AddObservation(condition);
// break;
//}

x++;
i++;
if (i > 2) i = 0;

Thread.Sleep(1000);
}



// Using a single Timestamp (per OnRead() call) can consolidate the SHDR output as well as make MTConnect data more "aligned" and easier to process
//var ts = UnixDateTime.Now;
//AddObservation("L2estop", EmergencyStop.ARMED, ts);
Expand Down
174 changes: 4 additions & 170 deletions adapter/MTConnect.NET-Adapter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using MTConnect.Assets.CuttingTools;
using MTConnect.Assets.CuttingTools.Measurements;
using MTConnect.Devices.Xml;
using MTConnect.Input;
using MTConnect.Observations;
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace MTConnect.Applications
{
Expand All @@ -33,170 +28,9 @@ public static void Main(string[] args)
var app = new MTConnectAdapterApplication(dataSource);

// Run the Agent ('true' parameter blocks the call so the application does not continue)
//app.Run(args, true);








// DEBUG !!!
app.Run(args, false);


//Console.ReadLine();

//var device = XmlDevice.FromXml(System.IO.File.ReadAllBytes(@"D:\TrakHound\Source-Code\MTConnect.NET\agent\MTConnect.NET-Agent\bin\Debug\net8.0\devices\device-mazak.xml"));
//app.DataSource.AddDevice(device);


var x = 0;
var i = 0;
var j = 0d;

while (true)
{
//Console.ReadLine();

var rnd = new Random();
var ts = DateTime.Now;

app.DataSource.AddObservation("Xload", x, ts);
app.DataSource.AddObservation("Yload", x, ts);
app.DataSource.AddObservation("Zload", x, ts);


var datasetEntries = new List<IDataSetEntry>();
for (var e = 0; e < 100; e++)
{
datasetEntries.Add(new DataSetEntry($"E{e.ToString("D3")}", rnd.NextDouble() * 100));
}
var dataset = new DataSetObservationInput("testDataSet", datasetEntries);
app.DataSource.AddObservation(dataset);


var tableEntries = new List<ITableEntry>();

var tableCells1 = new List<ITableCell>();
tableCells1.Add(new TableCell("Length", 102.310));
tableCells1.Add(new TableCell("Diameter", 12.493));
tableEntries.Add(new TableEntry("T1", tableCells1));

var tableCells2 = new List<ITableCell>();
tableCells2.Add(new TableCell("Length", 128.942));
tableCells2.Add(new TableCell("Diameter", 6.500));
tableEntries.Add(new TableEntry("T2", tableCells2));

var table = new TableObservationInput("testTable", tableEntries);
app.DataSource.AddObservation(table);


var samples = new List<double>();
samples.Add(1);
samples.Add(2);
samples.Add(3);
samples.Add(4);
samples.Add(5);
var timeSeries = new TimeSeriesObservationInput("testTimeSeries", samples, 100);
app.DataSource.AddObservation(timeSeries);


//app.DataSource.AddAsset(CuttingTool(device.Uuid, j));
j += 23.3455;




//Console.ReadLine();

//var device2 = XmlDevice.FromXml(System.IO.File.ReadAllBytes(@"D:\TrakHound\Source-Code\MTConnect.NET\agent\MTConnect.NET-Agent\bin\Debug\net8.0\devices\device-okuma.xml"));
//app.DataSource.AddDevice(device2);


//switch (i)
//{
// case 0:
// var warning = new ConditionFaultStateObservationInput("L2p1system", Observations.ConditionLevel.WARNING, ts);
// warning.NativeCode = "404";
// warning.Message = "Not Found";
// warning.Qualifier = Observations.ConditionQualifier.LOW;
// app.DataSource.AddObservation(warning);
// break;

// case 1:
// var fault = new ConditionFaultStateObservationInput("L2p1system", Observations.ConditionLevel.FAULT, ts);
// fault.NativeCode = "400";
// fault.Message = "Bad Request";
// app.DataSource.AddObservation(fault);
// break;

// case 2:
// var condition = new ConditionFaultStateObservationInput("L2p1system", Observations.ConditionLevel.NORMAL, ts);
// app.DataSource.AddObservation(condition);
// break;
//}

x++;
i++;
if (i > 2) i = 0;

Thread.Sleep(1000);
}

app.Run(args, true);
}

public static CuttingToolAsset CuttingTool(string deviceKey, double toolLifeValue)
{
var tool = new CuttingToolAsset();
tool.DeviceUuid = deviceKey;
tool.SerialNumber = "12345678946";
tool.AssetId = "5.12";
tool.ToolId = "12";

var cuttingToolLifeCycle = new CuttingToolLifeCycle
{
//Location = new Location
//{
// Type = LocationType.SPINDLE
//},
ProgramToolNumber = "12",
ProgramToolGroup = "5"
};

var measurements = new List<IMeasurement>();
measurements.Add(new FunctionalLengthMeasurement(7.6543));
measurements.Add(new CuttingDiameterMaxMeasurement(0.375));
cuttingToolLifeCycle.Measurements = measurements;


//tool.CuttingToolLifeCycle.CuttingItems.Add(new CuttingItem
//{
// ItemId = "12.1",
// Indices = "1",
//});

var cutterStatuses = new List<CutterStatusType>();
cutterStatuses.Add(CutterStatusType.AVAILABLE);
cutterStatuses.Add(CutterStatusType.NEW);
cutterStatuses.Add(CutterStatusType.MEASURED);
cuttingToolLifeCycle.CutterStatus = cutterStatuses;

var toolLife = new ToolLife();
toolLife.Value = toolLifeValue;
cuttingToolLifeCycle.ToolLife = new IToolLife[] { toolLife };

//tool.CuttingToolLifeCycle.ToolLife = new ToolLife();

tool.CuttingToolLifeCycle = cuttingToolLifeCycle;

tool.Timestamp = DateTime.Now;

return tool;
}


private static void PrintConsoleHeader()
{
var assembly = Assembly.GetExecutingAssembly();
Expand Down
Loading

0 comments on commit 925bb70

Please sign in to comment.