-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form_main.vb
1424 lines (1082 loc) · 61.1 KB
/
Form_main.vb
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
Imports System.IO
Imports System.ComponentModel
'server side
Public Class Form_main
Private listener As System.Net.Sockets.TcpListener
Private listenThread As System.Threading.Thread
Private clients As New List(Of ConnectedClient) 'This list will store all connected clients.
Const VER = "1.1 від 25.12.2016"
Const MAX_OBJECT = 500 'maximum number of object
Const MAX_OUTPUT = 8 'maximum number of outputs per object
Const MAX_INPUT = 8 'maximum number of inputs per object
Private object_row_height As Integer = 60
Private view_220 As Integer = 1
Private view_arm As Integer = 1
Private view_time As Integer = 1
Private save_log As Integer = 1
Private msg_event As Integer = 1
Const _220_ON = "220_on"
Const _220_OFF = "220_off"
Const LOW_ACC = "low_acc"
Const ARMED = "armed"
Const DISARMED = "disarmed"
Const INPUT_NORMA = "input_norma_"
Const INPUT_TREVOGA = "input_trevoga_"
Const OUTPUT_ON = "output_on_"
Const OUTPUT_OFF = "output_off_"
Const COMMAND_PREF = "COMMAND:"
Const COMMAND_END = ";"
Private num_object As Integer = MAX_OBJECT
Private num_input As Integer = MAX_INPUT
Private num_output As Integer = MAX_OUTPUT
Private max_num_object As Integer = MAX_OBJECT
Private BtnArray_input(num_input, num_object) As Button
Private BtnArray_output(num_output, num_object) As Button
Private BtnArray_object(num_object) As Button
Private BtnArray_arm(num_object) As Button
Private BtnArray_time(num_object) As Label
Private BtnArray_220(num_object) As Button
Private startup_path As String
Private EventQueue As New Queue() 'queue for events from objects
Private LogQueue As New Queue() 'queue for log saving
Private DataQueue_ As New Queue() 'queue for message receiving from object
' Creates a synchronized wrapper around the Queue.
Private DataQueue As Queue = Queue.Synchronized(DataQueue_) 'each object in separate thread
'Object properties
Structure object_properties_str
Dim name As String
Dim notes As String
Dim imei As String
Dim code As String
Dim arm As String
Dim disarm As String
Dim out1_on As String
Dim out1_off As String
Dim out2_on As String
Dim out2_off As String
Dim out3_on As String
Dim out3_off As String
Dim out4_on As String
Dim out4_off As String
Dim out5_on As String
Dim out5_off As String
Dim out6_on As String
Dim out6_off As String
Dim out7_on As String
Dim out7_off As String
Dim out8_on As String
Dim out8_off As String
Dim time_out_len As Integer
Dim time_out As Integer
Dim connected As Boolean
Dim view As Boolean
End Structure
Private object_properties(0) As object_properties_str 'array of object's properties
'Event properties
Structure event_properties_str
Dim event_code As String
Dim audio_file As String
Dim description As String
End Structure
Private event_properties(0) As event_properties_str 'array of activated events
Private num_event As Integer = 0
'message properties
Structure mesage_properties_str
Dim message As String
Dim options As Integer
End Structure
Const REMOVE_CLIENT_GUI = 0
Const APPEND_OUTPUT = 1
Const APPEND_INFO = 2
Const REFRESH_GUI = 3
'for file
Private f, fs, ts As Object
Private f_r_name As String
Const ForReading As Short = 1
Const ForWriting As Short = 8
Const ForAppending As Short = 3
Const TristateUseDefault As Short = -2
Const TristateTrue As Short = -1
Const TristateFalse As Short = 0
Private server_start As Boolean = False
Private exit_program As Boolean = False
Private bw_start_stop_button As BackgroundWorker = New BackgroundWorker
Private bw_save_log As BackgroundWorker = New BackgroundWorker
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim i As Integer
Dim k As Integer
Dim offset As Integer = 2
AddHandler bw_start_stop_button.DoWork, AddressOf bw_start_stop_button_DoWork
AddHandler bw_start_stop_button.RunWorkerCompleted, AddressOf bw_start_stop_button_RunWorkerCompleted
AddHandler bw_save_log.DoWork, AddressOf bw_save_log_DoWork
AddHandler bw_save_log.RunWorkerCompleted, AddressOf bw_save_log_RunWorkerCompleted
startup_path = Application.StartupPath()
Me.Text = """OKO-online"" - станція моніторингу та керування приладів ОКО, версія " & VER
open_config_file()
open_object_file()
open_event_file()
AppendLog("СТАРТ ПРОГРАМИ")
ReDim BtnArray_input(num_input, num_object)
ReDim BtnArray_output(num_output, num_object)
ReDim BtnArray_object(num_object)
ReDim BtnArray_arm(num_object)
ReDim BtnArray_time(num_object)
ReDim BtnArray_220(num_object)
For i = view_time + 1 + view_arm + view_220 + num_input + num_output To TableLayoutPanel_object.ColumnCount - 1
TableLayoutPanel_object.ColumnStyles(i).SizeType = SizeType.AutoSize
Next
For i = 0 To view_time + 1 + view_arm + view_220 + num_input + num_output - 1
TableLayoutPanel_object.ColumnStyles(i).SizeType = SizeType.Percent
TableLayoutPanel_object.ColumnStyles(i).Width = 95 / (view_time + 1 + view_arm + view_220 + num_input + num_output)
Next
For i = 0 To num_object - 1
TableLayoutPanel_object.RowStyles(TableLayoutPanel_object.RowCount - offset).Height = object_row_height
TableLayoutPanel_object.RowStyles.Add(New RowStyle(SizeType.Absolute, object_row_height))
TableLayoutPanel_object.RowCount += 1
Next
For i = 0 To num_object - 1
If (view_time = 1) Then
BtnArray_time(i) = New Label()
BtnArray_time(i).Name = "time_" & CStr(i)
BtnArray_time(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray_time(i).TextAlign = ContentAlignment.MiddleCenter
'Set up the ToolTip text for the Button
ToolTip1.SetToolTip(BtnArray_time(i), "Час надходження останніх даних від об'єкту ")
End If
BtnArray_object(i) = New Button()
BtnArray_object(i).Name = "object_" & CStr(i)
BtnArray_object(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray_object(i).Text = object_properties(i).name
BtnArray_object(i).BackColor = Color.Pink
'Set up the ToolTip text for the Button
ToolTip1.SetToolTip(BtnArray_object(i), object_properties(i).notes)
TableLayoutPanel_object.Controls.Add(BtnArray_object(i), view_time, i) 'TableLayoutPanel_object.RowCount - offset
AddHandler BtnArray_object(i).Click, AddressOf Object_ClickHandler
If (view_arm = 1) Then
BtnArray_arm(i) = New Button()
BtnArray_arm(i).Name = "arm_" & CStr(i)
BtnArray_arm(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray_arm(i).Text = "arm"
BtnArray_arm(i).TextAlign = ContentAlignment.TopLeft '
'Set up the ToolTip text for the Button
ToolTip1.SetToolTip(BtnArray_arm(i), "Переключення стану охорони на протилежний - об'єкт " & BtnArray_object(i).Text)
BtnArray_arm(i).BackgroundImageLayout = ImageLayout.Stretch
AddHandler BtnArray_arm(i).Click, AddressOf Arm_ClickHandler
End If
If (view_220 = 1) Then
BtnArray_220(i) = New Button()
BtnArray_220(i).Name = "220_" & CStr(i)
BtnArray_220(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray_220(i).Text = "220"
BtnArray_220(i).TextAlign = ContentAlignment.TopLeft ' 'ContentAlignment.MiddleCenter
'Set up the ToolTip text for the Button
ToolTip1.SetToolTip(BtnArray_220(i), "Поточний стан 220В на об'єкті " & BtnArray_object(i).Text)
BtnArray_220(i).BackgroundImageLayout = ImageLayout.Stretch
End If
For k = 0 To num_input - 1
BtnArray_input(k, i) = New Button()
'Set up the ToolTip text for the Button
ToolTip1.SetToolTip(BtnArray_input(k, i), "Поточний стан входу-" & CStr(k + 1) & " на об'єкті " & BtnArray_object(i).Text)
BtnArray_input(k, i).Name = "input_" & CStr(i) & "_" & CStr(k + 1)
BtnArray_input(k, i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray_input(k, i).Text = "i" & CStr(k + 1)
BtnArray_input(k, i).TextAlign = ContentAlignment.TopLeft
BtnArray_input(k, i).BackgroundImageLayout = ImageLayout.Stretch
Next
For k = 0 To num_output - 1
BtnArray_output(k, i) = New Button()
'Set up the ToolTip text for the Button
ToolTip1.SetToolTip(BtnArray_output(k, i), "Переключити стан виходу-" & CStr(k + 1) & " на протилежний - об'єкт " & BtnArray_object(i).Text)
BtnArray_output(k, i).Name = "output_" & CStr(i) & "_" & CStr(k + 1)
BtnArray_output(k, i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
BtnArray_output(k, i).Text = "o" & CStr(k + 1)
BtnArray_output(k, i).TextAlign = ContentAlignment.TopLeft
BtnArray_output(k, i).BackgroundImageLayout = ImageLayout.Stretch
AddHandler BtnArray_output(k, i).Click, AddressOf Output_ClickHandler
Next
Next
Update_No_connectionGUI()
Me.WindowState = FormWindowState.Maximized
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub StartStopButton_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles StartStopButton.CheckedChanged
Try
Me.Label_test.Text = "start/stop click "
If Not bw_start_stop_button.IsBusy = True Then
If StartStopButton.Checked Then
PortTextBox.Enabled = False
StartStopButton.Text = "Стоп"
StartStopButton.Image = My.Resources.Resources.stop_server
AppendLog("Натиснуто кнопку СТАРТ, порт: " & PortTextBox.Text)
listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, CInt(PortTextBox.Text)) 'The TcpListener will listen for incoming connections at port 43001
listener.Start() 'Start listening.
listenThread = New System.Threading.Thread(AddressOf doListen) 'This thread will run the doListen method
listenThread.IsBackground = True 'Since we dont want this thread to keep on running after the application closes, we set isBackground to true.
listenThread.Start() 'Start executing doListen on the worker thread.
server_start = True
Else
PortTextBox.Enabled = True
StartStopButton.Text = "Старт"
StartStopButton.Image = My.Resources.Resources.start_server
AppendLog("Натиснуто кнопку СТОП.")
bw_start_stop_button.RunWorkerAsync()
End If
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub bw_start_stop_button_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Try
'close active sockets
For Each opened_socket As ConnectedClient In clients
opened_socket.CloseSocket()
Next
'clients.Clear()
listener.Stop()
listener = Nothing
listenThread = Nothing
server_start = False
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub bw_start_stop_button_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
Try
AppendLog("Виконано СТОП.")
Me.Label_test.Text = "start/stop done"
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Sub Arm_ClickHandler(ByVal sender As Object, ByVal e As _
System.EventArgs)
Try
Me.Label_test.Text = "arm click "
Dim client As ConnectedClient
Dim fields As String() = Strings.Split(CType(sender, Button).Name, "_")
If fields.Length = 2 Then
If (object_properties(CInt(fields(1))).connected) Then
client = GetClientByIMEI(object_properties(CInt(fields(1))).imei)
If Not (client Is Nothing) Then
If (BtnArray_arm(CInt(fields(1))).Tag) Then
client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).disarm & COMMAND_END)
AppendLog("Натиснуто кнопку: зняття з охорони об'єкту " & object_properties(CInt(fields(1))).name & " (IMEI=" & object_properties(CInt(fields(1))).imei & ")")
Else
client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).arm & COMMAND_END)
AppendLog("Натиснуто кнопку: постановка на охорону об'єкту " & object_properties(CInt(fields(1))).name & " (IMEI=" & object_properties(CInt(fields(1))).imei & ")")
End If
End If
Else
No_Connection_Msg(object_properties(CInt(fields(1))).name)
End If
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Sub Object_ClickHandler(ByVal sender As Object, ByVal e As _
System.EventArgs)
Try
Me.Label_test.Text = "object click "
Dim client As ConnectedClient
Dim fields As String() = Strings.Split(CType(sender, Button).Name, "_")
If fields.Length = 2 Then
If (object_properties(CInt(fields(1))).connected) Then
client = GetClientByIMEI(object_properties(CInt(fields(1))).imei)
If Not (client Is Nothing) Then
client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & "70" & COMMAND_END)
AppendLog("Натиснуто кнопку: запит стану об'єкту " & object_properties(CInt(fields(1))).name & " (IMEI=" & object_properties(CInt(fields(1))).imei & ")")
End If
Else
No_Connection_Msg(object_properties(CInt(fields(1))).name)
End If
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Sub Output_ClickHandler(ByVal sender As Object, ByVal e As _
System.EventArgs)
Try
Me.Label_test.Text = "output click "
Dim client As ConnectedClient
Dim fields As String() = Strings.Split(CType(sender, Button).Name, "_")
If fields.Length = 3 Then
If (object_properties(CInt(fields(1))).connected) Then
client = GetClientByIMEI(object_properties(CInt(fields(1))).imei)
If Not (client Is Nothing) Then
If (BtnArray_output(CInt(fields(2)) - 1, CInt(fields(1))).Tag) Then
If (CInt(fields(2)) = 1) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out1_off & COMMAND_END)
If (CInt(fields(2)) = 2) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out2_off & COMMAND_END)
If (CInt(fields(2)) = 3) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out3_off & COMMAND_END)
If (CInt(fields(2)) = 4) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out4_off & COMMAND_END)
If (CInt(fields(2)) = 5) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out5_off & COMMAND_END)
If (CInt(fields(2)) = 6) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out6_off & COMMAND_END)
If (CInt(fields(2)) = 7) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out7_off & COMMAND_END)
If (CInt(fields(2)) = 8) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out8_off & COMMAND_END)
AppendLog("Натиснуто кнопку: вимкнути вихід-" & CInt(fields(2)) & " на об'єкті " & object_properties(CInt(fields(1))).name & " (IMEI=" & object_properties(CInt(fields(1))).imei & ")")
Else
If (CInt(fields(2)) = 1) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out1_on & COMMAND_END)
If (CInt(fields(2)) = 2) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out2_on & COMMAND_END)
If (CInt(fields(2)) = 3) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out3_on & COMMAND_END)
If (CInt(fields(2)) = 4) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out4_on & COMMAND_END)
If (CInt(fields(2)) = 5) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out5_on & COMMAND_END)
If (CInt(fields(2)) = 6) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out6_on & COMMAND_END)
If (CInt(fields(2)) = 7) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out7_on & COMMAND_END)
If (CInt(fields(2)) = 8) Then client.SendMessage(COMMAND_PREF & object_properties(CInt(fields(1))).code & object_properties(CInt(fields(1))).out8_on & COMMAND_END)
AppendLog("Натиснуто кнопку: ввімкнути вихід-" & CInt(fields(2)) & " на об'єкті " & object_properties(CInt(fields(1))).name & " (IMEI=" & object_properties(CInt(fields(1))).imei & ")")
End If
End If
Else
No_Connection_Msg(object_properties(CInt(fields(1))).name)
End If
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Sub No_Connection_Msg(ByVal object_ As String)
Try
Try
My.Computer.Audio.Play(startup_path & "\config\fault.wav")
Catch ex As Exception
End Try
MsgBox("Відсутній зв'язок з об'єктом " & object_)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub doListen()
Try
Dim incomingClient As System.Net.Sockets.TcpClient
Do
incomingClient = listener.AcceptTcpClient 'Accept the incoming connection. This is a blocking method so execution will halt here until someone tries to connect.
Dim connClient As New ConnectedClient(incomingClient, Me) 'Create a new instance of ConnectedClient (check its constructor to see whats happening now).
AddHandler connClient.dataReceived, AddressOf Me.messageReceived
clients.Add(connClient) 'Adds the connected client to the list of connected clients.
Loop
Catch ex As Exception
'MessageBox.Show(ex.ToString)
End Try
End Sub
Public Sub RemoveClient(ByVal client As ConnectedClient)
Try
If clients.Contains(client) Then
clients.Remove(client)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub AppendLog(message As String)
Try
message = Now().ToString("dd/MM/yy H:mm:ss") & "-->" & message & ControlChars.NewLine
If (save_log = 1) Then LogQueue.Enqueue(message)
If Form_log.RichTextBox_log.Text.Length <= 40000 Then
Else
Form_log.RichTextBox_log.Text = Mid(Form_log.RichTextBox_log.Text, 20000)
End If
Form_log.RichTextBox_log.AppendText(message)
Form_log.RichTextBox_log.ScrollToCaret()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub AppendInfo(message As String)
Try
AppendLog(message)
If Form_info.RichTextBox_info.Text.Length <= 40000 Then
Else
Form_info.RichTextBox_info.Text = Mid(Form_info.RichTextBox_info.Text, 20000)
End If
message = Now().ToString("dd/MM/yy H:mm:ss") & "-->" & message & ControlChars.NewLine
Form_info.RichTextBox_info.AppendText(message)
Form_info.RichTextBox_info.ScrollToCaret()
Form_info.Show()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub Update_No_connectionGUI()
Try
Dim i As Integer
Dim row_num As Integer = 0
DataGridView_no_connection.Rows.Clear()
DataGridView_no_connection.Columns.Clear()
DataGridView_no_connection.Columns.Add("name", "Об'єкт")
DataGridView_no_connection.Columns.Add("notes", "Опис")
For i = 0 To num_object - 1
If (Not object_properties(i).connected) Then
DataGridView_no_connection.Rows.Add(1)
row_num += 1
DataGridView_no_connection(0, row_num - 1).Value = object_properties(i).name
DataGridView_no_connection(0, row_num - 1).Style.BackColor = Color.Pink
DataGridView_no_connection(1, row_num - 1).Value = object_properties(i).notes
End If
Next
'Me.DataGridView_report.RowHeadersWidth = 30
'Me.DataGridView_report.Columns(0).Width = 180
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Function hex_8bits(s As String) As String
Dim state As String = ""
Try
Dim i As Integer = Convert.ToInt32(s, 16) 'conver hex to integer
state = Convert.ToString(i, 2) 'convert integer to binary string
If (state.Length = 1) Then state = "0000000" & state
If (state.Length = 2) Then state = "000000" & state
If (state.Length = 3) Then state = "00000" & state
If (state.Length = 4) Then state = "0000" & state
If (state.Length = 5) Then state = "000" & state
If (state.Length = 6) Then state = "00" & state
If (state.Length = 7) Then state = "0" & state
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Return state
End Function
Private Sub RefreshGUI(data As String)
Try
Dim i, k As Integer
Dim fields As String() = Strings.Split(data, ",")
Const OKO_S2 As Integer = 1 '{866104020810332,F9,0B,05C8,1F,3.0.3,OKO-S2}
Const OKO_U2_PRO As Integer = 2 '{863591021960863,00F9,5600,2F202000,0000,12,1A80808080808080,0000CCF10000000080CA1A7D0000B9DB00000000000000000000000000000000,072B06DE0000000000000FFB0FF90DEB0FFE0FFE0FFE0FFE0FFE0FFE0FFE0FFE,1.2.3,OKO-PRO,,,,,,,,,000007,}
Const OKO_U As Integer = 3 '{UD123456789098765,FB,С0,02,7А,5C,27,-10,-5,25,40,32,3,19,128,15grn,3.9}
Dim protocol_type As Integer = 0
For i = 0 To num_object - 1
If (object_properties(i).imei.Equals(fields(0))) Then
'analize protocol
If (fields.Length > 6) Then
If (fields(6).Equals("OKO-S2")) Then
protocol_type = OKO_S2
Else
If (fields.Length > 10) Then
If (fields(10).Equals("OKO-PRO") Or fields(10).Equals("OKO-U2")) Then
protocol_type = OKO_U2_PRO
Else
If (fields.Length > 16) Then
If (fields(0).Substring(0, 2).Equals("UD")) Then protocol_type = OKO_U
End If
End If
End If
End If
End If
If (protocol_type = OKO_S2 Or protocol_type = OKO_U2_PRO Or protocol_type = OKO_U) Then
Dim est220 As Boolean = False
Dim lowbat As Boolean = False
Dim ohrana As Boolean = False
Dim input(8) As Boolean
Dim output(8) As Boolean
Dim state As String = ""
Dim event_code As Integer = 200
'set timeout for data receiving
'Dim thisLock As New Object
'SyncLock thisLock
object_properties(i).time_out = object_properties(i).time_out_len
'End SyncLock
'event analizing
For k = 0 To num_event - 1
If (protocol_type = OKO_U) Then
event_code = Convert.ToInt32(fields(1), 16) 'conver hex to integer
Else
event_code = 300 + Convert.ToInt32(fields(1), 16) 'conver hex to integer with OFFSET 300
End If
If (CInt(event_properties(k).event_code) = event_code) Then
Try
My.Computer.Audio.Play(startup_path & "\config\" & event_properties(k).audio_file)
Catch ex As Exception
End Try
EventQueue.Enqueue("На об'єкті " & object_properties(i).name & " (" & object_properties(i).notes & ") відбуласа подія: " & event_properties(k).description & ", отримано в " & Now().ToString("H:mm:ss dd/MM/yyyy"))
AppendLog("Отримано повідомлення від об'єкту " & object_properties(i).name & " (IMEI=" & object_properties(i).imei & "), подія: " & event_properties(k).description)
k = num_event
End If
Next
'if oko-s2 protocol {866104020810332,F9,0B,05C8,1F,3.0.3,OKO-S2}
If (protocol_type = OKO_S2) Then
state = hex_8bits(fields(2))
ohrana = CBool(state.Substring(1, 1)) 'arm state
output(1) = CBool(state.Substring(2, 1)) 'out2 state
output(0) = CBool(state.Substring(3, 1)) 'out1 state
lowbat = Not CBool(state.Substring(4, 1)) 'low ACC state
est220 = CBool(state.Substring(5, 1)) '220 state
input(1) = CBool(state.Substring(6, 1)) 'in2 state
input(0) = CBool(state.Substring(7, 1)) 'in1 state
End If
'if oko-u2/pro protocol {863591021960863,00F9,5600,2F202000,0000,12,1A80808080808080,0000CCF10000000080CA1A7D0000B9DB00000000000000000000000000000000,072B06DE0000000000000FFB0FF90DEB0FFE0FFE0FFE0FFE0FFE0FFE0FFE0FFE,1.2.3,OKO-PRO,,,,,,,,,000007,}
If (protocol_type = OKO_U2_PRO) Then
If (Not fields(2).Substring(2, 2).Equals("00")) Then ohrana = True 'arm state
state = hex_8bits(fields(3).Substring(2, 2))
input(7) = CBool(state.Substring(0, 1)) 'in8 state
input(6) = CBool(state.Substring(1, 1)) 'in7 state
input(5) = CBool(state.Substring(2, 1)) 'in6 state
input(4) = CBool(state.Substring(3, 1)) 'in5 state
input(3) = CBool(state.Substring(4, 1)) 'in4 state
input(2) = CBool(state.Substring(5, 1)) 'in3 state
input(1) = CBool(state.Substring(6, 1)) 'in2 state
input(0) = CBool(state.Substring(7, 1)) 'in1 state
state = hex_8bits(fields(3).Substring(0, 2))
lowbat = Not CBool(state.Substring(4, 1)) 'low ACC state
est220 = CBool(state.Substring(5, 1)) '220 state
state = hex_8bits(fields(4).Substring(2, 2))
output(7) = CBool(state.Substring(0, 1)) 'out8 state
output(6) = CBool(state.Substring(1, 1)) 'out7 state
output(5) = CBool(state.Substring(2, 1)) 'out6 state
output(4) = CBool(state.Substring(3, 1)) 'out5 state
output(3) = CBool(state.Substring(4, 1)) 'out4 state
output(2) = CBool(state.Substring(5, 1)) 'out3 state
output(1) = CBool(state.Substring(6, 1)) 'out2 state
output(0) = CBool(state.Substring(7, 1)) 'out1 state
End If
'if oko-u protocol {UD123456789098765,FB,С0,02,7А,5C,27,-10,-5,25,40,32,3,19,128,15grn,3.9}
If (protocol_type = OKO_U) Then
state = hex_8bits(fields(2))
ohrana = CBool(state.Substring(0, 1)) 'arm state
est220 = CBool(state.Substring(1, 1)) '220 state
lowbat = CBool(state.Substring(2, 1)) 'low ACC state
output(0) = CBool(state.Substring(5, 1)) 'out1 state
output(1) = CBool(state.Substring(6, 1)) 'out2 state
output(2) = CBool(state.Substring(7, 1)) 'out3 state
'inputs state
state = hex_8bits(fields(3))
input(3) = CBool(state.Substring(4, 1)) 'in4 state
input(2) = CBool(state.Substring(5, 1)) 'in3 state
input(1) = CBool(state.Substring(6, 1)) 'in2 state
input(0) = CBool(state.Substring(7, 1)) 'in1 state
End If
If (Not object_properties(i).connected) Then
object_properties(i).connected = True
Update_No_connectionGUI()
End If
If (Not object_properties(i).view) Then
If (view_time = 1) Then TableLayoutPanel_object.Controls.Add(BtnArray_time(i), 0, i)
If (view_arm = 1) Then TableLayoutPanel_object.Controls.Add(BtnArray_arm(i), view_time + 1, i)
If (view_220 = 1) Then TableLayoutPanel_object.Controls.Add(BtnArray_220(i), view_time + 1 + view_arm, i)
For k = 0 To num_input - 1
TableLayoutPanel_object.Controls.Add(BtnArray_input(k, i), view_time + 1 + view_arm + view_220 + k, i)
Next
For k = 0 To num_output - 1
TableLayoutPanel_object.Controls.Add(BtnArray_output(k, i), view_time + 1 + view_arm + view_220 + num_input + k, i)
Next
object_properties(i).view = True
End If
'update time
If (view_time = 1) Then BtnArray_time(i).Text = Now().ToString("dd/MM/yy H:mm:ss")
'update object button colour
BtnArray_object(i).BackColor = Color.LightGreen
'update arm state
If (view_arm = 1) Then
If (ohrana) Then
BtnArray_arm(i).BackgroundImage = Image.FromFile(startup_path & "\config\" & ARMED & ".jpg")
BtnArray_arm(i).Tag = True
Else
BtnArray_arm(i).BackgroundImage = Image.FromFile(startup_path & "\config\" & DISARMED & ".jpg")
BtnArray_arm(i).Tag = False
End If
End If
'update power state
If (view_220 = 1) Then
If (est220) Then
BtnArray_220(i).BackgroundImage = Image.FromFile(startup_path & "\config\" & _220_ON & ".jpg")
Else
If (lowbat) Then
BtnArray_220(i).BackgroundImage = Image.FromFile(startup_path & "\config\" & LOW_ACC & ".jpg")
Else
BtnArray_220(i).BackgroundImage = Image.FromFile(startup_path & "\config\" & _220_OFF & ".jpg")
End If
End If
End If
'update input state
For k = 0 To num_input - 1
If (input(k)) Then
BtnArray_input(k, i).BackgroundImage = Image.FromFile(startup_path & "\config\" & INPUT_TREVOGA & CStr(k + 1) & ".jpg")
Else
BtnArray_input(k, i).BackgroundImage = Image.FromFile(startup_path & "\config\" & INPUT_NORMA & CStr(k + 1) & ".jpg")
End If
Next
'update output state
For k = 0 To num_output - 1
If (output(k)) Then
BtnArray_output(k, i).BackgroundImage = Image.FromFile(startup_path & "\config\" & OUTPUT_ON & CStr(k + 1) & ".jpg")
BtnArray_output(k, i).Tag = True
Else
BtnArray_output(k, i).BackgroundImage = Image.FromFile(startup_path & "\config\" & OUTPUT_OFF & CStr(k + 1) & ".jpg")
BtnArray_output(k, i).Tag = False
End If
Next
Else
AppendInfo("Об'єкт " & object_properties(i).name & " має невідомий протокол. Пакет {" & data & "}.")
End If
i = num_object
End If
Next
If (i = num_object) Then
'MsgBox("Об'єкт з IMEI=" & fields(0) & " не зареєстровано в конфігураційному файлі object.txt")
AppendInfo("Об'єкт з IMEI=" & fields(0) & " не зареєстровано в конфігураційному файлі object.txt.")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub RemoveClientGUI(imei As String)
Try
Dim i As Integer
For i = 0 To num_object - 1
If (object_properties(i).imei.Equals(imei)) Then
object_properties(i).connected = False
BtnArray_object(i).BackColor = Color.Pink
Update_No_connectionGUI()
End If
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub doGUI(data As mesage_properties_str)
Try
If (data.options = REMOVE_CLIENT_GUI) Then RemoveClientGUI(data.message)
If (data.options = APPEND_OUTPUT) Then AppendLog(data.message)
If (data.options = REFRESH_GUI) Then RefreshGUI(data.message)
If (data.options = APPEND_INFO) Then AppendInfo(data.message)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub messageReceived(ByVal sender As ConnectedClient, ByVal message As String, ByVal remove_client As Boolean, ByVal imei As String)
Try
Dim data(4) As Object
data(0) = sender
data(1) = message
data(2) = remove_client
data(3) = imei
DataQueue.Enqueue(data)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub messageReceived_(ByVal sender As ConnectedClient, ByVal message As String, ByVal remove_client As Boolean, ByVal imei As String)
Try
Dim unknown_packet = False
Dim data As mesage_properties_str 'message for GUI
If (remove_client) Then
data.message = imei
data.options = REMOVE_CLIENT_GUI
doGUI(data)
RemoveClient(sender)
Else
'logging
data.message = message
data.options = APPEND_OUTPUT
doGUI(data)
Dim parts_ As String() = Strings.Split(message, "{")
If parts_.Length = 2 Then
Dim parts As String() = Strings.Split(parts_(1), "}")
If parts.Length = 2 Then
message = "{" & parts(0) & "}"
Dim fields As String() = Strings.Split(parts(0), ",")
If fields.Length > 1 Then
sender.imei = fields(0)
data.message = parts(0)
data.options = REFRESH_GUI
doGUI(data)
Else
unknown_packet = True
End If
Else
unknown_packet = True
End If
Else
unknown_packet = True
End If
If (unknown_packet) Then
data.message = "Невідомий формат пакету: " + message
data.options = APPEND_INFO
doGUI(data)
End If
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Function GetClientByIMEI(ByVal name As String) As ConnectedClient
Try
For Each cc As ConnectedClient In clients
If cc.imei = name Then
Return cc 'client found, return it.
End If
Next
'If we've reached this part of the method, there is no client by that name
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Return Nothing
End Function
Private Sub ToolStripButton_log_Click(sender As Object, e As EventArgs) Handles ToolStripButton_log.Click
Form_log.Show()
End Sub
Private Sub open_config_file()
Try
'load config info - if exist
Dim curr_line As String
f_r_name = startup_path & "\config\config.txt"
fs = CreateObject("Scripting.FileSystemObject")
f = fs.GetFile(f_r_name)
'open source file
ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
While ts.AtEndOfStream <> True
curr_line = ts.ReadLine
If (Mid(curr_line, 1, 1) <> "#") Then
curr_line = curr_line.Replace(" ", "")
Dim parts As String() = Strings.Split(curr_line, "=") 'ans1.Split(New [Char]() { CChar(vbTab), CChar(" "), CChar(";") })
If parts.Length = 2 Then
If parts(0).Equals("MAX_OBJECT") Then
max_num_object = CInt(parts(1))
End If
If parts(0).Equals("MAX_INPUT") Then
num_input = CInt(parts(1))
If (num_input > 8 Or num_input < 0) Then num_input = 8
End If
If parts(0).Equals("MAX_OUTPUT") Then
num_output = CInt(parts(1))
If (num_output > 8 Or num_output < 0) Then num_output = 8
End If
If parts(0).Equals("view_220") Then
view_220 = CInt(parts(1))
If (view_220 > 1 Or view_220 < 0) Then view_220 = 1
End If
If parts(0).Equals("view_time") Then
view_time = CInt(parts(1))
If (view_time > 1 Or view_time < 0) Then view_time = 1
End If
If parts(0).Equals("view_arm") Then
view_arm = CInt(parts(1))
If (view_arm > 1 Or view_arm < 0) Then view_arm = 1
End If
If parts(0).Equals("object_row_height") Then
object_row_height = CInt(parts(1))
If (object_row_height > 500 Or object_row_height < 50) Then object_row_height = 60
End If
If parts(0).Equals("save_log") Then
save_log = CInt(parts(1))
If (save_log > 1 Or save_log < 0) Then save_log = 1
End If
If parts(0).Equals("msg_event") Then
msg_event = CInt(parts(1))
If (msg_event > 1 Or msg_event < 0) Then msg_event = 1
End If
End If
End If
End While
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub open_object_file()
Try
num_object = 0
'load config info - if exist
Dim curr_line As String
f_r_name = startup_path & "\config\object.txt"
fs = CreateObject("Scripting.FileSystemObject")
f = fs.GetFile(f_r_name)
'open source file
ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
While ts.AtEndOfStream <> True
curr_line = ts.ReadLine