-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAmiMessage.cs
117 lines (96 loc) · 3.92 KB
/
AmiMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright © Alex Forster. All rights reserved.
* https://github.com/alexforster/AmiClient/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Ami
{
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ByteArrayExtensions;
public sealed partial class AmiMessage : IEnumerable<KeyValuePair<String, String>>
{
public readonly List<KeyValuePair<String, String>> Fields = new List<KeyValuePair<String, String>>();
public DateTimeOffset Timestamp { get; private set; }
public AmiMessage()
{
this.Timestamp = DateTimeOffset.UtcNow;
}
public String this[String key]
{
get
{
if(String.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("value cannot be null or whitespace", nameof(key));
}
key = key.Trim();
return this.Fields
.Where(kvp => kvp.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
.Select(el => el.Value)
.FirstOrDefault();
}
private set
{
if(String.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key cannot be null or whitespace", nameof(key));
}
key = key.Trim();
if(value == null)
{
throw new ArgumentNullException(nameof(value));
}
value = value.Trim();
if(key.Equals("ActionID", StringComparison.OrdinalIgnoreCase))
{
// ActionIDs are overwritten so that the AmiMessage creator can override autogenerated values;
// all other keys can be added multiple times (though it is usually invalid to do so)
this.Fields.RemoveAll(field => field.Key.Equals("ActionID", StringComparison.OrdinalIgnoreCase));
}
this.Fields.Add(new KeyValuePair<String, String>(key, value));
if(key.Equals("Action", StringComparison.OrdinalIgnoreCase) &&
this.Fields.Any(field => field.Key.Equals("ActionID", StringComparison.OrdinalIgnoreCase)) == false)
{
// add an autogenerated ActionID when the Action field is set
// only if an ActionID has not already been set
this.Fields.Add(new KeyValuePair<String, String>("ActionID", Guid.NewGuid().ToString("D")));
}
if(key.Equals("Timestamp", StringComparison.OrdinalIgnoreCase))
{
if(Double.TryParse(value, out var seconds))
{
var dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
this.Timestamp = dt.AddSeconds(seconds);
}
}
}
}
public IEnumerator<KeyValuePair<String, String>> GetEnumerator()
{
return this.Fields.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public void Add(String key, String value)
{
this[key] = value;
}
}
}