-
Notifications
You must be signed in to change notification settings - Fork 129
/
api.rb
1694 lines (1542 loc) · 61.3 KB
/
api.rb
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
# encoding: ascii-8bit
# Copyright 2014 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
require 'cosmos/script/extract'
require 'cosmos/script/api_shared'
require 'cosmos/tools/tlm_viewer/tlm_viewer_config'
module Cosmos
module Api
include Extract
include ApiShared
# Sets api_requests to 0 and initializes the whitelist of allowable API
# method calls
def initialize
@api_whitelist = [
'cmd',
'cmd_no_range_check',
'cmd_no_hazardous_check',
'cmd_no_checks',
'cmd_raw',
'cmd_raw_no_range_check',
'cmd_raw_no_hazardous_check',
'cmd_raw_no_checks',
'send_raw',
'get_cmd_buffer',
'get_cmd_list',
'get_cmd_param_list',
'get_cmd_hazardous',
'get_cmd_value',
'get_cmd_time',
'tlm',
'tlm_raw',
'tlm_formatted',
'tlm_with_units',
'tlm_variable',
'set_tlm',
'set_tlm_raw',
'inject_tlm',
'override_tlm',
'override_tlm_raw',
'normalize_tlm',
'get_tlm_buffer',
'get_tlm_packet',
'get_tlm_values',
'get_tlm_list',
'get_tlm_item_list',
'get_tlm_details',
'get_out_of_limits',
'get_overall_limits_state',
'limits_enabled?',
'enable_limits',
'disable_limits',
'get_stale',
'get_limits',
'set_limits',
'get_limits_groups',
'enable_limits_group',
'disable_limits_group',
'get_limits_sets',
'set_limits_set',
'get_limits_set',
'get_target_list',
'subscribe_limits_events',
'unsubscribe_limits_events',
'get_limits_event',
'subscribe_packet_data',
'unsubscribe_packet_data',
'get_packet_data',
'subscribe_server_messages',
'unsubscribe_server_messages',
'get_server_message',
'get_interface_targets',
'get_interface_names',
'connect_interface',
'disconnect_interface',
'interface_state',
'map_target_to_interface',
'get_router_names',
'connect_router',
'disconnect_router',
'router_state',
'get_all_target_info',
'get_target_info',
'get_target_ignored_parameters',
'get_target_ignored_items',
'get_interface_info',
'get_all_interface_info',
'get_router_info',
'get_all_router_info',
'get_all_cmd_info',
'get_all_tlm_info',
'get_cmd_cnt',
'get_tlm_cnt',
'get_packet_loggers',
'get_packet_logger_info',
'get_all_packet_logger_info',
'get_background_tasks',
'start_background_task',
'stop_background_task',
'get_server_status',
'get_cmd_log_filename',
'get_tlm_log_filename',
'start_logging',
'stop_logging',
'start_cmd_log',
'start_tlm_log',
'stop_cmd_log',
'stop_tlm_log',
'start_raw_logging_interface',
'stop_raw_logging_interface',
'start_raw_logging_router',
'stop_raw_logging_router',
'get_server_message_log_filename',
'start_new_server_message_log',
'cmd_tlm_reload',
'cmd_tlm_clear_counters',
'get_output_logs_filenames',
'replay_select_file',
'replay_status',
'replay_set_playback_delay',
'replay_play',
'replay_reverse_play',
'replay_stop',
'replay_step_forward',
'replay_step_back',
'replay_move_start',
'replay_move_end',
'replay_move_index',
'get_screen_list',
'get_screen_definition',
'get_saved_config',
]
@tlm_viewer_config_filename = nil
@tlm_viewer_config = nil
end
############################################################################
# Methods Used by cosmos/script
############################################################################
# Send a command packet to a target.
#
# Accepts two different calling styles:
# cmd("TGT CMD with PARAM1 val, PARAM2 val")
# cmd('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args [String|Array<String>] See the description for calling style
# @return [Array<String, String, Hash>] target_name, command_name, parameters
def cmd(*args)
cmd_implementation(true, true, false, 'cmd', *args)
end
# Send a command packet to a target without performing any value range
# checks on the parameters. Useful for testing to allow sending command
# parameters outside the allowable range as defined in the configuration.
#
# Accepts two different calling styles:
# cmd_no_range_check("TGT CMD with PARAM1 val, PARAM2 val")
# cmd_no_range_check('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param (see #cmd)
# @return (see #cmd)
def cmd_no_range_check(*args)
cmd_implementation(false, true, false, 'cmd_no_range_check', *args)
end
# Send a command packet to a target without performing any hazardous checks
# both on the command itself and its parameters. Useful in scripts to
# prevent popping up warnings to the user.
#
# Accepts two different calling styles:
# cmd_no_hazardous_check("TGT CMD with PARAM1 val, PARAM2 val")
# cmd_no_hazardous_check('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param (see #cmd)
# @return (see #cmd)
def cmd_no_hazardous_check(*args)
cmd_implementation(true, false, false, 'cmd_no_hazardous_check', *args)
end
# Send a command packet to a target without performing any value range
# checks or hazardous checks both on the command itself and its parameters.
#
# Accepts two different calling styles:
# cmd_no_checks("TGT CMD with PARAM1 val, PARAM2 val")
# cmd_no_checks('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param (see #cmd)
# @return (see #cmd)
def cmd_no_checks(*args)
cmd_implementation(false, false, false, 'cmd_no_checks', *args)
end
# Send a command packet to a target without running conversions.
#
# Accepts two different calling styles:
# cmd_raw("TGT CMD with PARAM1 val, PARAM2 val")
# cmd_raw('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args [String|Array<String>] See the description for calling style
# @return [Array<String, String, Hash>] target_name, command_name, parameters
def cmd_raw(*args)
cmd_implementation(true, true, true, 'cmd_raw', *args)
end
# Send a command packet to a target without performing any value range
# checks on the parameters or running conversions. Useful for testing to allow sending command
# parameters outside the allowable range as defined in the configuration.
#
# Accepts two different calling styles:
# cmd_raw_no_range_check("TGT CMD with PARAM1 val, PARAM2 val")
# cmd_raw_no_range_check('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param (see #cmd)
# @return (see #cmd)
def cmd_raw_no_range_check(*args)
cmd_implementation(false, true, true, 'cmd_raw_no_range_check', *args)
end
# Send a command packet to a target without running conversions or performing any hazardous checks
# both on the command itself and its parameters. Useful in scripts to
# prevent popping up warnings to the user.
#
# Accepts two different calling styles:
# cmd_raw_no_hazardous_check("TGT CMD with PARAM1 val, PARAM2 val")
# cmd_raw_no_hazardous_check('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param (see #cmd)
# @return (see #cmd)
def cmd_raw_no_hazardous_check(*args)
cmd_implementation(true, false, true, 'cmd_raw_no_hazardous_check', *args)
end
# Send a command packet to a target without running conversions or performing any value range
# checks or hazardous checks both on the command itself and its parameters.
#
# Accepts two different calling styles:
# cmd_raw_no_checks("TGT CMD with PARAM1 val, PARAM2 val")
# cmd_raw_no_checks('TGT','CMD','PARAM1'=>val,'PARAM2'=>val)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param (see #cmd)
# @return (see #cmd)
def cmd_raw_no_checks(*args)
cmd_implementation(false, false, true, 'cmd_raw_no_checks', *args)
end
# Send a raw binary string to the specified interface.
#
# @param interface_name [String] The interface to send the raw binary
# @param data [String] The raw binary data
def send_raw(interface_name, data)
CmdTlmServer.commanding.send_raw(interface_name, data)
nil
end
# Returns the raw buffer from the most recent specified command packet.
#
# @param target_name [String] Target name of the command
# @param command_name [String] Packet name of the command
# @return [String] last command buffer packet
def get_cmd_buffer(target_name, command_name)
packet = System.commands.packet(target_name, command_name)
return packet.buffer
end
# Returns the list of all the command names and their descriptions from the
# given target.
#
# @param target_name [String] Name of the target
# @return [Array<Array<String, String>>] Array containing \[command name,
# command description] for all commands in the target
def get_cmd_list(target_name)
list = []
System.commands.packets(target_name).each do |name, command|
list << [name, command.description]
end
return list.sort
end
# Returns the list of all the parameters for the given command.
#
# @param target_name (see #get_cmd_list)
# @param command_name [String] Name of the command
# @return [Array<Array<String, Object, nil|Array, nil|String, nil|String,
# nil|String, Boolean>] Array containing \[name, default, states,
# description, units_full, units, required] for all parameters in the
# command
def get_cmd_param_list(target_name, command_name)
list = []
System.commands.params(target_name, command_name).each do |parameter|
if parameter.format_string
unless parameter.default.kind_of?(Array)
list << [parameter.name, sprintf(parameter.format_string, parameter.default), parameter.states, parameter.description, parameter.units_full, parameter.units, parameter.required, parameter.data_type.to_s]
else
list << [parameter.name, "[]", parameter.states, parameter.description, parameter.units_full, parameter.units, parameter.required, parameter.data_type.to_s]
end
else
list << [parameter.name, parameter.default, parameter.states, parameter.description, parameter.units_full, parameter.units, parameter.required, parameter.data_type.to_s]
end
end
return list
end
# Returns whether the specified command is hazardous
#
# @param target_name (see #get_cmd_param_list)
# @param command_name (see #get_cmd_param_list)
# @param params [Hash] Command parameter hash to test whether a particular
# parameter setting makes the command hazardous
# @return [Boolean] Whether the command is hazardous
def get_cmd_hazardous(target_name, command_name, params = {})
hazardous, _ = System.commands.cmd_hazardous?(target_name, command_name, params)
return hazardous
end
# Returns a value from the specified command
#
# @param target_name [String] Target name of the command
# @param command_name [String] Packet name of the command
# @param parameter_name [String] Parameter name in the command
# @param value_type [Symbol] How the values should be converted. Must be
# one of {Packet::VALUE_TYPES}
# @return [Varies] value
def get_cmd_value(target_name, command_name, parameter_name, value_type = :CONVERTED)
packet = System.commands.packet(target_name, command_name)
# Virtually support RECEIVED_TIMEFORMATTED, RECEIVED_TIMESECONDS, RECEIVED_COUNT
# Also PACKET_TIMEFORMATTED and PACKET_TIMESECONDS
case parameter_name.to_s.upcase
when 'PACKET_TIMEFORMATTED'
if packet.packet_time
return packet.packet_time.formatted
else
return 'No Packet Time'
end
when 'PACKET_TIMESECONDS'
if packet.packet_time
return packet.packet_time.to_f
else
return 0.0
end
when 'RECEIVED_TIMEFORMATTED'
if packet.received_time
return packet.received_time.formatted
else
return 'No Packet Received Time'
end
when 'RECEIVED_TIMESECONDS'
if packet.received_time
return packet.received_time.to_f
else
return 0.0
end
when 'RECEIVED_COUNT'
return packet.received_count
else
return packet.read(parameter_name, value_type.intern)
end
end
# Returns the time the most recent command was sent
#
# @param target_name [String] Target name of the command. If not given then
# the most recent time from all commands will be returned
# @param command_name [String] Packet name of the command. If not given then
# then most recent time from the given target will be returned.
# @return [Array<Target Name, Command Name, Time Seconds, Time Microseconds>]
def get_cmd_time(target_name = nil, command_name = nil)
last_command = nil
if target_name
if command_name
last_command = System.commands.packet(target_name, command_name)
else
System.commands.packets(target_name).each do |packet_name, command|
last_command = command if !last_command and command.received_time
if command.received_time and command.received_time > last_command.received_time
last_command = command
end
end
end
else
commands = System.commands.all
commands.each do |_, target_commands|
target_commands.each do |packet_name, command|
last_command = command if !last_command and command.received_time
if command.received_time and command.received_time > last_command.received_time
last_command = command
end
end
end
end
if last_command
if last_command.received_time
return [last_command.target_name, last_command.packet_name, last_command.received_time.tv_sec, last_command.received_time.tv_usec]
else
return [last_command.target_name, last_command.packet_name, nil, nil]
end
else
return [nil, nil, nil, nil]
end
end
# Request a converted telemetry item from a packet.
#
# Accepts two different calling styles:
# tlm("TGT PKT ITEM")
# tlm('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args [String|Array<String>] See the description for calling style
# @return [Numeric] The converted telemetry value without formatting or
# units
def tlm(*args)
target_name, packet_name, item_name = tlm_process_args(args, 'tlm')
System.telemetry.value(target_name, packet_name, item_name, :CONVERTED)
end
# Request a raw telemetry item from a packet.
#
# Accepts two different calling styles:
# tlm_raw("TGT PKT ITEM")
# tlm_raw('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args (see #tlm)
# @return [Numeric] The unconverted telemetry value without formatting or
# units
def tlm_raw(*args)
target_name, packet_name, item_name = tlm_process_args(args, 'tlm_raw')
System.telemetry.value(target_name, packet_name, item_name, :RAW)
end
# Request a formatted telemetry item from a packet.
#
# Accepts two different calling styles:
# tlm_formatted("TGT PKT ITEM")
# tlm_formatted('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args (see #tlm)
# @return [String] The converted telemetry value with formatting but
# without units
def tlm_formatted(*args)
target_name, packet_name, item_name = tlm_process_args(args, 'tlm_formatted')
System.telemetry.value(target_name, packet_name, item_name, :FORMATTED)
end
# Request a telemetry item with units from a packet.
#
# Accepts two different calling styles:
# tlm_with_units("TGT PKT ITEM")
# tlm_with_units('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args (see #tlm)
# @return [String] The converted, formatted telemetry value with units
def tlm_with_units(*args)
target_name, packet_name, item_name = tlm_process_args(args, 'tlm_with_units')
System.telemetry.value(target_name, packet_name, item_name, :WITH_UNITS)
end
# Request a telemetry item from a packet with the specified conversion
# applied. This method is equivalent to calling the other tlm_xxx methods.
#
# Accepts two different calling styles:
# tlm_variable("TGT PKT ITEM", :RAW)
# tlm_variable('TGT','PKT','ITEM', :RAW)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args The args must either be a string followed by a symbol or
# three strings followed by a symbol (see the calling style in the
# description). The symbol must be one of {Packet::VALUE_TYPES}.
# @return [Object] The converted telemetry value
def tlm_variable(*args)
target_name, packet_name, item_name, value_type = tlm_variable_process_args(args, 'tlm_variable')
System.telemetry.value(target_name, packet_name, item_name, value_type)
end
# Set a telemetry item in a packet to a particular value and then verifies
# the value is within the acceptable limits. This method uses any
# conversions that apply to the item when setting the value.
#
# Note: If this is done while COSMOS is currently receiving telemetry,
# this value could get overwritten at any time. Thus this capability is
# best used for testing or for telemetry items that are not received
# regularly through the target interface.
#
# Accepts two different calling styles:
# set_tlm("TGT PKT ITEM = 1.0")
# set_tlm('TGT','PKT','ITEM', 10.0)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args The args must either be a string followed by a value or
# three strings followed by a value (see the calling style in the
# description).
def set_tlm(*args)
target_name, packet_name, item_name, value = set_tlm_process_args(args, 'set_tlm')
if target_name == 'SYSTEM'.freeze and packet_name == 'META'.freeze
raise "set_tlm not allowed on #{target_name} #{packet_name} #{item_name}" if ['PKTID', 'CONFIG'].include?(item_name)
end
System.telemetry.set_value(target_name, packet_name, item_name, value, :CONVERTED)
if target_name == 'SYSTEM'.freeze and packet_name == 'META'.freeze
tlm_packet = System.telemetry.packet('SYSTEM', 'META')
cmd_packet = System.commands.packet('SYSTEM', 'META')
cmd_packet.buffer = tlm_packet.buffer
end
System.telemetry.packet(target_name, packet_name).check_limits(System.limits_set, true)
nil
end
# Set a telemetry item in a packet to a particular value and then verifies
# the value is within the acceptable limits. No conversions are applied.
#
# Note: If this is done while COSMOS is currently receiving telemetry,
# this value could get overwritten at any time. Thus this capability is
# best used for testing or for telemetry items that are not received
# regularly through the target interface.
#
# Accepts two different calling styles:
# set_tlm_raw("TGT PKT ITEM = 1.0")
# set_tlm_raw('TGT','PKT','ITEM', 10.0)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args The args must either be a string followed by a value or
# three strings followed by a value (see the calling style in the
# description).
def set_tlm_raw(*args)
target_name, packet_name, item_name, value = set_tlm_process_args(args, 'set_tlm_raw')
System.telemetry.set_value(target_name, packet_name, item_name, value, :RAW)
System.telemetry.packet(target_name, packet_name).check_limits(System.limits_set, true)
nil
end
# Injects a packet into the system as if it was received from an interface
#
# @param target_name[String] Target name of the packet
# @param packet_name[String] Packet name of the packet
# @param item_hash[Hash] Hash of item_name and value for each item you want to change from the current value table
# @param value_type[Symbol/String] Type of the values in the item_hash (RAW or CONVERTED)
# @param send_routers[Boolean] Whether or not to send to routers for the target's interface
# @param send_packet_log_writers[Boolean] Whether or not to send to the packet log writers for the target's interface
# @param create_new_logs[Boolean] Whether or not to create new log files before writing this packet to logs
def inject_tlm(target_name, packet_name, item_hash = nil, value_type = :CONVERTED, send_routers = true, send_packet_log_writers = true, create_new_logs = false)
received_time = Time.now
target = System.targets[target_name.upcase]
raise "Unknown target: #{target_name}" unless target
# Find and clone the telemetry packet
cvt_packet = System.telemetry.packet(target_name, packet_name)
packet = cvt_packet.clone
packet.received_time = received_time
if item_hash
# Update the packet with item_hash
value_type = value_type.to_s.intern
item_hash.each do |item_name, item_value|
packet.write(item_name.to_s, item_value, value_type)
end
end
# Update current value table
cvt_packet.synchronize do
cvt_packet.buffer = packet.buffer(false)
cvt_packet.received_time = received_time
# The interface does the following line, but I don't think inject_tlm should because it could confuse the interface
target.tlm_cnt += 1
packet.received_count += 1
cvt_packet.received_count += 1
CmdTlmServer.instance.identified_packet_callback(cvt_packet)
end
# Find the interface for this target
interface = target.interface
if interface
# Write to routers
if send_routers
interface.routers.each do |router|
begin
router.write(packet) if router.write_allowed? and router.connected?
rescue => err
Logger.error "Problem writing to router #{router.name} - #{err.class}:#{err.message}"
end
end
end
# Write to packet log writers
if create_new_logs or send_packet_log_writers
interface.packet_log_writer_pairs.each do |packet_log_writer_pair|
# Optionally create new log files
packet_log_writer_pair.tlm_log_writer.start if create_new_logs
# Optionally write to packet logs - Write errors are handled by the log writer
packet_log_writer_pair.tlm_log_writer.write(packet) if send_packet_log_writers
end
end
else
# Some packets don't have an interface - Can still write to standard routers and packet logs
# Write to routers
if send_routers
router = CmdTlmServer.instance.routers.all['PREIDENTIFIED_ROUTER']
if router
begin
router.write(packet) if router.write_allowed? and router.connected?
rescue => err
Logger.error "Problem writing to router #{router.name} - #{err.class}:#{err.message}"
end
end
end
if create_new_logs or send_packet_log_writers
# Handle packet logging
packet_log_writer_pair = CmdTlmServer.instance.packet_logging.all['DEFAULT']
if packet_log_writer_pair
# Optionally create new logs
packet_log_writer_pair.tlm_log_writer.start if create_new_logs
# Optionally write to packet logs - Write errors are handled by the log writer
packet_log_writer_pair.tlm_log_writer.write(packet) if send_packet_log_writers
end
end
end
nil
end
# Override a telemetry item in a packet to a particular value such that it
# is always returned even when new telemetry packets are received from the
# target.
#
# Accepts two different calling styles:
# override_tlm("TGT PKT ITEM = 1.0")
# override_tlm('TGT','PKT','ITEM', 10.0)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args The args must either be a string followed by a value or
# three strings followed by a value (see the calling style in the
# description).
def override_tlm(*args)
_override(__method__, set_tlm_process_args(args, __method__))
end
# Override a telemetry item in a packet to a particular value such that it
# is always returned even when new telemetry packets are received from the
# target. This only accepts RAW data items and any conversions are applied
# to the raw data when the packet is read.
#
# Accepts two different calling styles:
# override_tlm_raw("TGT PKT ITEM = 1.0")
# override_tlm_raw('TGT','PKT','ITEM', 10.0)
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args The args must either be a string followed by a value or
# three strings followed by a value (see the calling style in the
# description).
def override_tlm_raw(*args)
_override(__method__, set_tlm_process_args(args, __method__))
end
# Normalize a telemetry item in a packet to its default behavior. Called
# after override_tlm and override_tlm_raw to restore standard processing.
#
# Accepts two different calling styles:
# normalize_tlm("TGT PKT ITEM")
# normalize_tlm('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args The args must either be a string or three strings
# (see the calling style in the description).
def normalize_tlm(*args)
_override(__method__, tlm_process_args(args, __method__))
end
private
def _override(method, tgt_pkt_item)
target = System.targets[tgt_pkt_item[0]]
raise "Target '#{tgt_pkt_item[0]}' does not exist" unless target
interface = System.targets[tgt_pkt_item[0]].interface
raise "Target '#{tgt_pkt_item[0]}' has no interface" unless interface
found = false
if interface.read_protocols
interface.read_protocols.each do |protocol|
found = true if protocol.kind_of? OverrideProtocol
end
end
if found
# Test to see if this telemetry item exists
System.telemetry.value(tgt_pkt_item[0], tgt_pkt_item[1], tgt_pkt_item[2], :RAW)
interface.public_send("_#{method}", *tgt_pkt_item)
else
raise "Interface #{interface.name} does not have override ability. Is 'PROTOCOL READ_WRITE OverrideProtocol' under the interface definition?"
end
nil
end
public
# Returns the raw buffer for a telemetry packet.
#
# @param target_name [String] Name of the target
# @param packet_name [String] Name of the packet
# @return [String] last telemetry packet buffer
def get_tlm_buffer(target_name, packet_name)
packet = System.telemetry.packet(target_name, packet_name)
return packet.buffer
end
# Returns all the values (along with their limits state) for a packet.
#
# @param target_name [String] Name of the target
# @param packet_name [String] Name of the packet
# @param value_type [Symbol] How the values should be converted. Must be
# one of {Packet::VALUE_TYPES}
# @return (see Cosmos::Packet#read_all_with_limits_states)
def get_tlm_packet(target_name, packet_name, value_type = :CONVERTED)
packet = System.telemetry.packet(target_name, packet_name)
return packet.read_all_with_limits_states(value_type.to_s.intern)
end
# Returns all the item values (along with their limits state). The items
# can be from any target and packet and thus must be fully qualified with
# their target and packet names.
#
# @param (see Cosmos::Telemetry#values_and_limits_states)
# @return [Array< Array<Object>, Array<Symbol>, Array<Array<Numeric>>, String>]
# Array consisting of an Array of item values, an Array of item limits state
# given as symbols such as :RED, :YELLOW, :STALE, an Array of Arrays including
# the limits setting such as red low, yellow low, yellow high, red high and
# optionally green low and high, and the overall limits state which is
# one of {Cosmos::Limits::LIMITS_STATES}.
def get_tlm_values(item_array, value_types = :CONVERTED)
if !item_array.is_a?(Array) || (!item_array[0].is_a?(Array) and !item_array.empty?)
raise ArgumentError, "item_array must be nested array: [['TGT','PKT','ITEM'],...]"
end
return [[], [], [], System.limits_set] if item_array.empty?
if value_types.is_a?(Array)
elem = value_types[0]
else
elem = value_types
end
# Due to JSON round tripping from scripts, value_types can be a String
# so we must check for both Symbol and String
if !elem.is_a?(Symbol) && !elem.is_a?(String)
raise ArgumentError, "value_types must be a single symbol or array of symbols specifying the conversion method (:RAW, :CONVERTED, :FORMATTED, :WITH_UNITS)"
end
array = System.telemetry.values_and_limits_states(item_array, value_types)
array << System.limits_set
return array
end
# Returns the sorted packet names and their descriptions for a particular
# target.
#
# @param target_name (see #get_tlm_packet)
# @return [Array<String, String>] Array of \[packet name, packet
# description] sorted by packet name
def get_tlm_list(target_name)
list = []
packets = System.telemetry.packets(target_name)
packets.each do |packet_name, packet|
list << [packet_name, packet.description]
end
list.sort
end
# Returns the item names and their states and descriptions for a particular
# packet.
#
# @param target_name (see #get_tlm_packet)
# @param packet_name (see #get_tlm_packet)
# @return [Array<String, Hash, String>] Array of \[item name, item states,
# item description]
def get_tlm_item_list(target_name, packet_name)
list = []
System.telemetry.items(target_name, packet_name).each do |item|
list << [item.name, item.states, item.description]
end
list
end
# Returns an array of Hashes with all the attributes of the item.
#
# @param (see Cosmos::Telemetry#values_and_limits_states)
# @return [Array<Hash>] Array of hashes describing the items. All the
# attributes in {Cosmos::PacketItem} and {Cosmos::StructItem} are
# present in the Hash.
def get_tlm_details(item_array)
if !item_array.is_a?(Array) || !item_array[0].is_a?(Array)
raise ArgumentError, "item_array must be nested array: [['TGT','PKT','ITEM'],...]"
end
details = []
item_array.each do |target_name, packet_name, item_name|
_, item = System.telemetry.packet_and_item(target_name, packet_name, item_name)
details << item.to_hash
end
details
end
# (see Cosmos::Limits#out_of_limits)
def get_out_of_limits
return System.limits.out_of_limits
end
# (see Cosmos::Limits#overall_limits_state)
def get_overall_limits_state(ignored_items = nil)
return System.limits.overall_limits_state(ignored_items)
end
# Whether the limits are enabled for the given item
#
# Accepts two different calling styles:
# limits_enabled?("TGT PKT ITEM")
# limits_enabled?('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args [String|Array<String>] See the description for calling style
# @return [Boolean] Whether limits are enable for the itme
def limits_enabled?(*args)
target_name, packet_name, item_name = tlm_process_args(args, 'limits_enabled?')
return System.limits.enabled?(target_name, packet_name, item_name)
end
# Enable limits checking for a telemetry item
#
# Accepts two different calling styles:
# enable_limits("TGT PKT ITEM")
# enable_limits('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args [String|Array<String>] See the description for calling style
def enable_limits(*args)
target_name, packet_name, item_name = tlm_process_args(args, 'enable_limits')
System.limits.enable(target_name, packet_name, item_name)
nil
end
# Disable limit checking for a telemetry item
#
# Accepts two different calling styles:
# disable_limits("TGT PKT ITEM")
# disable_limits('TGT','PKT','ITEM')
#
# Favor the first syntax where possible as it is more succinct.
#
# @param args [String|Array<String>] See the description for calling style
def disable_limits(*args)
target_name, packet_name, item_name = tlm_process_args(args, 'disable_limits')
System.limits.disable(target_name, packet_name, item_name)
nil
end
# Get the list of stale packets for a specific target or pass nil to list
# all stale packets
#
# @param with_limits_only [Boolean] Return only the stale packets
# that have limits items and thus affect the overall limits
# state of the system
# @param target [String] The target to find stale packets for or nil to list
# all stale packets in the system
# @return [Array<Array<String, String>>] Array of arrays listing the target
# name and packet name
def get_stale(with_limits_only = false, target = nil)
stale = []
System.telemetry.stale(with_limits_only, target).each do |packet|
stale << [packet.target_name, packet.packet_name]
end
stale
end
# (see Cosmos::Limits#get)
def get_limits(target_name, packet_name, item_name, limits_set = nil)
System.limits.get(target_name, packet_name, item_name, limits_set)
end
# (see Cosmos::Limits#set)
def set_limits(target_name, packet_name, item_name, red_low, yellow_low, yellow_high, red_high, green_low = nil, green_high = nil, limits_set = :CUSTOM, persistence = nil, enabled = true)
result = System.limits.set(target_name, packet_name, item_name, red_low, yellow_low, yellow_high, red_high, green_low, green_high, limits_set, persistence, enabled)
if result[0] != nil
limits_settings = [target_name, packet_name, item_name].concat(result)
CmdTlmServer.instance.post_limits_event(:LIMITS_SETTINGS, limits_settings)
Logger.info("Limits Settings Changed: #{limits_settings}")
end
result
end
# (see Cosmos::Limits#groups)
def get_limits_groups
return System.limits.groups.keys
end
# (see Cosmos::Limits#enable_group)
def enable_limits_group(group_name)
Logger.info("Enabling Limits Group: #{group_name.upcase}")
System.limits.enable_group(group_name)
nil
end
# (see Cosmos::Limits#disable_group)
def disable_limits_group(group_name)
Logger.info("Disabling Limits Group: #{group_name.upcase}")
System.limits.disable_group(group_name)
nil
end
# Returns all defined limits sets
#
# @return [Array<Symbol>] All defined limits sets
def get_limits_sets
return System.limits.sets
end
# Changes the active limits set that applies to all telemetry
#
# @param limits_set [String] The name of the limits set
def set_limits_set(limits_set)
System.limits_set = limits_set if System.limits_set != limits_set.to_s.upcase.intern
end
# Returns the active limits set that applies to all telemetry
#
# @return [String] The current limits set
def get_limits_set
return System.limits_set.to_s
end
#
# General Purpose Methods
#
# Returns the list of all target names
#
# @return [Array<String>] All target names
def get_target_list
list = []
System.targets.each_key {|target_name| list << target_name }
return list.sort
end
# @see CmdTlmServer.subscribe_limits_events
def subscribe_limits_events(queue_size = CmdTlmServer::DEFAULT_LIMITS_EVENT_QUEUE_SIZE)
CmdTlmServer.subscribe_limits_events(queue_size)
end
# @see CmdTlmServer.unsubscribe_limits_events
def unsubscribe_limits_events(id)
CmdTlmServer.unsubscribe_limits_events(id)
end
# @see CmdTlmServer.get_limits_event
def get_limits_event(id, non_block = false)
CmdTlmServer.get_limits_event(id, non_block)
end
# @see CmdTlmServer.subscribe_packet_data
def subscribe_packet_data(packets,
queue_size = CmdTlmServer::DEFAULT_PACKET_DATA_QUEUE_SIZE)
CmdTlmServer.subscribe_packet_data(packets, queue_size)
end