diff --git a/Sharp7.Monitor/CustomStyles.cs b/Sharp7.Monitor/CustomStyles.cs new file mode 100644 index 0000000..a92c295 --- /dev/null +++ b/Sharp7.Monitor/CustomStyles.cs @@ -0,0 +1,7 @@ +using Spectre.Console; + +public static class CustomStyles +{ + public static Style Error { get; } = new Style(foreground: Color.Red); + public static Style Note { get; } = new(foreground: Color.DarkSlateGray1); +} \ No newline at end of file diff --git a/Sharp7.Monitor/ReadPlcCommand.cs b/Sharp7.Monitor/ReadPlcCommand.cs index af52cda..f420076 100644 --- a/Sharp7.Monitor/ReadPlcCommand.cs +++ b/Sharp7.Monitor/ReadPlcCommand.cs @@ -1,12 +1,10 @@ using System.ComponentModel; -using System.Reactive.Disposables; using System.Reactive.Linq; using System.Reactive.Threading.Tasks; using JetBrains.Annotations; using Sharp7.Read; using Sharp7.Rx; using Sharp7.Rx.Enums; -using Sharp7.Rx.Interfaces; using Spectre.Console; using Spectre.Console.Cli; using Spectre.Console.Rendering; @@ -73,6 +71,7 @@ internal sealed class ReadPlcCommand : AsyncCommand token.ThrowIfCancellationRequested(); + using var variableContainer = VariableContainer.Initialize(plc, settings.Variables); // Create a table @@ -138,68 +137,4 @@ internal sealed class ReadPlcCommand : AsyncCommand return ValidationResult.Success(); } } -} - -public static class CustomStyles -{ - public static Style Error { get; } = new Style(foreground: Color.Red); - public static Style Note { get; } = new(foreground: Color.DarkSlateGray1); -} - -public class VariableContainer : IDisposable -{ - private readonly IDisposable subscriptions; - - private VariableContainer(IReadOnlyList variableRecords, IDisposable subscriptions) - { - this.subscriptions = subscriptions; - VariableRecords = variableRecords; - } - - public IReadOnlyList VariableRecords { get; } - - public void Dispose() - { - subscriptions.Dispose(); - } - - public static VariableContainer Initialize(IPlc plc, IReadOnlyList variables) - { - var records = variables - .Select((v, i) => new VariableRecord - { - Address = v, - RowIdx = i, - Value = new Text("init", CustomStyles.Note) - }) - .ToList(); - - var disposables = new CompositeDisposable(); - foreach (var rec in records) - { - try - { - var disp = - plc.CreateNotification(rec.Address, TransmissionMode.OnChange) - .Subscribe( - data => rec.Value = data, - ex => rec.Value = new Text(ex.Message, CustomStyles.Error) - ); - disposables.Add(disp); - } - catch (Exception e) - { - rec.Value = new Text(e.Message, CustomStyles.Error); - } - } - - return new VariableContainer(records, disposables); - } -} - -public class VariableRecord -{ - public required string Address { get; init; } - public required int RowIdx { get; init; } - public object Value { get; set; } } \ No newline at end of file diff --git a/Sharp7.Monitor/VariableContainer.cs b/Sharp7.Monitor/VariableContainer.cs new file mode 100644 index 0000000..35a7f6d --- /dev/null +++ b/Sharp7.Monitor/VariableContainer.cs @@ -0,0 +1,55 @@ +using System.Reactive.Disposables; +using Sharp7.Rx.Enums; +using Sharp7.Rx.Interfaces; +using Spectre.Console; + +public class VariableContainer : IDisposable +{ + private readonly IDisposable subscriptions; + + private VariableContainer(IReadOnlyList variableRecords, IDisposable subscriptions) + { + this.subscriptions = subscriptions; + VariableRecords = variableRecords; + } + + public IReadOnlyList VariableRecords { get; } + + public void Dispose() + { + subscriptions.Dispose(); + } + + public static VariableContainer Initialize(IPlc plc, IReadOnlyList variables) + { + var records = variables + .Select((v, i) => new VariableRecord + { + Address = v, + RowIdx = i, + Value = new Text("init", CustomStyles.Note) + }) + .ToList(); + + var disposables = new CompositeDisposable(); + foreach (var rec in records) + { + try + { + var disp = + plc.CreateNotification(rec.Address, TransmissionMode.OnChange) + .Subscribe( + data => rec.Value = data, + ex => rec.Value = new Text(ex.Message, CustomStyles.Error) + ); + disposables.Add(disp); + } + catch (Exception e) + { + rec.Value = new Text(e.Message, CustomStyles.Error); + } + } + + return new VariableContainer(records, disposables); + } +} \ No newline at end of file diff --git a/Sharp7.Monitor/VariableRecord.cs b/Sharp7.Monitor/VariableRecord.cs new file mode 100644 index 0000000..70a8507 --- /dev/null +++ b/Sharp7.Monitor/VariableRecord.cs @@ -0,0 +1,6 @@ +public class VariableRecord +{ + public required string Address { get; init; } + public required int RowIdx { get; init; } + public object Value { get; set; } +} \ No newline at end of file