mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
Implemented INotifyPropertyChanged on MewtocolInterface
- added event for disconnect - counted up version number
This commit is contained in:
@@ -11,33 +11,58 @@ using MewtocolNet.RegisterAttributes;
|
|||||||
using MewtocolNet.Logging;
|
using MewtocolNet.Logging;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
namespace MewtocolNet {
|
namespace MewtocolNet {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The PLC com interface class
|
/// The PLC com interface class
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class MewtocolInterface {
|
public partial class MewtocolInterface : INotifyPropertyChanged {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets triggered when the PLC connection was established
|
/// Gets triggered when the PLC connection was established
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event Action<PLCInfo> Connected;
|
public event Action<PLCInfo> Connected;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets triggered when the PLC connection was closed or lost
|
||||||
|
/// </summary>
|
||||||
|
public event Action Disconnected;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets triggered when a registered data register changes its value
|
/// Gets triggered when a registered data register changes its value
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event Action<Register> RegisterChanged;
|
public event Action<Register> RegisterChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets triggered when a property of the interface changes
|
||||||
|
/// </summary>
|
||||||
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
|
private bool isConnected;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The current connection state of the interface
|
/// The current connection state of the interface
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsConnected { get; private set; }
|
public bool IsConnected {
|
||||||
|
get => isConnected;
|
||||||
|
private set {
|
||||||
|
isConnected = value;
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsConnected)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PLCInfo plcInfo;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generic information about the connected PLC
|
/// Generic information about the connected PLC
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public PLCInfo PlcInfo { get; private set; }
|
public PLCInfo PlcInfo {
|
||||||
|
get => plcInfo;
|
||||||
|
private set {
|
||||||
|
plcInfo = value;
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PlcInfo)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The registered data registers of the PLC
|
/// The registered data registers of the PLC
|
||||||
@@ -61,6 +86,19 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int StationNumber => stationNumber;
|
public int StationNumber => stationNumber;
|
||||||
|
|
||||||
|
private int cycleTimeMs;
|
||||||
|
/// <summary>
|
||||||
|
/// The duration of the last message cycle
|
||||||
|
/// </summary>
|
||||||
|
public int CycleTimeMs {
|
||||||
|
get { return cycleTimeMs; }
|
||||||
|
set {
|
||||||
|
cycleTimeMs = value;
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CycleTimeMs)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
internal List<Task> PriorityTasks { get; set; } = new List<Task>();
|
internal List<Task> PriorityTasks { get; set; } = new List<Task>();
|
||||||
|
|
||||||
#region Initialization
|
#region Initialization
|
||||||
@@ -90,7 +128,8 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
RegisterChanged += (o) => {
|
RegisterChanged += (o) => {
|
||||||
|
|
||||||
string address = $"{o.GetRegisterString()}{o.MemoryAdress}".PadRight(5, (char)32); ;
|
string address = $"{o.GetRegisterString()}{o.MemoryAdress}".PadRight(5, (char)32);
|
||||||
|
;
|
||||||
|
|
||||||
Logger.Log($"{address} " +
|
Logger.Log($"{address} " +
|
||||||
$"{(o.Name != null ? $"({o.Name}) " : "")}" +
|
$"{(o.Name != null ? $"({o.Name}) " : "")}" +
|
||||||
@@ -144,6 +183,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
if (OnFailed != null) {
|
if (OnFailed != null) {
|
||||||
OnFailed();
|
OnFailed();
|
||||||
|
Disconnected?.Invoke();
|
||||||
Logger.Log("Initial connection failed", LogLevel.Info, this);
|
Logger.Log("Initial connection failed", LogLevel.Info, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -561,7 +601,11 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
} catch (Exception) {
|
} catch (Exception) {
|
||||||
|
|
||||||
|
if (IsConnected) {
|
||||||
IsConnected = false;
|
IsConnected = false;
|
||||||
|
Disconnected?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
KillPoller();
|
KillPoller();
|
||||||
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<PackageId>MewtocolNet</PackageId>
|
<PackageId>MewtocolNet</PackageId>
|
||||||
<Version>0.3.2</Version>
|
<Version>0.3.3</Version>
|
||||||
<Authors>Felix Weiss</Authors>
|
<Authors>Felix Weiss</Authors>
|
||||||
<Company>Womed</Company>
|
<Company>Womed</Company>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
|||||||
Reference in New Issue
Block a user