Skip to content

Commit

Permalink
Create project
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Jul 9, 2019
1 parent f55e91d commit f214b4f
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
25 changes: 25 additions & 0 deletions TwinCAT.JsonExtension.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwinCAT.JsonExtension", "TwinCAT.JsonExtension\TwinCAT.JsonExtension.csproj", "{781232EA-EB55-49AE-BFA2-1926F590873D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{781232EA-EB55-49AE-BFA2-1926F590873D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{781232EA-EB55-49AE-BFA2-1926F590873D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{781232EA-EB55-49AE-BFA2-1926F590873D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{781232EA-EB55-49AE-BFA2-1926F590873D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D7CF3BB3-3B60-4C8D-B0F9-655F7A5346C2}
EndGlobalSection
EndGlobal
151 changes: 151 additions & 0 deletions TwinCAT.JsonExtension/AdsClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using TwinCAT.Ads;
using TwinCAT.TypeSystem;

namespace TwinCAT.JsonExtension
{
public static class AdsClientExtensions
{
public static Task WriteAsync<T>(this TcAdsClient client, string variablePath, T value)
{
return Task.Run(() =>
{
var symbolInfo = (ITcAdsSymbol5)client.ReadSymbolInfo(variablePath);
var targetType = symbolInfo.DataType.ManagedType;
var targetValue = targetType != null ? Convert.ChangeType(value, targetType) : value;
client.WriteSymbol(symbolInfo, targetValue);
});
}

public static Task<T> ReadAsync<T>(this TcAdsClient client, string variablePath)
{
return Task.Run(() =>
{
var symbolInfo = (ITcAdsSymbol5)client.ReadSymbolInfo(variablePath);
var obj = client.ReadSymbol(symbolInfo);
return (T) Convert.ChangeType(obj, typeof(T));
});
}

public static Task WriteJson(this TcAdsClient client, string variablePath, JObject obj)
{
return WriteRecursive(client, variablePath, obj, string.Empty);
}

private static async Task WriteRecursive(this TcAdsClient client, string variablePath, JObject parent, string jsonName)
{
var symbolInfo = (ITcAdsSymbol5)client.ReadSymbolInfo(variablePath);
var dataType = symbolInfo.DataType;
{
if (dataType.Category == DataTypeCategory.Array)
{
var array = parent.SelectToken(jsonName) as JArray;
var elementCount = array.Count < dataType.Dimensions.ElementCount ? array.Count : dataType.Dimensions.ElementCount;
for (int i = 0; i < elementCount; i++)
{
if (dataType.BaseType.ManagedType != null)
await WriteAsync(client, variablePath + $"[{i + dataType.Dimensions.LowerBounds.First()}]", array[i]);
else
{
await WriteRecursive(client, variablePath + $"[{i + dataType.Dimensions.LowerBounds.First()}]", parent, jsonName + $"[{i}]");
}
}
}
else if (dataType.ManagedType == null)
{
if (dataType.SubItems.Any())
{
foreach (var subItem in dataType.SubItems)
{
if (HasJsonName(subItem))
{
await WriteRecursive(client, variablePath + "." + subItem.SubItemName, parent, string.IsNullOrEmpty(jsonName) ? GetJsonName(subItem) : jsonName + "." + GetJsonName(subItem));
}
}
}
}
else
{
await WriteAsync(client, symbolInfo.Name, parent.SelectToken(jsonName));
}
}

}
public static Task<JObject> ReadJson(this TcAdsClient client, string variablePath)
{
return Task.Run(() => ReadRecursive(client, variablePath, new JObject(), GetVaribleNameFromFullPath(variablePath)));
}

private static JObject ReadRecursive(TcAdsClient client, string variablePath, JObject parent, string jsonName, bool isChild = false)
{
var symbolInfo = (ITcAdsSymbol5)client.ReadSymbolInfo(variablePath);
var dataType = symbolInfo.DataType;
{
if (dataType.Category == DataTypeCategory.Array)
{
if (dataType.BaseType.ManagedType != null)
{
var obj = client.ReadSymbol(symbolInfo);
parent.Add(jsonName, new JArray(obj));
}
else
{
var array = new JArray();
for (int i = dataType.Dimensions.LowerBounds.First(); i <= dataType.Dimensions.UpperBounds.First(); i++)
{
var child = new JObject();
ReadRecursive(client, variablePath + $"[{i}]", child, jsonName, false);
array.Add(child);
}
parent.Add(jsonName, array);

}
}
else if (dataType.ManagedType == null)
{
if (dataType.SubItems.Any())
{
var child = new JObject();
foreach (var subItem in dataType.SubItems)
{
if (HasJsonName(subItem))
{
ReadRecursive(client, variablePath + "." + subItem.SubItemName, isChild ? child : parent, GetJsonName(subItem), true);
}
}
if (isChild)
parent.Add(jsonName, child);
}
}
else
{
var obj = client.ReadSymbol(symbolInfo);
parent.Add(jsonName, new JValue(obj));
}
}

return parent;
}

private static string GetVaribleNameFromFullPath(this string variablePath)
{
return variablePath.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries).Last();
}


private static string GetJsonName(ITcAdsSubItem dataType)
{
var jsonName = dataType.Attributes.FirstOrDefault(attribute => attribute.Name.Equals("json", StringComparison.InvariantCultureIgnoreCase))?.Value;
return string.IsNullOrEmpty(jsonName) ? GetVaribleNameFromFullPath(dataType.SubItemName) : jsonName;
}

private static bool HasJsonName(this ITcAdsSubItem subItem)
{
return subItem.Attributes.Any(attribute => attribute.Name.Equals("json", StringComparison.InvariantCultureIgnoreCase));
}

}
}
22 changes: 22 additions & 0 deletions TwinCAT.JsonExtension/TwinCAT.JsonExtension.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net45</TargetFramework>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Federico Barresi</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/fbarresi/TwinCAT.JsonExtension</PackageProjectUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/fbarresi/TwinCAT.JsonExtension/master/LICENSE</PackageLicenseUrl>
<PackageTags>TwinCAT TwinCAT.Ads Json Json.net Beckhoff</PackageTags>
<Company />
<Version>1.0.0</Version>
<PackageIconUrl>https://raw.githubusercontent.com/fbarresi/TwinCAT.JsonExtension/master/doc/images/logo.jpg</PackageIconUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Beckhoff.TwinCAT.Ads" Version="4.3.7" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
Binary file added doc/images/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f214b4f

Please sign in to comment.