Class: OpenC3::DecomMicroservice

Inherits:
Microservice show all
Includes:
InterfaceDecomCommon
Defined in:
lib/openc3/microservices/decom_microservice.rb

Constant Summary collapse

LIMITS_STATE_INDEX =
{ RED_LOW: 0, YELLOW_LOW: 1, YELLOW_HIGH: 2, RED_HIGH: 3, GREEN_LOW: 4, GREEN_HIGH: 5 }

Instance Attribute Summary

Attributes inherited from Microservice

#count, #custom, #error, #logger, #microservice_status_thread, #name, #scope, #secrets, #state

Instance Method Summary collapse

Methods included from InterfaceDecomCommon

#handle_build_cmd, #handle_get_tlm_buffer, #handle_inject_tlm

Methods inherited from Microservice

#as_json, #microservice_cmd, run, #setup_microservice_topic, #shutdown

Constructor Details

#initialize(*args) ⇒ DecomMicroservice

Returns a new instance of DecomMicroservice.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/openc3/microservices/decom_microservice.rb', line 91

def initialize(*args)
  super(*args)
  # Should only be one target, but there might be multiple decom microservices for a given target
  # First Decom microservice has no number in the name
  if @name =~ /__DECOM__/
    @topics << "#{@scope}__DECOMINTERFACE__{#{@target_names[0]}}"
  end
  Topic.update_topic_offsets(@topics)
  System.telemetry.limits_change_callback = method(:limits_change_callback)
  LimitsEventTopic.sync_system(scope: @scope)
  @error_count = 0
  @metric.set(name: 'decom_total', value: @count, type: 'counter')
  @metric.set(name: 'decom_error_total', value: @error_count, type: 'counter')
  @limits_response_queue = Queue.new
  @limits_response_thread = nil
end

Instance Method Details

#decom_packet(_topic, msg_id, msg_hash, _redis) ⇒ Object



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
# File 'lib/openc3/microservices/decom_microservice.rb', line 155

def decom_packet(_topic, msg_id, msg_hash, _redis)
  OpenC3.in_span("decom_packet") do
    msgid_seconds_from_epoch = msg_id.split('-')[0].to_i / 1000.0
    delta = Time.now.to_f - msgid_seconds_from_epoch
    @metric.set(name: 'decom_topic_delta_seconds', value: delta, type: 'gauge', unit: 'seconds', help: 'Delta time between data written to stream and decom start')

    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

    #######################################
    # Build packet object from topic data
    #######################################
    target_name = msg_hash["target_name"]
    packet_name = msg_hash["packet_name"]
    packet = System.telemetry.packet(target_name, packet_name)
    packet.stored = ConfigParser.handle_true_false(msg_hash["stored"])
    # Note: Packet time will be recalculated as part of decom so not setting
    packet.received_time = Time.from_nsec_from_epoch(msg_hash["received_time"].to_i)
    packet.received_count = msg_hash["received_count"].to_i
    extra = msg_hash["extra"]
    if extra and extra.length > 0
      extra = JSON.parse(extra, allow_nan: true, create_additions: true)
      packet.extra = extra
    end
    packet.buffer = msg_hash["buffer"]

    ################################################################################
    # Break packet into subpackets (if necessary)
    # Subpackets are typically channelized data
    ################################################################################
    packet_and_subpackets = packet.subpacketize

    packet_and_subpackets.each do |packet_or_subpacket|
      if packet_or_subpacket.subpacket
        packet_or_subpacket = handle_subpacket(packet, packet_or_subpacket)
      end

      #####################################################################################
      # Run Processors
      # This must be before the full decom so that processor derived values are available
      #####################################################################################
      begin
        packet_or_subpacket.process # Run processors
      rescue Exception => e
        @error_count += 1
        @metric.set(name: 'decom_error_total', value: @error_count, type: 'counter')
        @error = e
        @logger.error e.message
      end

      #############################################################################
      # Process all the limits and call the limits_change_callback (as necessary)
      # This must be before the full decom so that limits states are available
      #############################################################################
      packet_or_subpacket.check_limits(System.limits_set)

      # This is what actually decommutates the packet and updates the CVT
      TelemetryDecomTopic.write_packet(packet_or_subpacket, scope: @scope)
    end

    diff = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start # seconds as a float
    @metric.set(name: 'decom_duration_seconds', value: diff, type: 'gauge', unit: 'seconds')
  end
end

#handle_subpacket(packet, subpacket) ⇒ Object



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
# File 'lib/openc3/microservices/decom_microservice.rb', line 219

def handle_subpacket(packet, subpacket)
  # Subpacket received time always = packet.received_time
  # Use packet_time appropriately if another timestamp is needed
  subpacket.received_time = packet.received_time
  subpacket.stored = packet.stored
  subpacket.extra = packet.extra

  if subpacket.stored
    # Stored telemetry does not update the current value table
    identified_subpacket = System.telemetry.identify_and_define_packet(subpacket, @target_names, subpackets: true)
  else
    # Identify and update subpacket
    if subpacket.identified?
      begin
        # Preidentifed subpacket - place it into the current value table
        identified_subpacket = System.telemetry.update!(subpacket.target_name,
                                                        subpacket.packet_name,
                                                        subpacket.buffer)
      rescue RuntimeError
        # Subpacket identified but we don't know about it
        # Clear packet_name and target_name and try to identify
        @logger.warn "#{@name}: Received unknown identified subpacket: #{subpacket.target_name} #{subpacket.packet_name}"
        subpacket.target_name = nil
        subpacket.packet_name = nil
        identified_subpacket = System.telemetry.identify!(subpacket.buffer,
                                                          @target_names, subpackets: true)
      end
    else
      # Packet needs to be identified
      identified_subpacket = System.telemetry.identify!(subpacket.buffer,
                                                        @target_names, subpackets: true)
    end
  end

  if identified_subpacket
    identified_subpacket.received_time = subpacket.received_time
    identified_subpacket.stored = subpacket.stored
    identified_subpacket.extra = subpacket.extra
    subpacket = identified_subpacket
  else
    unknown_subpacket = System.telemetry.update!('UNKNOWN', 'UNKNOWN', subpacket.buffer)
    unknown_subpacket.received_time = subpacket.received_time
    unknown_subpacket.stored = subpacket.stored
    unknown_subpacket.extra = subpacket.extra
    subpacket = unknown_subpacket
    num_bytes_to_print = [InterfaceMicroservice::UNKNOWN_BYTES_TO_PRINT, subpacket.length].min
    data = subpacket.buffer(false)[0..(num_bytes_to_print - 1)]
    prefix = data.each_byte.map { | byte | sprintf("%02X", byte) }.join()
    @logger.warn "#{@name} #{subpacket.target_name} packet length: #{subpacket.length} starting with: #{prefix}"
  end

  TargetModel.sync_tlm_packet_counts(subpacket, @target_names, scope: @scope)
  return subpacket
end

#limits_change_callback(packet, item, old_limits_state, value, log_change) ⇒ Object

Called when an item in any packet changes limits states.

Parameters:

  • packet (Packet)

    Packet which has had an item change limits state

  • item (PacketItem)

    The item which has changed limits state

  • old_limits_state (Symbol)

    The previous state of the item. See PacketItemLimits#state

  • value (Object)

    The current value of the item

  • log_change (Boolean)

    Whether to log this limits change event



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
# File 'lib/openc3/microservices/decom_microservice.rb', line 282

def limits_change_callback(packet, item, old_limits_state, value, log_change)
  return if @cancel_thread
  # Make a copy because packet_time is frozen
  if packet.packet_time
    packet_time = packet.packet_time.dup
  else
    packet_time = Time.now.utc
  end
  if value
    message = "#{packet.target_name} #{packet.packet_name} #{item.name} = #{value} is #{item.limits.state}"
    if item.limits.values
      values = item.limits.values[System.limits_set]
      # Check if the state is RED_LOW, YELLOW_LOW, YELLOW_HIGH, RED_HIGH, GREEN_LOW, GREEN_HIGH
      if LIMITS_STATE_INDEX[item.limits.state]
        # Directly index into the values and return the value
        message += " (#{values[LIMITS_STATE_INDEX[item.limits.state]]})"
      elsif item.limits.state == :GREEN
        # If we're green we display the green range (YELLOW_LOW - YELLOW_HIGH)
        message += " (#{values[1]} to #{values[2]})"
      elsif item.limits.state == :BLUE
        # If we're blue we display the blue range (GREEN_LOW - GREEN_HIGH)
        message += " (#{values[4]} to #{values[5]})"
      end
    end
  else
    message = "#{packet.target_name} #{packet.packet_name} #{item.name} is disabled"
  end

  # Include the packet_time in the log json but not the log message
  time = { packet_time: packet_time.utc.iso8601(6) }
  if log_change
    case item.limits.state
    when :BLUE, :GREEN, :GREEN_LOW, :GREEN_HIGH
      # Only print INFO messages if we're changing ... not on initialization
      @logger.info(message, other: time) if old_limits_state
    when :YELLOW, :YELLOW_LOW, :YELLOW_HIGH
      @logger.warn(message, other: time, type: Logger::NOTIFICATION)
    when :RED, :RED_LOW, :RED_HIGH
      @logger.error(message, other: time, type: Logger::ALERT)
    end
  end

  # The openc3_limits_events topic can be listened to for all limits events, it is a continuous stream
  event = { type: :LIMITS_CHANGE, target_name: packet.target_name, packet_name: packet.packet_name,
            item_name: item.name, old_limits_state: old_limits_state.to_s, new_limits_state: item.limits.state.to_s,
            time_nsec: packet_time.to_nsec_from_epoch, message: message.to_s }
  LimitsEventTopic.write(event, scope: @scope)

  if item.limits.response
    copied_packet = packet.deep_copy
    copied_item = packet.items[item.name]
    @limits_response_queue << [copied_packet, copied_item, old_limits_state]
  end
end

#runObject



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
# File 'lib/openc3/microservices/decom_microservice.rb', line 108

def run
  @limits_response_thread = LimitsResponseThread.new(microservice_name: @name, queue: @limits_response_queue, logger: @logger, metric: @metric, scope: @scope)
  @limits_response_thread.start()

  setup_microservice_topic()
  while true
    break if @cancel_thread

    begin
      OpenC3.in_span("read_topics") do
        Topic.read_topics(@topics) do |topic, msg_id, msg_hash, redis|
          break if @cancel_thread
          if topic == @microservice_topic
            microservice_cmd(topic, msg_id, msg_hash, redis)
          elsif topic =~ /__DECOMINTERFACE/
            if msg_hash.key?('inject_tlm')
              handle_inject_tlm(msg_hash['inject_tlm'])
              next
            end
            if msg_hash.key?('build_cmd')
              handle_build_cmd(msg_hash['build_cmd'], msg_id)
              next
            end
            if msg_hash.key?('get_tlm_buffer')
              handle_get_tlm_buffer(msg_hash['get_tlm_buffer'], msg_id)
              next
            end
          else
            decom_packet(topic, msg_id, msg_hash, redis)
            @metric.set(name: 'decom_total', value: @count, type: 'counter')
          end
          @count += 1
        end
      end
      LimitsEventTopic.sync_system_thread_body(scope: @scope)
    rescue => e
      @error_count += 1
      @metric.set(name: 'decom_error_total', value: @error_count, type: 'counter')
      @error = e
      @logger.error("Decom error: #{e.formatted}")
    end
  end

  @limits_response_thread.stop()
  @limits_response_thread = nil
end