Alejandro Acuña
2024-09-16 adba74e107bcda9e1cb510bc14364b02e781baef
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
package art.servers.rtzserver.controller;
 
import art.library.interop.InteropParameters;
import art.library.interop.serialization.Serialization;
import art.library.interop.serialization.SerializationException;
import art.library.model.devices.DeviceAction;
import art.library.model.devices.colors.controller.RTZ32.RTZ32_Controller;
import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerAlarms;
import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerCommands;
import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerConfiguration;
import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerRealtime;
import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerStatus;
import art.library.model.devices.colors.controller.RTZ32.alarms.RTZ32_Alarms;
import art.library.model.devices.colors.controller.RTZ32.commands.RTZ32_Commands_Configuration_Write;
import art.library.model.devices.colors.controller.RTZ32.configuration.RTZ32_Configuration;
import art.library.model.devices.colors.controller.RTZ32.configuration.RTZ32_Configuration_Group;
import art.library.model.devices.colors.controller.RTZ32.configuration.RTZ32_Configuration_Program;
import art.library.model.devices.colors.controller.RTZ32.configuration.constants.RTZ32_Constants;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_1;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_11_28;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_2;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_3;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_31;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_32;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_33;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_34;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_35;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_36;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_37;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_38_40;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_4;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_42;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_43;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_44;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_45;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_46;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_5;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_6;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.RTZ32_Configuration_Table_7;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.eprom.RTZ32_Configuration_Table_91;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.eprom.RTZ32_Configuration_Table_92;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.eprom.RTZ32_Configuration_Table_93;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.eprom.RTZ32_Configuration_Table_94;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.eprom.RTZ32_Configuration_Table_95;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.microregulation.RTZ32_Configuration_Table_65;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.microregulation.RTZ32_Configuration_Table_66;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.microregulation.RTZ32_Configuration_Table_67;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.microregulation.RTZ32_Configuration_Table_68;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.microregulation.RTZ32_Configuration_Table_69;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.timetables.RTZ32_Configuration_Table_50;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.timetables.RTZ32_Configuration_Table_51;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.timetables.RTZ32_Configuration_Table_52;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.timetables.RTZ32_Configuration_Table_53_60;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_70;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_71;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_72;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_73;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_74;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_75;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_78;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_79;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_80;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_81;
import art.library.model.devices.colors.controller.RTZ32.configuration.tables.trolleygroups.RTZ32_Configuration_Table_82_85;
import art.library.model.devices.colors.controller.RTZ32.information.RTZ32_Information_Program;
import art.library.model.devices.colors.controller.RTZ32.realtime.RTZ32_Realtime;
import art.library.model.devices.colors.controller.RTZ32.realtime.RTZ32_Realtime_Reading;
import art.library.model.devices.colors.controller.RTZ32.realtime.RTZ32_Realtime_Writing;
import art.library.model.devices.colors.controller.RTZ32.status.RTZ32_Status;
import art.library.model.devices.colors.controller.RTZ32.status.RTZ32_Status_Forcings;
import art.library.model.devices.colors.controller.RTZ32.status.RTZ32_Status_Pendings;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Persistent_Configuration;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Report_Information;
import art.library.model.devices.etd.EtdStatus;
import art.library.model.devices.etd.status.EtdStatusLane;
import art.library.utils.common.StringUtils;
import art.library.utils.common.TimeUtils;
import art.library.utils.synchro.Mutex;
import art.servers.ServerException;
import art.servers.Shared;
import art.servers.rtzserver.controller.RTZ32.RTZ32_Talker;
import art.servers.rtzserver.controller.RTZ32.RTZ32_Talker_Listener;
import art.servers.rtzserver.reports.Report_CCT_Juzgados;
import art.servers.rtzserver.reports.Report_Configuration;
import static java.lang.Thread.sleep;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
 
 
public class Controller_RTZ32 extends Controller
{
    public String name = null;
    protected RTZ32_Controller device_rtz32 = null;
    protected RTZ32_Talker talker = null;
    protected Mutex mutexUpdate = new Mutex();
    private boolean forceUpdate = false;
    private boolean updatingStatus = false;
    private boolean readingTables = false;
    private List<EtdStatus> ltrafficData = new ArrayList<EtdStatus>();
 
    
    protected Boolean realtime_junction = null;
    protected Boolean realtime_groups = null;
    protected Boolean realtime_phases = null;
    
    public Controller_RTZ32(RTZ32_Controller device_rtz32)
    {
        super(device_rtz32);
        this.device_rtz32 = device_rtz32;
        this.name = art.servers.Shared.getMessage("Controller") + " " + this.device_rtz32.getDeviceInformation().name;
        this.setName(this.name);
            
        try
        {
//            this.device_rtz32.configuratfion = ((RTZ32_Controller)Serialization.deserialize(RTZ32_Controller.class, new File("controller-zaragoza-90020_v1.json"))).configuration;
//            this.device_rtz32.status = new RTZ32_ControllerStatus();
//            this.device_rtz32.getDeviceStatus().rtz32 = new RTZ32_Status();
//            this.device_rtz32.getDeviceStatus().rtz32.realtime = new RTZ32_Status_Realtime();
//            this.device_rtz32.getDeviceStatus().rtz32.realtime.junction = true;
 
//            Controller_RTZ32_Factory_Configuration factory = new Controller_RTZ32_Factory_Configuration(this.device_rtz32.getDeviceConfiguration().rtz32);
//            factory.serialize();
//            Serialization.serialize(this.device_rtz32.getDeviceConfiguration().rtz32, new File("test1.json"));
//            RTZ32_Configuration config = factory.deserialize();
//            Serialization.serialize(config, new File("test2.json"));
 
//            RTZ32_Window_Realtime window = new RTZ32_Window_Realtime(device_rtz32);
//            window.setSize(new Dimension(1024, 768));
//            window.setLocation(0, 0);
//            window.setVisible(true);
            
//            RTZ32_Window window2 = new RTZ32_Window(device_rtz32, null);
//            window2.setSize(new Dimension(1024, 768));
//            window2.setLocation(0, 0);
//            window2.setVisible(true);            
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
        
            
        
        // Initialise
        
        if ((this.device_rtz32.status == null) || (this.device_rtz32.getDeviceStatus().rtz32 == null))
        {
            this.device_rtz32.status = new RTZ32_ControllerStatus();
            this.device_rtz32.getDeviceStatus().rtz32 = new RTZ32_Status();
        }
        
        if ((this.device_rtz32.alarms == null) || (this.device_rtz32.getDeviceAlarms().rtz32 == null))
        {
            this.device_rtz32.alarms = new RTZ32_ControllerAlarms();
            this.device_rtz32.getDeviceAlarms().rtz32 = new RTZ32_Alarms();
            this.device_rtz32.setAlarm("alarm_offline", System.currentTimeMillis());
        }
        
        if ((this.device_rtz32.realtime == null) || (this.device_rtz32.getDeviceRealtime().rtz32 == null))
        {
            this.device_rtz32.realtime  = new RTZ32_ControllerRealtime();
            this.device_rtz32.getDeviceRealtime().rtz32 = new RTZ32_Realtime();
        }
 
        if ((this.device_rtz32.configuration == null) || (this.device_rtz32.getDeviceConfiguration().rtz32 == null))
        {
            this.device_rtz32.configuration = new RTZ32_ControllerConfiguration();            
        }
 
        if (this.device_rtz32.getDeviceStatus().rtz32.forcings == null) this.device_rtz32.getDeviceStatus().rtz32.forcings = new RTZ32_Status_Forcings();
        if (this.device_rtz32.getDeviceStatus().rtz32.pendings == null) this.device_rtz32.getDeviceStatus().rtz32.pendings = new RTZ32_Status_Pendings();
        
        
        // Start talker
        
        talker = new RTZ32_Talker(name, this.device_rtz32, this);
        talker.setListener(new RTZ32_Talker_Listener()
        {
            public void connected()
            {
                if (device_rtz32.getDeviceInformation().number <= 9003)
                {
                    Shared.println(name, "1.Listener connected");
                }
                Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
                forceUpdate = true;
                if (device_rtz32.getDeviceInformation().number <= 9003)
                {
                    Shared.println(name, "2.Listener connected");
                }
            }
 
            public void disconnected()
            {
                {
                    talker.rtz32.setAlarm("alarm_offline", true);
                    Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
                    forceUpdate = true;
                }
            }
            
            public void realtimeUpdate()
            {
                long timestamp = System.currentTimeMillis();
                device_rtz32.getDeviceRealtime().lastTimestampUpdate = timestamp;
            }
            
            public void statusUpdate()
            {
                if ((updatingStatus == false) && (readingTables == false))
                {
                    if (device_rtz32.getDeviceInformation().number <= 9003)
                    {
                        Shared.println(name, "1.Listener status update");
                    }
                    mutexUpdate.lockWrite();
                    {
                        if (device_rtz32.getDeviceInformation().number <= 9003)
                        {
                            Shared.println(name, "2.Listener status update");
                        }
                        Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
                        if (device_rtz32.getDeviceInformation().number <= 9003)
                        {
                            Shared.println(name, "3.Listener status update");
                        }
                    }
                    mutexUpdate.releaseWrite();
                    if (device_rtz32.getDeviceInformation().number <= 9003)
                    {
                        Shared.println(name, "4.Listener status update");
                    }
                }
            }
            
            public void configurationUpdate()
            {
                if (device_rtz32.getDeviceInformation().number <= 9003)
                {
                    Shared.println(name, "1.Listener configuration update");
                }
                if ((updatingStatus == false) && (readingTables == false))
                {
                    mutexUpdate.lockWrite();
                    {
                        if (device_rtz32.getDeviceInformation().number <= 9003)
                        {
                            Shared.println(name, "2.Listener configuration update");
                        }
                        Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
                        if (device_rtz32.getDeviceInformation().number <= 9003)
                        {
                            Shared.println(name, "3.Listener configuration update");
                        }
                    }
                    mutexUpdate.releaseWrite();
                }
                if (device_rtz32.getDeviceInformation().number <= 9003)
                {
                    Shared.println(name, "4.Listener configuration update");
                }
            }
            
        });
        
    }
    
 
    
    public RTZ32_Controller getController()
    {
        return this.device_rtz32;
    }
    
    
    
    public void run()
    {
        Shared.traceInformation(name, "Starting");
        
        try
        {
            if (Shared.isServerEnabled() == true)
            {
                update();
            }
 
            long delay = 0;
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            long ts = TimeUtils.thisperiod(device_rtz32.getDeviceInformation().polling) + (device_rtz32.getDeviceInformation().polling * 1000L);
            delay = ts-calendar.getTimeInMillis();
 
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask() 
            {
                public void run () 
                {
                    try
                    {
                        if (talker.isConnected() == true)
                        {
                            talker.writer.CTRL_DET();
                        }
                        else
                        {
                            // Este periodo se ha de guardar el EtdStatus sin datos
                            addEtdStatus(createEtdStatus());
                        }
                    }
                    catch (Exception exception)
                    {
                    }
                }
 
            }, delay, device_rtz32.getDeviceInformation().polling * 1000);
 
            long timestamp1 = System.currentTimeMillis();
            
            while ((isInterrupted() == false) && (exit == false))
            {
                long timestamp2 = timestamp1 + (device_rtz32.getDeviceInformation().polling * 1000);
                
                if ((System.currentTimeMillis() > timestamp2) || (forceUpdate == true))
                {
                    try
                    {
                        forceUpdate = false;
                        
                        if (Shared.isServerEnabled() == true)
                        {
                            update();
                        }
                    }
                    catch (Exception exception)
                    {
                    }
                    
                    timestamp1 = timestamp1 + device_rtz32.getDeviceInformation().polling * 1000;
                }
                
                sleep(1000);
                
                
                if (Shared.model.existsDevice(device_rtz32.getIdentifier()) == false)
                {
                    Shared.println(name, Shared.getMessage("Device no longer exists"));
                    Shared.traceInformation(name, "Device no longer exists");
                    exit = true;
                }
            }
 
        }
        catch (Exception exception)
        {
        }
        
        Shared.traceInformation(name, "Finishing");
        
    }
    
    
    public byte[] printConfiguration(RTZ32_Report_Information reportInformation, String format, String language) throws ServerException
    {
        byte[] data = null;
 
        try
        {
            Report_Configuration report = new Report_Configuration(language);
            data = report.generate(format, reportInformation);
        }
        catch (ServerException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new ServerException(e);
        }
 
        return(data);
    }
    
    
    public byte[] printCCT(RTZ32_Report_Information reportInformation, String format, String language) throws ServerException
    {
        byte[] data = null;
 
        try
        {
            Report_CCT_Juzgados report = new Report_CCT_Juzgados(language);
            data = report.generateCCT(format, reportInformation, this.device_rtz32);
        }
        catch (ServerException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new ServerException(e);
        }
 
        return(data);
    }
    
    
    public byte[] printJuzgados(RTZ32_Report_Information reportInformation, String format, String language) throws ServerException
    {
        byte[] data = null;
 
        try
        {
            Report_CCT_Juzgados report = new Report_CCT_Juzgados(language);
            data = report.generateJuzgados(format, reportInformation, this.device_rtz32);
        }
        catch (ServerException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new ServerException(e);
        }
 
        return(data);
    }
    
 
    // <editor-fold defaultstate="collapsed" desc="Actions">
    
    
    public DeviceAction[] sendCommands(InteropParameters parameters) throws ServerException, SerializationException, Exception
    {
        RTZ32_ControllerCommands commands = parameters.getBodyContentValue(RTZ32_ControllerCommands.class);
        return Controller_RTZ32_Actions.sendCommands(this, parameters, commands);
    }
        
    
    
     //</editor-fold>
    
    
    public void addEtdStatus(EtdStatus etdStatus)
    {
        // Añadir el EtdStatus del periodo actual
        ltrafficData.add(etdStatus);
        if (ltrafficData.size() >= 1440)
            ltrafficData.remove(0);
    }
 
 
    public List<EtdStatus> getTrafficData(long timestamp)
    {
        List<EtdStatus> lresult = new ArrayList<EtdStatus>();
 
        try
        {
            for (EtdStatus etdStatus : ltrafficData)
            {
                if (etdStatus.measurementTimestamp >= timestamp)
                    lresult.add(etdStatus);
            }
        }
        catch (Exception e)
        {
            
        }
 
        return(lresult);
    }
 
 
    public EtdStatus createEtdStatus()
    {
        EtdStatus etdStatus = new EtdStatus();
        etdStatus.period = device_rtz32.getDeviceInformation().polling;
        etdStatus.measurementTimestamp = TimeUtils.thisperiod(device_rtz32.getDeviceInformation().polling);
        etdStatus.status = EtdStatus.STATUS_OFFLINE;
        etdStatus.llane = new ArrayList<EtdStatusLane>();
        etdStatus.total = new EtdStatusLane();
        return(etdStatus);
    }
 
 
    // <editor-fold defaultstate="collapsed" desc="Update">
    
    
    boolean init = true;
    
    public void forceUpdate()
    {
        this.forceUpdate = true;
    }
    
 
    
    
    protected void update() throws Exception
    {
        RTZ32_Controller rtz32 = getController();
 
        // Tables
        
        try
        {
            if (talker.isConnected() == true)
            {
                if ((rtz32.getDeviceConfiguration().rtz32 == null))  // || (rtz32.getDeviceConfiguration().rtz32.isValid() == false))
                {
                    configurationRead(Shared.getApplicationName(), Shared.getMessage("System initialization"), true, new ArrayList<String>());
                }
            }
        }
        catch (Exception exception)
        {
        }  
        
        
 
        // States and alarms
        
        mutexUpdate.lockWrite();
        
        updatingStatus = true;
                
        try
        {
            if (talker.isConnected() == true)
            {
                talker.writer.PET_ESTADO();
                talker.writer.PET_ALARMAS(0x01);
                talker.writer.PET_ALARMAS(0x02);
                talker.writer.PET_ALARMAS(0x03);
                talker.writer.PET_ALARMAS(0x04);
                talker.writer.PET_ALARMAS(0x05);
                talker.writer.PET_ALARMAS(0x06);
                talker.writer.PET_ALARMAS(0x07);
                talker.writer.PET_ALARMAS(0x08);
                talker.writer.PET_ALARMAS(0x09);
                talker.writer.PET_ALARMAS(0x0A);
                talker.writer.PET_ALARMAS(0x0F);
                talker.writer.PET_PLAN_DESFASE();
                talker.writer.PET_FH();
                talker.writer.PET_VERSION();
                
                
                RTZ32_Status_Pendings pendings = talker.rtz32.getDeviceStatus().rtz32.pendings;
                
                if ((pendings != null) && (pendings.hasPendings() == true))
                {
                    pendings.update(talker.rtz32.getDeviceStatus().rtz32);
                }
 
                Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
            }
            else
            {
                RTZ32_Status_Pendings pendings = talker.rtz32.getDeviceStatus().rtz32.pendings;
                
                if ((pendings != null) && (pendings.hasPendings() == true))
                {
                    pendings.update(talker.rtz32.getDeviceStatus().rtz32);
                }
                        
                Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
                
            }
        }
        catch (Exception exception)
        {
        }
        finally
        {
            updatingStatus = false;
            mutexUpdate.releaseWrite();
        }
 
    }
    
    
   
    // </editor-fold>
    
    
    public void adjustConfiguration (RTZ32_Information_Program program, RTZ32_Information_Program program0) throws ServerException
    {
        try
        {
            {
                RTZ32_Controller rtz32 = Serialization.clone(this.device_rtz32);
                for (RTZ32_Configuration_Group group : program.configuration.rtz32.groups)
                {
                    program0.configuration.rtz32.getGroup(group.number).setType(group.type);
                }
 
                Controller_RTZ32_Database.addProgram(program0);
            }
            
        }
        catch (Exception e)
        {
            
        }
    }
    
    
    public void adjustConfigurationPrograms (RTZ32_Information_Program program0) throws ServerException
    {
        try
        {
            for (int i=1; i<=RTZ32_Constants.MAXIMUM_NUMBER_PROGRAMS; i++)
            {
                try
                {
                    RTZ32_Information_Program programI = Controller_RTZ32_Database.getProgram(this.device_rtz32.getIdentifier(), "" + i);
                    RTZ32_Configuration_Program programCfg = programI.configuration.rtz32.getProgram(i);
                    RTZ32_Configuration_Program program0Cfg = program0.configuration.rtz32.getProgram(i);
                    // programI.configuration.rtz32.programs[i-1].subtableApproachingTimesTW = program0Cfg.subtableApproachingTimesTW;
                    // programI.configuration.rtz32.programs[i-1].subtableGroupTransitions = program0Cfg.subtableGroupTransitions;
                    // programI.configuration.rtz32.programs[i-1].subtableToleranceTimesTW = program0Cfg.subtableToleranceTimesTW;
                    for (int j=1; j<=RTZ32_Constants.MAXIMUM_NUMBER_GROUPS; j++)
                    {
                        try
                        {
                            programI.configuration.rtz32.groups[j-1].transitions = program0.configuration.rtz32.groups[j-1].transitions;
                        }
                        catch (Exception e)
                        {
                            
                        }
                    }
                    Controller_RTZ32_Database.addProgram(programI);
                }
                catch (Exception e)
                {
 
                }
            }
        }
        catch (Exception e)
        {
            
        }
    }
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Configuration read update">
 
    public void configurationRead(String username, String description, boolean update, List<String> programsDescriptions) throws ServerException
    {
        if ((device_rtz32.getDeviceRealtime().rtz32.reading != null) &&
            (device_rtz32.getDeviceRealtime().rtz32.reading.status == RTZ32_Realtime_Reading.READING_IN_PROGRESS))
        {
            // Already reading
            throw new ServerException(Shared.getMessage("Already reading tables"));
        }
 
        if ((device_rtz32.getDeviceRealtime().rtz32.writing != null) &&
            (device_rtz32.getDeviceRealtime().rtz32.writing.status == RTZ32_Realtime_Writing.WRITING_IN_PROGRESS))
        {
            // Already writing
            throw new ServerException(Shared.getMessage("Controller is writing tables"));
        }
        
        if (device_rtz32.getDeviceRealtime().rtz32.reading == null)
            device_rtz32.getDeviceRealtime().rtz32.reading = new RTZ32_Realtime_Reading();
        device_rtz32.getDeviceRealtime().rtz32.reading.status = RTZ32_Realtime_Reading.READING_IN_PROGRESS;
        device_rtz32.getDeviceRealtime().rtz32.reading.timestamp = System.currentTimeMillis();
        device_rtz32.getDeviceRealtime().rtz32.reading.progress = 0f;
        device_rtz32.getDeviceRealtime().rtz32.reading.table = 0;
        device_rtz32.getDeviceRealtime().rtz32.reading.configuration = null;
        device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis());
 
        long millis = System.currentTimeMillis();
        mutexUpdate.lockWrite(45000);
        if ((System.currentTimeMillis()-millis) >= 45000)
        {
            Shared.println(name, "LOCK READ TIMEOUT");
            device_rtz32.getDeviceRealtime().rtz32.reading.status = RTZ32_Realtime_Reading.READING_ERROR;
            mutexUpdate.releaseWrite();
            return;
        }
 
        try
        {
            readingTables = true;
            for (int i=1; i<=7; i++)  PET_TABLA(i);
            for (int i=11; i<=28; i++) PET_TABLA(i);
            for (int i=31; i<=40; i++) PET_TABLA(i);
            for (int i=42; i<=46; i++) PET_TABLA(i);
            for (int i=50; i<=60; i++) PET_TABLA(i);
            for (int i=65; i<=75; i++) PET_TABLA(i);
            for (int i=78; i<=85; i++) PET_TABLA(i);
            for (int i=91; i<=95; i++) PET_TABLA(i);
 
            talker.rtz32.getDeviceConfiguration().rtz32.valid = true;
 
            Shared.println(name, Shared.getMessage("Tables downloaded") + "!!!!!!!!!!!!!!!!!!");
 
            long recordingTimestamp = System.currentTimeMillis();
 
            if (update == true)
            {
                talker.rtz32.getDeviceConfiguration().rtz32.description = description;
                talker.rtz32.getDeviceConfiguration().rtz32.recordingTimestamp = recordingTimestamp;
                for (int i=0; i<talker.rtz32.getDeviceConfiguration().rtz32.programs.length; i++)
                {
                    if (talker.rtz32.getDeviceConfiguration().rtz32.programs[i] != null)
                    {
                        if ((programsDescriptions.size() > i) && (programsDescriptions.get(i) != null))
                            talker.rtz32.getDeviceConfiguration().rtz32.programs[i].description = programsDescriptions.get(i);
                        else
                            talker.rtz32.getDeviceConfiguration().rtz32.programs[i].description = description;
 
                        talker.rtz32.getDeviceConfiguration().rtz32.programs[i].recordingTimestamp = recordingTimestamp;
                    }
                }
 
                // Si lee todas las tablas correctamente se actualiza la configuracion
                Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
 
                // Tables readed, insert in historical
 
                RTZ32_Persistent_Configuration configuration = new RTZ32_Persistent_Configuration();
                configuration.device = device_rtz32.getIdentifier();
                configuration.datetime = new Date();
                configuration.username = username;
                configuration.action = Shared.getMessage("Tables downloading");
                configuration.operation = Shared.getMessage("All");
                configuration.distribution = Shared.getMessage("All");
                configuration.description = description;
                configuration.configuration = talker.rtz32.getDeviceConfiguration();
                Controller_RTZ32_Database.addConfiguration(configuration);
            }
            else
            {
                device_rtz32.getDeviceRealtime().rtz32.reading.configuration = talker.rtz32.getDeviceConfiguration().rtz32;
                talker.rtz32 = Serialization.clone(device_rtz32);  // Cambio 09092022
            }
 
            device_rtz32.getDeviceRealtime().rtz32.reading.status = RTZ32_Realtime_Reading.READING_OK;
            device_rtz32.getDeviceRealtime().rtz32.reading.table = 0;
        }
        catch (Exception exception)
        {
            device_rtz32.getDeviceRealtime().rtz32.reading.status = RTZ32_Realtime_Reading.READING_ERROR;
            device_rtz32.getDeviceRealtime().rtz32.reading.configuration = null;
            throw new ServerException(exception.getMessage());
        }
        finally
        {
            // device_rtz32.getDeviceRealtime().rtz32.reading = null;
            device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis());
            readingTables = false;
            mutexUpdate.releaseWrite();
        }
    }
    
    
    
        
    private void PET_TABLA(int number) throws Exception
    { 
        device_rtz32.getDeviceRealtime().rtz32.reading.table = number;
        
        long timestamp1 = System.currentTimeMillis();
        {
            talker.writer.PET_TABLA(number);        
        }
        long timestamp2 = System.currentTimeMillis();
        
        if ((timestamp2-timestamp1) > talker.rtz32.getDeviceInformation().connection.readTimeout) 
        {
            throw new Exception("Timeout");
        }
        device_rtz32.getDeviceRealtime().rtz32.reading.progress = device_rtz32.getDeviceRealtime().rtz32.reading.progress + 1.35f;
        device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis());
    }
 
    //</editor-fold>
 
    
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Configuration write">
 
    public void configurationWrite(String username, RTZ32_Commands_Configuration_Write command) throws ServerException
    {
        if ((device_rtz32.getDeviceRealtime().rtz32.writing != null) &&
            (device_rtz32.getDeviceRealtime().rtz32.writing.status == RTZ32_Realtime_Writing.WRITING_IN_PROGRESS))
        {
            // Already writing
            
            throw new ServerException("Already writing tables");
        }
        
        if ((device_rtz32.getDeviceRealtime().rtz32.reading != null) &&
            (device_rtz32.getDeviceRealtime().rtz32.reading.status == RTZ32_Realtime_Reading.READING_IN_PROGRESS))
        {
            // Already writing
            
            throw new ServerException("Conntroller is reading tables");
        }
 
 
        if (device_rtz32.getDeviceRealtime().rtz32.writing == null)
            device_rtz32.getDeviceRealtime().rtz32.writing = new RTZ32_Realtime_Writing();
        device_rtz32.getDeviceRealtime().rtz32.writing.timestamp = System.currentTimeMillis();
        device_rtz32.getDeviceRealtime().rtz32.writing.status = RTZ32_Realtime_Writing.WRITING_IN_PROGRESS;
        device_rtz32.getDeviceRealtime().rtz32.writing.progress = 0f;
        device_rtz32.getDeviceRealtime().rtz32.writing.table = 0;
        device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis());
 
        long millis = System.currentTimeMillis();
        mutexUpdate.lockWrite(45000);
        if ((System.currentTimeMillis()-millis) >= 45000)
        {
            Shared.println(name, "LOCK WRITE TIMEOUT");
            device_rtz32.getDeviceRealtime().rtz32.writing.status = RTZ32_Realtime_Writing.WRITING_ERROR;
            mutexUpdate.releaseWrite();
            return;
        }
 
        updatingStatus = true;
 
        try
        {
            // Tables write, insert in historical
            Shared.println(name, "############################################# CLONING");
            talker.rtz32 = Serialization.clone(device_rtz32);
            talker.rtz32.realtime = device_rtz32.realtime;
            Shared.println(name, "############################################# CLONED");
 
            RTZ32_Persistent_Configuration configuration = new RTZ32_Persistent_Configuration();
            configuration.device = device_rtz32.getIdentifier();
            configuration.datetime = new Date();
            configuration.username = username;
            configuration.action = "Uploaded tables";
            if (command.operationTables == true) configuration.operation = "All"; else configuration.operation = "None";
            
            if (command.distributionTables == true) 
            {
                if ((command.programs == null) || (command.programs.size() == 0))
                {
                    configuration.distribution = "All";
                }
                else
                {
                    configuration.distribution = command.getProgramsTables();
                }
            } 
            else 
            {
                configuration.distribution = "None";
            }
            
 
            // Antes de enviar las tablas se ha de poner el regulador en Grabacion:ON (pagina 7 protocolo)
            // Si al grabar una tabla devuelve 0(ok) se pasa a la siguiente, si devuelve 1(Error), no se puede enviar la siguiente
            // (Mirar pagina 9 protocolo)
            // Que hacer si da error?
            // Al acabar de grabar todas las tablas se ha de enviar Grabacion:OFF
            // Si todo ha ido bien (se ha activado grabacion, se han enviado todas las tablas correctamente, se ha desactivado grabacion)
            // entonces se ha de borrar la copia de trabajo enviada
 
            // Activar grabacion
            {
                SEND_GRABACION(true);
            }
 
            int[] programsTables = new int[0];
 
            // Enviar tablas
            {
                if (command.operationTables == true)
                {
                    // Send all tables less distribution tables
                    for (int i=1; i<=7; i++)  SEND_TABLA(i, command.configuration);
 
                    if (configuration.distribution.equalsIgnoreCase("All") == true)
                    {
                        programsTables = new int[18];
 
                        // Send all tables 11 to 28
                        for (int i=11; i<=28; i++)
                        {
                            programsTables[i-11] = i;
                            SEND_TABLA(i, command.configuration);
                        }
 
                        configuration.action = "Enviadas tablas de reparto y de funcionamiento";
                    }
                    else if (configuration.distribution.equalsIgnoreCase("None") == true)
                    {
                        // Nothing to do
                        configuration.action = "Enviadas tablas de funcionamiento";
                    }
                    else
                    {
                        // Send all tables 11 to 28 indicated in configuration.distribution
                        configuration.action = "Enviadas tablas de repartos (";
                        programsTables = StringUtils.obtenerTokensInt(configuration.distribution, ",");
                        for (int i=0; i<programsTables.length; i++)
                        {
                            SEND_TABLA(programsTables[i], command.configuration);
                            configuration.action += (programsTables[i]-10) + "";
                            if (i<(programsTables.length-1)) configuration.action += ",";
                        }
 
                        configuration.action += ") y tablas de funcionamiento";
                    }
 
                    for (int i=31; i<=40; i++) SEND_TABLA(i, command.configuration);
                    for (int i=42; i<=46; i++) SEND_TABLA(i, command.configuration);
                    for (int i=50; i<=60; i++) SEND_TABLA(i, command.configuration);
                    for (int i=65; i<=75; i++) SEND_TABLA(i, command.configuration);
                    for (int i=78; i<=85; i++) SEND_TABLA(i, command.configuration);
 
                    // Las tablas 91 a 94 (Incompatibilidades) no se graban, 
                    // están prefijadas en fabrica en el chipset del regulador
 
                    SEND_TABLA(95, command.configuration);
                }
                else
                {
                    if (configuration.distribution.equalsIgnoreCase("All") == true)
                    {
                        // Send tables 1 to 7
                        for (int i=1; i<=7; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send all tables 11 to 28
                        for (int i=11; i<=28; i++)
                        {
                            programsTables[i-11] = i;
                            SEND_TABLA(i, command.configuration);
                        }
 
                        // Send tables 35 to 37
                        for (int i=35; i<=37; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send tables 42 to 45
                        for (int i=42; i<=45; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send tables 72 to 73
                        for (int i=72; i<=73; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send tables 78 to 79
                        for (int i=78; i<=79; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send table 80 (Correlation times)
                        SEND_TABLA(80, command.configuration);
 
                        // Send tables 82 to 85
                        for (int i=82; i<=85; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send table 95 (Junction number and number of groups)
                        SEND_TABLA(95, command.configuration);
 
                        configuration.action = "Enviadas tablas de todos los repartos";
                    }
                    else if (configuration.distribution.equalsIgnoreCase("None") == true)
                    {
                        // Nothing to do
                    }
                    else
                    {
                        // Send tables 1 to 7
                        for (int i=1; i<=7; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send all tables 11 to 28 indicated in configuration.distribution
                        configuration.action = "Enviadas tablas de repartos (";
                        programsTables = StringUtils.obtenerTokensInt(configuration.distribution, ",");
                        for (int i=0; i<programsTables.length; i++)
                        {
                            SEND_TABLA(programsTables[i], command.configuration);
                            configuration.action += (programsTables[i]-10) + "";
                            if (i<(programsTables.length-1)) configuration.action += ",";
                        }
 
                        // Send tables 35 to 37
                        for (int i=35; i<=37; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send tables 42 to 45
                        for (int i=42; i<=45; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send tables 72 to 73
                        for (int i=72; i<=73; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send tables 78 to 79
                        for (int i=78; i<=79; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send table 80 (Correlation times)
                        SEND_TABLA(80, command.configuration);
 
                        // Send tables 82 to 85
                        for (int i=82; i<=85; i++)  SEND_TABLA(i, command.configuration);
 
                        // Send table 95 (Junction number and number of groups)
                        SEND_TABLA(95, command.configuration);
 
                        configuration.action += ")";
                    }
                }
            }
 
            // Desactivar grabacion
            {
                SEND_GRABACION(false);
            }
 
            long recordingTimestamp = System.currentTimeMillis();
 
            // Si todo ha ido bien se ha de registrar en la base de datos la configuración enviada
            {
                configuration.description = command.description;
                if (configuration.description == null) configuration.description = "?";
 
                // Fill talker.rtz32.configuration
                // RTZ32_Configuration newConfiguration = talker.rtz32.getDeviceConfiguration().rtz32;
                // newConfiguration.description = configuration.description;
                // newConfiguration.recordingTimestamp = recordingTimestamp;
 
                for (int i=0; i<programsTables.length; i++)
                {
                    int programNumber = programsTables[i] - 10;
                    RTZ32_Configuration_Program program = talker.rtz32.getDeviceConfiguration().rtz32.getProgram(programNumber);
                    talker.rtz32.getDeviceConfiguration().rtz32.programs[programNumber-1] = command.configuration.programs[programNumber-1];
                    talker.rtz32.getDeviceConfiguration().rtz32.programs[programNumber-1].recordingTimestamp = recordingTimestamp;
                    // if (program.description == null) program.description = command.configuration.programs[programNumber-1].description;
                }
            }
 
            {
                talker.rtz32.getDeviceConfiguration().rtz32.description = configuration.description;
                talker.rtz32.getDeviceConfiguration().rtz32.recordingTimestamp = recordingTimestamp;
                // Los programas no enviados mantienen su descripción y fecha
//                for (int i=0; i<talker.rtz32.getDeviceConfiguration().rtz32.programs.length; i++)
//                {
//                    if (talker.rtz32.getDeviceConfiguration().rtz32.programs[i] != null)
//                    {
//                        talker.rtz32.getDeviceConfiguration().rtz32.programs[i].description = configuration.description;
//                        talker.rtz32.getDeviceConfiguration().rtz32.programs[i].recordingTimestamp = recordingTimestamp;
//                    }
//                }
 
                // Si lee todas las tablas correctamente se actualiza la configuracion
                Shared.model.updateDevice(device_rtz32, Serialization.clone(talker.rtz32));
 
                configuration.configuration = talker.rtz32.getDeviceConfiguration();
                // configuration.configuration.rtz32 = command.configuration;
                Controller_RTZ32_Database.addConfiguration(configuration);
            }
 
 
            // Si todo ha ido bien se ha de eliminar la copia de trabajo
            {
                // TODO: Eliminar copia de trabajo
                for (int i=0; i<programsTables.length; i++)
                {
                    Shared.println(name, "Deleting work copy program: " + (programsTables[i]-10));
                    boolean result = Controller_RTZ32_Database.deleteProgram(device_rtz32.getIdentifier(), "" + (programsTables[i]-10));
                    Shared.println(name, "Deleted work copy program: " + (programsTables[i]-10) + ", " + result);
                }
            }
 
            device_rtz32.getDeviceRealtime().rtz32.writing.status = RTZ32_Realtime_Writing.WRITING_OK;
            device_rtz32.getDeviceRealtime().rtz32.writing.table = 0;
        }
        catch (Exception exception)
        {
            Shared.printstack(name, exception);
            try
            {
                SEND_GRABACION(false);
            }
            catch (Exception e)
            {
            }
 
            device_rtz32.getDeviceRealtime().rtz32.writing.status = RTZ32_Realtime_Writing.WRITING_ERROR;
        }
        finally
        {
            // device_rtz32.getDeviceRealtime().rtz32.writing = null;
            device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis());
            updatingStatus = false;
            mutexUpdate.releaseWrite();
        }
    }
    
    
    
        
    private void SEND_GRABACION(boolean value) throws Exception
    { 
        device_rtz32.getDeviceRealtime().rtz32.recording = RTZ32_Realtime.RECORDING_UNKNOWN;
 
        long timestamp1 = System.currentTimeMillis();
        {
            talker.writer.ORD_GRABACION(value);
        }
        long timestamp2 = System.currentTimeMillis();
        
        if ((timestamp2-timestamp1) > talker.rtz32.getDeviceInformation().connection.readTimeout) 
        {
            throw new Exception("Timeout");
        }
 
        device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis());
    }
    
    
    
        
    private void SEND_TABLA(int tableNumber, RTZ32_Configuration configuration) throws Exception
    { 
        device_rtz32.getDeviceRealtime().rtz32.writing.table = tableNumber;
        
        long timestamp1 = System.currentTimeMillis();
        {
            RTZ32_Configuration_Table table = getTable(tableNumber);
            if (table != null)
            {
                byte[] data = table.serialize(configuration);
                talker.writer.ORD_TABLA(table, data);
            }
        }
        long timestamp2 = System.currentTimeMillis();
        
        if ((timestamp2-timestamp1) > talker.rtz32.getDeviceInformation().connection.readTimeout) 
        {
            throw new Exception("Timeout");
        }
 
        if (device_rtz32.getDeviceRealtime().rtz32.writing.tableWrittenCorrect == false)
        {
            throw new Exception("Error writing table");
        }
 
        device_rtz32.getDeviceRealtime().rtz32.writing.progress = device_rtz32.getDeviceRealtime().rtz32.writing.progress + 1.41f;
        device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis());
    }
    
 
    private RTZ32_Configuration_Table getTable(int tableNumber)
    {
        RTZ32_Configuration_Table table = null;
 
        if ((tableNumber >= 11) && (tableNumber) <= 28)
        {
            table = new RTZ32_Configuration_Table_11_28(tableNumber); 
        }
        else if ((tableNumber >= 38) && (tableNumber) <= 40)
        {
            table = new RTZ32_Configuration_Table_38_40(tableNumber); 
        }
        else if ((tableNumber >= 53) && (tableNumber) <= 60)
        {
            table = new RTZ32_Configuration_Table_53_60(tableNumber); 
        }
        else if ((tableNumber >= 82) && (tableNumber) <= 85)
        {
            table = new RTZ32_Configuration_Table_82_85(tableNumber); 
        }
        else
        {
            switch (tableNumber)
            {
                case 1: table = new RTZ32_Configuration_Table_1(tableNumber); break;
                case 2: table = new RTZ32_Configuration_Table_2(tableNumber); break;
                case 3: table = new RTZ32_Configuration_Table_3(tableNumber); break;
                case 4: table = new RTZ32_Configuration_Table_4(tableNumber); break;
                case 5: table = new RTZ32_Configuration_Table_5(tableNumber); break;
                case 6: table = new RTZ32_Configuration_Table_6(tableNumber); break;
                case 7: table = new RTZ32_Configuration_Table_7(tableNumber); break;
                case 31: table = new RTZ32_Configuration_Table_31(tableNumber); break;
                case 32: table = new RTZ32_Configuration_Table_32(tableNumber); break;
                case 33: table = new RTZ32_Configuration_Table_33(tableNumber); break;
                case 34: table = new RTZ32_Configuration_Table_34(tableNumber); break;
                case 35: table = new RTZ32_Configuration_Table_35(tableNumber); break;
                case 36: table = new RTZ32_Configuration_Table_36(tableNumber); break;
                case 37: table = new RTZ32_Configuration_Table_37(tableNumber); break;
                case 42: table = new RTZ32_Configuration_Table_42(tableNumber); break;
                case 43: table = new RTZ32_Configuration_Table_43(tableNumber); break;
                case 44: table = new RTZ32_Configuration_Table_44(tableNumber); break;
                case 45: table = new RTZ32_Configuration_Table_45(tableNumber); break;
                case 46: table = new RTZ32_Configuration_Table_46(tableNumber); break;
                case 50: table = new RTZ32_Configuration_Table_50(tableNumber); break;
                case 51: table = new RTZ32_Configuration_Table_51(tableNumber); break;
                case 52: table = new RTZ32_Configuration_Table_52(tableNumber); break;
                case 65: table = new RTZ32_Configuration_Table_65(tableNumber); break;
                case 66: table = new RTZ32_Configuration_Table_66(tableNumber); break;
                case 67: table = new RTZ32_Configuration_Table_67(tableNumber); break;
                case 68: table = new RTZ32_Configuration_Table_68(tableNumber); break;
                case 69: table = new RTZ32_Configuration_Table_69(tableNumber); break;
                case 70: table = new RTZ32_Configuration_Table_70(tableNumber); break;
                case 71: table = new RTZ32_Configuration_Table_71(tableNumber); break;
                case 72: table = new RTZ32_Configuration_Table_72(tableNumber); break;
                case 73: table = new RTZ32_Configuration_Table_73(tableNumber); break;
                case 74: table = new RTZ32_Configuration_Table_74(tableNumber); break;
                case 75: table = new RTZ32_Configuration_Table_75(tableNumber); break;
                case 78: table = new RTZ32_Configuration_Table_78(tableNumber); break;
                case 79: table = new RTZ32_Configuration_Table_79(tableNumber); break;
                case 80: table = new RTZ32_Configuration_Table_80(tableNumber); break;
                case 81: table = new RTZ32_Configuration_Table_81(tableNumber); break;
                case 91: table = new RTZ32_Configuration_Table_91(tableNumber); break;
                case 92: table = new RTZ32_Configuration_Table_92(tableNumber); break;
                case 93: table = new RTZ32_Configuration_Table_93(tableNumber); break;
                case 94: table = new RTZ32_Configuration_Table_94(tableNumber); break;
                case 95: table = new RTZ32_Configuration_Table_95(tableNumber); break;
                default: table = new RTZ32_Configuration_Table(tableNumber); break;
            }
        }
 
        return(table);
    }
    //</editor-fold>
        
 
 
    
}