Skip to content

Commit

Permalink
fixed crash on read/write after manual disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Jun 12, 2021
1 parent fd6b239 commit 27c9ef7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
11 changes: 1 addition & 10 deletions TwinCatAdsTool.Gui/ViewModels/ExploreViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public ExploreViewModel(IClientService clientService,
public ReactiveCommand<SymbolObservationViewModel, Unit> CmdDelete { get; set; }

public ReactiveCommand<SymbolObservationViewModel, Unit> CmdRemoveGraph { get; set; }
public ReactiveCommand<SymbolObservationViewModel, Unit> CmdSubmit { get; set; }

public GraphViewModel GraphViewModel { get; set; }

Expand Down Expand Up @@ -207,15 +206,12 @@ private void AssignCommands(IObservable<bool> connected)
// Setup the command for the enter key on the textbox
TextBoxEnterCommand = new ReactiveRelayCommand(obj => { });

AddObserverCmd = ReactiveCommand.CreateFromTask<ISymbol, Unit>(RegisterSymbolObserver)
AddObserverCmd = ReactiveCommand.CreateFromTask<ISymbol, Unit>(RegisterSymbolObserver, canExecute: connected)
.AddDisposableTo(Disposables);

CmdDelete = ReactiveCommand.CreateFromTask<SymbolObservationViewModel, Unit>(DeleteSymbolObserver)
.AddDisposableTo(Disposables);

CmdSubmit = ReactiveCommand.CreateFromTask<SymbolObservationViewModel, Unit>(SubmitSymbol)
.AddDisposableTo(Disposables);

CmdAddGraph = ReactiveCommand.CreateFromTask<SymbolObservationViewModel, Unit>(AddGraph)
.AddDisposableTo(Disposables);

Expand Down Expand Up @@ -312,11 +308,6 @@ private Task<Unit> RemoveGraph(SymbolObservationViewModel symbolObservationViewM
return Task.FromResult(Unit.Default);
}

private Task<Unit> SubmitSymbol(SymbolObservationViewModel model)
{
return Task.FromResult(Unit.Default);
}

private void UpdateTree(ISymbolCollection<ISymbol> symbolList)
{
try
Expand Down
33 changes: 21 additions & 12 deletions TwinCatAdsTool.Gui/ViewModels/SymbolObservationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using System.Windows;
using ReactiveUI;
using TwinCAT;
using TwinCAT.Ads.Reactive;
using TwinCAT.TypeSystem;
using TwinCatAdsTool.Gui.Properties;
Expand All @@ -32,7 +33,7 @@ protected SymbolObservationViewModel(ISymbol model, IClientService clientService
public string Name { get; set; }
public string FullName { get; set; }

public bool SuportsGraph => GetSupportsGraph();
public bool SupportsGraph => GetSupportsGraph();
public bool SupportsSubmit => GetSupportsSubmit();

public object Value => helper.Value;
Expand All @@ -41,21 +42,29 @@ public override void Init()
{
Name = Model.InstanceName;
FullName = Model.InstancePath;
var readSymbolInfo = ClientService.Client.ReadSymbol(Model.InstancePath);
var initialValue = ClientService.Client.ReadValue(readSymbolInfo);
var observable = ((IValueSymbol) Model).WhenValueChanged().StartWith(initialValue);
try
{
var readSymbolInfo = ClientService.Client.ReadSymbol(Model.InstancePath);
var initialValue = ClientService.Client.ReadValue(readSymbolInfo);
var observable = ((IValueSymbol) Model).WhenValueChanged().StartWith(initialValue);

var obsLogger = LoggerFactory.GetObserverLogger();
var obsLogger = LoggerFactory.GetObserverLogger();

observable
.Do(value => obsLogger.Debug($"{FullName} value changed to: '{value.ToString()}'"))
.Subscribe()
.AddDisposableTo(Disposables);
observable
.Do(value => obsLogger.Debug($"{FullName} value changed to: '{value.ToString()}'"))
.Subscribe()
.AddDisposableTo(Disposables);

helper = observable.ToProperty(this, m => m.Value);
helper = observable.ToProperty(this, m => m.Value);

CmdSubmit = ReactiveCommand.CreateFromTask(_ => SubmitSymbol())
.AddDisposableTo(Disposables);
CmdSubmit = ReactiveCommand.CreateFromTask(_ => SubmitSymbol(),
ClientService.ConnectionState.Select(s => s == ConnectionState.Connected))
.AddDisposableTo(Disposables);
}
catch (Exception e)
{
Logger.Error($"Error while initializing vm for {Model.InstanceName}. Control will not be usable.", e);
}
}

protected abstract bool GetSupportsGraph();
Expand Down
4 changes: 2 additions & 2 deletions TwinCatAdsTool.Gui/Views/ExploreView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
CommandParameter="{Binding}"
ToolTip="{x:Static properties:Resources.AddGraph}"
Style="{StaticResource MaterialDesignFloatingActionMiniButton}"
Visibility="{Binding SuportsGraph, Converter={StaticResource VisibileIfTrue}}">
Visibility="{Binding SupportsGraph, Converter={StaticResource VisibileIfTrue}}">
<materialDesign:PackIcon Kind="ChartLine" />
</Button>
</DataTemplate>
Expand All @@ -247,7 +247,7 @@
CommandParameter="{Binding}"
ToolTip="{x:Static properties:Resources.RemoveGraph}"
Style="{StaticResource MaterialDesignFloatingActionMiniButton}"
Visibility="{Binding SuportsGraph, Converter={StaticResource VisibileIfTrue}}">
Visibility="{Binding SupportsGraph, Converter={StaticResource VisibileIfTrue}}">
<materialDesign:PackIcon Kind="TrayRemove" />
</Button>
</DataTemplate>
Expand Down

0 comments on commit 27c9ef7

Please sign in to comment.