forked from iliaplatone/indisharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINDIClient.cs
681 lines (645 loc) · 21.2 KB
/
INDIClient.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
MIT License
Copyright (c) 2015-2021 Ilia Platone
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Net.Sockets;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Windows.Forms;
using System.Xml;
using System.Linq;
using System.Text.RegularExpressions;
namespace INDI
{
#region Event Argument classes
public class MessageReceivedEventArgs : EventArgs
{
public string Message;
public MessageReceivedEventArgs(string msg)
{
Message = msg;
}
}
public class DeviceAddedEventArgs : EventArgs
{
public INDIDevice Device;
public DeviceAddedEventArgs(INDIDevice dev)
{
Device = dev;
}
}
public class MessageSentEventArgs : EventArgs
{
public string Message;
public MessageSentEventArgs(string msg)
{
Message = msg;
}
}
public class IsNewTextEventArgs : EventArgs
{
public ITextVector Vector;
public string Device;
public IsNewTextEventArgs(ITextVector vector, string dev)
{
Vector = vector;
Device = dev;
}
}
public class IsNewNumberEventArgs : EventArgs
{
public INumberVector Vector;
public string Device;
public IsNewNumberEventArgs(INumberVector vector, string dev)
{
Vector = vector;
Device = dev;
}
}
public class IsNewSwitchEventArgs : EventArgs
{
public ISwitchVector Vector;
public string Device;
public IsNewSwitchEventArgs(ISwitchVector vector, string dev)
{
Vector = vector;
Device = dev;
}
}
public class IsNewBlobEventArgs : EventArgs
{
public IBlobVector Vector;
public string Device;
public IsNewBlobEventArgs(IBlobVector vector, string dev)
{
Vector = vector;
Device = dev;
}
}
public class IsDelPropertyEventArgs : EventArgs
{
public string Vector;
public string Device;
public IsDelPropertyEventArgs(string vector, string dev)
{
Vector = vector;
Device = dev;
}
}
public class IsNewMessageEventArgs : EventArgs
{
public string Message;
public string Device;
public DateTime Timestamp;
public IsNewMessageEventArgs(string message, DateTime timestamp, string dev)
{
Timestamp = timestamp;
Message = message;
Device = dev;
}
}
#endregion
public class INDIClient : IDisposable
{
#region Private Variables
TcpClient client;
Stream stream;
Thread ReadThread;
Thread SendThread;
Boolean ThreadsRunning;
String _inputString = "";
String _outputString = "";
Queue inputString = new Queue();
Queue outputString = new Queue();
Int32 _BufferSize = 0x1000000;
int _CommandSize = 0x10;
#endregion
#region Public Properties
public Int32 BufferSize
{
get
{
return _BufferSize;
}
set
{
_BufferSize = value;
}
}
public Int32 CommandSize
{
get
{
return _CommandSize;
}
set
{
_CommandSize = value;
}
}
public Boolean Connected
{
get
{
if (client != null)
return client.Connected;
return false;
}
}
public String InputString
{
get
{
return _inputString;
}
set
{
_inputString = value.Replace("\r", "").Replace("\n", "");
ReadThread = new Thread(new ParameterizedThreadStart(_readThread));
ReadThread.IsBackground = true;
ReadThread.Name = "INDISharp Client read thread";
ReadThread.Start(_inputString);
}
}
string _lastmessage = "";
string lastmessage { get { return _lastmessage; } set { if(_lastmessage == "") _lastmessage = value; else _lastmessage = ""; } }
public String OutputString
{
get
{
if (outputString.Count > 0)
return (string)outputString.Dequeue();
return "";
}
set
{
if (!String.IsNullOrEmpty (value)) {
_outputString += value.Replace ("\r", "").Replace ("\n", "");
outputString.Enqueue (_outputString);
}
_outputString = "";
}
}
#endregion
#region Public Variables
public string Name = "";
public string Address = "127.0.0.1";
public int Port = 7624;
public List<INDIDevice> Devices = new List<INDIDevice>();
public event EventHandler<MessageSentEventArgs> MessageSent;
public event EventHandler<DeviceAddedEventArgs> DeviceAdded;
public event EventHandler<IsDelPropertyEventArgs> IsDelProperty;
public event EventHandler<IsNewBlobEventArgs> IsNewBlob;
public event EventHandler<IsNewTextEventArgs> IsNewText;
public event EventHandler<IsNewNumberEventArgs> IsNewNumber;
public event EventHandler<IsNewSwitchEventArgs> IsNewSwitch;
public event EventHandler<IsNewMessageEventArgs> IsNewMessage;
#endregion
#region Constructors / Initialization
public INDIClient(string uri)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Uri u = new Uri("http://" + uri);
Port = u.Port;
if (!uri.Contains(":"))
Port = 7624;
Address = u.Host;
}
public INDIClient(string address, int port)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Address = address;
Port = port;
}
public INDIClient(Stream s)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Address = "";
Port = 0;
stream = s;
}
public INDIClient()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Address = "";
Port = 0;
stream = null;
}
public bool Connect(string address = "", int port = 0)
{
if (address != "")
Address = address;
if (port != 0)
Port = port;
ThreadsRunning = true;
if (Address != "" && Port > 0)
{
try
{
client = new TcpClient(Address, Port);
if (Connected)
{
stream = client.GetStream();
SendThread = new Thread(_sendThread);
SendThread.IsBackground = true;
SendThread.Name = "INDISharp Client send thread";
SendThread.Start();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR");
}
}
if (stream != null)
{
ReadThread = new Thread(new ParameterizedThreadStart(_readThread));
ReadThread.IsBackground = true;
ReadThread.Name = "INDISharp Client read thread";
ReadThread.Start(null);
return true;
}
return false;
}
public void Dispose()
{
Disconnect();
Devices.Clear();
}
public void Disconnect()
{
ThreadsRunning = false;
Thread.Sleep(100);
outputString.Clear();
inputString.Clear();
if (stream != null)
stream.Close();
if (client != null)
client.Close();
}
#endregion
#region Threads
void _sendThread()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
while (ThreadsRunning)
{
try
{
if (outputString.Count > 0 && Connected)
{
string message = OutputString;
lastmessage = message;
if (!String.IsNullOrEmpty(message))
{
byte[] buffer = Encoding.UTF8.GetBytes(message);
stream.Write(buffer, 0, buffer.Length);
MessageSent?.Invoke(this, new MessageSentEventArgs(message));
}
}
}
catch
{
continue;
}
Thread.Sleep(100);
}
}
void _readThread(object s)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
string message = "";
int tmr = 0;
while (ThreadsRunning)
{
try
{
if(s == null) {
while (((NetworkStream)stream).DataAvailable)
{
tmr = 0;
byte[] buffer = new byte[client.Available];
stream.Read(buffer, 0, buffer.Length);
message += RemoveInvalidXmlChars(Encoding.UTF8.GetString(buffer));
if(IsValidXml(message))
{
ParseXML(message.Replace("\n", "").Replace("\r", ""));
message = "";
}
Thread.Sleep(300);
}
if(tmr++ > 5)
{
OutputString = lastmessage;
message = "";
}
}
else {
ParseXML(((String)s).Replace("\n", "").Replace("\r", ""));
break;
}
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
continue;
}
Thread.Sleep(100);
}
}
public static bool IsValidXml(string xmlString)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<document>" + xmlString + "</document>");
return true;
}
catch (Exception e1)
{
Console.WriteLine (e1.Message);
return false;
}
}
bool ParseXML(String xml)
{
string action = "";
string target = "";
string device = "";
string name = "";
string label = "";
string vectorname = "";
string vectorlabel = "";
string group = "";
string permission = "";
string rule = "";
string format = "";
string length = "";
string minimum = "";
string maximum = "";
string step = "";
IBlobVector blobvector = new IBlobVector ("", "", "", "", "", "", null);
List<INDIBlob> blobs = new List<INDIBlob> ();
ISwitchVector switchvector = new ISwitchVector ("", "", "", "", "", "", null);
List<INDISwitch> switches = new List<INDISwitch> ();
INumberVector numbervector = new INumberVector ("", "", "", "", "", "", null);
List<INDINumber> numbers = new List<INDINumber> ();
ITextVector textvector = new ITextVector ("", "", "", "", "", "", null);
List<INDIText> texts = new List<INDIText> ();
try {
XmlReaderSettings settings = new XmlReaderSettings ();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(new StringReader(RemoveInvalidXmlChars(xml)), settings);
while (reader.Read ()) {
try {
switch (reader.NodeType) {
case XmlNodeType.Element:
action = reader.Name.ToLower ().Substring (0, 3);
target = reader.Name.Substring (3, reader.Name.Length - 3).ToLower ();
action = action == null ? "" : action;
target = target == null ? "" : target;
if (action == "mes") {
try {
if (IsNewMessage != null)
IsNewMessage (this, new IsNewMessageEventArgs (reader.GetAttribute ("message"), DateTime.Parse (reader.GetAttribute ("timestamp")), reader.GetAttribute ("device")));
} catch {
}
}
if (action == "del" && target.Contains ("property")) {
try {
if (IsDelProperty != null && name != "")
IsDelProperty (this, new IsDelPropertyEventArgs (name, reader.GetAttribute ("device")));
} catch {
}
}
if (action == "get" && target.Contains ("properties")) {
try {
DefineProperties (reader.GetAttribute ("device"));
} catch {
}
}
if (target.Contains ("vector")) {
device = reader.GetAttribute ("device");
vectorname = reader.GetAttribute ("name");
vectorlabel = reader.GetAttribute ("label");
group = reader.GetAttribute ("group");
permission = reader.GetAttribute ("perm");
rule = reader.GetAttribute ("rule");
device = device == null ? "" : device;
vectorname = vectorname == null ? "" : vectorname;
vectorlabel = vectorlabel == null ? "" : vectorlabel;
group = group == null ? "" : group;
permission = permission == null ? "ro" : permission;
rule = rule == null ? "" : rule;
if (device == "" || vectorname == "")
break;
AddDevice (new INDIDevice (device, this));
if (target.Contains ("blob")) {
blobs = new List<INDIBlob> ();
blobvector = GetDevice (device).GetBlobVector (vectorname);
if (blobvector == null)
blobvector = new IBlobVector (device, vectorname, vectorlabel, group, permission, rule, blobs);
blobvector.Values = blobs;
}
if (target.Contains ("switch")) {
switches = new List<INDISwitch> ();
switchvector = GetDevice (device).GetSwitchVector (vectorname);
if (switchvector == null)
switchvector = new ISwitchVector (device, vectorname, vectorlabel, group, permission, rule, switches);
switchvector.Values = switches;
}
if (target.Contains ("number")) {
numbers = new List<INDINumber> ();
numbervector = GetDevice (device).GetNumberVector (vectorname);
if (numbervector == null)
numbervector = new INumberVector (device, vectorname, vectorlabel, group, permission, rule, numbers);
numbervector.Values = numbers;
}
if (target.Contains ("text")) {
texts = new List<INDIText> ();
textvector = GetDevice (device).GetTextVector (vectorname);
if (textvector == null)
textvector = new ITextVector (device, vectorname, vectorlabel, group, permission, rule, texts);
textvector.Values = texts;
}
} else {
name = reader.GetAttribute ("name");
label = reader.GetAttribute ("label");
name = name == null ? "" : name;
label = label == null ? "" : label;
if (target.Contains ("blob")) {
format = reader.GetAttribute ("format");
length = reader.GetAttribute ("size");
format = format == null ? "" : format;
length = length == null ? "1" : length;
}
if (target.Contains ("number")) {
format = reader.GetAttribute ("format");
minimum = reader.GetAttribute ("min");
maximum = reader.GetAttribute ("max");
step = reader.GetAttribute ("step");
format = format == null ? "" : format;
minimum = minimum == null ? "1" : minimum;
maximum = maximum == null ? "1" : maximum;
step = step == null ? "1" : step;
}
}
break;
case XmlNodeType.Text:
if (!target.Contains ("vector")) {
if (target.Contains ("blob")) {
try {
blobs.Add (new INDIBlob (name, label, format, Convert.FromBase64String (reader.Value.Replace ("\n", "")), Int32.Parse (length)));
}
catch {
blobs.Add (new INDIBlob (name, label, format, new byte[Int32.Parse (length)], Int32.Parse (length)));
}
}
if (target.Contains ("switch")) {
switches.Add (new INDISwitch (name, label, reader.Value.Replace ("\n", "").Contains ("On")));
}
if (target.Contains ("number")) {
numbers.Add (new INDINumber (name, label, format, Double.Parse (minimum), Double.Parse (maximum), Double.Parse (step), Double.Parse (reader.Value.Replace ("\n", ""))));
}
if (target.Contains ("text")) {
texts.Add (new INDIText (name, label, reader.Value.Replace ("\n", "")));
}
}
break;
case XmlNodeType.XmlDeclaration:
Console.WriteLine ("<?xml version='1.0'?>");
break;
case XmlNodeType.EntityReference:
Console.WriteLine (reader.Name);
break;
case XmlNodeType.EndElement:
if (reader.Name.ToLower ().Contains ("vector")) {
if (device == "" || vectorname == "")
break;
if (target.Contains ("blob")) {
if (IsNewBlob != null)
IsNewBlob (this, new IsNewBlobEventArgs (blobvector, device));
}
if (target.Contains ("switch")) {
if (IsNewSwitch != null)
IsNewSwitch (this, new IsNewSwitchEventArgs (switchvector, device));
}
if (target.Contains ("number")) {
if (IsNewNumber != null)
IsNewNumber (this, new IsNewNumberEventArgs (numbervector, device));
}
if (target.Contains ("text")) {
if (IsNewText != null)
IsNewText (this, new IsNewTextEventArgs (textvector, device));
}
}
break;
}
} catch (XmlException ex) {
Console.WriteLine (ex.Message + Environment.NewLine + ex.StackTrace);
return false;
} catch (Exception ex) {
Console.WriteLine (ex.Message + Environment.NewLine + ex.StackTrace);
return false;
}
}
reader.Close ();
return true;
} catch (Exception ex) {
Console.WriteLine (ex.Message + Environment.NewLine + ex.StackTrace);
return false;
}
}
static string RemoveInvalidXmlChars(string text) {
var validXmlChars = text.Where(ch => XmlConvert.IsXmlChar(ch)).ToArray();
return new string(validXmlChars);
}
static bool IsValidXmlString(string text) {
try {
XmlConvert.VerifyXmlChars(text);
return true;
} catch {
return false;
}
}
#endregion
#region Device releated methods
public DRIVER_INTERFACE Interfaces = DRIVER_INTERFACE.GENERAL_INTERFACE;
public void AddDevice(INDIDevice dev)
{
if (GetDevice(dev.Name) == null && dev != null && dev.Name != "" && (dev.DriverInterface & Interfaces) == Interfaces)
{
Devices.Add(dev);
if (DeviceAdded != null)
DeviceAdded(this, new DeviceAddedEventArgs(GetDevice(dev.Name)));
}
}
public void RemoveDevice(string dev)
{
if (GetDevice(dev) != null)
Devices.Remove(GetDevice(dev));
}
public INDIDevice GetDevice(string name)
{
foreach (INDIDevice d in Devices)
if (d.Name == name)
return d;
return null;
}
public string QueryProperties()
{
string ret =
new XElement("getProperties",
new XAttribute("version", "1.7")).ToString();
OutputString = ret;
return ret;
}
public string DefineProperties(string device = "")
{
string ret = "";
if (device == "" || device == null)
{
foreach (INDIDevice d in Devices)
ret += d.DefineProperties();
}
else
ret = GetDevice(device).DefineProperties();
OutputString = ret;
return ret;
}
#endregion
#region Stream releated methods, misc
public Stream GetStream()
{
return stream;
}
#endregion
}
}