openc3 7.5.1 → 7.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -0
  3. data/bin/openc3cli +82 -5
  4. data/data/config/_params.yaml +3 -0
  5. data/data/config/command_modifiers.yaml +5 -3
  6. data/data/config/default_plugins.yaml +1 -0
  7. data/data/config/interface_modifiers.yaml +17 -1
  8. data/data/config/item_modifiers.yaml +24 -1
  9. data/data/config/microservice.yaml +15 -1
  10. data/data/config/parameter_modifiers.yaml +4 -4
  11. data/data/config/plugins.yaml +511 -3
  12. data/data/config/settings.yaml +17 -0
  13. data/data/config/target.yaml +53 -0
  14. data/data/config/telemetry.yaml +58 -2
  15. data/data/config/widgets.yaml +30 -0
  16. data/lib/openc3/accessors/accessor.rb +14 -2
  17. data/lib/openc3/accessors/binary_accessor.rb +25 -5
  18. data/lib/openc3/api/CLAUDE.md +1 -0
  19. data/lib/openc3/api/api.rb +1 -0
  20. data/lib/openc3/api/cmd_api.rb +9 -1
  21. data/lib/openc3/api/interface_api.rb +7 -2
  22. data/lib/openc3/api/limits_api.rb +93 -41
  23. data/lib/openc3/api/router_api.rb +7 -2
  24. data/lib/openc3/api/settings_api.rb +4 -1
  25. data/lib/openc3/api/shared_resource_api.rb +107 -0
  26. data/lib/openc3/api/tlm_api.rb +10 -0
  27. data/lib/openc3/interfaces/http_client_interface.rb +84 -27
  28. data/lib/openc3/interfaces/protocols/preidentified_protocol.rb +6 -2
  29. data/lib/openc3/interfaces/tcpip_server_interface.rb +205 -125
  30. data/lib/openc3/io/json_api.rb +2 -10
  31. data/lib/openc3/io/json_api_object.rb +2 -10
  32. data/lib/openc3/logs/packet_log_writer.rb +8 -3
  33. data/lib/openc3/microservices/decom_common.rb +4 -1
  34. data/lib/openc3/microservices/decom_microservice.rb +52 -6
  35. data/lib/openc3/microservices/interface_decom_common.rb +13 -0
  36. data/lib/openc3/microservices/interface_microservice.rb +50 -11
  37. data/lib/openc3/microservices/limits_group_microservice.rb +513 -0
  38. data/lib/openc3/microservices/log_microservice.rb +22 -8
  39. data/lib/openc3/microservices/microservice.rb +8 -2
  40. data/lib/openc3/microservices/multi_microservice.rb +76 -22
  41. data/lib/openc3/microservices/queue_microservice.rb +29 -24
  42. data/lib/openc3/migrations/20260704000000_default_bridge.rb +1 -2
  43. data/lib/openc3/migrations/20261103000001_limits_group_backfill.rb +193 -0
  44. data/lib/openc3/models/bridge_model.rb +3 -3
  45. data/lib/openc3/models/cvt_model.rb +3 -1
  46. data/lib/openc3/models/host_interface_microservice_model.rb +4 -4
  47. data/lib/openc3/models/interface_model.rb +27 -7
  48. data/lib/openc3/models/microservice_group.rb +167 -0
  49. data/lib/openc3/models/microservice_model.rb +88 -2
  50. data/lib/openc3/models/panel_model.rb +109 -0
  51. data/lib/openc3/models/plugin_model.rb +1006 -26
  52. data/lib/openc3/models/plugin_store_model.rb +6 -1
  53. data/lib/openc3/models/protected_storage_model.rb +64 -0
  54. data/lib/openc3/models/queue_model.rb +19 -10
  55. data/lib/openc3/models/scope_model.rb +80 -3
  56. data/lib/openc3/models/setting_model.rb +2 -2
  57. data/lib/openc3/models/shared_resource_model.rb +381 -0
  58. data/lib/openc3/models/target_model.rb +324 -68
  59. data/lib/openc3/operators/microservice_operator.rb +76 -9
  60. data/lib/openc3/operators/operator.rb +29 -0
  61. data/lib/openc3/packets/commands.rb +16 -16
  62. data/lib/openc3/packets/limits.rb +2 -0
  63. data/lib/openc3/packets/packet.rb +75 -4
  64. data/lib/openc3/packets/packet_config.rb +152 -13
  65. data/lib/openc3/packets/packet_item.rb +35 -14
  66. data/lib/openc3/packets/packet_item_limits.rb +67 -6
  67. data/lib/openc3/packets/parsers/limits_parser.rb +69 -2
  68. data/lib/openc3/packets/parsers/packet_item_parser.rb +6 -0
  69. data/lib/openc3/packets/parsers/packet_parser.rb +45 -0
  70. data/lib/openc3/packets/parsers/state_parser.rb +1 -1
  71. data/lib/openc3/script/CLAUDE.md +1 -0
  72. data/lib/openc3/script/api_shared.rb +17 -9
  73. data/lib/openc3/script/calendar.rb +18 -6
  74. data/lib/openc3/script/commands.rb +11 -9
  75. data/lib/openc3/script/critical_cmd.rb +22 -2
  76. data/lib/openc3/script/metadata.rb +30 -4
  77. data/lib/openc3/script/plugins.rb +8 -0
  78. data/lib/openc3/script/script.rb +8 -24
  79. data/lib/openc3/script/web_socket_api.rb +23 -17
  80. data/lib/openc3/system/system.rb +9 -3
  81. data/lib/openc3/top_level.rb +17 -0
  82. data/lib/openc3/topics/command_topic.rb +7 -1
  83. data/lib/openc3/topics/decom_interface_topic.rb +9 -3
  84. data/lib/openc3/topics/discovery_topic.rb +84 -0
  85. data/lib/openc3/topics/limits_event_topic.rb +236 -17
  86. data/lib/openc3/topics/telemetry_decom_topic.rb +12 -3
  87. data/lib/openc3/topics/topic.rb +2 -1
  88. data/lib/openc3/utilities/authentication.rb +67 -16
  89. data/lib/openc3/utilities/cli_generator.rb +117 -25
  90. data/lib/openc3/utilities/client_ip_header_filter.rb +38 -0
  91. data/lib/openc3/utilities/config_overlay.rb +24 -1
  92. data/lib/openc3/utilities/cosmos_rails_formatter.rb +18 -10
  93. data/lib/openc3/utilities/hardware_discovery.rb +279 -0
  94. data/lib/openc3/utilities/limits_rollup.rb +81 -0
  95. data/lib/openc3/utilities/local_mode.rb +48 -5
  96. data/lib/openc3/utilities/questdb_client.rb +130 -22
  97. data/lib/openc3/utilities/reingest_job.rb +12 -3
  98. data/lib/openc3/utilities/running_script.rb +206 -49
  99. data/lib/openc3/utilities/script.rb +24 -12
  100. data/lib/openc3/utilities/script_env.rb +164 -0
  101. data/lib/openc3/utilities/script_message_signing.rb +133 -0
  102. data/lib/openc3/utilities/store_autoload.rb +32 -1
  103. data/lib/openc3/utilities/store_queued.rb +5 -1
  104. data/lib/openc3/version.rb +6 -6
  105. data/templates/command_validator/command_validator.py +9 -9
  106. data/templates/{tool_react/pnpm-workspace.yaml → panel/.prettierrc.cjs} +7 -5
  107. data/templates/panel/Rakefile +24 -0
  108. data/templates/panel/package.json +17 -0
  109. data/templates/{tool_angular → panel}/pnpm-workspace.yaml +5 -0
  110. data/templates/panel/src/Panel.js +52 -0
  111. data/templates/panel/src/Panel.vue +60 -0
  112. data/templates/panel/src/panelSdk.js +24 -0
  113. data/templates/panel/vite-Panel.config.js +43 -0
  114. data/templates/tool_vue/package.json +3 -3
  115. data/templates/widget/package.json +2 -2
  116. metadata +24 -56
  117. data/templates/tool_angular/.editorconfig +0 -16
  118. data/templates/tool_angular/.gitignore +0 -44
  119. data/templates/tool_angular/.vscode/extensions.json +0 -4
  120. data/templates/tool_angular/.vscode/launch.json +0 -20
  121. data/templates/tool_angular/.vscode/tasks.json +0 -42
  122. data/templates/tool_angular/angular.json +0 -111
  123. data/templates/tool_angular/extra-webpack.config.js +0 -8
  124. data/templates/tool_angular/package.json +0 -48
  125. data/templates/tool_angular/src/app/app-routing.module.ts +0 -15
  126. data/templates/tool_angular/src/app/app.component.html +0 -22
  127. data/templates/tool_angular/src/app/app.component.scss +0 -18
  128. data/templates/tool_angular/src/app/app.component.spec.ts +0 -29
  129. data/templates/tool_angular/src/app/app.component.ts +0 -52
  130. data/templates/tool_angular/src/app/app.module.ts +0 -30
  131. data/templates/tool_angular/src/app/custom-overlay-container.ts +0 -17
  132. data/templates/tool_angular/src/app/empty-route/empty-route.component.ts +0 -7
  133. data/templates/tool_angular/src/app/openc3-api.d.ts +0 -1
  134. data/templates/tool_angular/src/assets/.gitkeep +0 -0
  135. data/templates/tool_angular/src/environments/environment.prod.ts +0 -3
  136. data/templates/tool_angular/src/environments/environment.ts +0 -16
  137. data/templates/tool_angular/src/favicon.ico +0 -0
  138. data/templates/tool_angular/src/index.html +0 -13
  139. data/templates/tool_angular/src/main.single-spa.ts +0 -40
  140. data/templates/tool_angular/src/single-spa/asset-url.ts +0 -12
  141. data/templates/tool_angular/src/single-spa/single-spa-props.ts +0 -8
  142. data/templates/tool_angular/src/styles.scss +0 -1
  143. data/templates/tool_angular/tsconfig.app.json +0 -13
  144. data/templates/tool_angular/tsconfig.json +0 -33
  145. data/templates/tool_angular/tsconfig.spec.json +0 -14
  146. data/templates/tool_react/.eslintrc +0 -7
  147. data/templates/tool_react/.gitignore +0 -71
  148. data/templates/tool_react/.prettierignore +0 -8
  149. data/templates/tool_react/babel.config.json +0 -29
  150. data/templates/tool_react/jest.config.js +0 -12
  151. data/templates/tool_react/package.json +0 -51
  152. data/templates/tool_react/src/openc3-tool_name.js +0 -24
  153. data/templates/tool_react/src/root.component.js +0 -88
  154. data/templates/tool_react/src/root.component.test.js +0 -9
  155. data/templates/tool_react/webpack.config.js +0 -27
  156. data/templates/tool_svelte/.gitignore +0 -71
  157. data/templates/tool_svelte/.prettierignore +0 -8
  158. data/templates/tool_svelte/.prettierrc.js +0 -5
  159. data/templates/tool_svelte/babel.config.js +0 -12
  160. data/templates/tool_svelte/build/smui.css +0 -1
  161. data/templates/tool_svelte/jest.config.js +0 -9
  162. data/templates/tool_svelte/package.json +0 -49
  163. data/templates/tool_svelte/pnpm-workspace.yaml +0 -20
  164. data/templates/tool_svelte/rollup.config.js +0 -74
  165. data/templates/tool_svelte/src/App.svelte +0 -42
  166. data/templates/tool_svelte/src/App.test.js +0 -9
  167. data/templates/tool_svelte/src/theme/_smui-theme.scss +0 -25
  168. data/templates/tool_svelte/src/tool_name.js +0 -17
  169. data/templates/widget/LICENSE.md +0 -20
@@ -17,6 +17,7 @@
17
17
 
18
18
  require 'openc3'
19
19
  require 'openc3/models/microservice_model'
20
+ require 'openc3/models/shared_resource_model'
20
21
  require 'openc3/operators/operator'
21
22
  require 'openc3/utilities/python_venv'
22
23
  require 'openc3/utilities/secrets'
@@ -222,7 +223,7 @@ module OpenC3
222
223
  # Remove regular microservice
223
224
  @removed_microservices[microservice_name] = microservice_config
224
225
  end
225
- else
226
+ elsif enabled
226
227
  # Newly enabled microservice
227
228
  @new_microservices[microservice_name] = microservice_config
228
229
  end
@@ -249,16 +250,67 @@ module OpenC3
249
250
  end
250
251
  end
251
252
 
253
+ # Confirm that shared resource members on this shard which are waiting to be
254
+ # released have stopped. Called at the start of a cycle so any process the
255
+ # previous cycle removed has already been shut down by remove_old.
256
+ #
257
+ # @param records [Hash] SharedResourceModel.all, updated in place
258
+ def acknowledge_shared_resources(records)
259
+ records.keys.each do |name|
260
+ record = records[name]
261
+ next unless record['state'] == SharedResourceModel::RELEASING
262
+
263
+ stopped = []
264
+ @mutex.synchronize do
265
+ (record['waiting'] || []).each do |member|
266
+ next unless member['shard'].to_i == @shard
267
+ next if @processes[member['name']] or @removed_processes[member['name']]
268
+ stopped << member['name']
269
+ end
270
+ end
271
+ next if stopped.empty?
272
+
273
+ Logger.info("Shared resource #{name} members stopped: #{stopped.join(', ')}")
274
+ updated = SharedResourceModel.acknowledge(name, stopped)
275
+ if updated
276
+ records[name] = updated
277
+ else
278
+ records.delete(name)
279
+ end
280
+ end
281
+ end
282
+
283
+ # Disable microservices belonging to a shared resource which isn't active in
284
+ # their scope. The rest of update then handles them like any other disabled
285
+ # microservice: killed if running and not spawned until enabled.
286
+ def apply_shared_resources(microservices, records)
287
+ microservices.each do |microservice_name, microservice_config|
288
+ resource = microservice_config['shared_resource']
289
+ next unless resource
290
+ scope = microservice_name.split("__")[0]
291
+ unless SharedResourceModel.active?(records[resource], scope)
292
+ microservice_config['enabled'] = false
293
+ end
294
+ end
295
+ end
296
+
252
297
  def update
253
- @previous_microservices = @microservices.dup
254
- # Get all the microservice configuration
255
- @microservices = MicroserviceModel.all
298
+ shared_resources = SharedResourceModel.all
299
+ acknowledge_shared_resources(shared_resources)
256
300
 
257
- # Filter to just this shard
258
- @microservices = @microservices.select do |microservice_name, microservice_config|
301
+ # Get all the microservice configuration for just this shard
302
+ microservices = MicroserviceModel.all.select do |_microservice_name, microservice_config|
259
303
  microservice_shard = microservice_config['shard'] || 0
260
304
  microservice_shard == @shard
261
305
  end
306
+ apply_shared_resources(microservices, shared_resources)
307
+ apply_disabled_scopes(microservices)
308
+
309
+ # Only move on to the new config once every lookup above has succeeded.
310
+ # Otherwise a failed cycle would leave the raw config (without disabled
311
+ # scopes applied) as the baseline the next cycle compares against.
312
+ @previous_microservices = @microservices
313
+ @microservices = microservices
262
314
 
263
315
  # Detect new and changed microservices
264
316
  @new_microservices = {}
@@ -266,8 +318,12 @@ module OpenC3
266
318
  @removed_microservices = {}
267
319
  @microservices.each do |microservice_name, microservice_config|
268
320
  if @previous_microservices[microservice_name]
269
- if @previous_microservices[microservice_name] != microservice_config
270
- if not microservice_config['ignore_changes']
321
+ previous_config = @previous_microservices[microservice_name]
322
+ if previous_config != microservice_config
323
+ # ignore_changes skips restarts for config changes (e.g. topics
324
+ # added by a dynamic packet) but must not keep a microservice
325
+ # running after it has been disabled, or stopped once re-enabled
326
+ if not microservice_config['ignore_changes'] or enabled_changed?(previous_config, microservice_config)
271
327
  handle_changed_microservice(microservice_name, microservice_config)
272
328
  end
273
329
  end
@@ -283,6 +339,12 @@ module OpenC3
283
339
  end
284
340
  end
285
341
 
342
+ # A child changing asks for its parent to be respawned, but a disabled
343
+ # parent (e.g. its scope is disabled) must stay stopped, and a parent
344
+ # being started already gets the new config (starting it from both
345
+ # queues would orphan the first process)
346
+ @changed_microservices.reject! { |name, config| config['enabled'] == false or @new_microservices.key?(name) }
347
+
286
348
  # Convert to processes
287
349
  @mutex.synchronize do
288
350
  @new_microservices.each do |microservice_name, microservice_config|
@@ -317,7 +379,12 @@ module OpenC3
317
379
  @removed_microservices.each do |microservice_name, _microservice_config|
318
380
  process = @processes[microservice_name]
319
381
  @processes.delete(microservice_name)
320
- @removed_processes[microservice_name] = process
382
+ # Drop it from the per-cycle start queues so it isn't started (or
383
+ # restarted) after we've stopped tracking it
384
+ @new_processes.delete(microservice_name)
385
+ @changed_processes.delete(microservice_name)
386
+ # Nothing to stop if it was never started
387
+ @removed_processes[microservice_name] = process if process
321
388
  end
322
389
  end
323
390
  end
@@ -278,6 +278,35 @@ module OpenC3
278
278
  raise "Implement in subclass"
279
279
  end
280
280
 
281
+ # Disable every top level microservice in a disabled scope. The rest of
282
+ # update then handles them like any other disabled microservice: killed if
283
+ # running and not spawned until the scope is enabled again. Children aren't
284
+ # touched as they only run inside their (now disabled) parent.
285
+ #
286
+ # @param microservices [Hash] MicroserviceModel.all, updated in place
287
+ def apply_disabled_scopes(microservices)
288
+ # Required here as scope_model requires (via process_manager) this file
289
+ require 'openc3/models/scope_model'
290
+ disabled_scopes = ScopeModel.disabled_scopes
291
+ return if disabled_scopes.empty?
292
+ microservices.each do |microservice_name, microservice_config|
293
+ next if microservice_config['parent']
294
+ scope = microservice_name.split("__")[0]
295
+ microservice_config['enabled'] = false if disabled_scopes.include?(scope)
296
+ end
297
+ end
298
+
299
+ # Whether a microservice was enabled or disabled between two cycles, which
300
+ # must be acted on even for a microservice with ignore_changes set
301
+ #
302
+ # @param previous_config [Hash] Microservice config from the last cycle
303
+ # @param config [Hash] Microservice config from this cycle
304
+ # @return [Boolean]
305
+ def enabled_changed?(previous_config, config)
306
+ # A missing enabled (nil) means enabled
307
+ (previous_config['enabled'] != false) != (config['enabled'] != false)
308
+ end
309
+
281
310
  # Report cpu and memory for every microservice this operator is running.
282
311
  #
283
312
  # Microservices that are themselves OpenC3::Microservices could report this
@@ -205,11 +205,14 @@ module OpenC3
205
205
  command.stored = false
206
206
  command.extra = nil
207
207
  command.given_values = params
208
+ # Check required parameters before writing defaults. A required
209
+ # parameter's default is never used, so it shouldn't be able to
210
+ # cause an error that hides the missing parameter.
211
+ check_required_params(command, params.keys.map { |name| name.to_s.upcase }) if check_required_params
208
212
  command.restore_defaults(command.buffer(false), params.keys)
209
213
  command.raw = raw
210
214
 
211
- given_item_names = set_parameters(command, params, range_checking)
212
- check_required_params(command, given_item_names) if check_required_params
215
+ set_parameters(command, params, range_checking)
213
216
 
214
217
  return command
215
218
  end
@@ -294,8 +297,6 @@ module OpenC3
294
297
  protected
295
298
 
296
299
  def set_parameters(command, params, range_checking)
297
- given_item_names = []
298
-
299
300
  # Identify length fields that are auto-managed by variable_bit_size arrays
300
301
  # These should not be written directly - the array write will auto-update them
301
302
  auto_length_fields = Set.new
@@ -309,10 +310,7 @@ module OpenC3
309
310
  item_upcase = item_name.to_s.upcase
310
311
 
311
312
  # Skip auto-managed length fields - they will be set when the array is written
312
- if auto_length_fields.include?(item_upcase)
313
- given_item_names << item_upcase
314
- next
315
- end
313
+ next if auto_length_fields.include?(item_upcase)
316
314
 
317
315
  item = command.get_item(item_upcase)
318
316
  range_check_value = value
@@ -346,15 +344,17 @@ module OpenC3
346
344
  end
347
345
 
348
346
  # Update parameter in command - use write_item directly to avoid redundant get_item call
349
- if command.raw
350
- command.write_item(item, value, :RAW)
351
- else
352
- command.write_item(item, value, :CONVERTED)
347
+ begin
348
+ if command.raw
349
+ command.write_item(item, value, :RAW)
350
+ else
351
+ command.write_item(item, value, :CONVERTED)
352
+ end
353
+ rescue ArgumentError => e
354
+ # Name the parameter since the accessor error doesn't
355
+ raise ArgumentError, "Command parameter '#{command.target_name} #{command.packet_name} #{item_upcase}': #{e.message}", e.backtrace
353
356
  end
354
-
355
- given_item_names << item_upcase
356
357
  end
357
- given_item_names
358
358
  end
359
359
 
360
360
  def check_required_params(command, given_item_names)
@@ -363,7 +363,7 @@ module OpenC3
363
363
  item_defs = command.items
364
364
  item_defs.each do |item_name, item_def|
365
365
  if item_def.required and not given_item_names.include? item_name
366
- raise "Required command parameter '#{command.target_name} #{command.packet_name} #{item_name}' not given"
366
+ raise ArgumentError, "Required command parameter '#{command.target_name} #{command.packet_name} #{item_name}' not given"
367
367
  end
368
368
  end
369
369
  end
@@ -118,6 +118,8 @@ module OpenC3
118
118
  packet = _get_packet(target_name, packet_name)
119
119
  item = packet.get_item(item_name)
120
120
  limits = item.limits
121
+ raise "Cannot set numeric limits on #{target_name} #{packet_name} #{item_name} which has #{limits.change_type} limits" if limits.change_type
122
+
121
123
  if limits_set
122
124
  limits_set = limits_set.to_s.upcase.intern
123
125
  else
@@ -919,7 +919,14 @@ module OpenC3
919
919
  # Only skip if key is explicitly set (different from item name) - this distinguishes
920
920
  # JsonAccessor (key="$.field") from TemplateAccessor (key=name="FIELD")
921
921
  next if item.key and item.key != item.name and @template and use_template
922
- write_item(item, item.default, :CONVERTED, buffer) unless skip_item_names and upcase_skip_item_names.include?(item.name)
922
+ next if skip_item_names and upcase_skip_item_names.include?(item.name)
923
+
924
+ begin
925
+ write_item(item, item.default, :CONVERTED, buffer)
926
+ rescue ArgumentError => e
927
+ # Name the item since the accessor error doesn't
928
+ raise ArgumentError, "#{@target_name} #{@packet_name} #{item.name} default value: #{e.message}", e.backtrace
929
+ end
923
930
  end
924
931
  end
925
932
  end
@@ -984,8 +991,14 @@ module OpenC3
984
991
  def disable_limits(name)
985
992
  item = get_item(name)
986
993
  item.limits.enabled = false
987
- unless item.limits.state == :STALE
988
- old_limits_state = item.limits.state
994
+ # Forget the previous values so change limits restart cleanly when re-enabled
995
+ item.limits.last_value = nil
996
+ item.limits.last_stored_value = nil
997
+ old_limits_state = item.limits.state
998
+ # Only report an actual change. Disabling an item that is already disabled
999
+ # (nil) must not fire the callback, because sync_system re-disables every
1000
+ # persisted-disabled item and each callback publishes a limits event.
1001
+ unless old_limits_state.nil?
989
1002
  item.limits.state = nil
990
1003
  @limits_change_callback.call(self, item, old_limits_state, nil, false) if @limits_change_callback
991
1004
  end
@@ -996,7 +1009,7 @@ module OpenC3
996
1009
  # This is an optimization so we don't have to iterate through all the items when
997
1010
  # checking for limits.
998
1011
  def update_limits_items_cache(item)
999
- if item.limits_values || item.state_colors
1012
+ if item.limits_values || item.limits_change_type || item.state_colors
1000
1013
  @limits_items ||= []
1001
1014
  @limits_items_hash ||= {}
1002
1015
  unless @limits_items_hash[item]
@@ -1060,6 +1073,8 @@ module OpenC3
1060
1073
  # Handle state monitoring and value monitoring differently
1061
1074
  if item.states
1062
1075
  handle_limits_states(item, value)
1076
+ elsif item.limits.change_type
1077
+ handle_limits_change(item, value, ignore_persistence)
1063
1078
  elsif item.limits.values
1064
1079
  handle_limits_values(item, value, limits_set, ignore_persistence)
1065
1080
  end
@@ -1512,6 +1527,62 @@ module OpenC3
1512
1527
  end
1513
1528
  end
1514
1529
 
1530
+ update_limits_state(item, value, limits_state, ignore_persistence)
1531
+ end
1532
+
1533
+ # Determine the limits state of an item with change limits (SEQUENCE,
1534
+ # CHANGE, or STATIC) by comparing the value to the previous value.
1535
+ # Stored packets are compared against the previous stored value so a
1536
+ # stored telemetry dump doesn't interleave with realtime values.
1537
+ def handle_limits_change(item, value, ignore_persistence)
1538
+ if @stored
1539
+ last_value = item.limits.last_stored_value
1540
+ item.limits.last_stored_value = value
1541
+ else
1542
+ last_value = item.limits.last_value
1543
+ item.limits.last_value = value
1544
+ end
1545
+
1546
+ if last_value.nil?
1547
+ # The first value has nothing to compare against so it is GREEN
1548
+ ok = true
1549
+ else
1550
+ case item.limits.change_type
1551
+ when :SEQUENCE
1552
+ ok = (value.is_a?(Numeric) && last_value.is_a?(Numeric) && value == next_sequence_value(item, last_value))
1553
+ when :CHANGE
1554
+ ok = (value != last_value)
1555
+ else # :STATIC
1556
+ ok = (value == last_value)
1557
+ end
1558
+ end
1559
+ limits_state = ok ? :GREEN : item.limits.change_color
1560
+
1561
+ update_limits_state(item, value, limits_state, ignore_persistence)
1562
+ end
1563
+
1564
+ # @return [Numeric] The value expected after last_value in a SEQUENCE.
1565
+ # Raw integer items roll over at the limit of their bit size.
1566
+ def next_sequence_value(item, last_value)
1567
+ next_value = last_value + item.limits.sequence_step
1568
+ if !item.read_conversion && item.bit_size > 0
1569
+ case item.data_type
1570
+ when :UINT
1571
+ return next_value % (2**item.bit_size)
1572
+ when :INT
1573
+ half = 2**(item.bit_size - 1)
1574
+ return ((next_value + half) % (2**item.bit_size)) - half
1575
+ else
1576
+ # FLOAT and DERIVED values don't roll over
1577
+ return next_value
1578
+ end
1579
+ end
1580
+ next_value
1581
+ end
1582
+
1583
+ # Update the item limits state, applying persistence, and call the
1584
+ # limits_change_callback if the state changes
1585
+ def update_limits_state(item, value, limits_state, ignore_persistence)
1515
1586
  if item.limits.state != limits_state # limits state has changed
1516
1587
  # Save old limits state for use in the callback
1517
1588
  old_limits_state = item.limits.state
@@ -27,6 +27,7 @@ require 'openc3/packets/parsers/processor_parser'
27
27
  require 'openc3/packets/parsers/xtce_parser'
28
28
  require 'openc3/packets/parsers/xtce_converter'
29
29
  require 'openc3/utilities/python_proxy'
30
+ require 'openc3/utilities/logger'
30
31
  require 'openc3/conversions'
31
32
  require 'openc3/processors'
32
33
  require 'openc3/accessors'
@@ -39,6 +40,20 @@ require 'tempfile'
39
40
  module OpenC3
40
41
  # Reads a command or telemetry configuration file and builds a hash of packets.
41
42
  class PacketConfig
43
+ # Limits groups become items in the <TARGET> LIMITS_GROUP packet of the target
44
+ # which defines them, named <GROUP> and <GROUP>_ENABLED
45
+ LIMITS_GROUP_PACKET = 'LIMITS_GROUP'.freeze
46
+ LIMITS_GROUP_ENABLED_SUFFIX = '_ENABLED'.freeze
47
+ # BLUE when every member is BLUE, DISABLED when limits are disabled on every member
48
+ LIMITS_GROUP_STATES = { 'GREEN' => 0, 'YELLOW' => 1, 'RED' => 2, 'BLUE' => 3, 'DISABLED' => 4 }.freeze
49
+ # PARTIAL when limits are enabled on some members but not all
50
+ LIMITS_GROUP_ENABLED_STATES = { 'FALSE' => 0, 'TRUE' => 1, 'PARTIAL' => 2 }.freeze
51
+ # QuestDB column name limit (TargetModel::MAX_COLUMN_HEADER_LENGTH) minus room for _ENABLED
52
+ MAX_LIMITS_GROUP_NAME_LENGTH = 127 - LIMITS_GROUP_ENABLED_SUFFIX.length
53
+ # Characters QuestDBClient.sanitize_column_name replaces with '_'. Group names
54
+ # can't contain them so every group has its own column (TEMP.A and TEMP_A would share one).
55
+ LIMITS_GROUP_COLUMN_INVALID_CHARS = /[?\.,'"\\\/:\)\(\+=\-\*\%~;!@#\$\^&]/
56
+
42
57
  # @return [String] The name of this configuration. To be used by higher
43
58
  # level classes to store information about the current PacketConfig.
44
59
  attr_accessor :name
@@ -51,9 +66,11 @@ module OpenC3
51
66
  # keyed by the packet name.
52
67
  attr_reader :commands
53
68
 
54
- # @return [Hash<String=>Array(String, String, String)>] Hash of all the
55
- # limits groups keyed by the group name. The value is a three element
56
- # array consisting of the target_name, packet_name, and item_name.
69
+ # @return [Hash<String=>Hash<String=>Array<Array(String, String, String)>>>]
70
+ # Limits groups keyed by the name of the target which defines them and then
71
+ # by the group name. Each member is a three element array consisting of the
72
+ # target_name, packet_name, and item_name. A nested group is the member
73
+ # <TARGET> LIMITS_GROUP <GROUP>.
57
74
  attr_reader :limits_groups
58
75
 
59
76
  # @return [Array<Symbol>] The defined limits sets for all items in the
@@ -207,6 +224,11 @@ module OpenC3
207
224
 
208
225
  when 'TELEMETRY'
209
226
  finish_packet()
227
+ # LIMITS_GROUP is generated from the target's limits groups (see build_limits_group_packet)
228
+ if params[1].to_s.upcase == LIMITS_GROUP_PACKET
229
+ raise parser.error("Telemetry packet name #{LIMITS_GROUP_PACKET} is reserved for the limits groups a target defines. Rename the packet.")
230
+ end
231
+
210
232
  @current_packet = PacketParser.parse_telemetry(parser, process_target_name, @telemetry, @latest_data, @warnings)
211
233
  @current_cmd_or_tlm = TELEMETRY
212
234
 
@@ -238,13 +260,48 @@ module OpenC3
238
260
  usage = "LIMITS_GROUP <GROUP NAME>"
239
261
  parser.verify_num_parameters(1, 1, usage)
240
262
  @current_limits_group = params[0].to_s.upcase
241
- @limits_groups[@current_limits_group] = [] unless @limits_groups.include?(@current_limits_group)
263
+ # The group becomes items in <TARGET> LIMITS_GROUP so a name which can't
264
+ # is skipped, along with its LIMITS_GROUP_ITEM and NESTED_LIMITS_GROUP lines
265
+ if (error = self.class.limits_group_name_error(@current_limits_group))
266
+ limits_group_warning(parser, "LIMITS_GROUP #{@current_limits_group} #{error}. Skipping the group.")
267
+ @current_limits_group = nil
268
+ else
269
+ (@limits_groups[process_target_name] ||= {})[@current_limits_group] ||= []
270
+ end
242
271
 
243
272
  # Add a telemetry item to the limits group
244
273
  when 'LIMITS_GROUP_ITEM'
245
274
  usage = "LIMITS_GROUP_ITEM <TARGET NAME> <PACKET NAME> <ITEM NAME>"
246
275
  parser.verify_num_parameters(3, 3, usage)
247
- @limits_groups[@current_limits_group] << [params[0].to_s.upcase, params[1].to_s.upcase, params[2].to_s.upcase] if @current_limits_group
276
+ if @current_limits_group
277
+ item = [params[0].to_s.upcase, params[1].to_s.upcase, params[2].to_s.upcase]
278
+ # Members in a LIMITS_GROUP packet are nested groups, added with NESTED_LIMITS_GROUP
279
+ if item[1] == LIMITS_GROUP_PACKET
280
+ limits_group_warning(parser, "LIMITS_GROUP #{@current_limits_group} member #{item.join(' ')}: use NESTED_LIMITS_GROUP #{item[0]} #{item[2]} to nest a limits group. Skipping the member.")
281
+ else
282
+ @limits_groups[process_target_name][@current_limits_group] << item
283
+ end
284
+ end
285
+
286
+ # Add a nested limits group to the limits group
287
+ when 'NESTED_LIMITS_GROUP'
288
+ usage = "NESTED_LIMITS_GROUP <GROUP NAME> or NESTED_LIMITS_GROUP <TARGET NAME> <GROUP NAME>"
289
+ parser.verify_num_parameters(1, 2, usage)
290
+ if @current_limits_group
291
+ # A nested group is stored as the item which carries its rollup. A
292
+ # group name alone is a group of this target.
293
+ if params.length == 1
294
+ item = [process_target_name, LIMITS_GROUP_PACKET, params[0].to_s.upcase]
295
+ else
296
+ item = [params[0].to_s.upcase, LIMITS_GROUP_PACKET, params[1].to_s.upcase]
297
+ end
298
+ # The name must be one LIMITS_GROUP accepts (the child can be installed later)
299
+ if (error = self.class.limits_group_name_error(item[2]))
300
+ limits_group_warning(parser, "LIMITS_GROUP #{@current_limits_group} member NESTED_LIMITS_GROUP #{item[0]} #{item[2]}: LIMITS_GROUP #{item[2]} #{error}. Skipping the member.")
301
+ else
302
+ @limits_groups[process_target_name][@current_limits_group] << item
303
+ end
304
+ end
248
305
 
249
306
  #######################################################################
250
307
  # All the following keywords must have a current packet defined
@@ -285,6 +342,68 @@ module OpenC3
285
342
  finish_packet()
286
343
  end
287
344
 
345
+ # Adds the <TARGET> LIMITS_GROUP packet for the limits groups the target defines.
346
+ # Call once all of the target's configuration files are processed.
347
+ #
348
+ # @param target_name [String] The target name
349
+ def build_limits_group_packet(target_name)
350
+ target_name = target_name.to_s.upcase
351
+ groups = @limits_groups[target_name]
352
+ return if groups.nil? or groups.empty?
353
+
354
+ (@telemetry[target_name] ||= {})[LIMITS_GROUP_PACKET] = self.class.generate_limits_group_packet(target_name, groups.keys)
355
+ end
356
+
357
+ # Group names become item names and QuestDB column names in <TARGET> LIMITS_GROUP.
358
+ # They follow the naming rules of LIMITS_GROUP parameters (see
359
+ # ConfigParser#verify_parameter_naming) plus the rules of the generated items.
360
+ #
361
+ # @param group_name [String] Upper case group name
362
+ # @return [String, nil] Why the name can't be a limits group, nil if it can
363
+ def self.limits_group_name_error(group_name)
364
+ return "cannot end with an underscore ('_')" if group_name.end_with?('_')
365
+ return "cannot contain a double underscore ('__')" if group_name.include?('__')
366
+ return "cannot contain double brackets ('[[' or ']]')" if group_name.include?('[[') or group_name.include?(']]')
367
+ return "cannot contain a space (' ')" if group_name.include?(' ')
368
+ return "cannot contain a quote (' or \")" if group_name.include?('"') or group_name.include?("'")
369
+ return "cannot contain a curly bracket ('{' or '}')" if group_name.include?('{') or group_name.include?('}')
370
+ return "must be #{MAX_LIMITS_GROUP_NAME_LENGTH} characters or less" if group_name.length > MAX_LIMITS_GROUP_NAME_LENGTH
371
+ if (char = group_name[LIMITS_GROUP_COLUMN_INVALID_CHARS])
372
+ return "cannot contain '#{char}' since the time-series database replaces it with an underscore"
373
+ end
374
+ return "cannot end with #{LIMITS_GROUP_ENABLED_SUFFIX}" if group_name.end_with?(LIMITS_GROUP_ENABLED_SUFFIX)
375
+ return "is a reserved item name" if Packet::RESERVED_ITEM_NAMES.include?(group_name)
376
+ # Packet#packet_time reads an item named PACKET_TIME instead of the received time
377
+ return "would replace the packet time of #{LIMITS_GROUP_PACKET}" if group_name == 'PACKET_TIME'
378
+
379
+ nil
380
+ end
381
+
382
+ # Builds the <TARGET> LIMITS_GROUP packet with a <GROUP> and <GROUP>_ENABLED
383
+ # item for each group, sorted by group name. The packet is virtual so it's never
384
+ # used to identify received data (the limits group microservice publishes it)
385
+ # but unlike a VIRTUAL packet it stays visible and enabled in the tools.
386
+ #
387
+ # @param target_name [String] Target which defines the groups
388
+ # @param group_names [Array<String>] Limits group names
389
+ # @return [Packet]
390
+ def self.generate_limits_group_packet(target_name, group_names)
391
+ packet = Packet.new(target_name, LIMITS_GROUP_PACKET, :BIG_ENDIAN, "Rolled-up limits state of each limits group defined by #{target_name}")
392
+ packet.virtual = true
393
+ packet.hidden = false
394
+ packet.disabled = false
395
+ packet.define_reserved_items()
396
+ group_names.sort.each do |group|
397
+ item = packet.append_item(group, 8, :UINT)
398
+ item.states = LIMITS_GROUP_STATES.dup
399
+ item.description = "Most severe limits state of the enabled members, or DISABLED when none are, of limits group #{group}"
400
+ item = packet.append_item("#{group}#{LIMITS_GROUP_ENABLED_SUFFIX}", 8, :UINT)
401
+ item.states = LIMITS_GROUP_ENABLED_STATES.dup
402
+ item.description = "Whether limits are enabled on all (TRUE), none (FALSE) or some (PARTIAL) of the members of limits group #{group}"
403
+ end
404
+ packet
405
+ end
406
+
288
407
  # Convert the PacketConfig back to OpenC3 configuration files for each target
289
408
  def to_config(output_dir)
290
409
  FileUtils.mkdir_p(output_dir)
@@ -299,7 +418,10 @@ module OpenC3
299
418
  rescue
300
419
  # Doesn't exist
301
420
  end
302
- packets.each do |_packet_name, packet|
421
+ packets.each do |packet_name, packet|
422
+ # The limits group packet is generated from the limits groups written below
423
+ next if packet_name == LIMITS_GROUP_PACKET
424
+
303
425
  File.open(filename, 'a') do |file|
304
426
  file.puts packet.to_config(:TELEMETRY)
305
427
  file.puts ""
@@ -325,15 +447,25 @@ module OpenC3
325
447
  end
326
448
  end
327
449
 
328
- # Put limits groups into SYSTEM target
329
- if @limits_groups.length > 0
330
- FileUtils.mkdir_p(File.join(output_dir, 'SYSTEM', 'cmd_tlm'))
331
- filename = File.join(output_dir, 'SYSTEM', 'cmd_tlm', 'limits_groups.txt')
450
+ # Limits groups go into the target which defines them
451
+ @limits_groups.each do |target_name, groups|
452
+ next if groups.empty?
453
+
454
+ FileUtils.mkdir_p(File.join(output_dir, target_name, 'cmd_tlm'))
455
+ filename = File.join(output_dir, target_name, 'cmd_tlm', 'limits_groups.txt')
332
456
  File.open(filename, 'w') do |file|
333
- @limits_groups.each do |limits_group_name, limits_group_items|
457
+ groups.each do |limits_group_name, limits_group_items|
334
458
  file.puts "LIMITS_GROUP #{limits_group_name.to_s.quote_if_necessary}"
335
- limits_group_items.each do |target_name, packet_name, item_name|
336
- file.puts " LIMITS_GROUP_ITEM #{target_name.to_s.quote_if_necessary} #{packet_name.to_s.quote_if_necessary} #{item_name.to_s.quote_if_necessary}"
459
+ limits_group_items.each do |item_target_name, packet_name, item_name|
460
+ if packet_name == LIMITS_GROUP_PACKET
461
+ if item_target_name == target_name
462
+ file.puts " NESTED_LIMITS_GROUP #{item_name.to_s.quote_if_necessary}"
463
+ else
464
+ file.puts " NESTED_LIMITS_GROUP #{item_target_name.to_s.quote_if_necessary} #{item_name.to_s.quote_if_necessary}"
465
+ end
466
+ else
467
+ file.puts " LIMITS_GROUP_ITEM #{item_target_name.to_s.quote_if_necessary} #{packet_name.to_s.quote_if_necessary} #{item_name.to_s.quote_if_necessary}"
468
+ end
337
469
  end
338
470
  file.puts ""
339
471
  end
@@ -947,5 +1079,12 @@ module OpenC3
947
1079
  @current_item = nil
948
1080
  end
949
1081
  end
1082
+
1083
+ # Skipping a limits group isn't fatal, but log it since parser warnings aren't shown on install
1084
+ def limits_group_warning(parser, message)
1085
+ message = "#{message} (#{parser.filename}:#{parser.line_number})"
1086
+ @warnings << message
1087
+ Logger.warn(message)
1088
+ end
950
1089
  end
951
1090
  end