diff --git a/Common/CommonConsole/CommonConsole.csproj b/Common/CommonConsole/CommonConsole.csproj index 2526aa83..303f1777 100644 --- a/Common/CommonConsole/CommonConsole.csproj +++ b/Common/CommonConsole/CommonConsole.csproj @@ -36,58 +36,26 @@ false - - False - ..\packages\Newtonsoft.Json.13.0.3\lib\net40\Newtonsoft.Json.dll - ..\..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll - - - - - - {D0C8D887-ED52-40AB-A069-90BCE0E801E2} - CordonelPreadjustmentUi - - - {B23BFAF9-08AE-4311-8C08-FB2BB49EA2AD} - Common.Hardware.Interfaces.Ports.SIRT - - - {9AE071C0-DB12-4E09-A3C7-43F6DCD518D6} - Common.Hardware.Interfaces.Ports - + + + {9D89FA2A-1217-4CE9-B217-E7602B974EA9} Common.Hardware.WaterMeter.OmniPlus.Features - - {6480CC2A-AB54-4A26-8413-D5141431C080} - Common.Hardware.WaterMeter.OmniPlus - - - {F8D84B9A-0FCF-4083-B067-8C39B7314152} - OmniPlus - - - {A33E3C6D-EAAE-4A20-A0EE-F9E6922FD029} - SoftwareAccessHelper - - - - \ No newline at end of file diff --git a/Common/CommonConsole/Program.cs b/Common/CommonConsole/Program.cs index 863499b8..83106c67 100644 --- a/Common/CommonConsole/Program.cs +++ b/Common/CommonConsole/Program.cs @@ -1,32 +1,35 @@ -namespace CommonConsole +using Common.Hardware.WaterMeter.OmniPlus.Features; +using System; + +namespace CommonConsole { - using Common.Hardware.Interfaces.Ports; - using Common.Hardware.Interfaces.Ports.SIRT; - - using System; - public static class Program { public static void Main() { - var broadcaster = new SirtBroadcaster(new SerialPortSettings + var systemTime = new SystemTime { - BaudRate = SerialPortBaudRate.X115200, - DataBits = SerialPortDataBits.X8, - Parity = SerialPortParity.None, - PortName = "COM6", - StopBits = SerialPortStopBits.One, - RWTimeout = -1 - }); - broadcaster.Open(); - - Console.WriteLine($"READY: {broadcaster.Ready}"); - Console.WriteLine($"SIRT-ID: {broadcaster.SirtId}"); - Console.WriteLine($"FREQUENCY: {broadcaster.Frequency:0 MHz}"); - - Console.ReadKey(); - - broadcaster.Close(); + DateTime = DateTime.Now + }; + Console.WriteLine(systemTime.SecondsSince); + var initilalSystemTime = new SystemTime + { + SecondsSince = BitConverter.ToUInt32(new byte[] { 0x69, 0x65, 0x08, 0x2C }, 0) + }; + Console.WriteLine(initilalSystemTime.SecondsSince); + Console.WriteLine(initilalSystemTime.DateTime); } } -} \ No newline at end of file +} + +/* + * 01 + * 38-31-36-32-33-31-33-30-33-37-36-31-00-00-00-00 + * 69-65-08-2C + * 9B-AD-16-2E + * 02 + * 52-31-2E-32-34-2C-4F-4D-4E-49-2D-30-31-2C-52-30-2E-33-2E-31-38-00-00-00-00-00 + * 00-DC-0C-C8 + * 38 + * 00-00-00 + */ diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Common.Hardware.Interfaces.Ports.SIRT.csproj b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Common.Hardware.Interfaces.Ports.SIRT.csproj index 6fad4836..b4ab79cf 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Common.Hardware.Interfaces.Ports.SIRT.csproj +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Common.Hardware.Interfaces.Ports.SIRT.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + IDE0003;IDE0049; pdbonly @@ -59,15 +60,17 @@ + - - - + + + + - + diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadPamRequest.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadPamRequest.cs index 28fe2081..1286ac80 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadPamRequest.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadPamRequest.cs @@ -4,7 +4,7 @@ using System; - public class ReadPamRequest : SirtMessage + public class ReadPamRequest : SIRTRequest { /// /// @@ -20,16 +20,16 @@ /// public Boolean PamStruct { - get => this.P1 == 1; - set => this.P1 = (Byte)(value ? 1 : 0); + get => this.message.P1 == 1; + set => this.message.P1 = (Byte)(value ? 1 : 0); } protected override Byte[] ToByteArray() { - this.Start = SirtConstants.Pc2Sirt; - this.Cmd = SirtConstants.ReadPam; + this.message.Start = SIRTConstants.Pc2Sirt; + this.message.Cmd = SIRTConstants.ReadPam; - return base.ToByteArray(); + return this.message.ToByteArray(); } } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadRegRequest.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadRegRequest.cs index d8dc2672..63204a19 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadRegRequest.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/ReadRegRequest.cs @@ -4,21 +4,20 @@ using System; - public class ReadRegRequest : SirtMessage + public class ReadRegRequest : SIRTRequest { public Byte ReadLength { - get => this.P2; - set => this.P2 = value; + get => this.message.P2; + set => this.message.P2 = value; } protected override Byte[] ToByteArray() { - this.Start = SirtConstants.Pc2Sirt; - this.Cmd = SirtConstants.ReadReg; - this.P2 = this.ReadLength; + this.message.Start = SIRTConstants.Pc2Sirt; + this.message.Cmd = SIRTConstants.ReadReg; - return base.ToByteArray(); + return this.message.ToByteArray(); } } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/SIRTRequest.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/SIRTRequest.cs new file mode 100644 index 00000000..772f268f --- /dev/null +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/SIRTRequest.cs @@ -0,0 +1,26 @@ +namespace Common.Hardware.Interfaces.Ports.SIRT.Requests +{ + using System; + + public abstract class SIRTRequest + { + internal readonly SIRTMessage message = new SIRTMessage(); + + public UInt32 Address + { + get => this.message.Address; + set => this.message.Address = value; + } + + public Byte[] Payload + { + get => this.message.Payload; + set => this.message.Payload = value; + } + + protected abstract Byte[] ToByteArray(); + + public static implicit operator Byte[] (SIRTRequest request) + => request.ToByteArray(); + } +} diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WritePamRequest.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WritePamRequest.cs index 8013eaa2..6ee93778 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WritePamRequest.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WritePamRequest.cs @@ -5,10 +5,13 @@ using System; - public class WritePamRequest : SirtMessage + public class WritePamRequest : SIRTRequest { private readonly P811 parameter811 = default(Byte); + public WritePamRequest() + => this.parameter811 = this.message.P1; + /// /// /// @@ -74,11 +77,11 @@ protected override Byte[] ToByteArray() { - this.Start = SirtConstants.Pc2Sirt; - this.Cmd = SirtConstants.WritePam; - this.P1 = this.parameter811; + this.message.Start = SIRTConstants.Pc2Sirt; + this.message.Cmd = SIRTConstants.WritePam; + this.message.P1 = this.parameter811; - return base.ToByteArray(); + return this.message.ToByteArray(); } } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WriteRegRequest.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WriteRegRequest.cs index d3e0f3a5..ed0d0118 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WriteRegRequest.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Requests/WriteRegRequest.cs @@ -4,15 +4,20 @@ using System; - public class WriteRegRequest : SirtMessage + public class WriteRegRequest : SIRTRequest { + public Byte WriteLength + { + get => this.message.P2; + set => this.message.P2 = value; + } + protected override Byte[] ToByteArray() { - this.Start = SirtConstants.Pc2Sirt; - this.Cmd = SirtConstants.WriteReg; - this.P2 = this.Length; + this.message.Start = SIRTConstants.Pc2Sirt; + this.message.Cmd = SIRTConstants.WriteReg; - return base.ToByteArray(); + return this.message.ToByteArray(); } } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/AckOrAnswerResponse.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/AckOrAnswerResponse.cs index 0d9b727e..e4a5e024 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/AckOrAnswerResponse.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/AckOrAnswerResponse.cs @@ -4,12 +4,12 @@ using System; - public class AckOrAnswerResponse : SirtMessage + public class AckOrAnswerResponse : SIRTResponse { private readonly P12 parameterP12; public AckOrAnswerResponse(Byte[] bytes) : base(bytes) - => this.parameterP12 = this.P1; + => this.parameterP12 = this.message.P1; /// /// @@ -51,6 +51,6 @@ /// public Boolean WildCardAddr => this.parameterP12.WildCardAddr; - public Byte LastCmd => this.P2; + public Byte LastCmd => this.message.P2; } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/FromAirResponse.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/FromAirResponse.cs index ee8e3f0f..4e698ed0 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/FromAirResponse.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/FromAirResponse.cs @@ -4,14 +4,14 @@ using System; - public class FromAirResponse : SirtMessage + public class FromAirResponse : SIRTResponse { private readonly P11 parameterP11; private Int32 mhz; public FromAirResponse(Byte[] bytes) : base(bytes) - => this.parameterP11 = this.P1; + => this.parameterP11 = this.message.P1; /// /// @@ -59,8 +59,8 @@ set { this.mhz = value; - this.ReceivePowerDbm = this.CalculateReceivePowerDbm(this.mhz, this.P2); - this.ReceivePowerPer = this.CalculateReceivePowerPer(this.mhz, this.P2); + this.ReceivePowerDbm = this.CalculateReceivePowerDbm(this.mhz, this.message.P2); + this.ReceivePowerPer = this.CalculateReceivePowerPer(this.mhz, this.message.P2); } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/SIRTResponse.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/SIRTResponse.cs new file mode 100644 index 00000000..cd97bef9 --- /dev/null +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/Responses/SIRTResponse.cs @@ -0,0 +1,17 @@ +namespace Common.Hardware.Interfaces.Ports.SIRT.Responses +{ + using System; + + public class SIRTResponse + { + internal readonly SIRTMessage message; + + public SIRTResponse(Byte[] bytes) => this.message = new SIRTMessage(bytes); + + public Byte Cmd => this.message.Cmd; + + public UInt32 Address => this.message.Address; + + public Byte[] Payload => this.message.Payload; + } +} diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtBroadcaster.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtBroadcaster.cs index ed5b3ea2..fc6090ff 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtBroadcaster.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtBroadcaster.cs @@ -2,18 +2,22 @@ { using Common.Hardware.Interfaces.Ports.SIRT.Parameters; using Common.Hardware.Interfaces.Ports.SIRT.Requests; + using Common.Hardware.Interfaces.Ports.SIRT.Responses; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; + using System.Threading.Tasks; - public class SirtBroadcaster : SirtSerialPort + public class SIRTBroadcaster : SIRTSerialPort { - private static readonly ConcurrentDictionary> tasks - = new ConcurrentDictionary>(); + private static readonly ConcurrentDictionary availableDevices + = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary> tasks + = new ConcurrentDictionary>(); - public SirtBroadcaster(SerialPortSettings settings) : base(settings) + public SIRTBroadcaster(SerialPortSettings settings) : base(settings) => this.DataReceived += this.SirtBroadcaster_DataReceived; public String SirtId { get; private set; } @@ -22,11 +26,15 @@ public Boolean Ready { get; private set; } + public event Action DeviceAvailable; + + public event Action DeviceUnavailable; + public override void Open() { base.Open(); - _ = this.Write(new WriteRegRequest + _ = this.Send(new WriteRegRequest { Address = 0x20000000, Payload = new Byte[] @@ -55,7 +63,7 @@ }); var manuelResetEventSlim = new ManualResetEventSlim(); - var sirtTask = this.Write(new ReadRegRequest + var sirtTask = this.Send(new ReadRegRequest { ReadLength = 8, Address = 0x11001820 @@ -79,21 +87,25 @@ manuelResetEventSlim.Set(); }; - manuelResetEventSlim.Wait(); + manuelResetEventSlim.Wait(10000); if (!this.Ready) { this.Close(); } + else + { + this.RemoveUnavailableDevices(); + } } - public SirtTask Write(SirtMessage request) + public SIRTTask Send(SIRTRequest request) { - var task = new SirtTask(request); + var task = new SIRTTask(request); task.Done += Task_Done; var queue = tasks - .GetOrAdd(task.Address, new Queue()); + .GetOrAdd(task.Address, new Queue()); queue.Enqueue(task); if (queue.Count == 1) @@ -104,7 +116,60 @@ return task; } - private void Task_Done(SirtTask task) + private void RemoveUnavailableDevices() + { + Task.Factory + .StartNew(state => + { + if (state is SIRTBroadcaster broadcaster) + { + while (true) + { + var now = DateTime.Now; + + foreach (var address in availableDevices.Keys) + { + if (availableDevices.TryGetValue(address, out var timestamp) && (now - timestamp).TotalSeconds > 30) + { + availableDevices.TryRemove(address, out var _); + broadcaster.DeviceUnavailable?.Invoke(address); + } + } + + Thread.Sleep(10000); + } + } + }, this, CancellationToken.None) + .ContinueWith(task => + { + if (task.Exception != null && task.AsyncState is SIRTBroadcaster broadcaster) + { + broadcaster.Notify(task.Exception.ToString()); + } + }, CancellationToken.None); + } + + private void SirtBroadcaster_DataReceived(Byte[] data) + { + var response = new SIRTResponse(data); + var address = response.Address; + + if (tasks.TryGetValue(address, out var queue) && queue.Count > 0) + { + queue.Peek().AddResponse(response); + } + + availableDevices.AddOrUpdate(address, + addValueFactory: key => + { + this.DeviceAvailable?.Invoke(key); + + return DateTime.Now; + }, + updateValueFactory: (_1, _2) => DateTime.Now); + } + + private void Task_Done(SIRTTask task) { if (tasks.TryGetValue(task.Address, out var queue)) { @@ -123,15 +188,5 @@ } } } - - private void SirtBroadcaster_DataReceived(Byte[] data) - { - var response = new SirtMessage(data); - - if (tasks.TryGetValue(response.Address, out var queue) && queue.Count > 0) - { - queue.Peek().AddResponse(response); - } - } } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtConstants.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtConstants.cs index 14775ac1..412e78c0 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtConstants.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtConstants.cs @@ -2,7 +2,7 @@ { using System; - public static class SirtConstants + public static class SIRTConstants { public const Int32 CmdIndex = 1; public const Int32 LengthIndex = 8; diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtMessage.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtMessage.cs index 9d30d585..4ba93a4f 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtMessage.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtMessage.cs @@ -4,18 +4,18 @@ using System; - public class SirtMessage + internal class SIRTMessage { private Byte[] bytes; private Int32 bytesLength; - public SirtMessage() + public SIRTMessage() { - this.bytes = new Byte[SirtConstants.MinLength]; + this.bytes = new Byte[SIRTConstants.MinLength]; this.bytesLength = this.bytes.Length; } - public SirtMessage(Byte[] bytes) + public SIRTMessage(Byte[] bytes) { this.bytes = bytes; this.bytesLength = this.bytes.Length; @@ -71,7 +71,7 @@ set { this.bytes[8] = value; - var newLength = SirtConstants.MinLength + value; + var newLength = SIRTConstants.MinLength + value; Array.Resize(ref this.bytes, newLength); this.bytesLength = this.bytes.Length; @@ -96,7 +96,7 @@ } this.Length = (Byte)value.Length; - var newLength = SirtConstants.MinLength + this.Length; + var newLength = SIRTConstants.MinLength + this.Length; Array.Resize(ref this.bytes, newLength); Array.Copy(value, 0, this.bytes, 9, this.Length); @@ -116,24 +116,24 @@ public override String ToString() => BitConverter.ToString(this.ToByteArray()); - protected virtual Byte[] ToByteArray() + internal Byte[] ToByteArray() { var crc = CRC1021.CalculateCRC1021(this.bytes, 1, this.bytesLength - 3); this.bytes[this.bytesLength - 3] = crc.MSB; this.bytes[this.bytesLength - 2] = crc.LSB; - this.bytes[this.bytesLength - 1] = SirtConstants.StopByte; + this.bytes[this.bytesLength - 1] = SIRTConstants.StopByte; return this.bytes; } - public static implicit operator SirtMessage(Byte[] bytes) - => new SirtMessage(bytes); + public static implicit operator SIRTMessage(Byte[] bytes) + => new SIRTMessage(bytes); - public static implicit operator Byte[](SirtMessage message) + public static implicit operator Byte[](SIRTMessage message) => message.ToByteArray(); - public static implicit operator String (SirtMessage message) + public static implicit operator String (SIRTMessage message) => message.ToString(); } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtTask.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtTask.cs index 0221bcf5..78f92550 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtTask.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/SirtTask.cs @@ -1,29 +1,32 @@ namespace Common.Hardware.Interfaces.Ports.SIRT { + using Common.Hardware.Interfaces.Ports.SIRT.Requests; + using Common.Hardware.Interfaces.Ports.SIRT.Responses; + using System; using System.Collections.Generic; - public class SirtTask + public class SIRTTask { - private readonly SirtMessage request; - private readonly ICollection responses; + private readonly SIRTRequest request; + private readonly ICollection responses; - public SirtTask(SirtMessage request) + internal SIRTTask(SIRTRequest request) { this.request = request; this.Address = this.request.Address; - this.responses = new List(); + this.responses = new List(); } - public event Action Done; + public event Action Done; public UInt32 Address { get; } public Boolean IsDone { get; private set; } - public SirtMessage Last { get; private set; } + public SIRTResponse Last { get; private set; } - internal void AddResponse(SirtMessage response) + internal void AddResponse(SIRTResponse response) { if (!this.IsDone) { @@ -32,8 +35,8 @@ this.responses.Add(response); - if ((response.Cmd == SirtConstants.FromAir && response.Payload[0] == SirtConstants.Semi) - || (response.Cmd == SirtConstants.AckOrAnswer && response.Payload.Length > 0)) + if ((response.Cmd == SIRTConstants.FromAir && response.Payload[0] == SIRTConstants.Semi) + || (response.Cmd == SIRTConstants.AckOrAnswer && response.Payload.Length > 0)) { this.IsDone = true; @@ -41,7 +44,7 @@ } } - public static implicit operator Byte[](SirtTask task) + public static implicit operator Byte[](SIRTTask task) => task.request; } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/Common.Hardware.Interfaces.Ports.csproj b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/Common.Hardware.Interfaces.Ports.csproj index 9697023d..0e45a2ee 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/Common.Hardware.Interfaces.Ports.csproj +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/Common.Hardware.Interfaces.Ports.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + IDE0003;IDE0049; pdbonly @@ -40,7 +41,7 @@ - + @@ -50,7 +51,7 @@ - + diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs index 314dc94c..63cf221a 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs @@ -3,9 +3,9 @@ using System; using System.Threading; - public class IrdaSerialPort : SerialPort + public class IRDASerialPort : SerialPort { - public IrdaSerialPort(SerialPortSettings settings) : base(settings) + public IRDASerialPort(SerialPortSettings settings) : base(settings) { } @@ -15,7 +15,7 @@ if (data.TryParseToIrda(out var bytes)) { - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { this.serialPortBase.Write(new Byte[] { 0x01, 0x01, 0x01, 0x01 }, 0, 4); diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPort.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPort.cs index 145c55b8..cd39c941 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPort.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPort.cs @@ -28,6 +28,8 @@ } } + protected void Notify(String message) => this.serialPortBase.Notify(message); + public virtual void Open() => this.serialPortBase.Open(); public abstract Byte[] Write(params Byte[] data); diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs index b555ef26..c3429410 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs @@ -42,21 +42,21 @@ serialPort.Dispose(); serialPort = null; - this.NotifyInfoMessage($"Disconnected: {this}"); + this.Notify($"Disconnected: {this}"); } else { - this.NotifyInfoMessage($"{this.PortName} is null!"); + this.Notify($"{this.PortName} is null!"); } } else { - this.NotifyInfoMessage($"{this.PortName} dose not exists!"); + this.Notify($"{this.PortName} dose not exists!"); } } catch (Exception e) { - this.NotifyInfoMessage(e.ToString()); + this.Notify(e.ToString()); } } @@ -83,16 +83,16 @@ { serialPort.Open(); - this.NotifyInfoMessage($"Connected: {this}"); + this.Notify($"Connected: {this}"); } else { - this.NotifyInfoMessage($"{this.PortName} cann't be addedd!"); + this.Notify($"{this.PortName} cann't be addedd!"); } } catch (Exception e) { - this.NotifyInfoMessage(e.ToString()); + this.Notify(e.ToString()); } } @@ -113,11 +113,11 @@ Buffer.BlockCopy(buffer, 0, bytes, 0, bytesToCopy); - this.NotifyInfoMessage($"RX|{BitConverter.ToString(bytes)}"); + this.Notify($"RX|{BitConverter.ToString(bytes)}"); } catch (Exception e) { - this.NotifyInfoMessage(e.ToString()); + this.Notify(e.ToString()); } } @@ -150,16 +150,16 @@ serialPort.Write(bytes, start, length); } - this.NotifyInfoMessage($"TX|{BitConverter.ToString(bytes)}"); + this.Notify($"TX|{BitConverter.ToString(bytes)}"); } catch (Exception e) { - this.NotifyInfoMessage(e.ToString()); + this.Notify(e.ToString()); } } } - public void NotifyInfoMessage(String state) + public void Notify(String state) => this.Logging?.Invoke(state); } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SirtSerialPort.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SirtSerialPort.cs index aa58f613..b6c917ca 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SirtSerialPort.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SirtSerialPort.cs @@ -4,14 +4,14 @@ using System.Threading; using System.Threading.Tasks; - public class SirtSerialPort : SerialPort + public class SIRTSerialPort : SerialPort { private CancellationTokenSource cancellationTokenSource; private CancellationToken cancellationToken; private Task listener; private bool listening; - public SirtSerialPort(SerialPortSettings settings) : base(settings) + public SIRTSerialPort(SerialPortSettings settings) : base(settings) { } @@ -56,7 +56,7 @@ this.cancellationToken = this.cancellationTokenSource.Token; this.listener = Task.Factory.StartNew(asyncState => { - if (asyncState is SirtSerialPort sirtSerialPort) + if (asyncState is SIRTSerialPort sirtSerialPort) { sirtSerialPort.listening = true; @@ -115,11 +115,11 @@ }, this, this.cancellationToken) .ContinueWith(task => { - if (task.AsyncState is SirtSerialPort sirtSerialPort) + if (task.AsyncState is SIRTSerialPort sirtSerialPort) { if (task.Exception != null) { - sirtSerialPort.serialPortBase.NotifyInfoMessage(task.Exception.ToString()); + sirtSerialPort.serialPortBase.Notify(task.Exception.ToString()); } sirtSerialPort.listening = false; @@ -137,7 +137,7 @@ } catch (Exception e) { - this.serialPortBase.NotifyInfoMessage(e.ToString()); + this.serialPortBase.Notify(e.ToString()); } } } diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus.Features/Common.Hardware.WaterMeter.OmniPlus.Features.csproj b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus.Features/Common.Hardware.WaterMeter.OmniPlus.Features.csproj index f7f430da..cafd1a8a 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus.Features/Common.Hardware.WaterMeter.OmniPlus.Features.csproj +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus.Features/Common.Hardware.WaterMeter.OmniPlus.Features.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + IDE0003;IDE0049; pdbonly diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Common.Hardware.WaterMeter.OmniPlus.csproj b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Common.Hardware.WaterMeter.OmniPlus.csproj index 03e20ce6..9bf68a94 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Common.Hardware.WaterMeter.OmniPlus.csproj +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Common.Hardware.WaterMeter.OmniPlus.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + IDE0003;IDE0049; pdbonly diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs index 87e0dbb0..c6a505f7 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs @@ -16,7 +16,7 @@ PortName = portName, StopBits = SerialPortStopBits.One }; - this.serialPort = new IrdaSerialPort(settings); + this.serialPort = new IRDASerialPort(settings); } public override Byte[] Send(params Byte[] bytes) diff --git a/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/Common.Hardware.WaterMeter.eRegister.Features.csproj b/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/Common.Hardware.WaterMeter.eRegister.Features.csproj new file mode 100644 index 00000000..dd7680ec --- /dev/null +++ b/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/Common.Hardware.WaterMeter.eRegister.Features.csproj @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {E19C1694-132E-477C-86CB-8E146D5F59B0} + Library + Properties + Common.Hardware.WaterMeter.eRegister.Features + Common.Hardware.WaterMeter.eRegister.Features + v4.0 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + {B23BFAF9-08AE-4311-8C08-FB2BB49EA2AD} + Common.Hardware.Interfaces.Ports.SIRT + + + + \ No newline at end of file diff --git a/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/PamBuilder.cs b/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/PamBuilder.cs new file mode 100644 index 00000000..abf3ab1c --- /dev/null +++ b/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/PamBuilder.cs @@ -0,0 +1,55 @@ +namespace Common.Hardware.WaterMeter.eRegister.Features +{ + using Common.Hardware.Interfaces.Ports.SIRT.Requests; + + using System; + + public class PamBuilder + { + public class PamFeatures + { + public Boolean WakeUpModul { get; set; } + + public Boolean WakeUpCmd { get; set; } + + public Boolean IgnoreTimeout { get; set; } + + public Boolean DelPamSemi { get; set; } + + public Boolean DelPamTx { get; set; } + + public Boolean DelPamAdr { get; set; } + + public Boolean DelPamAll { get; set; } + + public UInt32 Address { get; set; } + + internal Byte[] Payload { get; set; } + } + + public class MultiPamFeatures : PamFeatures + { + + } + + public static WritePamRequest MultiPamCommand(Action featuresBuilder) + { + var features = new MultiPamFeatures(); + + featuresBuilder?.Invoke(features); + + return new WritePamRequest + { + Address = features.Address, + DelPamAdr = features.DelPamAdr, + DelPamAll = features.DelPamAll, + DelPamSemi = features.DelPamSemi, + DelPamTx = features.DelPamTx, + IgnoreTimeout = features.IgnoreTimeout, + WakeUpCmd = features.WakeUpCmd, + WakeUpModul = features.WakeUpModul, + Payload = features.Payload + }; + } + } +} diff --git a/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/Properties/AssemblyInfo.cs b/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..c9edf645 --- /dev/null +++ b/Common/Hardware/WaterMeter/eRegister/Common.Hardware.WaterMeter.eRegister.Features/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Common.Hardware.WaterMeter.eRegister.Features")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Common.Hardware.WaterMeter.eRegister.Features")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e19c1694-132e-477c-86cb-8e146d5f59b0")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Common/Logic/Common.Logic.Extensions.Http/Common.Logic.Extensions.Http.csproj b/Common/Logic/Common.Logic.Extensions.Http/Common.Logic.Extensions.Http.csproj index cf69de48..89f776fc 100644 --- a/Common/Logic/Common.Logic.Extensions.Http/Common.Logic.Extensions.Http.csproj +++ b/Common/Logic/Common.Logic.Extensions.Http/Common.Logic.Extensions.Http.csproj @@ -23,6 +23,7 @@ DEBUG;TRACE prompt 4 + IDE0003;IDE0049; pdbonly diff --git a/Common/Logic/Common.Logic.Extensions.SQL/Common.Logic.Extensions.SQL.csproj b/Common/Logic/Common.Logic.Extensions.SQL/Common.Logic.Extensions.SQL.csproj index 273cd76a..cf0e3855 100644 --- a/Common/Logic/Common.Logic.Extensions.SQL/Common.Logic.Extensions.SQL.csproj +++ b/Common/Logic/Common.Logic.Extensions.SQL/Common.Logic.Extensions.SQL.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + IDE0003;IDE0049; pdbonly diff --git a/Common/OmniPlus/InspectionUI.Abstraction/InspectionUI.Abstraction.csproj b/Common/OmniPlus/InspectionUI.Abstraction/InspectionUI.Abstraction.csproj index ca7f0f64..06791b6d 100644 --- a/Common/OmniPlus/InspectionUI.Abstraction/InspectionUI.Abstraction.csproj +++ b/Common/OmniPlus/InspectionUI.Abstraction/InspectionUI.Abstraction.csproj @@ -25,6 +25,7 @@ prompt 4 false + IDE0003;IDE0049; pdbonly diff --git a/Common/OmniPlus/InspectionUI.Abstraction/TestBatchAttribute.cs b/Common/OmniPlus/InspectionUI.Abstraction/TestBatchAttribute.cs index c42122be..7300ba40 100644 --- a/Common/OmniPlus/InspectionUI.Abstraction/TestBatchAttribute.cs +++ b/Common/OmniPlus/InspectionUI.Abstraction/TestBatchAttribute.cs @@ -5,8 +5,6 @@ [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class TestBatchAttribute : Attribute { - private readonly string displayName; - public TestBatchAttribute(string displayName) => this.DisplayName = displayName; diff --git a/Common/OmniPlus/InspectionUI.Plugins.Omni/InspectionUI.Plugins.Omni.csproj b/Common/OmniPlus/InspectionUI.Plugins.Omni/InspectionUI.Plugins.Omni.csproj index 23eaf7dd..5d28677e 100644 --- a/Common/OmniPlus/InspectionUI.Plugins.Omni/InspectionUI.Plugins.Omni.csproj +++ b/Common/OmniPlus/InspectionUI.Plugins.Omni/InspectionUI.Plugins.Omni.csproj @@ -50,7 +50,7 @@ none AnyCPU prompt - InspectionUI.Plugins.Omni.ruleset + MinimumRecommendedRules.ruleset false On false @@ -97,7 +97,6 @@ - diff --git a/Common/OmniPlus/InspectionUI.Plugins.Omni/InspectionUI.Plugins.Omni.ruleset b/Common/OmniPlus/InspectionUI.Plugins.Omni/InspectionUI.Plugins.Omni.ruleset deleted file mode 100644 index b6bdd0e2..00000000 --- a/Common/OmniPlus/InspectionUI.Plugins.Omni/InspectionUI.Plugins.Omni.ruleset +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Common/OmniPlus/InspectionUI.RemoteControl/InspectionUI.RemoteControl.csproj b/Common/OmniPlus/InspectionUI.RemoteControl/InspectionUI.RemoteControl.csproj index e2c93cdf..294ef551 100644 --- a/Common/OmniPlus/InspectionUI.RemoteControl/InspectionUI.RemoteControl.csproj +++ b/Common/OmniPlus/InspectionUI.RemoteControl/InspectionUI.RemoteControl.csproj @@ -23,6 +23,7 @@ prompt 4 false + IDE0003;IDE0049; pdbonly diff --git a/Common/OmniPlus/InspectionUI/InspectionUI.csproj b/Common/OmniPlus/InspectionUI/InspectionUI.csproj index 4b39002c..7c77c408 100644 --- a/Common/OmniPlus/InspectionUI/InspectionUI.csproj +++ b/Common/OmniPlus/InspectionUI/InspectionUI.csproj @@ -28,6 +28,7 @@ prompt 4 false + IDE0003;IDE0049; AnyCPU diff --git a/Common/OmniPlus/InspectionUI/Plugins/VM.cs b/Common/OmniPlus/InspectionUI/Plugins/VM.cs index 6f6414ea..b055d217 100644 --- a/Common/OmniPlus/InspectionUI/Plugins/VM.cs +++ b/Common/OmniPlus/InspectionUI/Plugins/VM.cs @@ -23,7 +23,7 @@ { dispatcher.Invoke(action); } - catch (Exception _) + catch (Exception) { } diff --git a/Common/OmniPlus/OmniPlus/OmniPlus.csproj b/Common/OmniPlus/OmniPlus/OmniPlus.csproj index 32e98ff4..bfc055c7 100644 --- a/Common/OmniPlus/OmniPlus/OmniPlus.csproj +++ b/Common/OmniPlus/OmniPlus/OmniPlus.csproj @@ -34,6 +34,7 @@ prompt 4 false + IDE0003;IDE0049; pdbonly diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/App.config b/Common/Ui/Common.UI.SIRTBroadcaster/App.config new file mode 100644 index 00000000..16bb9140 --- /dev/null +++ b/Common/Ui/Common.UI.SIRTBroadcaster/App.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml b/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml index b9b8ebc6..6f249b31 100644 --- a/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml +++ b/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml @@ -1,7 +1,6 @@  diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml.cs b/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml.cs index a78e0aab..dbdf2a12 100644 --- a/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml.cs +++ b/Common/Ui/Common.UI.SIRTBroadcaster/App.xaml.cs @@ -1,8 +1,34 @@ namespace Common.UI.SIRTBroadcaster { + using System; using System.Windows; public partial class App : Application { + private static Boolean activatedForFirstTime; + public static event Action Exited; + + public static event Action Started; + + public static void Invoke(Action action) => Current?.Dispatcher?.Invoke(action); + + protected override void OnActivated(EventArgs e) + { + base.OnActivated(e); + + if (!activatedForFirstTime) + { + activatedForFirstTime = true; + + Started?.Invoke(); + } + } + + protected override void OnExit(ExitEventArgs e) + { + Exited?.Invoke(); + + base.OnExit(e); + } } } diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/Common.UI.SIRTBroadcaster.csproj b/Common/Ui/Common.UI.SIRTBroadcaster/Common.UI.SIRTBroadcaster.csproj index d33169b0..76bd57c7 100644 --- a/Common/Ui/Common.UI.SIRTBroadcaster/Common.UI.SIRTBroadcaster.csproj +++ b/Common/Ui/Common.UI.SIRTBroadcaster/Common.UI.SIRTBroadcaster.csproj @@ -23,6 +23,7 @@ DEBUG;TRACE prompt 4 + IDE0003;IDE0049; AnyCPU @@ -35,6 +36,7 @@ + @@ -53,6 +55,10 @@ MSBuild:Compile Designer + + + + MSBuild:Compile Designer @@ -67,6 +73,7 @@ + Code @@ -84,10 +91,25 @@ ResXFileCodeGenerator Resources.Designer.cs + SettingsSingleFileGenerator Settings.Designer.cs + + + {b23bfaf9-08ae-4311-8c08-fb2bb49ea2ad} + Common.Hardware.Interfaces.Ports.SIRT + + + {9ae071c0-db12-4e09-a3c7-43f6dcd518d6} + Common.Hardware.Interfaces.Ports + + + {E19C1694-132E-477C-86CB-8E146D5F59B0} + Common.Hardware.WaterMeter.eRegister.Features + + \ No newline at end of file diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/MainWindow.xaml b/Common/Ui/Common.UI.SIRTBroadcaster/MainWindow.xaml index f08388ca..7dc34212 100644 --- a/Common/Ui/Common.UI.SIRTBroadcaster/MainWindow.xaml +++ b/Common/Ui/Common.UI.SIRTBroadcaster/MainWindow.xaml @@ -1,12 +1,65 @@  + xmlns:VM="clr-namespace:Common.UI.SIRTBroadcaster.ViewModels" + FontFamily="Verdana" + Title="MainWindow" + Height="450" + Width="800"> + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/Models/SIRTBroadcasterModel.cs b/Common/Ui/Common.UI.SIRTBroadcaster/Models/SIRTBroadcasterModel.cs new file mode 100644 index 00000000..608fea17 --- /dev/null +++ b/Common/Ui/Common.UI.SIRTBroadcaster/Models/SIRTBroadcasterModel.cs @@ -0,0 +1,60 @@ +namespace Common.UI.SIRTBroadcaster.Models +{ + using Common.Hardware.Interfaces.Ports; + using Common.Hardware.Interfaces.Ports.SIRT; + using Common.Hardware.WaterMeter.eRegister.Features; + + using System; + + public class SIRTBroadcasterModel + { + private readonly SIRTBroadcaster broadcaster; + + public SIRTBroadcasterModel(String portName, Int32 timeout) + { + this.broadcaster = new SIRTBroadcaster(new SerialPortSettings + { + BaudRate = SerialPortBaudRate.X115200, + DataBits = SerialPortDataBits.X8, + Parity = SerialPortParity.None, + PortName = portName, + StopBits = SerialPortStopBits.One, + RWTimeout = timeout + }); + } + + public Int32 Frequency => this.broadcaster?.Frequency ?? 0; + + public String Id => this.broadcaster?.SirtId ?? String.Empty; + + public Boolean IsConnected => this.broadcaster?.IsOpen ?? false; + + public event Action DeviceAvailable + { + add => this.broadcaster.DeviceAvailable += value; + remove => this.broadcaster.DeviceAvailable -= value; + } + + public event Action DeviceUnavailable + { + add => this.broadcaster.DeviceUnavailable += value; + remove => this.broadcaster.DeviceUnavailable -= value; + } + + public void Activate() => this.broadcaster.Open(); + + public void Deactivate() => this.broadcaster.Close(); + + public void WakeUp() + { + var pam = PamBuilder.MultiPamCommand(options => + { + options.Address = 319020441; + options.DelPamSemi = true; + options.WakeUpCmd = true; + }); + + var task = this.broadcaster.Send(pam); + } + } +} diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/Utilities/Appsettings.cs b/Common/Ui/Common.UI.SIRTBroadcaster/Utilities/Appsettings.cs new file mode 100644 index 00000000..805ea280 --- /dev/null +++ b/Common/Ui/Common.UI.SIRTBroadcaster/Utilities/Appsettings.cs @@ -0,0 +1,27 @@ +namespace Common.UI.SIRTBroadcaster.Utilities +{ + using System; + using System.Configuration; + + public class Appsettings + { + private static Configuration Configuration => ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + + private static AppSettingsSection AppSettings => Configuration.AppSettings; + + private static String AppSettingsSectionName => AppSettings.SectionInformation.Name; + + public static readonly Appsettings Current = new Appsettings(); + + public Appsettings() { } + + public String this[String key] => GetSetting(key); + + private static String GetSetting(String settingKey) + { + ConfigurationManager.RefreshSection(AppSettingsSectionName); + + return AppSettings.Settings[settingKey].Value ?? String.Empty; + } + } +} diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/Utilities/CompilerServices.cs b/Common/Ui/Common.UI.SIRTBroadcaster/Utilities/CompilerServices.cs new file mode 100644 index 00000000..698b37d5 --- /dev/null +++ b/Common/Ui/Common.UI.SIRTBroadcaster/Utilities/CompilerServices.cs @@ -0,0 +1,17 @@ +namespace System.Runtime.CompilerServices +{ + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] + public class CallerMemberNameAttribute : Attribute + { + } + + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] + public class CallerFilePathAttribute : Attribute + { + } + + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] + public class CallerLineNumberAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/ViewModels/SIRTBroadcasterViewModel.cs b/Common/Ui/Common.UI.SIRTBroadcaster/ViewModels/SIRTBroadcasterViewModel.cs new file mode 100644 index 00000000..38bbfd51 --- /dev/null +++ b/Common/Ui/Common.UI.SIRTBroadcaster/ViewModels/SIRTBroadcasterViewModel.cs @@ -0,0 +1,81 @@ +namespace Common.UI.SIRTBroadcaster.ViewModels +{ + using Common.UI.SIRTBroadcaster.Models; + using Common.UI.SIRTBroadcaster.Utilities; + + using System; + using System.Collections.Generic; + using System.Collections.ObjectModel; + using System.Threading.Tasks; + + public class SIRTBroadcasterViewModel : VM + { + private readonly SortedDictionary devices; + private readonly SIRTBroadcasterModel model; + + public SIRTBroadcasterViewModel() + { + App.Started += App_Started; + App.Exited += App_Exited; + this.model = this.CreateSIRTBroadcasterModel(); + this.devices = new SortedDictionary(); + } + + private Int32 availableAddressesCount; + public Int32 AvailableDevicesCount + { + get => this.availableAddressesCount; + set => this.Set(() => this.availableAddressesCount = value); + } + + public ObservableCollection AvailableDevices { get; private set; } + = new ObservableCollection(); + + private void App_Exited() => Task.Factory.StartNew(this.model.Deactivate); + + private void App_Started() => Task.Factory.StartNew(() => + { + this.model.Activate(); + this.model.WakeUp(); + }); + + private void Model_DeviceAvailable(UInt32 address) + => App.Invoke(() => + { + this.devices[address] = true; + this.RefreshAvailableDevices(); + }); + + private void Model_DeviceUnavailable(UInt32 address) + => App.Invoke(() => + { + this.devices.Remove(address); + this.RefreshAvailableDevices(); + }); + + private SIRTBroadcasterModel CreateSIRTBroadcasterModel() + { + var portName = Appsettings.Current["SIRT.PortName"]; + var _timeout = Appsettings.Current["SIRT.RWTimeout"]; + var parsed = Int32.TryParse(_timeout, out var timeout); + + var broadcasterModel = new SIRTBroadcasterModel(portName, parsed ? timeout : 3000); + broadcasterModel.DeviceAvailable += Model_DeviceAvailable; + broadcasterModel.DeviceUnavailable += Model_DeviceUnavailable; + + return broadcasterModel; + } + + private void RefreshAvailableDevices() + { + this.AvailableDevices.Clear(); + + foreach (var key in this.devices.Keys) + { + this.AvailableDevices.Add(key); + } + + this.AvailableDevicesCount = this.AvailableDevices.Count; + } + } +} diff --git a/Common/Ui/Common.UI.SIRTBroadcaster/ViewModels/VM.cs b/Common/Ui/Common.UI.SIRTBroadcaster/ViewModels/VM.cs new file mode 100644 index 00000000..e2eeb122 --- /dev/null +++ b/Common/Ui/Common.UI.SIRTBroadcaster/ViewModels/VM.cs @@ -0,0 +1,79 @@ +namespace Common.UI.SIRTBroadcaster.ViewModels +{ + using System; + using System.ComponentModel; + using System.Runtime.CompilerServices; + using System.Windows.Input; + + public abstract class VM : INotifyPropertyChanged, IDisposable + { + protected VM() => App.Exited += this.Dispose; + + public event PropertyChangedEventHandler PropertyChanged; + + public virtual void Dispose() => App.Exited -= this.Dispose; + + protected void NotifyPropertyChanged([CallerMemberName] string property = "") + { + this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); + } + + protected void Set(Action setExpression, [CallerMemberName] string property = "") + { + setExpression?.Invoke(); + this.NotifyPropertyChanged(property); + } + + protected class Command : ICommand + { + private readonly Action executable; + + public Command(Action executable) + { + this.executable = executable; + } + + public event EventHandler CanExecuteChanged + { + add => CommandManager.RequerySuggested += value; + remove => CommandManager.RequerySuggested -= value; + } + + public bool CanExecute(object _) + { + return this.executable != null; + } + + public void Execute(object _) + { + this.executable?.Invoke(); + } + } + + protected class Command : ICommand + { + private readonly Action executable; + + public Command(Action executable) + { + this.executable = executable; + } + + public event EventHandler CanExecuteChanged + { + add => CommandManager.RequerySuggested += value; + remove => CommandManager.RequerySuggested -= value; + } + + public bool CanExecute(object _) + { + return this.executable != null; + } + + public void Execute(object parameter) + { + this.executable?.Invoke(parameter is T t ? t : default(T)); + } + } + } +}