Skip to content

Commit

Permalink
Include the messages, refactor to schemas and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikDeBruijn committed Sep 16, 2024
1 parent 77f051d commit d85e09a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 74 deletions.
7 changes: 4 additions & 3 deletions lib/s2-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
require "active_support/all"

require_relative "s2/version"

require_relative "s2/messages/types"
require_relative "s2/messages/handshake"
require_relative "s2/messages/handshake_response"
require_relative "s2/messages/reception_status"
Dir[File.join(__dir__, 's2/schemas', '*.rb')].each { |file| require_relative file }
Dir[File.join(__dir__, 's2/messages', '*.rb')].each { |file| require_relative file }

require_relative "s2/message_factory"
require_relative "s2/message_handler"
require_relative "s2/message_handler_callbacks"
Expand Down
35 changes: 1 addition & 34 deletions lib/s2/messages/frbc_leakage_behaviour.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,12 @@ module MessageType
FRBCLeakageBehaviour = "FRBC.LeakageBehaviour"
end

# The fill level range for which this FRBC.LeakageBehaviourElement applies. The start of
# the range must be less than the end of the range.
class NumberRange < Dry::Struct

# Number that defines the end of the range
attribute :end_of_range, Types::Double

# Number that defines the start of the range
attribute :start_of_range, Types::Double

def self.from_dynamic!(d)
d = Types::Hash[d]
new(
end_of_range: d.fetch("end_of_range"),
start_of_range: d.fetch("start_of_range"),
)
end

def self.from_json!(json)
from_dynamic!(JSON.parse(json))
end

def to_dynamic
{
"end_of_range" => end_of_range,
"start_of_range" => start_of_range,
}
end

def to_json(options = nil)
JSON.generate(to_dynamic, options)
end
end

class FRBCLeakageBehaviourElement < Dry::Struct

# The fill level range for which this FRBC.LeakageBehaviourElement applies. The start of
# the range must be less than the end of the range.
attribute :fill_level_range, NumberRange
attribute :fill_level_range, Schemas::NumberRange

# Indicates how fast the momentary fill level will decrease per second due to leakage
# within the given range of the fill level. A positive value indicates that the fill level
Expand Down
44 changes: 7 additions & 37 deletions lib/s2/messages/frbc_system_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,7 @@ module Types
# The range in which the fill_level should remain. It is expected of the CEM to keep the
# fill_level within this range. When the fill_level is not within this range, the Resource
# Manager can ignore instructions from the CEM (except during abnormal conditions).
class NumberRange < Dry::Struct

# Number that defines the end of the range
attribute :end_of_range, Types::Double

# Number that defines the start of the range
attribute :start_of_range, Types::Double

def self.from_dynamic!(d)
d = Types::Hash[d]
new(
end_of_range: d.fetch("end_of_range"),
start_of_range: d.fetch("start_of_range"),
)
end

def self.from_json!(json)
from_dynamic!(JSON.parse(json))
end

def to_dynamic
{
"end_of_range" => end_of_range,
"start_of_range" => start_of_range,
}
end

def to_json(options = nil)
JSON.generate(to_dynamic, options)
end
end

# The power quantity the values refer to
#
Expand Down Expand Up @@ -124,12 +94,12 @@ class FRBCOperationModeElement < Dry::Struct

# The range of the fill level for which this FRBC.OperationModeElement applies. The start
# of the NumberRange shall be smaller than the end of the NumberRange.
attribute :fill_level_range, NumberRange
attribute :fill_level_range, S2::Schemas::NumberRange

# Indicates the change in fill_level per second. The lower_boundary of the NumberRange is
# associated with an operation_mode_factor of 0, the upper_boundary is associated with an
# operation_mode_factor of 1.
attribute :fill_rate, NumberRange
attribute :fill_rate, S2::Schemas::NumberRange

# The power produced or consumed by this operation mode. The start of each PowerRange is
# associated with an operation_mode_factor of 0, the end is associated with an
Expand All @@ -140,15 +110,15 @@ class FRBCOperationModeElement < Dry::Struct
# Additional costs per second (e.g. wear, services) associated with this operation mode in
# the currency defined by the ResourceManagerDetails, excluding the commodity cost. The
# range is expressing uncertainty and is not linked to the operation_mode_factor.
attribute :running_costs, NumberRange.optional
attribute :running_costs, S2::Schemas::NumberRange.optional

def self.from_dynamic!(d)
d = Types::Hash[d]
new(
fill_level_range: NumberRange.from_dynamic!(d.fetch("fill_level_range")),
fill_rate: NumberRange.from_dynamic!(d.fetch("fill_rate")),
fill_level_range: S2::Schemas::NumberRange.from_dynamic!(d.fetch("fill_level_range")),
fill_rate: S2::Schemas::NumberRange.from_dynamic!(d.fetch("fill_rate")),
power_ranges: d.fetch("power_ranges").map { |x| PowerRange.from_dynamic!(x) },
running_costs: d["running_costs"] ? NumberRange.from_dynamic!(d["running_costs"]) : nil,
running_costs: d["running_costs"] ? S2::Schemas::NumberRange.from_dynamic!(d["running_costs"]) : nil,
)
end

Expand Down Expand Up @@ -402,7 +372,7 @@ class FRBCStorageDescription < Dry::Struct
# The range in which the fill_level should remain. It is expected of the CEM to keep the
# fill_level within this range. When the fill_level is not within this range, the Resource
# Manager can ignore instructions from the CEM (except during abnormal conditions).
attribute :fill_level_range, NumberRange
attribute :fill_level_range, S2::Schemas::NumberRange

# Indicates whether the Storage could provide a target profile for the fill level through
# the FRBC.FillLevelTargetProfile.
Expand Down
38 changes: 38 additions & 0 deletions lib/s2/schemas/number_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module S2
module Schemas

# The fill level range for which this FRBC.LeakageBehaviourElement applies. The start of
# the range must be less than the end of the range.
class NumberRange < Dry::Struct

# Number that defines the end of the range
attribute :end_of_range, S2::Messages::Types::Double

# Number that defines the start of the range
attribute :start_of_range, S2::Messages::Types::Double

def self.from_dynamic!(d)
d = S2::Messages::Types::Hash[d]
new(
end_of_range: d.fetch("end_of_range"),
start_of_range: d.fetch("start_of_range"),
)
end

def self.from_json!(json)
from_dynamic!(JSON.parse(json))
end

def to_dynamic
{
"end_of_range" => end_of_range,
"start_of_range" => start_of_range,
}
end

def to_json(options = nil)
JSON.generate(to_dynamic, options)
end
end
end
end

0 comments on commit d85e09a

Please sign in to comment.