From e8bf78f79543dec5be9092667c677dd562ad5a2a Mon Sep 17 00:00:00 2001 From: Jason Thomas Date: Mon, 28 Oct 2024 10:12:10 -0600 Subject: [PATCH] Make params contain all params passed to constructor --- .../openc3/conversions/bit_reverse_conversion.rb | 1 + .../openc3/conversions/processor_conversion.rb | 13 ++++++++++--- .../unix_time_formatted_conversion.rb | 7 ++++--- .../conversions/unix_time_seconds_conversion.rb | 7 ++++--- .../openc3/conversions/bit_reverse_conversion.py | 1 + .../openc3/conversions/processor_conversion.py | 5 ++++- .../unix_time_formatted_conversion.py | 3 ++- .../conversions/unix_time_seconds_conversion.py | 3 ++- .../conversions/test_bit_reverse_conversion.py | 2 +- .../conversions/test_processor_conversion.py | 16 ++++++++++++++++ .../conversions/bit_reverse_conversion_spec.rb | 2 +- .../conversions/processor_conversion_spec.rb | 2 +- 12 files changed, 47 insertions(+), 15 deletions(-) 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/processor_conversion.rb b/openc3/lib/openc3/conversions/processor_conversion.rb index 5623389e2..19831f617 100644 --- a/openc3/lib/openc3/conversions/processor_conversion.rb +++ b/openc3/lib/openc3/conversions/processor_conversion.rb @@ -34,13 +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) - @params = [@processor_name, @result_name] end # @param (see Conversion#call) 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/processor_conversion.py b/openc3/python/openc3/conversions/processor_conversion.py index adebb916d..0e9f59ff5 100644 --- a/openc3/python/openc3/conversions/processor_conversion.py +++ b/openc3/python/openc3/conversions/processor_conversion.py @@ -37,15 +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 = [self.processor_name, self.result_name] + self.params.append(self.converted_array_size) # @param (see Conversion#call) # @return [Varies] The result of the associated processor 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_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/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/processor_conversion_spec.rb b/openc3/spec/conversions/processor_conversion_spec.rb index 02d834ee6..974e225f7 100644 --- a/openc3/spec/conversions/processor_conversion_spec.rb +++ b/openc3/spec/conversions/processor_conversion_spec.rb @@ -60,7 +60,7 @@ module OpenC3 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'] + 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)