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 System.Collections;
using System.Diagnostics;
using System.ComponentModel;
namespace MewtocolNet {
/// <summary>
/// The PLC com interface class
/// </summary>
public partial class MewtocolInterface {
public partial class MewtocolInterface : INotifyPropertyChanged {
/// <summary>
/// Gets triggered when the PLC connection was established
/// </summary>
public event Action<PLCInfo> Connected;
/// <summary>
/// Gets triggered when the PLC connection was closed or lost
/// </summary>
public event Action Disconnected;
/// <summary>
/// Gets triggered when a registered data register changes its value
/// </summary>
public event Action<Register> RegisterChanged;
/// <summary>
/// Gets triggered when a property of the interface changes
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
private bool isConnected;
/// <summary>
/// The current connection state of the interface
/// </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>
/// Generic information about the connected PLC
/// </summary>
public PLCInfo PlcInfo { get; private set; }
public PLCInfo PlcInfo {
get => plcInfo;
private set {
plcInfo = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PlcInfo)));
}
}
/// <summary>
/// The registered data registers of the PLC
@@ -61,6 +86,19 @@ namespace MewtocolNet {
/// </summary>
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>();
#region Initialization
@@ -90,7 +128,8 @@ namespace MewtocolNet {
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} " +
$"{(o.Name != null ? $"({o.Name}) " : "")}" +
@@ -144,6 +183,7 @@ namespace MewtocolNet {
if (OnFailed != null) {
OnFailed();
Disconnected?.Invoke();
Logger.Log("Initial connection failed", LogLevel.Info, this);
}
@@ -190,7 +230,7 @@ namespace MewtocolNet {
string propName = prop.Name;
foreach (var attr in attributes) {
if(attr is RegisterAttribute cAttribute) {
if (attr is RegisterAttribute cAttribute) {
if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex == -1) {
if (cAttribute.SpecialAddress == SpecialAddress.None) {
@@ -227,7 +267,7 @@ namespace MewtocolNet {
//read number as bit array
if (prop.PropertyType == typeof(BitArray)) {
if(cAttribute.BitCount == BitCount.B16) {
if (cAttribute.BitCount == BitCount.B16) {
AddRegister<short>(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
} else {
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
if (reg is NRegister<short> shortReg) {
@@ -360,7 +400,7 @@ namespace MewtocolNet {
collection.OnInterfaceLinked(this);
Connected += (i) => {
if(collection != null)
if (collection != null)
collection.OnInterfaceLinkedAndOnline(this);
};
@@ -473,7 +513,7 @@ namespace MewtocolNet {
string response = null;
if(ContinousReaderRunning) {
if (ContinousReaderRunning) {
//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 {
Success = false,
Error = "0000",
@@ -559,9 +599,13 @@ namespace MewtocolNet {
return response.ToString();
}
} catch(Exception) {
} catch (Exception) {
if (IsConnected) {
IsConnected = false;
Disconnected?.Invoke();
}
KillPoller();
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
return null;

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MewtocolNet</PackageId>
<Version>0.3.2</Version>
<Version>0.3.3</Version>
<Authors>Felix Weiss</Authors>
<Company>Womed</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>