Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add args to conversion to be returnable as json #1512

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion openc3/lib/openc3/conversions/conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions openc3/lib/openc3/conversions/generic_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
9 changes: 1 addition & 8 deletions openc3/lib/openc3/conversions/object_read_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
11 changes: 3 additions & 8 deletions openc3/lib/openc3/conversions/polynomial_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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']
Expand Down Expand Up @@ -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
13 changes: 4 additions & 9 deletions openc3/lib/openc3/conversions/processor_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -40,6 +40,7 @@ def initialize(processor_name, result_name, converted_type = nil, converted_bit_
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MIssing converted_ params

Copy link
Member

@jmthomas jmthomas Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base class records those ... they are output in the to_config method. as_json outputs them separately and the remaining parameters get put into params.

end

# @param (see Conversion#call)
Expand All @@ -63,11 +64,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
14 changes: 3 additions & 11 deletions openc3/lib/openc3/conversions/segmented_polynomial_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -107,6 +108,7 @@ def initialize(segments = [])
# given coefficients.
# @param coeffs [Array<Integer>] The polynomial coefficients
def add_segment(lower_bound, *coeffs)
@params << [lower_bound, coeffs]
@segments << Segment.new(lower_bound, coeffs)
@segments.sort!
end
Expand Down Expand Up @@ -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
11 changes: 4 additions & 7 deletions openc3/lib/openc3/conversions/unix_time_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
4 changes: 4 additions & 0 deletions openc3/python/openc3/conversions/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class Conversion:
# attr_reader :converted_bit_size
# # self.return [Integer] The size in bits of the converted array value
# attr_reader :converted_array_size
# attr_reader :args
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 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.
#
Expand Down Expand Up @@ -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
11 changes: 1 addition & 10 deletions openc3/python/openc3/conversions/generic_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 1 addition & 7 deletions openc3/python/openc3/conversions/object_read_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
8 changes: 2 additions & 6 deletions openc3/python/openc3/conversions/polynomial_conversion.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 1 addition & 11 deletions openc3/python/openc3/conversions/processor_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 = [self.processor_name, self.result_name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing converted_ params

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base class records those ... they are output in the to_config method. as_json outputs them separately and the remaining parameters get put into params.


# @param (see Conversion#call)
# @return [Varies] The result of the associated processor
Expand All @@ -71,14 +72,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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -84,6 +85,7 @@ def __init__(self, segments=[]):
# given coefficients.
# @param coeffs [Array<Integer>] 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()

Expand Down Expand Up @@ -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
6 changes: 1 addition & 5 deletions openc3/python/openc3/conversions/unix_time_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
12 changes: 9 additions & 3 deletions openc3/python/test/conversions/test_object_read_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Loading
Loading