-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceKey.cs
More file actions
66 lines (60 loc) · 2.41 KB
/
ResourceKey.cs
File metadata and controls
66 lines (60 loc) · 2.41 KB
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
namespace S7Tools.Core.Models.Jobs;
/// <summary>
/// Represents the type of hardware resource used in bootloader operations.
/// </summary>
public enum ResourceType
{
/// <summary>
/// Serial port resource (e.g., /dev/ttyUSB0, COM1).
/// </summary>
Serial = 0,
/// <summary>
/// TCP network port resource (e.g., port 10102 for socat bridge).
/// </summary>
Tcp = 1,
/// <summary>
/// Modbus communication channel for power supply control.
/// </summary>
Modbus = 2
}
/// <summary>
/// Represents a unique identifier for a system resource.
/// Used for resource locking and coordination across concurrent jobs.
/// </summary>
/// <param name="Kind">The type of resource (e.g., "serial", "tcp", "power").</param>
/// <param name="Id">The unique identifier for the specific resource instance.</param>
/// <remarks>
/// Equality comparison is case-insensitive for both Kind and Id to prevent duplicate
/// resource keys caused by inconsistent casing (e.g., "Serial" vs "serial", "COM1" vs "com1").
/// </remarks>
public readonly record struct ResourceKey(string Kind, string Id)
{
/// <summary>
/// Returns a string representation of the resource key.
/// </summary>
/// <returns>A string in the format "Kind:Id".</returns>
public override string ToString() => $"{Kind}:{Id}";
/// <summary>
/// Determines whether this ResourceKey equals another ResourceKey using case-insensitive comparison.
/// </summary>
/// <param name="other">The other ResourceKey to compare.</param>
/// <returns>True if both Kind and Id match case-insensitively; otherwise, false.</returns>
public bool Equals(ResourceKey other)
{
return string.Equals(Kind, other.Kind, StringComparison.OrdinalIgnoreCase) &&
string.Equals(Id, other.Id, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Returns a hash code for this ResourceKey using case-insensitive string comparison.
/// </summary>
/// <returns>A hash code computed from the uppercase Kind and Id strings.</returns>
public override int GetHashCode()
{
// Use OrdinalIgnoreCase comparer's hash code to ensure consistent hashing
// for case-insensitive equality
return HashCode.Combine(
StringComparer.OrdinalIgnoreCase.GetHashCode(Kind ?? string.Empty),
StringComparer.OrdinalIgnoreCase.GetHashCode(Id ?? string.Empty)
);
}
}