1using System.Collections.ObjectModel;
2using System.ComponentModel;
3using System.Globalization;
4using System.Runtime.CompilerServices;
5using System.Windows.Input;
6using Dreamine.PLC.Abstractions.Clients;
7using Dreamine.PLC.Abstractions.Connections;
8using Dreamine.PLC.Core.Clients;
9using Dreamine.PLC.Core.Devices;
10using Dreamine.MVVM.ViewModels;
106 private PlcConnectionState
_state = PlcConnectionState.Disconnected;
117 : this(new InMemoryPlcClient(),
"InMemory PLC")
155 _client = client ??
throw new ArgumentNullException(nameof(client));
187 public ObservableCollection<PlcOperationLogItem>
Logs {
get; } = [];
340 private set => SetField(ref
_state, value);
397 public void SetClient(IPlcClient client,
string channelName)
399 ArgumentNullException.ThrowIfNull(client);
406 AddLog(
"Client",
string.Empty,
string.Empty,
true, $
"Client changed: {channelName}");
457 public void AppendLog(
string operation,
string address,
string values,
bool isSuccess,
string message)
459 AddLog(operation, address, values, isSuccess, message);
481 await
_client.DisposeAsync().ConfigureAwait(
false);
502 var result = await
_client.ConnectAsync().ConfigureAwait(
true);
503 StatusMessage = result.IsSuccess ?
"Connected." : result.Message ??
"Connect failed.";
525 var result = await
_client.DisconnectAsync().ConfigureAwait(
true);
526 StatusMessage = result.IsSuccess ?
"Disconnected." : result.Message ??
"Disconnect failed.";
553 var result = await
_client.ReadBitsAsync(address, count).ConfigureAwait(
true);
554 var values = result.Value is
null ? string.Empty :
string.Join(
',', result.Value.Select(x => x ?
"1" :
"0"));
555 StatusMessage = result.IsSuccess ? $
"Read bits: {values}" : result.Message ??
"Read bits failed.";
582 var result = await
_client.ReadWordsAsync(address, count).ConfigureAwait(
true);
583 var values = result.Value is
null ? string.Empty :
string.Join(
',', result.Value.Select(x => x.ToString(CultureInfo.InvariantCulture)));
584 StatusMessage = result.IsSuccess ? $
"Read words: {values}" : result.Message ??
"Read words failed.";
616 var result = await
_client.WriteBitsAsync(address, values).ConfigureAwait(
true);
617 var valuesText =
string.Join(
',', values.Select(x => x ?
"1" :
"0"));
618 StatusMessage = result.IsSuccess ? $
"Write bits: {valuesText}" : result.Message ??
"Write bits failed.";
650 var result = await
_client.WriteWordsAsync(address, values).ConfigureAwait(
true);
651 var valuesText =
string.Join(
',', values.Select(x => x.ToString(CultureInfo.InvariantCulture)));
652 StatusMessage = result.IsSuccess ? $
"Write words: {valuesText}" : result.Message ??
"Write words failed.";
712 if (!
int.TryParse(
CountText, NumberStyles.Integer, CultureInfo.InvariantCulture, out count) || count <= 0)
749 if (!result.IsSuccess)
757 address = result.Value;
798 values = text.Split(
',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
801 return values.Length > 0;
848 values = text.Split(
',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
849 .Select(x =>
short.Parse(x, CultureInfo.InvariantCulture))
851 return values.Length > 0;
899 _ when
bool.TryParse(text, out var value) => value,
900 _ =>
throw new FormatException($
"Invalid bit value: {text}")
952 private void AddLog(
string operation,
string address,
string values,
bool isSuccess,
string message)
957 Operation = operation,
960 IsSuccess = isSuccess,
964 while (
Logs.Count > 500)
1047 private bool SetField<T>(ref T field, T value, [CallerMemberName]
string? propertyName =
null)
1049 if (EqualityComparer<T>.Default.Equals(field, value))
1077 PropertyChanged?.Invoke(
this,
new PropertyChangedEventArgs(propertyName));
void OnPropertyChanged([CallerMemberName] string? propertyName=null)
void AppendLog(string operation, string address, string values, bool isSuccess, string message)
ICommand WriteWordsCommand
ICommand WriteBitsCommand
ObservableCollection< PlcOperationLogItem > Logs
static bool ParseBit(string text)
bool TryCreateReadRequest(out Abstractions.Devices.PlcAddress address, out int count)
bool SetField< T >(ref T field, T value, [CallerMemberName] string? propertyName=null)
ICommand ReadWordsCommand
bool TryParseAddress(out Abstractions.Devices.PlcAddress address)
async Task ReadWordsAsync()
bool TryParseBits(string text, out bool[] values)
void OnClientStateChanged(object? sender, PlcConnectionState e)
async Task DisconnectAsync()
async Task WriteWordsAsync()
void SetClient(IPlcClient client, string channelName)
async Task ConnectAsync()
async Task WriteBitsAsync()
void AddLog(string operation, string address, string values, bool isSuccess, string message)
readonly DefaultPlcAddressParser _addressParser
ICommand DisconnectCommand
PlcMonitorViewModel(IPlcClient client, string channelName="PLC")
PlcConnectionState _state
PropertyChangedEventHandler? PropertyChanged
async Task ReadBitsAsync()
bool TryParseWords(string text, out short[] values)
async ValueTask DisposeAsync()