Skip to content

Commit

Permalink
- Added Timeout properties
Browse files Browse the repository at this point in the history
- Changed multthreading to use ThreadPool
- Fixed bug with support for multiple Assets
  • Loading branch information
PatrickRitchie committed Oct 19, 2017
1 parent 529d3a3 commit 48e38d2
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions src/Clients/MTConnectClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public MTConnectClient(string baseUrl, string deviceName)
private void Init()
{
Interval = 500;
Timeout = 5000;
MaximumSampleCount = 200;
RetryInterval = 10000;
}

public string BaseUrl { get; set; }
Expand All @@ -41,6 +43,10 @@ private void Init()

public int Interval { get; set; }

public int Timeout { get; set; }

public int RetryInterval { get; set; }

public long MaximumSampleCount { get; set; }

public string LastChangedAssetId { get; set; }
Expand All @@ -56,9 +62,7 @@ private void Init()
public event StreamStatusHandler Started;
public event StreamStatusHandler Stopped;

private Thread thread;
private ManualResetEvent stop;

private Stream sampleStream;

private SequenceRange _sampleRange;
Expand All @@ -77,8 +81,7 @@ public void Start()

stop = new ManualResetEvent(false);

thread = new Thread(new ThreadStart(Worker));
thread.Start();
ThreadPool.QueueUserWorkItem(new WaitCallback(Worker));
}

public void Stop()
Expand All @@ -88,26 +91,15 @@ public void Stop()
if (stop != null) stop.Set();
}

public void Abort()
{
if (thread != null)
{
try
{
thread.Abort();
}
catch { }
}
}

private void Worker()
private void Worker(object obj)
{
long instanceId = -1;
bool initialize = true;

do
{
var probe = new Probe(BaseUrl, DeviceName);
probe.Timeout = Timeout;
probe.Error += MTConnectErrorRecieved;
probe.ConnectionError += ProcessConnectionError;
var probeDoc = probe.Execute();
Expand All @@ -119,6 +111,7 @@ private void Worker()
do
{
var current = new Current(BaseUrl, DeviceName);
current.Timeout = Timeout;
current.Error += MTConnectErrorRecieved;
current.ConnectionError += ProcessConnectionError;
var currentDoc = current.Execute();
Expand Down Expand Up @@ -182,14 +175,15 @@ private void Worker()
// Create and Start the Sample Stream
if (sampleStream != null) sampleStream.Stop();
sampleStream = new Stream(url, "MTConnectStreams");
sampleStream.ConnectionTimeout = Timeout;
sampleStream.XmlReceived += ProcessSampleResponse;
sampleStream.XmlError += SampleStream_XmlError;
sampleStream.ConnectionError += ProcessConnectionError;
sampleStream.Start();
sampleStream.Run();
}
} while (!stop.WaitOne(5000, true));
} while (!stop.WaitOne(RetryInterval, true));
}
} while (!stop.WaitOne(5000, true));
} while (!stop.WaitOne(RetryInterval, true));

Stopped?.Invoke();
}
Expand Down Expand Up @@ -227,8 +221,8 @@ private void ProcessSampleResponse(string xml)
SampleRange.To = doc.Header.NextSequence;

SampleReceived?.Invoke(doc);
}
}
}
}
}
else
{
Expand All @@ -243,17 +237,20 @@ private void CheckAssetChanged(List<MTConnectStreams.DataItem> dataItems)
{
if (dataItems != null && dataItems.Count > 0)
{
var assetChanged = dataItems.Find(o => o.Type == "AssetChanged");
if (assetChanged != null)
var assetsChanged = dataItems.FindAll(o => o.Type == "AssetChanged");
if (assetsChanged != null)
{
string assetId = assetChanged.CDATA;
if (assetId != "UNAVAILABLE" && assetId != LastChangedAssetId)
foreach (var assetChanged in assetsChanged)
{
var asset = new Asset(BaseUrl, assetId);
asset.Successful += ProcessAssetResponse;
asset.Error += MTConnectErrorRecieved;
asset.ExecuteAsync();
}
string assetId = assetChanged.CDATA;
if (assetId != "UNAVAILABLE" && assetId != LastChangedAssetId)
{
var asset = new Asset(BaseUrl, assetId);
asset.Successful += ProcessAssetResponse;
asset.Error += MTConnectErrorRecieved;
asset.ExecuteAsync();
}
}
}
}
}
Expand Down

0 comments on commit 48e38d2

Please sign in to comment.