diff --git a/openc3/lib/openc3/conversions/bit_reverse_conversion.rb b/openc3/lib/openc3/conversions/bit_reverse_conversion.rb index ef9abc341..ff1942047 100644 --- a/openc3/lib/openc3/conversions/bit_reverse_conversion.rb +++ b/openc3/lib/openc3/conversions/bit_reverse_conversion.rb @@ -27,6 +27,7 @@ def initialize(converted_type, converted_bit_size) if @converted_type == :FLOAT raise "Float Bit Reverse Not Yet Supported" end + @params = [@converted_type, @converted_bit_size] end # Perform the conversion on the value. diff --git a/openc3/lib/openc3/conversions/conversion.rb b/openc3/lib/openc3/conversions/conversion.rb index 243079997..2f5a12972 100644 --- a/openc3/lib/openc3/conversions/conversion.rb +++ b/openc3/lib/openc3/conversions/conversion.rb @@ -30,12 +30,15 @@ class Conversion attr_reader :converted_bit_size # @return [Integer] The size in bits of the converted array value attr_reader :converted_array_size + # @return [Array] The arguments passed to the conversion + attr_reader :params # Create a new conversion def initialize @converted_type = nil @converted_bit_size = nil @converted_array_size = nil + @params = nil end # Perform the conversion on the value. @@ -61,12 +64,13 @@ def to_config(read_or_write) " #{read_or_write}_CONVERSION #{self.class.name.class_name_to_filename}\n" end - def as_json(*_a) + def as_json(*a) result = {} result['class'] = self.class.name.to_s result['converted_type'] = @converted_type if @converted_type result['converted_bit_size'] = @converted_bit_size if @converted_bit_size result['converted_array_size'] = @converted_array_size if @converted_array_size + result['params'] = @params.as_json(*a) if @params result end end diff --git a/openc3/lib/openc3/conversions/generic_conversion.rb b/openc3/lib/openc3/conversions/generic_conversion.rb index 4f0f02d27..66ebd6202 100644 --- a/openc3/lib/openc3/conversions/generic_conversion.rb +++ b/openc3/lib/openc3/conversions/generic_conversion.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'openc3/conversions/conversion' @@ -49,6 +49,7 @@ def initialize(code_to_eval, converted_type = nil, converted_bit_size = nil, con end @converted_bit_size = Integer(converted_bit_size) if ConfigParser.handle_nil(converted_bit_size) @converted_array_size = Integer(converted_array_size) if ConfigParser.handle_nil(converted_array_size) + @params = [@code_to_eval, @converted_type, @converted_bit_size, @converted_array_size] end # (see OpenC3::Conversion#call) @@ -76,11 +77,5 @@ def to_config(read_or_write) config << " GENERIC_#{read_or_write}_CONVERSION_END\n" config end - - def as_json(*a) - result = super(*a) - result['params'] = [@code_to_eval, @converted_type, @converted_bit_size, @converted_array_size] - result - end end end diff --git a/openc3/lib/openc3/conversions/object_read_conversion.rb b/openc3/lib/openc3/conversions/object_read_conversion.rb index 14e126727..ca0fa3ac9 100644 --- a/openc3/lib/openc3/conversions/object_read_conversion.rb +++ b/openc3/lib/openc3/conversions/object_read_conversion.rb @@ -35,6 +35,7 @@ def initialize(cmd_or_tlm, target_name, packet_name) @packet_name = packet_name.to_s.upcase @converted_type = :OBJECT @converted_bit_size = 0 + @params = [@cmd_or_tlm, @target_name, @packet_name] end def lookup_packet @@ -76,13 +77,5 @@ def to_s def to_config(read_or_write) " #{read_or_write}_CONVERSION #{self.class.name.class_name_to_filename} #{@cmd_or_tlm ? @cmd_or_tlm : "nil"} #{@target_name} #{@packet_name}\n" end - - def as_json(*a) - result = super(*a) - result['cmd_or_tlm'] = @cmd_or_tlm - result['target_name'] = @target_name - result['packet_name'] = @packet_name - return result - end end end diff --git a/openc3/lib/openc3/conversions/polynomial_conversion.rb b/openc3/lib/openc3/conversions/polynomial_conversion.rb index 13b40d2bb..e91b0a31e 100644 --- a/openc3/lib/openc3/conversions/polynomial_conversion.rb +++ b/openc3/lib/openc3/conversions/polynomial_conversion.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'openc3/conversions/conversion' @@ -38,6 +38,7 @@ def initialize(*coeffs) @coeffs = coeffs.map { |coeff| coeff.to_f } @converted_type = :FLOAT @converted_bit_size = 64 + @params = @coeffs end if RUBY_ENGINE != 'ruby' or ENV['OPENC3_NO_EXT'] @@ -80,11 +81,5 @@ def to_s def to_config(read_or_write) " POLY_#{read_or_write}_CONVERSION #{@coeffs.join(' ')}\n" end - - def as_json(*a) - result = super(*a) - result['params'] = @coeffs - result - end end end diff --git a/openc3/lib/openc3/conversions/processor_conversion.rb b/openc3/lib/openc3/conversions/processor_conversion.rb index 8dd6c21f5..19831f617 100644 --- a/openc3/lib/openc3/conversions/processor_conversion.rb +++ b/openc3/lib/openc3/conversions/processor_conversion.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'openc3/conversions/conversion' @@ -34,12 +34,20 @@ def initialize(processor_name, result_name, converted_type = nil, converted_bit_ super() @processor_name = processor_name.to_s.upcase @result_name = result_name.to_s.upcase.intern + @params = [@processor_name, @result_name] if ConfigParser.handle_nil(converted_type) @converted_type = converted_type.to_s.upcase.intern raise ArgumentError, "Unknown converted type: #{converted_type}" if !BinaryAccessor::DATA_TYPES.include?(@converted_type) + @params << @converted_type + end + if ConfigParser.handle_nil(converted_bit_size) + @converted_bit_size = Integer(converted_bit_size) + @params << @converted_bit_size + end + if ConfigParser.handle_nil(converted_array_size) + @converted_array_size = Integer(converted_array_size) + @params << @converted_array_size end - @converted_bit_size = Integer(converted_bit_size) if ConfigParser.handle_nil(converted_bit_size) - @converted_array_size = Integer(converted_array_size) if ConfigParser.handle_nil(converted_array_size) end # @param (see Conversion#call) @@ -63,11 +71,5 @@ def to_config(read_or_write) config << "\n" config end - - def as_json(*a) - result = super(*a) - result['params'] = [@processor_name, @result_name, @converted_type, @converted_bit_size, @converted_array_size] - result - end - end # class ProcessorConversion + end end diff --git a/openc3/lib/openc3/conversions/segmented_polynomial_conversion.rb b/openc3/lib/openc3/conversions/segmented_polynomial_conversion.rb index 8575d17ea..95d16057e 100644 --- a/openc3/lib/openc3/conversions/segmented_polynomial_conversion.rb +++ b/openc3/lib/openc3/conversions/segmented_polynomial_conversion.rb @@ -14,7 +14,7 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # # This file may also be used under the terms of a commercial license @@ -93,6 +93,7 @@ def calculate(value) # and the other entry is an array of the coefficients for that segment. def initialize(segments = []) super() + @params = [] @segments = [] segments.each { |lower_bound, coeffs| add_segment(lower_bound, *coeffs) } @converted_type = :FLOAT @@ -107,6 +108,7 @@ def initialize(segments = []) # given coefficients. # @param coeffs [Array] The polynomial coefficients def add_segment(lower_bound, *coeffs) + @params << [lower_bound, coeffs] @segments << Segment.new(lower_bound, coeffs) @segments.sort! end @@ -160,15 +162,5 @@ def to_config(read_or_write) end config end - - def as_json(*a) - params = [] - @segments.each do |segment| - params << [segment.lower_bound, segment.coeffs] - end - result = super(*a) - result['params'] = [params] - result - end end end diff --git a/openc3/lib/openc3/conversions/unix_time_conversion.rb b/openc3/lib/openc3/conversions/unix_time_conversion.rb index a772f7e3e..310732085 100644 --- a/openc3/lib/openc3/conversions/unix_time_conversion.rb +++ b/openc3/lib/openc3/conversions/unix_time_conversion.rb @@ -39,6 +39,9 @@ def initialize(seconds_item_name, microseconds_item_name = nil, seconds_type = ' @converted_bit_size = 0 @seconds_type = seconds_type.to_sym @microseconds_type = microseconds_type.to_sym + @params = [@seconds_item_name, @microseconds_item_name] + @params << @seconds_type if @seconds_type != :RAW + @params << @microseconds_type if @microseconds_type != :RAW end # @param (see Conversion#call) @@ -65,11 +68,5 @@ def to_s def to_config(read_or_write) " #{read_or_write}_CONVERSION #{self.class.name.class_name_to_filename} #{@seconds_item_name} #{@microseconds_item_name}\n" end - - def as_json(*a) - result = super(*a) - result['params'] = [@seconds_item_name, @microseconds_item_name, @seconds_type, @microseconds_type] - result - end - end # class UnixTimeConversion + end end diff --git a/openc3/lib/openc3/conversions/unix_time_formatted_conversion.rb b/openc3/lib/openc3/conversions/unix_time_formatted_conversion.rb index 8255e3521..58d222f05 100644 --- a/openc3/lib/openc3/conversions/unix_time_formatted_conversion.rb +++ b/openc3/lib/openc3/conversions/unix_time_formatted_conversion.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'openc3/conversions/unix_time_conversion' @@ -33,6 +33,7 @@ class UnixTimeFormattedConversion < UnixTimeConversion # @param microseconds_item_name [String] The telemetry item in the packet # which represents microseconds def initialize(seconds_item_name, microseconds_item_name = nil) + # @params is set by the parent class in super() super(seconds_item_name, microseconds_item_name) @converted_type = :STRING @converted_bit_size = 0 @@ -48,5 +49,5 @@ def call(value, packet, buffer) def to_s super << ".formatted" end - end # class UnixTimeFormattedConversion + end end diff --git a/openc3/lib/openc3/conversions/unix_time_seconds_conversion.rb b/openc3/lib/openc3/conversions/unix_time_seconds_conversion.rb index d4361dcce..f26c8f68d 100644 --- a/openc3/lib/openc3/conversions/unix_time_seconds_conversion.rb +++ b/openc3/lib/openc3/conversions/unix_time_seconds_conversion.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'openc3/conversions/unix_time_conversion' @@ -33,6 +33,7 @@ class UnixTimeSecondsConversion < UnixTimeConversion # @param microseconds_item_name [String] The telemetry item in the packet # which represents microseconds def initialize(seconds_item_name, microseconds_item_name = nil) + # @params is set by the parent class in super() super(seconds_item_name, microseconds_item_name) @converted_type = :FLOAT @converted_bit_size = 64 @@ -48,5 +49,5 @@ def call(value, packet, buffer) def to_s super << ".to_f" end - end # class UnixTimeSecondsConversion + end end diff --git a/openc3/python/openc3/conversions/bit_reverse_conversion.py b/openc3/python/openc3/conversions/bit_reverse_conversion.py index 00cd34f7e..ac944c6b1 100644 --- a/openc3/python/openc3/conversions/bit_reverse_conversion.py +++ b/openc3/python/openc3/conversions/bit_reverse_conversion.py @@ -24,6 +24,7 @@ def __init__(self, converted_type, converted_bit_size): self.converted_bit_size = int(converted_bit_size) if self.converted_type == "FLOAT": raise RuntimeError("Float Bit Reverse Not Yet Supported") + self.params = [self.converted_type, self.converted_bit_size] # Perform the conversion on the value. # diff --git a/openc3/python/openc3/conversions/conversion.py b/openc3/python/openc3/conversions/conversion.py index 63a3b2cba..6f2fb9023 100644 --- a/openc3/python/openc3/conversions/conversion.py +++ b/openc3/python/openc3/conversions/conversion.py @@ -19,18 +19,20 @@ class Conversion: """Performs a general conversion via the implementation of the call method""" # self.return [Symbol] The converted data type. Must be one of - # {OpenC3:'S'tructureItem#data_type} + # {OpenC3::StructureItem#data_type} # attr_reader :converted_type # # self.return [Integer] The size in bits of the converted value # attr_reader :converted_bit_size # # self.return [Integer] The size in bits of the converted array value # attr_reader :converted_array_size + # attr_reader :params # Create a new conversion def __init__(self): self.converted_type = None self.converted_bit_size = None self.converted_array_size = None + self.params = None # Perform the conversion on the value. # @@ -61,4 +63,6 @@ def as_json(self): result["converted_bit_size"] = self.converted_bit_size if self.converted_array_size is not None: result["converted_array_size"] = self.converted_array_size + if self.params is not None: + result["params"] = self.params return result diff --git a/openc3/python/openc3/conversions/generic_conversion.py b/openc3/python/openc3/conversions/generic_conversion.py index 2a578827c..c40573f8c 100644 --- a/openc3/python/openc3/conversions/generic_conversion.py +++ b/openc3/python/openc3/conversions/generic_conversion.py @@ -48,6 +48,7 @@ def __init__( self.converted_bit_size = int(converted_bit_size) if ConfigParser.handle_none(converted_array_size): self.converted_array_size = int(converted_array_size) + self.params = [code_to_eval, converted_type, converted_bit_size, converted_array_size] def call(self, value, packet, buffer): myself = packet # For backwards compatibility @@ -72,13 +73,3 @@ def to_config(self, read_or_write): config << self.code_to_eval config += f" GENERIC_{read_or_write}_CONVERSION_END\n" return config - - def as_json(self): - result = super().as_json() - result["params"] = [ - self.code_to_eval, - self.converted_type, - self.converted_bit_size, - self.converted_array_size, - ] - return result diff --git a/openc3/python/openc3/conversions/object_read_conversion.py b/openc3/python/openc3/conversions/object_read_conversion.py index 4c92b0478..dcc836b12 100644 --- a/openc3/python/openc3/conversions/object_read_conversion.py +++ b/openc3/python/openc3/conversions/object_read_conversion.py @@ -34,6 +34,7 @@ def __init__(self, cmd_or_tlm, target_name, packet_name): self.packet_name = str(packet_name).upper() self.converted_type = "OBJECT" self.converted_bit_size = 0 + self.params = [self.cmd_or_tlm, self.target_name, self.packet_name] def lookup_packet(self): if self.cmd_or_tlm: @@ -62,10 +63,3 @@ def __str__(self): # @return [String] Config fragment for this conversion def to_config(self, read_or_write): return f"{read_or_write}_CONVERSION openc3/conversions/object_read_conversion.py {self.cmd_or_tlm if self.cmd_or_tlm else 'None'} {self.target_name} {self.packet_name}\n" - - def as_json(self, *a): - result = super().as_json(*a) - result["cmd_or_tlm"] = self.cmd_or_tlm - result["target_name"] = self.target_name - result["packet_name"] = self.packet_name - return result diff --git a/openc3/python/openc3/conversions/polynomial_conversion.py b/openc3/python/openc3/conversions/polynomial_conversion.py index 60f7e106a..59297881c 100644 --- a/openc3/python/openc3/conversions/polynomial_conversion.py +++ b/openc3/python/openc3/conversions/polynomial_conversion.py @@ -1,4 +1,4 @@ -# Copyright 2023 OpenC3, Inc. +# Copyright 2024 OpenC3, Inc. # All Rights Reserved. # # This program is free software; you can modify and/or redistribute it @@ -29,6 +29,7 @@ def __init__(self, *coeffs): self.coeffs = [float(coeff) for coeff in coeffs] self.converted_type = "FLOAT" self.converted_bit_size = 64 + self.params = coeffs # @param (see Conversion#call) # @return [Float] The value with the polynomial applied @@ -61,8 +62,3 @@ def __str__(self): # @return [String] Config fragment for this conversion def to_config(self, read_or_write): return f" POLY_{read_or_write}_CONVERSION {' '.join(self.coeffs)}\n" - - def as_json(self): - result = super().as_json() - result["params"] = self.coeffs - return result diff --git a/openc3/python/openc3/conversions/processor_conversion.py b/openc3/python/openc3/conversions/processor_conversion.py index 4ec40d7ca..0e9f59ff5 100644 --- a/openc3/python/openc3/conversions/processor_conversion.py +++ b/openc3/python/openc3/conversions/processor_conversion.py @@ -37,14 +37,18 @@ def __init__( super().__init__() self.processor_name = str(processor_name).upper() self.result_name = str(result_name).upper() + self.params = [self.processor_name, self.result_name] if ConfigParser.handle_none(converted_type): self.converted_type = str(converted_type).upper() if self.converted_type not in BinaryAccessor.DATA_TYPES: raise AttributeError(f"Unknown converted type: {converted_type}") + self.params.append(self.converted_type) if ConfigParser.handle_none(converted_bit_size): self.converted_bit_size = int(converted_bit_size) + self.params.append(self.converted_bit_size) if ConfigParser.handle_none(converted_array_size): self.converted_array_size = int(converted_array_size) + self.params.append(self.converted_array_size) # @param (see Conversion#call) # @return [Varies] The result of the associated processor @@ -71,14 +75,3 @@ def to_config(self, read_or_write): config += f" {self.converted_array_size}" config += "\n" return config - - def as_json(self): - result = super().as_json() - result["params"] = [ - self.processor_name, - self.result_name, - self.converted_type, - self.converted_bit_size, - self.converted_array_size, - ] - return result diff --git a/openc3/python/openc3/conversions/segmented_polynomial_conversion.py b/openc3/python/openc3/conversions/segmented_polynomial_conversion.py index faed777a3..96e45f08c 100644 --- a/openc3/python/openc3/conversions/segmented_polynomial_conversion.py +++ b/openc3/python/openc3/conversions/segmented_polynomial_conversion.py @@ -71,6 +71,7 @@ def calculate(self, value): def __init__(self, segments=[]): super().__init__() self.segments = [] + self.params = [] for lower_bound, coeffs in segments: self.add_segment(lower_bound, *coeffs) self.converted_type = "FLOAT" @@ -84,6 +85,7 @@ def __init__(self, segments=[]): # given coefficients. # @param coeffs [Array] The polynomial coefficients def add_segment(self, lower_bound, *coeffs): + self.params.append([lower_bound, coeffs]) self.segments.append(SegmentedPolynomialConversion.Segment(lower_bound, coeffs)) self.segments.sort() @@ -128,11 +130,3 @@ def to_config(self, read_or_write): for segment in self.segments: config += f" SEG_POLY_{read_or_write}_CONVERSION {segment.lower_bound} {' '.join(segment.coeffs)}\n" return config - - def as_json(self): - params = [] - for segment in self.segments: - params.append([segment.lower_bound, segment.coeffs]) - result = super().as_json() - result["params"] = [params] - return result diff --git a/openc3/python/openc3/conversions/unix_time_conversion.py b/openc3/python/openc3/conversions/unix_time_conversion.py index aab6e5418..fcdb242c2 100644 --- a/openc3/python/openc3/conversions/unix_time_conversion.py +++ b/openc3/python/openc3/conversions/unix_time_conversion.py @@ -33,6 +33,7 @@ def __init__(self, seconds_item_name, microseconds_item_name=None): self.microseconds_item_name = microseconds_item_name self.converted_type = "RUBY_TIME" self.converted_bit_size = 0 + self.params = [seconds_item_name, microseconds_item_name] # @param (see Conversion#call) # @return [Float] Packet time in seconds since UNIX epoch @@ -50,8 +51,3 @@ def __str__(self): # @return [String] Config fragment for this conversion def to_config(self, read_or_write): return f" {read_or_write}_CONVERSION {self.__class__.__name__} {self.seconds_item_name} {self.microseconds_item_name}\n" - - def as_json(self): - result = super().as_json() - result["params"] = [self.seconds_item_name, self.microseconds_item_name] - return result diff --git a/openc3/python/openc3/conversions/unix_time_formatted_conversion.py b/openc3/python/openc3/conversions/unix_time_formatted_conversion.py index 906cd88e2..a854ddf68 100644 --- a/openc3/python/openc3/conversions/unix_time_formatted_conversion.py +++ b/openc3/python/openc3/conversions/unix_time_formatted_conversion.py @@ -1,4 +1,4 @@ -# Copyright 2023 OpenC3, Inc. +# Copyright 2024 OpenC3, Inc. # All Rights Reserved. # # This program is free software; you can modify and/or redistribute it @@ -29,6 +29,7 @@ class UnixTimeFormattedConversion(UnixTimeConversion): # @param microseconds_item_name [String] The telemetry item in the packet # which represents microseconds def __init__(self, seconds_item_name, microseconds_item_name=None): + # self.params is set by the parent class in super() super().__init__(seconds_item_name, microseconds_item_name) self.converted_type = "STRING" self.converted_bit_size = 0 diff --git a/openc3/python/openc3/conversions/unix_time_seconds_conversion.py b/openc3/python/openc3/conversions/unix_time_seconds_conversion.py index 34d6c7fec..1d61a050d 100644 --- a/openc3/python/openc3/conversions/unix_time_seconds_conversion.py +++ b/openc3/python/openc3/conversions/unix_time_seconds_conversion.py @@ -1,4 +1,4 @@ -# Copyright 2023 OpenC3, Inc. +# Copyright 2024 OpenC3, Inc. # All Rights Reserved. # # This program is free software; you can modify and/or redistribute it @@ -28,6 +28,7 @@ class UnixTimeSecondsConversion(UnixTimeConversion): # @param microseconds_item_name [String] The telemetry item in the packet # which represents microseconds def __init__(self, seconds_item_name, microseconds_item_name=None): + # self.params is set by the parent class in super() super().__init__(seconds_item_name, microseconds_item_name) self.converted_type = "FLOAT" self.converted_bit_size = 64 diff --git a/openc3/python/test/conversions/test_bit_reverse_conversion.py b/openc3/python/test/conversions/test_bit_reverse_conversion.py index 5845af7f5..e848318ab 100644 --- a/openc3/python/test/conversions/test_bit_reverse_conversion.py +++ b/openc3/python/test/conversions/test_bit_reverse_conversion.py @@ -51,7 +51,7 @@ def test_creates_a_reproducable_format(self): self.assertEqual(json["class"], "BitReverseConversion") self.assertEqual(json["converted_type"], "UINT") self.assertEqual(json["converted_bit_size"], 8) - new_brc = BitReverseConversion(json["converted_type"], json["converted_bit_size"]) + new_brc = BitReverseConversion(*json["params"]) self.assertEqual(brc.converted_type, (new_brc.converted_type)) self.assertEqual(brc.converted_bit_size, (new_brc.converted_bit_size)) self.assertEqual(brc.call(0x11, None, None), 0x88) diff --git a/openc3/python/test/conversions/test_object_read_conversion.py b/openc3/python/test/conversions/test_object_read_conversion.py index e10ef3517..dea3610b5 100644 --- a/openc3/python/test/conversions/test_object_read_conversion.py +++ b/openc3/python/test/conversions/test_object_read_conversion.py @@ -75,6 +75,12 @@ def test_creates_a_reproducable_format(self): self.assertEqual(json["class"], "ObjectReadConversion") self.assertEqual(json["converted_type"], "OBJECT") self.assertEqual(json["converted_bit_size"], 0) - self.assertEqual(json["cmd_or_tlm"], "TLM") - self.assertEqual(json["target_name"], "INST") - self.assertEqual(json["packet_name"], "PARAMS") + self.assertEqual(json["params"], ["TLM", "INST", "PARAMS"]) + new_orc = ObjectReadConversion(*json["params"]) + self.assertEqual(orc.converted_type, new_orc.converted_type) + self.assertEqual(orc.converted_bit_size, new_orc.converted_bit_size) + pkt = System.telemetry.packet("INST", "PARAMS") + pkt.write("VALUE0", 1) + pkt.write("VALUE2", 1) + pkt.write("VALUE4", 1) + self.assertEqual(orc.call(pkt.buffer, pkt, pkt.buffer), new_orc.call(pkt.buffer, pkt, pkt.buffer)) diff --git a/openc3/python/test/conversions/test_object_write_conversion.py b/openc3/python/test/conversions/test_object_write_conversion.py index dec9f9f55..5569c8eed 100644 --- a/openc3/python/test/conversions/test_object_write_conversion.py +++ b/openc3/python/test/conversions/test_object_write_conversion.py @@ -85,6 +85,4 @@ def test_creates_a_reproducable_format(self): self.assertEqual(json["class"], "ObjectWriteConversion") self.assertEqual(json["converted_type"], "OBJECT") self.assertEqual(json["converted_bit_size"], 0) - self.assertEqual(json["cmd_or_tlm"], "TLM") - self.assertEqual(json["target_name"], "INST") - self.assertEqual(json["packet_name"], "PARAMS") + self.assertEqual(json["params"], ["TLM", "INST", "PARAMS"]) diff --git a/openc3/python/test/conversions/test_processor_conversion.py b/openc3/python/test/conversions/test_processor_conversion.py index d1bc4bce7..0817a8e6b 100644 --- a/openc3/python/test/conversions/test_processor_conversion.py +++ b/openc3/python/test/conversions/test_processor_conversion.py @@ -55,3 +55,19 @@ def test_returns_the_equation(self): str(ProcessorConversion("TEST1", "TEST2", "FLOAT", "64", "128")), "ProcessorConversion TEST1 TEST2", ) + + def test_as_json_creates_a_reproducible_format(self): + pc = ProcessorConversion('TEST1', 'TEST2', 'FLOAT', '64', '128') + json = pc.as_json() + self.assertEqual(json['class'], "ProcessorConversion") + self.assertEqual(json['converted_type'], "FLOAT") + self.assertEqual(json['converted_bit_size'], 64) + self.assertEqual(json['converted_array_size'], 128) + self.assertEqual(json['params'], ['TEST1', 'TEST2', "FLOAT", 64, 128]) + new_pc = ProcessorConversion(*json['params']) + packet = Packet("tgt", "pkt") + packet.append_item('ITEM1', 64, "FLOAT") + proc = Processor() + proc.results = {"TEST2": 6.0} + packet.processors['TEST1'] = proc + self.assertEqual(pc.call(1, packet, None), new_pc.call(1, packet, None)) diff --git a/openc3/python/test/conversions/test_segmented_polynomial_conversion.py b/openc3/python/test/conversions/test_segmented_polynomial_conversion.py index 0a23bc84c..78dea84d4 100644 --- a/openc3/python/test/conversions/test_segmented_polynomial_conversion.py +++ b/openc3/python/test/conversions/test_segmented_polynomial_conversion.py @@ -1,4 +1,4 @@ -# Copyright 2023 OpenC3, Inc. +# Copyright 2024 OpenC3, Inc. # All Rights Reserved. # # This program is free software; you can modify and/or redistribute it @@ -56,7 +56,7 @@ def test_creates_a_reproducable_format(self): spc.add_segment(15, 3, 2) json = spc.as_json() self.assertEqual(json["class"], "SegmentedPolynomialConversion") - new_spc = SegmentedPolynomialConversion(*json["params"]) + new_spc = SegmentedPolynomialConversion(json["params"]) for index, segment in enumerate(spc.segments): self.assertEqual(segment, new_spc.segments[index]) self.assertEqual(spc.converted_type, (new_spc.converted_type)) diff --git a/openc3/python/test/conversions/test_unix_time_formatted_conversion.py b/openc3/python/test/conversions/test_unix_time_formatted_conversion.py index 3bdb7b8be..79b824fe9 100644 --- a/openc3/python/test/conversions/test_unix_time_formatted_conversion.py +++ b/openc3/python/test/conversions/test_unix_time_formatted_conversion.py @@ -26,22 +26,22 @@ class TestUnixTimeFormattedConversion(unittest.TestCase): def test_initializes_converted_type_and_converted_bit_size(self): - gc = UnixTimeFormattedConversion("TIME") - self.assertEqual(gc.converted_type, "STRING") - self.assertEqual(gc.converted_bit_size, 0) + utfc = UnixTimeFormattedConversion("TIME") + self.assertEqual(utfc.converted_type, "STRING") + self.assertEqual(utfc.converted_bit_size, 0) def test_returns_the_formatted_packet_time_based_on_seconds(self): - gc = UnixTimeFormattedConversion("TIME") + utfc = UnixTimeFormattedConversion("TIME") packet = Packet("TGT", "PKT") packet.append_item("TIME", 32, "UINT") time = datetime(2020, 1, 31, 12, 15, 30, tzinfo=timezone.utc).timestamp() packet.write("TIME", time) self.assertEqual( - gc.call(None, packet, packet.buffer), "2020/01/31 12:15:30.000" + utfc.call(None, packet, packet.buffer), "2020/01/31 12:15:30.000" ) def test_returns_the_formatted_packet_time_based_on_seconds_and_microseconds(self): - gc = UnixTimeFormattedConversion("TIME", "TIME_US") + utfc = UnixTimeFormattedConversion("TIME", "TIME_US") packet = Packet("TGT", "PKT") packet.append_item("TIME", 32, "UINT") time = datetime(2020, 1, 31, 12, 15, 30, tzinfo=timezone.utc).timestamp() @@ -49,30 +49,46 @@ def test_returns_the_formatted_packet_time_based_on_seconds_and_microseconds(sel packet.append_item("TIME_US", 32, "UINT") packet.write("TIME_US", 500000) self.assertEqual( - gc.call(None, packet, packet.buffer), "2020/01/31 12:15:30.500" + utfc.call(None, packet, packet.buffer), "2020/01/31 12:15:30.500" ) def test_complains_if_the_seconds_item_doesnt_exist(self): - gc = UnixTimeFormattedConversion("TIME") + utfc = UnixTimeFormattedConversion("TIME") packet = Packet("TGT", "PKT") with self.assertRaisesRegex( AttributeError, "Packet item 'TGT PKT TIME' does not exist" ): - gc.call(None, packet, packet.buffer) + utfc.call(None, packet, packet.buffer) def test_complains_if_the_microseconds_item_doesnt_exist(self): - gc = UnixTimeFormattedConversion("TIME", "TIME_US") + utfc = UnixTimeFormattedConversion("TIME", "TIME_US") packet = Packet("TGT", "PKT") packet.append_item("TIME", 32, "UINT") with self.assertRaisesRegex( AttributeError, "Packet item 'TGT PKT TIME_US' does not exist" ): - gc.call(None, packet, packet.buffer) + utfc.call(None, packet, packet.buffer) def test_returns_the_seconds_conversion(self): - gc = UnixTimeFormattedConversion("TIME") - self.assertEqual(str(gc), "UnixTimeFormattedConversion TIME") + utfc = UnixTimeFormattedConversion("TIME") + self.assertEqual(str(utfc), "UnixTimeFormattedConversion TIME") def test_returns_the_microseconds_conversion(self): - gc = UnixTimeFormattedConversion("TIME", "TIME_US") - self.assertEqual(str(gc), "UnixTimeFormattedConversion TIME TIME_US") + utfc = UnixTimeFormattedConversion("TIME", "TIME_US") + self.assertEqual(str(utfc), "UnixTimeFormattedConversion TIME TIME_US") + + def test_creates_a_reproducable_format(self): + utfc = UnixTimeFormattedConversion("TIME", "TIME_US") + json = utfc.as_json() + self.assertEqual(json["class"], "UnixTimeFormattedConversion") + self.assertEqual(json['converted_type'], "STRING") + self.assertEqual(json['converted_bit_size'], 0) + self.assertEqual(json['params'], ["TIME", "TIME_US"]) + new_utfc = UnixTimeFormattedConversion(*json['params']) + packet = Packet("TGT", "PKT") + packet.append_item("TIME", 32, "UINT") + time = datetime(2020, 1, 31, 12, 15, 30, tzinfo=timezone.utc).timestamp() + packet.write("TIME", time) + packet.append_item("TIME_US", 32, "UINT") + packet.write("TIME_US", 500000) + self.assertEqual(utfc.call(None, packet, packet.buffer), new_utfc.call(None, packet, packet.buffer)) diff --git a/openc3/python/test/conversions/test_unix_time_seconds_conversion.py b/openc3/python/test/conversions/test_unix_time_seconds_conversion.py index 17c004950..7c2203cdb 100644 --- a/openc3/python/test/conversions/test_unix_time_seconds_conversion.py +++ b/openc3/python/test/conversions/test_unix_time_seconds_conversion.py @@ -24,49 +24,65 @@ class TestUnixTimeSecondsConversion(unittest.TestCase): def test_initializes_converted_type_and_converted_bit_size(self): - gc = UnixTimeSecondsConversion("TIME") - self.assertEqual(gc.converted_type, "FLOAT") - self.assertEqual(gc.converted_bit_size, 64) + utsc = UnixTimeSecondsConversion("TIME") + self.assertEqual(utsc.converted_type, "FLOAT") + self.assertEqual(utsc.converted_bit_size, 64) def test_returns_the_formatted_packet_time_based_on_seconds(self): - gc = UnixTimeSecondsConversion("TIME") + utsc = UnixTimeSecondsConversion("TIME") packet = Packet("TGT", "PKT") packet.append_item("TIME", 32, "UINT") time = datetime(2020, 1, 31, 12, 15, 30, tzinfo=timezone.utc).timestamp() packet.write("TIME", time) - self.assertEqual(gc.call(None, packet, packet.buffer), time) + self.assertEqual(utsc.call(None, packet, packet.buffer), time) def test_returns_the_formatted_packet_time_based_on_seconds_and_microseconds(self): - gc = UnixTimeSecondsConversion("TIME", "TIME_US") + utsc = UnixTimeSecondsConversion("TIME", "TIME_US") packet = Packet("TGT", "PKT") packet.append_item("TIME", 32, "UINT") time = datetime(2020, 1, 31, 12, 15, 30, tzinfo=timezone.utc).timestamp() packet.write("TIME", time) packet.append_item("TIME_US", 32, "UINT") packet.write("TIME_US", 500000) - self.assertEqual(gc.call(None, packet, packet.buffer), time + 0.5) + self.assertEqual(utsc.call(None, packet, packet.buffer), time + 0.5) def test_complains_if_the_seconds_item_doesnt_exist(self): - gc = UnixTimeSecondsConversion("TIME") + utsc = UnixTimeSecondsConversion("TIME") packet = Packet("TGT", "PKT") with self.assertRaisesRegex( AttributeError, "Packet item 'TGT PKT TIME' does not exist" ): - gc.call(None, packet, packet.buffer) + utsc.call(None, packet, packet.buffer) def test_complains_if_the_microseconds_item_doesnt_exist(self): - gc = UnixTimeSecondsConversion("TIME", "TIME_US") + utsc = UnixTimeSecondsConversion("TIME", "TIME_US") packet = Packet("TGT", "PKT") packet.append_item("TIME", 32, "UINT") with self.assertRaisesRegex( AttributeError, "Packet item 'TGT PKT TIME_US' does not exist" ): - gc.call(None, packet, packet.buffer) + utsc.call(None, packet, packet.buffer) def test_returns_the_seconds_conversion(self): - gc = UnixTimeSecondsConversion("TIME") - self.assertEqual(str(gc), "UnixTimeSecondsConversion TIME") + utsc = UnixTimeSecondsConversion("TIME") + self.assertEqual(str(utsc), "UnixTimeSecondsConversion TIME") def test_returns_the_microseconds_conversion(self): - gc = UnixTimeSecondsConversion("TIME", "TIME_US") - self.assertEqual(str(gc), "UnixTimeSecondsConversion TIME TIME_US") + utsc = UnixTimeSecondsConversion("TIME", "TIME_US") + self.assertEqual(str(utsc), "UnixTimeSecondsConversion TIME TIME_US") + + def test_creates_a_reproducable_format(self): + utsc = UnixTimeSecondsConversion("TIME", "TIME_US") + json = utsc.as_json() + self.assertEqual(json["class"], "UnixTimeSecondsConversion") + self.assertEqual(json['converted_type'], "FLOAT") + self.assertEqual(json['converted_bit_size'], 64) + self.assertEqual(json['params'], ["TIME", "TIME_US"]) + new_utsc = UnixTimeSecondsConversion(*json['params']) + packet = Packet("TGT", "PKT") + packet.append_item("TIME", 32, "UINT") + time = datetime(2020, 1, 31, 12, 15, 30, tzinfo=timezone.utc).timestamp() + packet.write("TIME", time) + packet.append_item("TIME_US", 32, "UINT") + packet.write("TIME_US", 500000) + self.assertEqual(utsc.call(None, packet, packet.buffer), new_utsc.call(None, packet, packet.buffer)) diff --git a/openc3/spec/conversions/bit_reverse_conversion_spec.rb b/openc3/spec/conversions/bit_reverse_conversion_spec.rb index e6e5becf5..fce419e9b 100644 --- a/openc3/spec/conversions/bit_reverse_conversion_spec.rb +++ b/openc3/spec/conversions/bit_reverse_conversion_spec.rb @@ -67,7 +67,7 @@ module OpenC3 expect(json['converted_type']).to eql :UINT expect(json['converted_bit_size']).to eql 8 expect(json['converted_array_size']).to eql nil - new_brc = OpenC3::const_get(json['class']).new(json['converted_type'], json['converted_bit_size']) + new_brc = OpenC3::const_get(json['class']).new(*json['params']) expect(brc.converted_type).to eql(new_brc.converted_type) expect(brc.converted_bit_size).to eql(new_brc.converted_bit_size) expect(brc.converted_array_size).to eql(new_brc.converted_array_size) diff --git a/openc3/spec/conversions/object_read_conversion_spec.rb b/openc3/spec/conversions/object_read_conversion_spec.rb index fe905939a..aa4fe890c 100644 --- a/openc3/spec/conversions/object_read_conversion_spec.rb +++ b/openc3/spec/conversions/object_read_conversion_spec.rb @@ -90,9 +90,16 @@ module OpenC3 expect(json['class']).to eql "OpenC3::ObjectReadConversion" expect(json['converted_type']).to eql :OBJECT expect(json['converted_bit_size']).to eql 0 - expect(json['cmd_or_tlm']).to eql :TLM - expect(json['target_name']).to eql "INST" - expect(json['packet_name']).to eql "PARAMS" + expect(json['params']).to eql ["TLM", "INST", "PARAMS"] + new_orc = OpenC3::const_get(json['class']).new(*json['params']) + expect(orc.converted_type).to eql(new_orc.converted_type) + expect(orc.converted_bit_size).to eql(new_orc.converted_bit_size) + expect(orc.converted_array_size).to eql(new_orc.converted_array_size) + pkt = System.telemetry.packet("INST", "PARAMS") + pkt.write("VALUE0", 1) + pkt.write("VALUE2", 1) + pkt.write("VALUE4", 1) + expect(orc.call(pkt.buffer, pkt, pkt.buffer)).to eql new_orc.call(pkt.buffer, pkt, pkt.buffer) end end end diff --git a/openc3/spec/conversions/object_write_conversion_spec.rb b/openc3/spec/conversions/object_write_conversion_spec.rb index e897c961d..d01b5ca05 100644 --- a/openc3/spec/conversions/object_write_conversion_spec.rb +++ b/openc3/spec/conversions/object_write_conversion_spec.rb @@ -100,9 +100,7 @@ module OpenC3 expect(json['class']).to eql "OpenC3::ObjectWriteConversion" expect(json['converted_type']).to eql :OBJECT expect(json['converted_bit_size']).to eql 0 - expect(json['cmd_or_tlm']).to eql :TLM - expect(json['target_name']).to eql "INST" - expect(json['packet_name']).to eql "PARAMS" + expect(json['params']).to eql ["TLM", "INST", "PARAMS"] end end end diff --git a/openc3/spec/conversions/processor_conversion_spec.rb b/openc3/spec/conversions/processor_conversion_spec.rb index 2ffefea45..974e225f7 100644 --- a/openc3/spec/conversions/processor_conversion_spec.rb +++ b/openc3/spec/conversions/processor_conversion_spec.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'spec_helper' @@ -51,5 +51,22 @@ module OpenC3 expect(ProcessorConversion.new('TEST1', 'TEST2', 'FLOAT', '64', '128').to_s).to eql "ProcessorConversion TEST1 TEST2" end end + + describe "as_json" do + it "creates a reproducible format" do + pc = ProcessorConversion.new('TEST1', 'TEST2', 'FLOAT', '64', '128') + json = pc.as_json + expect(json['class']).to eql "OpenC3::ProcessorConversion" + expect(json['converted_type']).to eql :FLOAT + expect(json['converted_bit_size']).to eql 64 + expect(json['converted_array_size']).to eql 128 + expect(json['params']).to eql ['TEST1', 'TEST2', "FLOAT", 64, 128] + new_pc = OpenC3::const_get(json['class']).new(*json['params']) + packet = Packet.new("tgt", "pkt") + packet.append_item('ITEM1', 64, :FLOAT) + packet.processors['TEST1'] = double("processor", :results => { :TEST2 => 6.0 }) + expect(pc.call(1, packet, nil)).to eql new_pc.call(1, packet, nil) + end + end end end diff --git a/openc3/spec/conversions/segmented_polynomial_conversion_spec.rb b/openc3/spec/conversions/segmented_polynomial_conversion_spec.rb index 8ef7eb57e..17f7b1dab 100644 --- a/openc3/spec/conversions/segmented_polynomial_conversion_spec.rb +++ b/openc3/spec/conversions/segmented_polynomial_conversion_spec.rb @@ -14,7 +14,7 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # # This file may also be used under the terms of a commercial license @@ -65,7 +65,7 @@ module OpenC3 spc.add_segment(15, 3, 2) json = spc.as_json(:allow_nan => true) expect(json['class']).to eql "OpenC3::SegmentedPolynomialConversion" - new_spc = OpenC3::const_get(json['class']).new(*json['params']) + new_spc = OpenC3::const_get(json['class']).new(json['params']) spc.segments.each_with_index do |segment, index| expect(segment).to be == new_spc.segments[index] end diff --git a/openc3/spec/conversions/unix_time_formatted_conversion_spec.rb b/openc3/spec/conversions/unix_time_formatted_conversion_spec.rb index 81cda5409..cb45402c2 100644 --- a/openc3/spec/conversions/unix_time_formatted_conversion_spec.rb +++ b/openc3/spec/conversions/unix_time_formatted_conversion_spec.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'spec_helper' @@ -28,54 +28,71 @@ module OpenC3 describe UnixTimeFormattedConversion do describe "initialize" do it "initializes converted_type and converted_bit_size" do - gc = UnixTimeFormattedConversion.new('TIME') - expect(gc.converted_type).to eql :STRING - expect(gc.converted_bit_size).to eql 0 + utfc = UnixTimeFormattedConversion.new('TIME') + expect(utfc.converted_type).to eql :STRING + expect(utfc.converted_bit_size).to eql 0 end end describe "call" do it "returns the formatted packet time based on seconds" do - gc = UnixTimeFormattedConversion.new('TIME') + utfc = UnixTimeFormattedConversion.new('TIME') packet = Packet.new("TGT", "PKT") packet.append_item("TIME", 32, :UINT) packet.write("TIME", Time.new(2020, 1, 31, 12, 15, 30).to_f) - expect(gc.call(nil, packet, packet.buffer)).to eql "2020/01/31 12:15:30.000" + expect(utfc.call(nil, packet, packet.buffer)).to eql "2020/01/31 12:15:30.000" end it "returns the formatted packet time based on seconds and microseconds" do - gc = UnixTimeFormattedConversion.new('TIME', 'TIME_US') + utfc = UnixTimeFormattedConversion.new('TIME', 'TIME_US') packet = Packet.new("TGT", "PKT") packet.append_item("TIME", 32, :UINT) packet.write("TIME", Time.new(2020, 1, 31, 12, 15, 30).to_f) packet.append_item("TIME_US", 32, :UINT) packet.write("TIME_US", 500000) - expect(gc.call(nil, packet, packet.buffer)).to eql "2020/01/31 12:15:30.500" + expect(utfc.call(nil, packet, packet.buffer)).to eql "2020/01/31 12:15:30.500" end it "complains if the seconds item doesn't exist" do - gc = UnixTimeFormattedConversion.new('TIME') + utfc = UnixTimeFormattedConversion.new('TIME') packet = Packet.new("TGT", "PKT") - expect { gc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME' does not exist") + expect { utfc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME' does not exist") end it "complains if the microseconds item doesn't exist" do - gc = UnixTimeFormattedConversion.new('TIME', 'TIME_US') + utfc = UnixTimeFormattedConversion.new('TIME', 'TIME_US') packet = Packet.new("TGT", "PKT") packet.append_item("TIME", 32, :UINT) - expect { gc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME_US' does not exist") + expect { utfc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME_US' does not exist") end end describe "to_s" do it "returns the seconds conversion" do - gc = UnixTimeFormattedConversion.new('TIME') - expect(gc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), 0).sys.formatted" + utfc = UnixTimeFormattedConversion.new('TIME') + expect(utfc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), 0).sys.formatted" end it "returns the microseconds conversion" do - gc = UnixTimeFormattedConversion.new('TIME', 'TIME_US') - expect(gc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), packet.read('TIME_US', :RAW, buffer)).sys.formatted" + utfc = UnixTimeFormattedConversion.new('TIME', 'TIME_US') + expect(utfc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), packet.read('TIME_US', :RAW, buffer)).sys.formatted" + end + end + + describe "to_json" do + it "creates a reproducible format" do + utfc = UnixTimeFormattedConversion.new('TIME', 'TIME_US') + json = utfc.as_json(:allow_nan => true) + expect(json['class']).to eql "OpenC3::UnixTimeFormattedConversion" + expect(json['converted_type']).to eql :STRING + expect(json['converted_bit_size']).to eql 0 + packet = Packet.new("TGT", "PKT") + packet.append_item("TIME", 32, :UINT) + packet.write("TIME", Time.new(2020, 1, 31, 12, 15, 30).to_f) + packet.append_item("TIME_US", 32, :UINT) + packet.write("TIME_US", 500000) + new_utfc = OpenC3.const_get(json['class']).new(*json['params']) + expect(utfc.call(nil, packet, packet.buffer)).to eql new_utfc.call(nil, packet, packet.buffer) end end end diff --git a/openc3/spec/conversions/unix_time_seconds_conversion_spec.rb b/openc3/spec/conversions/unix_time_seconds_conversion_spec.rb index 436441711..1627b60ad 100644 --- a/openc3/spec/conversions/unix_time_seconds_conversion_spec.rb +++ b/openc3/spec/conversions/unix_time_seconds_conversion_spec.rb @@ -14,10 +14,10 @@ # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'spec_helper' @@ -28,56 +28,74 @@ module OpenC3 describe UnixTimeSecondsConversion do describe "initialize" do it "initializes converted_type and converted_bit_size" do - gc = UnixTimeSecondsConversion.new('TIME') - expect(gc.converted_type).to eql :FLOAT - expect(gc.converted_bit_size).to eql 64 + utsc = UnixTimeSecondsConversion.new('TIME') + expect(utsc.converted_type).to eql :FLOAT + expect(utsc.converted_bit_size).to eql 64 end end describe "call" do it "returns the formatted packet time based on seconds" do - gc = UnixTimeSecondsConversion.new('TIME') + utsc = UnixTimeSecondsConversion.new('TIME') packet = Packet.new("TGT", "PKT") packet.append_item("TIME", 32, :UINT) time = Time.new(2020, 1, 31, 12, 15, 30).to_f packet.write("TIME", time) - expect(gc.call(nil, packet, packet.buffer)).to eql time + expect(utsc.call(nil, packet, packet.buffer)).to eql time end it "returns the formatted packet time based on seconds and microseconds" do - gc = UnixTimeSecondsConversion.new('TIME', 'TIME_US') + utsc = UnixTimeSecondsConversion.new('TIME', 'TIME_US') packet = Packet.new("TGT", "PKT") packet.append_item("TIME", 32, :UINT) time = Time.new(2020, 1, 31, 12, 15, 30).to_f packet.write("TIME", time) packet.append_item("TIME_US", 32, :UINT) packet.write("TIME_US", 500000) - expect(gc.call(nil, packet, packet.buffer)).to eql time + 0.5 + expect(utsc.call(nil, packet, packet.buffer)).to eql time + 0.5 end it "complains if the seconds item doesn't exist" do - gc = UnixTimeSecondsConversion.new('TIME') + utsc = UnixTimeSecondsConversion.new('TIME') packet = Packet.new("TGT", "PKT") - expect { gc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME' does not exist") + expect { utsc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME' does not exist") end it "complains if the microseconds item doesn't exist" do - gc = UnixTimeSecondsConversion.new('TIME', 'TIME_US') + utsc = UnixTimeSecondsConversion.new('TIME', 'TIME_US') packet = Packet.new("TGT", "PKT") packet.append_item("TIME", 32, :UINT) - expect { gc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME_US' does not exist") + expect { utsc.call(nil, packet, packet.buffer) }.to raise_error("Packet item 'TGT PKT TIME_US' does not exist") end end describe "to_s" do it "returns the seconds conversion" do - gc = UnixTimeSecondsConversion.new('TIME') - expect(gc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), 0).sys.to_f" + utsc = UnixTimeSecondsConversion.new('TIME') + expect(utsc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), 0).sys.to_f" end it "returns the microseconds conversion" do - gc = UnixTimeSecondsConversion.new('TIME', 'TIME_US') - expect(gc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), packet.read('TIME_US', :RAW, buffer)).sys.to_f" + utsc = UnixTimeSecondsConversion.new('TIME', 'TIME_US') + expect(utsc.to_s).to eql "Time.at(packet.read('TIME', :RAW, buffer), packet.read('TIME_US', :RAW, buffer)).sys.to_f" + end + end + + describe "to_json" do + it "creates a reproducible format" do + utsc = UnixTimeSecondsConversion.new('TIME', 'TIME_US') + json = utsc.as_json(:allow_nan => true) + expect(json['class']).to eql "OpenC3::UnixTimeSecondsConversion" + expect(json['converted_type']).to eql :FLOAT + expect(json['converted_bit_size']).to eql 64 + packet = Packet.new("TGT", "PKT") + packet.append_item("TIME", 32, :UINT) + time = Time.new(2020, 1, 31, 12, 15, 30).to_f + packet.write("TIME", time) + packet.append_item("TIME_US", 32, :UINT) + packet.write("TIME_US", 500000) + new_utsc = OpenC3.const_get(json['class']).new(*json['params']) + expect(utsc.call(nil, packet, packet.buffer)).to eql new_utsc.call(nil, packet, packet.buffer) end end end diff --git a/openc3/templates/conversion/conversion.rb b/openc3/templates/conversion/conversion.rb index 41e2d2178..1b6236b3d 100644 --- a/openc3/templates/conversion/conversion.rb +++ b/openc3/templates/conversion/conversion.rb @@ -12,6 +12,8 @@ def initialize # Size of the converted type in bits # Use 0 for :STRING or :BLOCK where the size can be variable @converted_bit_size = 0 + # return the arguments used + @params = nil end # @param value [Object] Value based on the item definition. This could be