Method: OpenC3::DecomMicroservice#limits_change_callback

Defined in:
lib/openc3/microservices/decom_microservice.rb

#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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
# File 'lib/openc3/microservices/decom_microservice.rb', line 202

def limits_change_callback(packet, item, old_limits_state, value, log_change)
  return if @cancel_thread
  # Make a copy because packet_time is frozen
  packet_time = packet.packet_time.dup
  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