-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathExtensionMethods.cs
155 lines (139 loc) · 6.34 KB
/
ExtensionMethods.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
namespace ASCOM.Remote
{
public static class ExtensionMethods
{
/// <summary>
/// Lookup dictionary to translate lower case method names to mixed case.
/// The device type is included in order to disambiguate the same method name if used in different device types AND cased differently/>
/// </summary>
private static readonly Dictionary<string, string> methodLookup = new()
{
{ "connected","Connected" },
// Telescope
{ "tracking","Tracking" },
{ "doesrefraction","DoesRefraction" },
{ "slewsettletime","SlewSettleTime" },
{ "declinationrate","DeclinationRate" },
{ "rightascensionrate","RightAscensionRate" },
{ "guideratedeclination","GuideRateDeclination" },
{ "guideraterightascension","GuideRateRightAscension" },
{ "sideofpier", "SideOfPier" },
{ "siteelevation","SiteElevation" },
{ "sitelatitude","SiteLatitude" },
{ "sitelongitude","SiteLongitude" },
{ "targetdeclination","TargetDeclination" },
{ "targetrightascension","TargetRightAscension" },
{ "utcdate","UTCDate" },
{ "trackingrate","TrackingRate" },
{ SharedConstants.AXIS_PARAMETER_NAME,SharedConstants.AXIS_PARAMETER_NAME },
{ SharedConstants.RA_PARAMETER_NAME,SharedConstants.RA_PARAMETER_NAME },
{ SharedConstants.DEC_PARAMETER_NAME,SharedConstants.DEC_PARAMETER_NAME },
{ SharedConstants.RATE_PARAMETER_NAME,SharedConstants.RATE_PARAMETER_NAME },
{ SharedConstants.DIRECTION_PARAMETER_NAME,SharedConstants.DIRECTION_PARAMETER_NAME },
{ SharedConstants.DURATION_PARAMETER_NAME,SharedConstants.DURATION_PARAMETER_NAME },
{ SharedConstants.ALT_PARAMETER_NAME,SharedConstants.ALT_PARAMETER_NAME },
{ SharedConstants.AZ_PARAMETER_NAME,SharedConstants.AZ_PARAMETER_NAME },
// Rotator
{ SharedConstants.POSITION_PARAMETER_NAME,SharedConstants.POSITION_PARAMETER_NAME},
{ "reverse","Reverse" },
// Camera
{ "binx","BinX" },
{ "biny","BinY"},
{ "cooleron","CoolerOn"},
{ "fastreadout","FastReadout"},
{ "gain","Gain"},
{ "numx","NumX"},
{ "numy","NumY"},
{ "offset","Offset"},
{ "readoutmode","ReadoutMode"},
{ "setccdtemperature","SetCCDTemperature"},
{ "startx","StartX"},
{ "starty","StartY"},
{ "subexposureduration","SubExposureDuration"},
{ "slaved","Slaved" },
{ "Brightness","Brightness" },
{ "position","Position" },
{ "tempcomp","TempComp" },
{ SharedConstants.SENSORNAME_PARAMETER_NAME,SharedConstants.SENSORNAME_PARAMETER_NAME },
{ "averageperiod","AveragePeriod" },
{ "sensordescription","SensorDescription" },
{ SharedConstants.ID_PARAMETER_NAME,SharedConstants.ID_PARAMETER_NAME },
{ SharedConstants.NAME_PARAMETER_NAME,SharedConstants.NAME_PARAMETER_NAME },
{ SharedConstants.VALUE_PARAMETER_NAME,SharedConstants.VALUE_PARAMETER_NAME },
{ SharedConstants.STATE_PARAMETER_NAME,SharedConstants.STATE_PARAMETER_NAME },
{ "id","Id" }
};
/// <summary>
/// Look up the cased version of a method name.
/// </summary>
/// <param name="methodName">The method name to look up</param>
/// <param name="deviceType">Optional device type to disambiguate method names that are used in more than 1 device type AND cased differently</param>
/// <exception cref="KeyNotFoundException">When the required method name key has not yet been added to the lookup dictionary.</exception>
/// <returns>The mixed case equivalent of the supplied method name</returns>
public static string ToCorrectCase(this string methodName, string deviceType = null)
{
string lookupKey = "ValueNotSet";
// Look up the cased version of the method. If this fails a KeyNotFoiund exception will be thrown
try
{
lookupKey = $"{methodName}{(deviceType is null ? "" : $".{deviceType}")}";
return methodLookup[lookupKey];
}
catch (Exception ex)
{
return $"UnknownMethod:'{lookupKey}' - {ex}";
}
}
/// <summary>
/// Append a byte array to an existing array
/// </summary>
/// <param name="first">First array</param>
/// <param name="second">Second array</param>
/// <returns>Concatenated array of byte</returns>
public static byte[] Append(this byte[] first, byte[] second)
{
byte[] ret = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, ret, 0, first.Length);
Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
return ret;
}
public static string ToConcatenatedString(this List<string> list, string separator)
{
string concatenatedList = "";
foreach (string item in list)
{
concatenatedList += item + separator;
}
return concatenatedList.Trim(separator.ToCharArray());
}
public static void FromConcatenatedString(this List<string> list, string concatenatedString, string separator)
{
string[] items = concatenatedString.Split(separator.ToCharArray());
list.Clear();
foreach (string item in items)
{
list.Add(item);
}
}
public static List<StringValue> ToListStringValue(this List<string> fromList)
{
List<StringValue> toList = [];
foreach (string item in fromList)
{
toList.Add(new StringValue(item));
}
return toList;
}
public static List<string> ToListString(this List<StringValue> fromList)
{
List<string> toList = [];
foreach (StringValue item in fromList)
{
toList.Add(item.Value);
}
return toList;
}
}
}