-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAMP.cs
305 lines (247 loc) · 12 KB
/
SAMP.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Net.Http;
namespace EAACtrl
{
internal class SAMP
{
public bool StandardProfile = false;
public bool SAMPConnected = false;
public string SAMP_PrivateKey = "";
public string Samp_hub_url = @"http://127.0.0.1:21012"; //Web Profile
private static readonly HttpClient httpClient = new HttpClient();
private string sMsg = "";
public string Message
{
get
{
return sMsg;
}
}
public string SampMessage(string url, string message)
{
string result = "";
try
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var content = new StringContent(message, Encoding.UTF8, "application/xml");
HttpResponseMessage response = httpClient.PostAsync(url, content).GetAwaiter().GetResult();
result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
TimeSpan ts = stopwatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}.{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
sMsg = $"SAMP Request to {url} {elapsedTime}";
}
catch (HttpRequestException e)
{
sMsg = $"SAMP Request {url} ERROR {e.Message}";
result = "exception";
}
return result;
}
public string SampRegister()
{
string result = "";
//string sXML = "<?xml version='1.0'?><methodCall><methodName>samp.hub.register</methodName><params><param><value><struct><member><name>samp.name</name><value><string>EAACtrl</string></value></member></struct></value></param></params></methodCall>";
string sSAMPRegister = "<?xml version='1.0'?><methodCall><methodName>samp.hub.register</methodName><params><param><value>[SECRETorNAME]</value></param></params></methodCall>";
if (StandardProfile)
{
// Locate home folder and read the lock file
string sHomeFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (File.Exists(sHomeFolder + @"\.samp"))
{
// Read the lock file
string sSAMPLockFileContent = File.ReadAllText(sHomeFolder + @"\.samp");
int ipos = sSAMPLockFileContent.IndexOf("samp.secret=");
int ilastpos = sSAMPLockFileContent.IndexOf(Environment.NewLine, ipos);
string sSamp_secret = sSAMPLockFileContent.Substring(ipos + 12, ilastpos - (ipos + 12));
sSAMPRegister = sSAMPRegister.Replace("[SECRETorNAME]", sSamp_secret);
ipos = sSAMPLockFileContent.IndexOf("samp.hub.xmlrpc.url=");
ilastpos = sSAMPLockFileContent.IndexOf(Environment.NewLine, ipos);
Samp_hub_url = sSAMPLockFileContent.Substring(ipos + 20, ilastpos - (ipos + 20));
sMsg ="SampRegister: Standard Profile, " + "Secret=" + sSamp_secret + ", " + "URL=" + Samp_hub_url + "\r\n";
}
else { return ("nolockfile"); }
}
else
{
StandardProfile = false;
sSAMPRegister = sSAMPRegister.Replace("[SECRETorNAME]", "EAACtrl");
sSAMPRegister = sSAMPRegister.Replace("samp.hub.register", "samp.webhub.register");
sMsg = "SampRegister: Web Profile, " + "URL=" + Samp_hub_url + "\r\n";
}
try
{
result = SampMessage(Samp_hub_url, sSAMPRegister);
if (result != "")
{
int iStart = 0, iEnd = 0, iPos = 0;
iPos = result.IndexOf("samp.private-key", 0);
iStart = result.IndexOf("<value>", iPos) + 7;
iEnd = result.IndexOf("</value>", iStart);
result = result.Substring(iStart, iEnd - iStart);
SAMP_PrivateKey = result;
sMsg = $"SampRegister: key={result}, {sMsg}\r\n";
SAMPConnected = true;
}
else
{
sMsg = $"SampRegister: Can't Connect!, {sMsg}\r\n";
}
}
catch (Exception e)
{
sMsg = $"SampRegister ERROR {e.Message}\r\n";
result = "exception";
}
return result;
}
public string SampDisconnect()
{
string result = "";
//string sXML = "<?xml version='1.0'?><methodCall><methodName>samp.webhub.register</methodName><params><param><value><struct><member><name>samp.name</name><value><string>EAACtrl</string></value></member></struct></value></param></params></methodCall>";
string sSAMPDisconnect = "<?xml version='1.0'?><methodCall><methodName>samp.hub.unregister</methodName><params><param><value>" + SAMP_PrivateKey + "</value></param></params></methodCall>";
if (!StandardProfile)
{
sSAMPDisconnect = sSAMPDisconnect.Replace("samp.hub.unregister", "samp.webhub.unregister");
}
try
{
result = SampMessage(Samp_hub_url, sSAMPDisconnect);
SAMPConnected = false;
sMsg = $"SampDisconnect, {sMsg}\r\n";
}
catch (Exception e)
{
sMsg = $"SampDisconnect ERROR {e.Message}\r\n";
result = "exception";
}
return result;
}
public string SampMetaData()
{
string result = "";
string sSAMPMetaData = "<?xml version='1.0'?><methodCall><methodName>samp.hub.declareMetadata</methodName><params><param><value><string>[PRIVATEKEY]</string></value></param>" +
"<param><value><struct><member><name>samp.name</name><value><string>EAACtrl</string></value></member><member><name>samp.description</name>" +
"<value><string>Manges EAA workflow by coordinating astronomy apps such as AstroPlanner, SharpCap, Stellarium etc.</string></value></member>" +
"<member><name>samp.author</name><value><string>Pete Gallop</string></value></member></struct></value></param></params></methodCall>";
sSAMPMetaData = sSAMPMetaData.Replace("[PRIVATEKEY]", SAMP_PrivateKey);
if (!StandardProfile)
{
sSAMPMetaData = sSAMPMetaData.Replace("samp.hub.declareMetadata", "samp.webhub.declareMetadata");
}
try
{
result = SampMessage(Samp_hub_url, sSAMPMetaData);
sMsg = $"SampMetaData, {sMsg}\r\n";
}
catch (Exception e)
{
sMsg = $"SampMetaData ERROR {e.Message}\r\n";
result = "exception";
}
return result;
}
public string Samp_coord_pointAt_sky(string RA, string Dec)
{
if (!SAMPConnected)
{ return "NOTCONNECTED"; }
string result = "";
string sSAMPCoordPointAt = "<?xml version='1.0'?><methodCall><methodName>samp.hub.notifyAll</methodName><params>";
sSAMPCoordPointAt += "<param><value>" + SAMP_PrivateKey + "</value></param>";
sSAMPCoordPointAt += "<param><value><struct><member><name>samp.mtype</name><value>coord.pointAt.sky</value></member><member><name>samp.params</name><value><struct>";
sSAMPCoordPointAt += "<member><name>ra</name><value>" + RA + "</value></member>";
sSAMPCoordPointAt += "<member><name>dec</name><value>" + Dec + "</value></member>";
sSAMPCoordPointAt += "</struct></value></member></struct></value></param></params></methodCall>";
if (!StandardProfile)
{
sSAMPCoordPointAt = sSAMPCoordPointAt.Replace("samp.hub.notifyAll", "samp.webhub.notifyAll");
}
try
{
result = SampMessage(Samp_hub_url, sSAMPCoordPointAt);
sMsg = $"SampCoordPointAt {RA}, {Dec}, {sMsg} \r\n";
}
catch (Exception e)
{
sMsg = $"SampCoordPointAt ERROR {e.Message}\r\n";
result = "exception";
}
return result;
}
public string Samp_script_aladin_send(string sScriptCmd)
{
if (!SAMPConnected)
{ return "NOTCONNECTED"; }
string result = "";
string sSAMPScript = "<?xml version='1.0'?><methodCall><methodName>samp.hub.notifyAll</methodName><params>";
sSAMPScript += "<param><value>" + SAMP_PrivateKey + "</value></param>";
sSAMPScript += "<param><value><struct><member><name>samp.mtype</name><value>script.aladin.send</value></member><member><name>samp.params</name><value><struct>";
sSAMPScript += "<member><name>script</name><value>" + sScriptCmd + "</value></member>";
sSAMPScript += "</struct></value></member></struct></value></param></params></methodCall>";
if (!StandardProfile)
{
sSAMPScript = sSAMPScript.Replace("samp.hub.notifyAll", "samp.webhub.notifyAll");
}
try
{
result = SampMessage(Samp_hub_url, sSAMPScript);
sMsg = $"SampAladinCmd {sScriptCmd}, {sMsg}\r\n";
}
catch (Exception e)
{
sMsg = $"SampScript ERROR {e.Message}\r\n";
result = "exception";
}
return result;
}
private string Samp_getRegisteredClients()
{
string result = "";
//string sXML = "<?xml version='1.0'?><methodCall><methodName>samp.webhub.register</methodName><params><param><value><struct><member><name>samp.name</name><value><string>EAACtrl</string></value></member></struct></value></param></params></methodCall>";
string sSAMPGetRegClients = "<?xml version='1.0'?><methodCall><methodName>samp.hub.getRegisteredClients</methodName><params><param><value>" + SAMP_PrivateKey + "</value></param></params></methodCall>";
if (!StandardProfile)
{
sSAMPGetRegClients = sSAMPGetRegClients.Replace("samp.hub.unregister", "samp.webhub.unregister");
}
try
{
result = SampMessage(Samp_hub_url, sSAMPGetRegClients);
sMsg = $"SampGetRegClients, {sMsg}\r\n";
}
catch (Exception e)
{
sMsg = $"SAMPGetRegClients ERROR {e.Message}\r\n";
result = "exception";
}
return result;
}
private string Samp_coord_get_sky()
{
if (!SAMPConnected)
{ return "NOTCONNECTED"; }
string result = "";
string sSAMPGetCoordPoint = "<?xml version='1.0'?><methodCall><methodName>samp.hub.notify</methodName><params>";
sSAMPGetCoordPoint += "<param><value>" + SAMP_PrivateKey + "</value></param><param><value>c1</value></param>";
sSAMPGetCoordPoint += "<param><value><struct><member><name>samp.mtype</name><value>coord.get.sky</value></member>";
sSAMPGetCoordPoint += "</struct></value></param></params></methodCall>";
if (!StandardProfile)
{
sSAMPGetCoordPoint = sSAMPGetCoordPoint.Replace("samp.hub.notifyAll", "samp.webhub.notifyAll");
}
try
{
result = SampMessage(Samp_hub_url, sSAMPGetCoordPoint);
sMsg = $"SampGetCoordPoint, {sMsg}\r\n";
}
catch (Exception e)
{
sMsg = $"SampGetCoordPoint ERROR {e.Message}\r\n";
result = "exception";
}
return result;
}
}
}