Skip to content

Commit

Permalink
fixed usage of local ADS in new beckhoff client
Browse files Browse the repository at this point in the history
- enhanced logging
- #15
  • Loading branch information
fbarresi committed Sep 18, 2022
1 parent f12ee54 commit 1eaaf32
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions TFU001/TFU001/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ public async Task OnExecute()

logger.Debug("Starting API Call");
logger.Debug($"Connecting to Beckhoff Port: {AdsPort} - AdsNet: '{AdsNetId}'");
adsClient.Connect(AdsNetId, AdsPort);

if (string.IsNullOrEmpty(AdsNetId))
{
adsClient.Connect(AdsPort);
}
else
{
adsClient.Connect(AdsNetId, AdsPort);
}

logger.Debug($"Beckhoff connection state: {adsClient.ConnectionState}");

var callAddress = Address.IsValidUrl() ? Address : await adsClient.ReadAsync<string>(Address);
logger.Debug($"Url: {callAddress}");
Expand Down Expand Up @@ -88,32 +98,35 @@ public async Task OnExecute()

if (!string.IsNullOrEmpty(ResponseCode))
{
logger.Debug($"Wrinting status code into {ResponseCode}...");
logger.Debug($"Writing status code into {ResponseCode}...");
await adsClient.WriteAsync(ResponseCode, response.StatusCode);
logger.Debug($"Written status code into {ResponseCode}.");
}

//Try parsing object or array from response content
logger.Debug($"Wrinting json response into {Response}...");
logger.Debug($"Writing json response into {Response}...");
try
{
var objectResponse = JObject.Parse(response.Content);
await adsClient.WriteJson(Response, objectResponse);
logger.Debug($"Written response into {Response}.");
}
catch (JsonReaderException exception)
{
logger.Error("Unable to write response content parsing a json object", exception);
logger.Error("Unable to write response content parsing a json object - 2nd try with json array", exception);

try
{
var arrayResponse = JArray.Parse(response.Content);
await adsClient.WriteJson(Response, arrayResponse);
logger.Debug($"Written response into {Response}.");
}
catch (JsonReaderException innerException)
{
logger.Error("Unable to write response content parsing a json array", innerException);
}
}

logger.Debug($"Disconnecting to Beckhoff");
adsClient.Disconnect();
}
catch (Exception e)
Expand Down

0 comments on commit 1eaaf32

Please sign in to comment.