Implemented INotifyPropertyChanged on MewtocolInterface

- added event for disconnect
- counted up version number
This commit is contained in:
Felix Weiß
2022-06-22 09:21:50 +02:00
parent 0afa146712
commit 14659ffaad
2 changed files with 66 additions and 22 deletions

View File

@@ -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);
} }
@@ -190,7 +230,7 @@ namespace MewtocolNet {
string propName = prop.Name; string propName = prop.Name;
foreach (var attr in attributes) { foreach (var attr in attributes) {
if(attr is RegisterAttribute cAttribute) { if (attr is RegisterAttribute cAttribute) {
if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex == -1) { if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex == -1) {
if (cAttribute.SpecialAddress == SpecialAddress.None) { if (cAttribute.SpecialAddress == SpecialAddress.None) {
@@ -227,7 +267,7 @@ namespace MewtocolNet {
//read number as bit array //read number as bit array
if (prop.PropertyType == typeof(BitArray)) { if (prop.PropertyType == typeof(BitArray)) {
if(cAttribute.BitCount == BitCount.B16) { if (cAttribute.BitCount == BitCount.B16) {
AddRegister<short>(cAttribute.MemoryArea, _name: propName, _isBitwise: true); AddRegister<short>(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
} else { } else {
AddRegister<int>(cAttribute.MemoryArea, _name: propName, _isBitwise: true); AddRegister<int>(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
@@ -329,7 +369,7 @@ namespace MewtocolNet {
} }
} else if(foundToUpdate.PropertyType == typeof(BitArray)) { } else if (foundToUpdate.PropertyType == typeof(BitArray)) {
//setting back bit registers //setting back bit registers
if (reg is NRegister<short> shortReg) { if (reg is NRegister<short> shortReg) {
@@ -360,7 +400,7 @@ namespace MewtocolNet {
collection.OnInterfaceLinked(this); collection.OnInterfaceLinked(this);
Connected += (i) => { Connected += (i) => {
if(collection != null) if (collection != null)
collection.OnInterfaceLinkedAndOnline(this); collection.OnInterfaceLinkedAndOnline(this);
}; };
@@ -473,7 +513,7 @@ namespace MewtocolNet {
string response = null; string response = null;
if(ContinousReaderRunning) { if (ContinousReaderRunning) {
//if the poller is active then add all messages to a qeueue //if the poller is active then add all messages to a qeueue
@@ -492,7 +532,7 @@ namespace MewtocolNet {
} }
if(response == null) { if (response == null) {
return new CommandResult { return new CommandResult {
Success = false, Success = false,
Error = "0000", Error = "0000",
@@ -559,9 +599,13 @@ namespace MewtocolNet {
return response.ToString(); return response.ToString();
} }
} catch(Exception) { } catch (Exception) {
if (IsConnected) {
IsConnected = false;
Disconnected?.Invoke();
}
IsConnected = false;
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;

View File

@@ -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>