forked from BySlin/VSCaptureMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass1.cs
1900 lines (1522 loc) · 80.5 KB
/
Class1.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of VitalSignsCaptureMP v1.012.
* Copyright (C) 2017-24 John George K., [email protected]
VitalSignsCaptureMP is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VitalSignsCaptureMP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with VitalSignsCaptureMP. If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Threading;
using System.Net.Http;
using System.Text.Json;
using MQTTnet;
using MQTTnet.Client;
//using MQTTnet.Client.Options;
using MQTTnet.Extensions.ManagedClient;
//using MQTTnet.Client.Connecting;
//using MQTTnet.Diagnostics;
namespace VSCaptureMP
{
public class MPudpclient : UdpClient
{
public IPEndPoint m_remoteIPtarget;
public List<NumericValResult> m_NumericValList = new List<NumericValResult>();
public List<string> m_NumValHeaders = new List<string>();
public StringBuilder m_strbuildvalues = new StringBuilder();
public StringBuilder m_strbuildheaders = new StringBuilder();
public List<WaveValResult> m_WaveValResultList = new List<WaveValResult>();
public StringBuilder m_strbuildwavevalues = new StringBuilder();
public bool m_transmissionstart = true;
public string m_strTimestamp;
public ushort m_actiontype;
public int m_elementcount = 0;
public int m_headerelementcount = 0;
public int m_csvexportset = 1;
public List<SaSpec> m_SaSpecList = new List<SaSpec>();
public List<SaCalibData16> m_SaCalibDataSpecList = new List<SaCalibData16>();
public List<ScaleRangeSpec16> m_ScaleRangeSpecList = new List<ScaleRangeSpec16>();
public List<IDLabel> m_IDLabelList = new List<IDLabel>();
public bool m_calibratewavevalues = false;
public ushort m_obpollhandle = 0;
public uint m_idlabelhandle = 0;
public string m_idlabelstring;
public DateTime m_baseDateTime = new DateTime();
public DateTime m_pollDateTime = new DateTime();
public uint m_baseRelativeTime = 0;
public string m_DeviceID;
public string m_jsonposturl;
public int m_dataexportset = 1;
public string m_MQTTUrl;
public string m_MQTTtopic;
public string m_MQTTuser;
public string m_MQTTpassw;
public string m_MQTTclientId = Guid.NewGuid().ToString();
public class NumericValResult
{
public string Timestamp;
public string Relativetimestamp;
public string SystemLocalTime;
public string PhysioID;
public string Value;
public string DeviceID;
}
public class WaveValResult
{
public string Timestamp;
public string Relativetimestamp;
public string SystemLocalTime;
public string PhysioID;
public byte[] Value;
public string DeviceID;
public ushort obpoll_handle;
public SaSpec saSpecData = new SaSpec();
public SaCalibData16 saCalibData = new SaCalibData16();
public ScaleRangeSpec16 ScaleRangeSpec16 = new ScaleRangeSpec16();
}
public class IDLabel
{
public uint idlabelhandle;
public string idlabelstring;
public ushort obpoll_handle;
}
//Create a singleton udpclient subclass
private static volatile MPudpclient MPClient = null;
public static MPudpclient getInstance
{
get
{
if (MPClient == null)
{
lock (typeof(MPudpclient))
if (MPClient == null)
{
MPClient = new MPudpclient();
}
}
return MPClient;
}
}
public MPudpclient()
{
MPClient = this;
m_remoteIPtarget = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 24105);
MPClient.Client.ReceiveTimeout = 20000;
}
public void SendAssociationRequest()
{
//MPClient.Send(DataConstants.aarq_msg, DataConstants.aarq_msg.Length);
MPClient.Send(DataConstants.aarq_msg_ext_poll2, DataConstants.aarq_msg_ext_poll2.Length);
}
public void SendWaveAssociationRequest()
{
//MPClient.Send(DataConstants.aarq_msg_ext_poll2, DataConstants.aarq_msg_ext_poll2.Length);
MPClient.Send(DataConstants.aarq_msg_wave_ext_poll2, DataConstants.aarq_msg_wave_ext_poll2.Length);
}
public void SendMDSCreateEventResult()
{
MPClient.Send(DataConstants.mds_create_resp_msg, DataConstants.mds_create_resp_msg.Length);
}
public void SendMDSPollDataRequest()
{
MPClient.Send(DataConstants.poll_mds_request_msg, DataConstants.poll_mds_request_msg.Length);
}
public void SendPollDataRequest()
{
MPClient.Send(DataConstants.poll_request_msg, DataConstants.poll_request_msg.Length);
}
public void SendExtendedPollDataRequest()
{
MPClient.Send(DataConstants.ext_poll_request_msg3, DataConstants.ext_poll_request_msg3.Length);
//MPClient.Send(DataConstants.ext_poll_request_msg, DataConstants.ext_poll_request_msg.Length);
}
public void SendExtendedPollWaveDataRequest()
{
MPClient.Send(DataConstants.ext_poll_request_wave_msg, DataConstants.ext_poll_request_wave_msg.Length);
//MPClient.Send(DataConstants.ext_poll_request_msg3, DataConstants.ext_poll_request_msg3.Length)
}
public void GetRTSAPriorityListRequest()
{
MPClient.Send(DataConstants.get_rtsa_prio_msg, DataConstants.get_rtsa_prio_msg.Length);
}
public void SetRTSAPriorityListRequest()
{
MPClient.Send(DataConstants.set_rtsa_prio_msg, DataConstants.set_rtsa_prio_msg.Length);
}
public void SetRTSAPriorityList(int nWaveSetType)
{
List<byte> WaveTrType = new List<byte>();
CreateWaveformSet(nWaveSetType, WaveTrType);
SendRTSAPriorityMessage(WaveTrType.ToArray());
}
public static void CreateWaveformSet(int nWaveSetType, List<byte> WaveTrtype)
{
//Upto 3 ECG and/or 8 non-ECG waveforms can be polled by selecting the appropriate labels
//in the Wave object priority list
switch (nWaveSetType)
{
case 0:
break;
case 1:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x03))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x0C))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_II")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_I")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_III")))));
break;
case 2:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x06))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x18))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_II")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_ART_ABP")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_ART")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PULS_OXIM_PLETH")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_VEN_CENT")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_RESP")))));
break;
case 3:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x03))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x0C))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_AVR")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_AVL")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_AVF")))));
break;
case 4:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x03))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x0C))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_V1")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_V2")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_V3")))));
break;
case 5:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x03))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x0C))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_V4")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_V5")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_V6")))));
break;
case 6:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x04))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x10))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_EEG_NAMES_EEG_CHAN1_LBL")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_EEG_NAMES_EEG_CHAN2_LBL")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_EEG_NAMES_EEG_CHAN3_LBL")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_EEG_NAMES_EEG_CHAN4_LBL")))));
break;
case 7:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x02))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x08))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_ART_ABP")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_ART")))));
break;
case 8:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x06))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x18))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PULS_OXIM_PLETH")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_ART_ABP")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_ART")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_VEN_CENT")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_AWAY_CO2")))));
break;
case 9:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x06))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x18))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_II")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_ART")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_INTRA_CRAN")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_INTRA_CRAN_2")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_PRESS_BLD_VEN_CENT")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_TEMP_BLD")))));
break;
case 10:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x04))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x10))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_VUELINK_FLX1_NPS_TEXT_WAVE1")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_VUELINK_FLX1_NPS_TEXT_WAVE2")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_VUELINK_FLX1_NPS_TEXT_WAVE3")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_VUELINK_FLX1_NPS_TEXT_WAVE4")))));
break;
case 11:
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x04))); //count
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianshortus(0x10))); //length
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_EEG_ELEC_POTL_CRTX")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_EEG_ELEC_POTL_CRTX_LEFT")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_EEG_ELEC_POTL_CRTX_RIGHT")))));
WaveTrtype.AddRange(BitConverter.GetBytes(correctendianuint((uint)(Enum.Parse(typeof(DataConstants.WavesIDLabels), "NLS_NOM_ECG_ELEC_POTL_II")))));
break;
}
}
public void SendRTSAPriorityMessage(byte[] WaveTrType)
{
List<byte> tempbufflist = new List<byte>();
//Assemble request in reverse order first to calculate lengths
//Insert TextIdList
tempbufflist.InsertRange(0, WaveTrType);
Ava avatype = new Ava();
avatype.attribute_id = (ushort)IntelliVue.AttributeIDs.NOM_ATTR_POLL_RTSA_PRIO_LIST;
avatype.length = (ushort)WaveTrType.Length;
//avatype.length = (ushort)tempbufflist.Count;
tempbufflist.InsertRange(0, BitConverter.GetBytes(correctendianshortus(avatype.length)));
tempbufflist.InsertRange(0, BitConverter.GetBytes(correctendianshortus(avatype.attribute_id)));
byte[] AttributeModEntry = { 0x00, 0x00 };
tempbufflist.InsertRange(0, AttributeModEntry);
byte[] ModListlength = BitConverter.GetBytes(correctendianshortus((ushort)tempbufflist.Count));
byte[] ModListCount = { 0x00, 0x01 };
tempbufflist.InsertRange(0, ModListlength);
tempbufflist.InsertRange(0, ModListCount);
byte[] ManagedObjectID = { 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
tempbufflist.InsertRange(0, ManagedObjectID);
ROIVapdu rovi = new ROIVapdu();
rovi.length = (ushort)tempbufflist.Count;
rovi.command_type = (ushort)IntelliVue.Commands.CMD_CONFIRMED_SET;
rovi.inovke_id = 0x0000;
tempbufflist.InsertRange(0, BitConverter.GetBytes(correctendianshortus(rovi.length)));
tempbufflist.InsertRange(0, BitConverter.GetBytes(correctendianshortus(rovi.command_type)));
tempbufflist.InsertRange(0, BitConverter.GetBytes(correctendianshortus(rovi.inovke_id)));
ROapdus roap = new ROapdus();
roap.length = (ushort)tempbufflist.Count;
roap.ro_type = (ushort)IntelliVue.RemoteOperationHeader.ROIV_APDU;
tempbufflist.InsertRange(0, BitConverter.GetBytes(correctendianshortus(roap.length)));
tempbufflist.InsertRange(0, BitConverter.GetBytes(correctendianshortus(roap.ro_type)));
byte[] Spdu = { 0xE1, 0x00, 0x00, 0x02 };
tempbufflist.InsertRange(0, Spdu);
byte[] finaltxbuff = tempbufflist.ToArray();
MPClient.Send(finaltxbuff, finaltxbuff.Length);
}
public async Task SendCycledExtendedPollWaveDataRequest(int nInterval)
{
int nmillisecond = nInterval;
if (nmillisecond != 0)
{
do
{
MPClient.Send(DataConstants.ext_poll_request_wave_msg, DataConstants.ext_poll_request_wave_msg.Length);
await Task.Delay(nmillisecond);
}
while (true);
}
else MPClient.Send(DataConstants.ext_poll_request_wave_msg, DataConstants.ext_poll_request_wave_msg.Length);
}
public async Task SendCycledExtendedPollDataRequest(int nInterval)
{
int nmillisecond = nInterval;
if (nmillisecond != 0)
{
do
{
MPClient.Send(DataConstants.ext_poll_request_msg, DataConstants.ext_poll_request_msg.Length);
await Task.Delay(nmillisecond);
}
while (true);
}
else MPClient.Send(DataConstants.ext_poll_request_msg, DataConstants.ext_poll_request_msg.Length);
}
public async Task KeepConnectionAlive(int nInterval)
{
int nmillisecond = 6 * 1000;
if (nmillisecond != 0 && nInterval > 1000)
{
do
{
SendMDSCreateEventResult();
await Task.Delay(nmillisecond);
}
while (true);
}
}
public async Task RecheckMDSAttributes(int nInterval)
{
int nmillisecond = 60 * 1000;
if (nmillisecond != 0 && nInterval > 1000)
{
do
{
SendMDSPollDataRequest();
await Task.Delay(nmillisecond);
}
while (true);
}
}
public void ParseMDSCreateEventReport(byte[] readmdsconnectbuffer)
{
MemoryStream memstream = new MemoryStream(readmdsconnectbuffer);
BinaryReader binreader = new BinaryReader(memstream);
byte[] header = binreader.ReadBytes(34);
ushort attriblist_count = correctendianshortus(binreader.ReadUInt16());
ushort attriblist_length = correctendianshortus(binreader.ReadUInt16());
int avaobjectscount = Convert.ToInt32(attriblist_count);
if (avaobjectscount > 0)
{
byte[] attriblistobjects = binreader.ReadBytes(attriblist_length);
MemoryStream memstream2 = new MemoryStream(attriblistobjects);
BinaryReader binreader2 = new BinaryReader(memstream2);
for (int i = 0; i < avaobjectscount; i++)
{
Ava avaobjects = new Ava();
DecodeMDSAttribObjects(ref avaobjects, ref binreader2);
}
}
}
public void DecodeMDSAttribObjects(ref Ava avaobject, ref BinaryReader binreader)
{
avaobject.attribute_id = correctendianshortus(binreader.ReadUInt16());
avaobject.length = correctendianshortus(binreader.ReadUInt16());
//avaobject.attribute_val = correctendianshortus(binreader4.ReadUInt16());
if (avaobject.length > 0)
{
byte[] avaattribobjects = binreader.ReadBytes(avaobject.length);
switch (avaobject.attribute_id)
{
//Get Date and Time
case DataConstants.NOM_ATTR_TIME_ABS:
m_baseDateTime = GetAbsoluteTimeFromBCDFormat(avaattribobjects);
break;
//Get Relative Time attribute
case DataConstants.NOM_ATTR_TIME_REL:
GetBaselineRelativeTimestamp(avaattribobjects);
break;
//Get Patient demographics
case DataConstants.NOM_ATTR_PT_ID:
break;
case DataConstants.NOM_ATTR_PT_NAME_GIVEN:
break;
case DataConstants.NOM_ATTR_PT_NAME_FAMILY:
break;
case DataConstants.NOM_ATTR_PT_DOB:
break;
}
}
}
private static int BinaryCodedDecimalToInteger(int value)
{
if (value != 0xFF)
{
int lowerNibble = value & 0x0F;
int upperNibble = value >> 4;
int multipleOfOne = lowerNibble;
int multipleOfTen = upperNibble * 10;
return (multipleOfOne + multipleOfTen);
}
else return 0;
}
public DateTime GetAbsoluteTimeFromBCDFormat(byte[] bcdtimebuffer)
{
int century = BinaryCodedDecimalToInteger(bcdtimebuffer[0]);
int year = BinaryCodedDecimalToInteger(bcdtimebuffer[1]);
int month = BinaryCodedDecimalToInteger(bcdtimebuffer[2]);
int day = BinaryCodedDecimalToInteger(bcdtimebuffer[3]);
int hour = BinaryCodedDecimalToInteger(bcdtimebuffer[4]);
int minute = BinaryCodedDecimalToInteger(bcdtimebuffer[5]);
int second = BinaryCodedDecimalToInteger(bcdtimebuffer[6]);
int fraction = BinaryCodedDecimalToInteger(bcdtimebuffer[7]);
int formattedyear = (century * 100) + year;
DateTime dateTime = m_baseDateTime;
if (formattedyear != 0)
{
dateTime = new DateTime(formattedyear, month, day, hour, minute, second, fraction);
}
//m_baseDateTime = dateTime;
return dateTime;
}
public void GetBaselineRelativeTimestamp(byte[] timebuffer)
{
m_baseRelativeTime = correctendianuint(BitConverter.ToUInt32(timebuffer, 0));
}
public DateTime GetAbsoluteTimeFromRelativeTimestamp(uint currentRelativeTime)
{
double ElapsedTimeMilliseconds = Math.Abs(((double)currentRelativeTime - (double)m_baseRelativeTime) * 125 / 1000);
DateTime dtDateTime = m_baseDateTime.AddMilliseconds(ElapsedTimeMilliseconds);
return dtDateTime;
}
public void ReadData(byte[] readbuffer)
{
ProcessPacket(readbuffer);
}
public void ProcessPacket(byte[] packetbuffer)
{
MemoryStream memstream = new MemoryStream(packetbuffer);
BinaryReader binreader = new BinaryReader(memstream);
byte[] sessionheader = binreader.ReadBytes(4);
ushort ROapdu_type = correctendianshortus(binreader.ReadUInt16());
switch (ROapdu_type)
{
case DataConstants.ROIV_APDU:
// This is an MDS create event, answer with create response
ParseMDSCreateEventReport(packetbuffer);
SendMDSCreateEventResult();
break;
case DataConstants.RORS_APDU:
CheckPollPacketActionType(packetbuffer);
break;
case DataConstants.RORLS_APDU:
CheckLinkedPollPacketActionType(packetbuffer);
break;
case DataConstants.ROER_APDU:
break;
default:
break;
}
}
public void CheckPollPacketActionType(byte[] packetbuffer)
{
MemoryStream memstream = new MemoryStream(packetbuffer);
BinaryReader binreader = new BinaryReader(memstream);
byte[] header = binreader.ReadBytes(20);
ushort action_type = correctendianshortus(binreader.ReadUInt16());
m_actiontype = action_type;
switch (action_type)
{
case DataConstants.NOM_ACT_POLL_MDIB_DATA:
PollPacketDecoder(packetbuffer, 44);
break;
case DataConstants.NOM_ACT_POLL_MDIB_DATA_EXT:
PollPacketDecoder(packetbuffer, 46);
break;
default:
break;
}
}
public void CheckLinkedPollPacketActionType(byte[] packetbuffer)
{
MemoryStream memstream = new MemoryStream(packetbuffer);
BinaryReader binreader = new BinaryReader(memstream);
byte[] header = binreader.ReadBytes(22);
ushort action_type = correctendianshortus(binreader.ReadUInt16());
m_actiontype = action_type;
switch (action_type)
{
case DataConstants.NOM_ACT_POLL_MDIB_DATA:
PollPacketDecoder(packetbuffer, 46);
break;
case DataConstants.NOM_ACT_POLL_MDIB_DATA_EXT:
PollPacketDecoder(packetbuffer, 48);
break;
default:
break;
}
}
public void PollPacketDecoder(byte[] packetbuffer, int headersize)
{
int packetsize = packetbuffer.GetLength(0);
MemoryStream memstream = new MemoryStream(packetbuffer);
BinaryReader binreader = new BinaryReader(memstream);
byte[] header = binreader.ReadBytes(headersize);
byte[] packetdata = new byte[packetsize - header.Length];
Array.Copy(packetbuffer, header.Length, packetdata, 0, packetdata.Length);
m_strTimestamp = GetPacketTimestamp(header);
//DateTime dtDateTime = DateTime.Now;
uint currentRelativeTime = UInt32.Parse(m_strTimestamp);
DateTime dtDateTime = GetAbsoluteTimeFromRelativeTimestamp(currentRelativeTime);
string strDateTime = dtDateTime.ToString("dd-MM-yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
Console.WriteLine("Time:{0}", strDateTime);
Console.WriteLine("Time:{0}", m_strTimestamp);
//ParsePacketType
PollInfoList pollobjects = new PollInfoList();
int scpollobjectscount = DecodePollObjects(ref pollobjects, packetdata);
if (scpollobjectscount > 0)
{
MemoryStream memstream2 = new MemoryStream(pollobjects.scpollarray);
BinaryReader binreader2 = new BinaryReader(memstream2);
for (int i = 0; i < scpollobjectscount; i++)
{
SingleContextPoll scpoll = new SingleContextPoll();
int obpollobjectscount = DecodeSingleContextPollObjects(ref scpoll, ref binreader2);
if (obpollobjectscount > 0)
{
MemoryStream memstream3 = new MemoryStream(scpoll.obpollobjectsarray);
BinaryReader binreader3 = new BinaryReader(memstream3);
for (int j = 0; j < obpollobjectscount; j++)
{
ObservationPoll obpollobject = new ObservationPoll();
int avaobjectscount = DecodeObservationPollObjects(ref obpollobject, ref binreader3);
if (avaobjectscount > 0)
{
MemoryStream memstream4 = new MemoryStream(obpollobject.avaobjectsarray);
BinaryReader binreader4 = new BinaryReader(memstream4);
for (int k = 0; k < avaobjectscount; k++)
{
Ava avaobject = new Ava();
DecodeAvaObjects(ref avaobject, ref binreader4);
}
}
}
}
}
if (m_dataexportset == 2) ExportNumValListToJSON("Numeric");
if (m_dataexportset == 3) ExportNumValListToMQTT("Numeric");
if (m_dataexportset != 3)
{
ExportDataToCSV();
ExportWaveToCSV();
}
//clear memory
m_WaveValResultList.RemoveRange(0, m_WaveValResultList.Count);
}
}
public int DecodePollObjects(ref PollInfoList pollobjects, byte[] packetbuffer)
{
MemoryStream memstream = new MemoryStream(packetbuffer);
BinaryReader binreader = new BinaryReader(memstream);
pollobjects.count = correctendianshortus(binreader.ReadUInt16());
if (pollobjects.count > 0) pollobjects.length = correctendianshortus(binreader.ReadUInt16());
int scpollobjectscount = Convert.ToInt32(pollobjects.count);
if (pollobjects.length > 0) pollobjects.scpollarray = binreader.ReadBytes(pollobjects.length);
return scpollobjectscount;
}
public int DecodeSingleContextPollObjects(ref SingleContextPoll scpoll, ref BinaryReader binreader2)
{
scpoll.context_id = correctendianshortus(binreader2.ReadUInt16());
scpoll.count = correctendianshortus(binreader2.ReadUInt16());
//There can be empty singlecontextpollobjects
//if(scpoll.count>0) scpoll.length = correctendianshortus(binreader2.ReadUInt16());
scpoll.length = correctendianshortus(binreader2.ReadUInt16());
int obpollobjectscount = Convert.ToInt32(scpoll.count);
if (scpoll.length > 0) scpoll.obpollobjectsarray = binreader2.ReadBytes(scpoll.length);
return obpollobjectscount;
}
public int DecodeObservationPollObjects(ref ObservationPoll obpollobject, ref BinaryReader binreader3)
{
obpollobject.obj_handle = correctendianshortus(binreader3.ReadUInt16());
m_obpollhandle = obpollobject.obj_handle;
AttributeList attributeliststruct = new AttributeList();
attributeliststruct.count = correctendianshortus(binreader3.ReadUInt16());
if (attributeliststruct.count > 0) attributeliststruct.length = correctendianshortus(binreader3.ReadUInt16());
int avaobjectscount = Convert.ToInt32(attributeliststruct.count);
if (attributeliststruct.length > 0) obpollobject.avaobjectsarray = binreader3.ReadBytes(attributeliststruct.length);
return avaobjectscount;
}
public void DecodeAvaObjects(ref Ava avaobject, ref BinaryReader binreader4)
{
avaobject.attribute_id = correctendianshortus(binreader4.ReadUInt16());
avaobject.length = correctendianshortus(binreader4.ReadUInt16());
//avaobject.attribute_val = correctendianshortus(binreader4.ReadUInt16());
if (avaobject.length > 0)
{
byte[] avaattribobjects = binreader4.ReadBytes(avaobject.length);
switch (avaobject.attribute_id)
{
case DataConstants.NOM_ATTR_ID_HANDLE:
//ReadIDHandle(avaattribobjects);
break;
case DataConstants.NOM_ATTR_ID_LABEL:
ReadIDLabel(avaattribobjects);
break;
case DataConstants.NOM_ATTR_NU_VAL_OBS:
ReadNumericObservationValue(avaattribobjects);
break;
case DataConstants.NOM_ATTR_NU_CMPD_VAL_OBS:
ReadCompoundNumericObsValue(avaattribobjects);
break;
case DataConstants.NOM_ATTR_METRIC_SPECN:
break;
case DataConstants.NOM_ATTR_ID_LABEL_STRING:
ReadIDLabelString(avaattribobjects);
break;
case DataConstants.NOM_ATTR_SA_VAL_OBS:
ReadWaveSaObservationValueObject(avaattribobjects);
break;
case DataConstants.NOM_ATTR_SA_CMPD_VAL_OBS:
ReadCompoundWaveSaObservationValue(avaattribobjects);
break;
case DataConstants.NOM_ATTR_SA_SPECN:
ReadSaSpecifications(avaattribobjects);
break;
case DataConstants.NOM_ATTR_SCALE_SPECN_I16:
ReadSaScaleSpecifications(avaattribobjects);
break;
case DataConstants.NOM_ATTR_SA_CALIB_I16:
ReadSaCalibrationSpecifications(avaattribobjects);
break;
default:
// unknown attribute -> do nothing
break;
}
}
}
public string GetPacketTimestamp(byte[] header)
{
MemoryStream memstream = new MemoryStream(header);
BinaryReader binreader = new BinaryReader(memstream);
int pollmdibdatareplysize = 20;
if (m_actiontype == DataConstants.NOM_ACT_POLL_MDIB_DATA) pollmdibdatareplysize = 20;
else if (m_actiontype == DataConstants.NOM_ACT_POLL_MDIB_DATA_EXT) pollmdibdatareplysize = 22;
int firstpartheaderlength = (header.Length - pollmdibdatareplysize);
byte[] firstpartheader = binreader.ReadBytes(firstpartheaderlength);
byte[] pollmdibdatareplyarray = binreader.ReadBytes(pollmdibdatareplysize);
uint relativetime = 0;
byte[] absolutetimearray = new byte[8];
ushort pollresultcode = 0;
if (m_actiontype == DataConstants.NOM_ACT_POLL_MDIB_DATA)
{
PollMdibDataReply pollmdibdatareply = new PollMdibDataReply();
MemoryStream memstream2 = new MemoryStream(pollmdibdatareplyarray);
BinaryReader binreader2 = new BinaryReader(memstream2);
pollmdibdatareply.poll_number = correctendianshortus(binreader2.ReadUInt16());
pollmdibdatareply.rel_time_stamp = correctendianuint(binreader2.ReadUInt32());
relativetime = pollmdibdatareply.rel_time_stamp;
absolutetimearray = binreader2.ReadBytes(8);
pollmdibdatareply.type.partition = correctendianshortus(binreader2.ReadUInt16());
pollmdibdatareply.type.code = correctendianshortus(binreader2.ReadUInt16());
pollresultcode = pollmdibdatareply.type.code;
}
else if (m_actiontype == DataConstants.NOM_ACT_POLL_MDIB_DATA_EXT)
{
PollMdibDataReplyExt pollmdibdatareplyext = new PollMdibDataReplyExt();
MemoryStream memstream2 = new MemoryStream(pollmdibdatareplyarray);
BinaryReader binreader2 = new BinaryReader(memstream2);
pollmdibdatareplyext.poll_number = correctendianshortus(binreader2.ReadUInt16());
pollmdibdatareplyext.sequence_no = correctendianshortus(binreader2.ReadUInt16());
pollmdibdatareplyext.rel_time_stamp = correctendianuint(binreader2.ReadUInt32());
relativetime = pollmdibdatareplyext.rel_time_stamp;
absolutetimearray = binreader2.ReadBytes(8);
pollmdibdatareplyext.type.partition = correctendianshortus(binreader2.ReadUInt16());
pollmdibdatareplyext.type.code = correctendianshortus(binreader2.ReadUInt16());
pollresultcode = pollmdibdatareplyext.type.code;
}
string strRelativeTime = relativetime.ToString();
if (pollresultcode == DataConstants.NOM_MOC_VMS_MDS)
{
//Get baseline timestamps if packet type is MDS attributes
m_baseRelativeTime = relativetime;
m_baseDateTime = GetAbsoluteTimeFromBCDFormat(absolutetimearray);
}
//m_pollDateTime = GetAbsoluteTimeFromBCDFormat(absolutetimearray);
//AbsoluteTime is not supported by several monitors
/*AbsoluteTime absolutetime = new AbsoluteTime();
absolutetime.century = binreader2.ReadByte();
absolutetime.year = binreader2.ReadByte();
absolutetime.month = binreader2.ReadByte();
absolutetime.day = binreader2.ReadByte();
absolutetime.hour = binreader2.ReadByte();
absolutetime.minute = binreader2.ReadByte();
absolutetime.second = binreader2.ReadByte();
absolutetime.fraction = binreader2.ReadByte();*/
return strRelativeTime;
}
public void ReadIDHandle(byte[] avaattribobjects)
{
MemoryStream memstream5 = new MemoryStream(avaattribobjects);
BinaryReader binreader5 = new BinaryReader(memstream5);
ushort IDhandle = correctendianshortus(binreader5.ReadUInt16());
}
public void ReadIDLabel(byte[] avaattribobjects)
{
MemoryStream memstream5 = new MemoryStream(avaattribobjects);
BinaryReader binreader5 = new BinaryReader(memstream5);
uint IDlabel = correctendianuint(binreader5.ReadUInt32());
m_idlabelhandle = IDlabel;
}
public void ReadIDLabelString(byte[] avaattribobjects)
{
MemoryStream memstream5 = new MemoryStream(avaattribobjects);
BinaryReader binreader5 = new BinaryReader(memstream5);
StringMP strmp = new StringMP();
strmp.length = correctendianshortus(binreader5.ReadUInt16());
//strmp.value1 = correctendianshortus(binreader5.ReadUInt16());
byte[] stringval = binreader5.ReadBytes(strmp.length);
string label = Encoding.UTF8.GetString(stringval);
m_idlabelstring = label.Replace("\0", string.Empty).Trim();
Console.WriteLine("Label String: {0}", m_idlabelstring);
AddIDLabelToList();
}
public void AddIDLabelToList()
{
IDLabel cIDLabel = new IDLabel();
cIDLabel.obpoll_handle = m_obpollhandle;
cIDLabel.idlabelhandle = m_idlabelhandle;
cIDLabel.idlabelstring = m_idlabelstring;
//Add to a list of ID Labels if it's not already present
int idlistindex = m_IDLabelList.FindIndex(x => x.obpoll_handle == cIDLabel.obpoll_handle);
if (idlistindex == -1)
{
m_IDLabelList.Add(cIDLabel);
}
else
{
m_IDLabelList.RemoveAt(idlistindex);
m_IDLabelList.Add(cIDLabel);
}
}
public void ReadNumericObservationValue(byte[] avaattribobjects)
{
MemoryStream memstream5 = new MemoryStream(avaattribobjects);
BinaryReader binreader5 = new BinaryReader(memstream5);
NuObsValue NumObjectValue = new NuObsValue();
NumObjectValue.physio_id = correctendianshortus(binreader5.ReadUInt16());
NumObjectValue.state = correctendianshortus(binreader5.ReadUInt16());
NumObjectValue.unit_code = correctendianshortus(binreader5.ReadUInt16());
NumObjectValue.value = correctendianuint(binreader5.ReadUInt32());
double value = FloattypeToValue(NumObjectValue.value);
string physio_id = Enum.GetName(typeof(IntelliVue.AlertSource), NumObjectValue.physio_id);
if (physio_id == "NOM_METRIC_NOS" || physio_id == null)
{
IDLabel cIDLabel = new IDLabel();
cIDLabel = m_IDLabelList.Find(x => x.obpoll_handle == m_obpollhandle);
if (cIDLabel != null) physio_id = cIDLabel.idlabelstring;
}
string state = NumObjectValue.state.ToString();
string unit_code = NumObjectValue.unit_code.ToString();
string valuestr;
if (value != DataConstants.FLOATTYPE_NAN)
{
valuestr = String.Format("{0:0.##}", value);
}
else valuestr = "-";
NumericValResult NumVal = new NumericValResult();
NumVal.Relativetimestamp = m_strTimestamp;
//DateTime dtDateTime = DateTime.Now;
uint currentRelativeTime = UInt32.Parse(m_strTimestamp);
DateTime dtDateTime = GetAbsoluteTimeFromRelativeTimestamp(currentRelativeTime);
//NumVal.Timestamp = dtDateTime.ToString();
string strDateTime = dtDateTime.ToString("dd-MM-yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
//string strDateTime = dtDateTime.ToString("G", DateTimeFormatInfo.InvariantInfo);
NumVal.Timestamp = strDateTime;
//NumVal.Timestamp = DateTime.Now.ToString();
DateTime dtSystemDateTime = DateTime.Now;
string strSystemLocalDateTime = dtSystemDateTime.ToString("dd-MM-yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
NumVal.SystemLocalTime = strSystemLocalDateTime;
NumVal.PhysioID = physio_id;
NumVal.Value = valuestr;
NumVal.DeviceID = m_DeviceID;
m_NumericValList.Add(NumVal);
m_NumValHeaders.Add(NumVal.PhysioID);
Console.WriteLine("Physiological ID: {0}", physio_id);
//Console.WriteLine("State: {0}", state);
//Console.WriteLine("Unit code: {0}", unit_code);
Console.WriteLine("Value: {0}", valuestr);
Console.WriteLine();
}
public void ReadCompoundNumericObsValue(byte[] avaattribobjects)