-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
accepted user commands and alerts are now registered as events (#127)
* accepted user commands and alerts are now registered as events By registering these low frequency events it will be possible later to check relevant actions and alerts that have been triggered for the system Also: * fixed bug in multi command alerts * CommandHandler.AlertFired has been removed, use CommandHandler.EventFired and check type == EventType.Alert instead * fixed double firing of command events
- Loading branch information
1 parent
e988ea9
commit 9e1fae4
Showing
6 changed files
with
106 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace CA_DataUploaderLib | ||
{ | ||
public class EventFiredArgs : EventArgs | ||
{ | ||
public EventFiredArgs(string data, EventType eventType, DateTime timespan) : this(data, (byte) eventType, timespan) { } | ||
public EventFiredArgs(string data, byte eventType, DateTime timespan) | ||
{ | ||
Data = data; | ||
EventType = eventType; | ||
TimeSpan = timespan; | ||
} | ||
|
||
public string Data { get; } | ||
public byte EventType { get; } | ||
public DateTime TimeSpan { get; } | ||
|
||
public byte[] ToByteArray() | ||
{ | ||
var timeTicks = TimeSpan.Ticks; | ||
var encoding = Encoding.UTF8; | ||
var dataBytesCount = encoding.GetByteCount(Data); | ||
var bytes = new byte[1 + 1 + sizeof(long) + dataBytesCount]; //version + event type + time ticks + data bytes | ||
bytes[0] = 0; | ||
bytes[1] = EventType; | ||
MemoryMarshal.Write(bytes.AsSpan()[2..], ref timeTicks); | ||
var startOfDataIndex = 1 + 1 + sizeof(long); | ||
if (dataBytesCount != encoding.GetBytes(Data, 0, Data.Length, bytes, startOfDataIndex)) | ||
throw new InvalidOperationException($"unexpected error getting utf8 bytes for event: {Data}"); | ||
return bytes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace CA_DataUploaderLib | ||
{ | ||
public enum EventType : byte | ||
{ | ||
Alert = 0, | ||
Command = 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using CA_DataUploaderLib; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace UnitTests | ||
{ | ||
[TestClass] | ||
public class EventFiredArgsTests | ||
{ | ||
[TestMethod] | ||
public void ToByteArrayReturnsExpectedBytes() | ||
{ | ||
const string Data = "my message"; | ||
DateTime timespan = new DateTime(2012, 1, 1, 1, 1, 1, 1, DateTimeKind.Utc); | ||
var args = new EventFiredArgs(Data, 2, timespan); | ||
var bytes = args.ToByteArray(); | ||
CollectionAssert.AreEqual(new byte[] { 0, 2 }, bytes.Take(2).ToList()); | ||
CollectionAssert.AreEqual(BitConverter.GetBytes(timespan.Ticks), bytes.Skip(2).Take(sizeof(long)).ToList()); | ||
CollectionAssert.AreEqual(Encoding.UTF8.GetBytes(Data), bytes.Skip(2).Skip(sizeof(long)).ToList()); | ||
} | ||
} | ||
} |