Skip to content

Commit

Permalink
Added support for non global persistent variables
Browse files Browse the repository at this point in the history
- removed flat symbols checkbox because buggy
- minor fixes and better logging into RestoreViewModel
- related to #17
  • Loading branch information
fbarresi committed Apr 26, 2021
1 parent 3fa7911 commit c4dbaaa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
17 changes: 3 additions & 14 deletions TwinCatAdsTool.Gui/ViewModels/BackupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class BackupViewModel : ViewModelBase
private readonly Subject<JObject> variableSubject = new Subject<JObject>();
private string backupText;
private ObservableAsPropertyHelper<string> currentTaskHelper;
private bool useFlatSymbolList;

public BackupViewModel(IClientService clientService, IPersistentVariableService persistentVariableService)
{
Expand All @@ -45,17 +44,6 @@ public string BackupText
public ReactiveCommand<Unit, Unit> Read { get; set; }
public ReactiveCommand<Unit, Unit> Save { get; set; }

public bool UseFlatSymbolList
{
get => useFlatSymbolList;
set
{
if (value == useFlatSymbolList) return;
useFlatSymbolList = value;
raisePropertyChanged();
}
}

public override void Init()
{
variableSubject
Expand All @@ -79,8 +67,9 @@ public override void Init()

private async Task<Unit> ReadVariables()
{
var persistentVariables = await persistentVariableService.ReadGlobalPersistentVariables(clientService.Client,
UseFlatSymbolList ? clientService.FlatViewSymbols : clientService.TreeViewSymbols);
var persistentVariables = await persistentVariableService.ReadGlobalPersistentVariables(
clientService.Client,
clientService.TreeViewSymbols);
variableSubject.OnNext(persistentVariables);
Logger.Debug(Resources.ReadPersistentVariables);

Expand Down
43 changes: 28 additions & 15 deletions TwinCatAdsTool.Gui/ViewModels/RestoreViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,39 @@ private async Task<Unit> WriteVariables()
{
foreach (var variable in DisplayVariables)
{
try
var jObject = await JObject.LoadAsync(new JsonTextReader(new StringReader(variable.Json)));

foreach (var p in jObject.Properties())
{
var jObject = JObject.Load(new JsonTextReader(new StringReader(variable.Json)));
foreach (var p in jObject.Properties())
try
{
Logger.Debug($"Restoring variable '{variable.Name}.{p.Name}' from backup...");
if(p.Value is JObject)
await clientService.Client.WriteJson(variable.Name + "." + p.Name, (JObject) p.Value, force: true);
else if(p.Value is JArray)
await clientService.Client.WriteJson(variable.Name + "." + p.Name, (JArray) p.Value, force: true);
else if (p.Value is JValue)
await clientService.Client.WriteAsync(variable.Name + "." + p.Name, p.Value);
else
Logger.Error($"Unable to write variable '{variable.Name}.{p.Name}' from backup: no type case match!");
switch (p.Value)
{
case JObject value:
await clientService.Client.WriteJson(variable.Name + "." + p.Name, value,
force: true);
break;
case JArray array:
await clientService.Client.WriteJson(variable.Name + "." + p.Name, array,
force: true);
break;
case JValue:
await clientService.Client.WriteAsync(variable.Name + "." + p.Name, p.Value);
break;
default:
Logger.Error(
$"Unable to write variable '{variable.Name}.{p.Name} := {p.Value.ToString(Formatting.None)}' from backup: no type case match!");
break;
}
}
catch (Exception ex)
{
Logger.Error($"Unable to write variable '{variable.Name}.{p.Name} := {p.Value.ToString(Formatting.None)}' from backup!", ex);
MessageBox.Show($"{variable.Name}.{p.Name} := {p.Value.ToString(Formatting.None)} \n {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"{ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

}
}

Expand Down
1 change: 0 additions & 1 deletion TwinCatAdsTool.Gui/Views/BackupView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
>
</Button>

<CheckBox IsChecked="{Binding UseFlatSymbolList, UpdateSourceTrigger=PropertyChanged}" Margin="16" Content="{x:Static properties:Resources.UseFlatSymbolList}"/>
<Label Margin="24,16,0,16" Content="{Binding CurrentTask}"/>


Expand Down
12 changes: 9 additions & 3 deletions TwinCatAdsTool.Logic/Services/PersistentVariableService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ public async Task<JObject> ReadGlobalPersistentVariables(AdsClient client, IInst
{
if (client.IsConnected)
{
var iterator = new SymbolIterator(symbols, s => s.IsPersistent && s.InstancePath.Split('.').Length == 2 && !s.InstancePath.Contains("["));
var iterator = new SymbolIterator(symbols,
s => s.IsPersistent && s.InstancePath.Split('.').Length >= 2 &&
!s.InstancePath.Contains("["))
;

var peristentSymbols = iterator.Where(s => s.Parent != null ? !iterator.Contains(s.Parent) : true);

var variables = new Dictionary<string, List<JObject>>();
foreach (var symbol in iterator)
foreach (var symbol in peristentSymbols)
{
var splitPath = symbol.InstancePath.Split('.');
var globalName = splitPath.First();
var localName = splitPath.Last();
var globalName = symbol.InstancePath.Replace($".{localName}", string.Empty);

if (!variables.ContainsKey(globalName))
{
variables.Add(globalName, new List<JObject>());
Expand Down

0 comments on commit c4dbaaa

Please sign in to comment.