-
Notifications
You must be signed in to change notification settings - Fork 0
/
KiwiDevice.cs
43 lines (34 loc) · 1017 Bytes
/
KiwiDevice.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
using JsonPathway;
using kiwi.Exceptions;
namespace kiwi;
public class KiwiDevice
{
private Kiwi _kiwi;
public string Name { get; }
public string Guid { get; }
public KiwiDevice(Kiwi kiwi, string name, string guid)
{
_kiwi = kiwi ?? throw new ArgumentNullException(nameof(kiwi));
Name = name ?? throw new ArgumentNullException(nameof(name));
Guid = guid ?? throw new ArgumentNullException(nameof(guid));
}
public string GetValue(string tag)
{
const string VALUE_PROPERTY = "value";
try
{
var valueSelector = $"result.items[*].tagValues[?(@.tagName=='{tag}' && @.guid == '{Guid}')]";
var element = JsonPath.ExecutePath(valueSelector, _kiwi.JsonContent);
return element.First().GetProperty(VALUE_PROPERTY).ToString();
}
catch (InvalidOperationException ex)
{
throw new TagNotFoundException(tag, ex);
}
catch (KeyNotFoundException ex)
{
throw new PropertyNotFoundException(VALUE_PROPERTY, ex);
}
}
public override string ToString() => $"{Name} ({Guid})";
}