#4 Split EquipmentDataModel into focused stores

The god-class is gone.  Each capability is now its own focused store:
StatusVariableStore, DataVariableStore, EquipmentConstantStore (with EAC
range validation), EventReportSubscriptions, AlarmRegistry, RecipeStore,
Clock, HostCommandRegistry.  Each is independently testable.

EquipmentDataModel becomes a small composite that holds one of each store
as a public member, plus three convenience methods (vid_value, vid_exists,
compose_reports_for) that span SVIDs+DVIDs and inject the right callbacks
into the EventReportSubscriptions.

New under include/secsgem/gem/store/:

  status_variables.hpp   StatusVariable, StatusVariableStore,
                         DataVariable, DataVariableStore
  equipment_constants.hpp EquipmentConstant, EquipmentConstantStore,
                          EquipmentAck. set_value() now validates
                          numeric values against min_str/max_str and
                          returns EAC=4 on out-of-range — closes the
                          COMPLIANCE.md gap about EC range validation.
  event_reports.hpp      CollectionEvent, Report, ReportData,
                         EventReportSubscriptions + DefineReportAck,
                         LinkEventAck, EnableEventAck. The store is
                         pure data; VidLookup / VidExists callbacks
                         are injected at define / emit time so the
                         service doesn't back-reference the SVID
                         store.
  alarms.hpp             Alarm, AlarmAck, AlarmRegistry.
                         Encapsulates the (enabled, active) sets and
                         ALCD byte computation.
  recipes.hpp            ProcessProgramAck, RecipeStore.
  clock.hpp              TimeAck, Clock. set_time_string applies an
                         offset so subsequent reads reflect the host
                         time without mutating system clock.
  host_commands.hpp      HostCmdAck, CommandParameter,
                         HostCommandRegistry with Spec/Result types.

include/secsgem/gem/data_model.hpp shrinks to a 50-line composite:

  struct EquipmentDataModel {
    StatusVariableStore       svids;
    DataVariableStore         dvids;
    EquipmentConstantStore    ecids;
    EventReportSubscriptions  events;
    AlarmRegistry             alarms;
    RecipeStore               recipes;
    Clock                     clock;
    HostCommandRegistry       commands;
    /* + vid_value, vid_exists, compose_reports_for sugar */
  };

src/gem/data_model.cpp is gone — every store is inline header-only.

include/secsgem/gem/messages_helpers.hpp picks up EventReportAck and
TerminalAck (S6F12 / S10F2-F4 ack enums that aren't tied to any one
store).

Call-site updates:

  apps/secs_server.cpp   model->status_variable(id) -> model->svids.get(id),
                         model->equipment_constant(id) -> model->ecids.get(id),
                         model->alarm_set(id) -> model->alarms.set_active(id),
                         model->dispatch_command(...) -> model->commands.dispatch(...),
                         and similar across every handler.  Plus
                         model->current_time_string() -> model->clock....

  src/config/loader.cpp  model.add_status_variable(sv) -> model.svids.add(sv),
                         and similar.  HostCommandRegistry::Spec replaces
                         EquipmentDataModel::CommandSpec.

  apps/secs_client.cpp   std::vector<EquipmentDataModel::CommandParam> ->
                         std::vector<CommandParameter>.

  tests/test_data_model.cpp  Rewritten around the individual stores;
                         each gets its own TEST_CASE block.  Adds three
                         new cases covering EC range validation (in
                         range / out of range / non-numeric skipped).

  tests/test_loader.cpp  m.has_event(100) -> m.events.has_event(100),
                         etc.

Verified:

  - Tests: 69 cases / 370 assertions pass (was 67 / 384; -14 stale
    composite-API assertions + 16 new store-level assertions covering
    EC range validation and the per-store add/get/list/delete paths).
  - Demo: byte-identical behaviour across the full 17-step flow.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-02 09:51:54 +02:00
parent 29db1caedb
commit 711ee1b40f
16 changed files with 877 additions and 770 deletions
+45 -271
View File
@@ -1,283 +1,57 @@
#pragma once
#include <algorithm>
#include <cstdint>
#include <functional>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "secsgem/secs2/item.hpp"
#include "secsgem/gem/store/alarms.hpp"
#include "secsgem/gem/store/clock.hpp"
#include "secsgem/gem/store/equipment_constants.hpp"
#include "secsgem/gem/store/event_reports.hpp"
#include "secsgem/gem/store/host_commands.hpp"
#include "secsgem/gem/store/recipes.hpp"
#include "secsgem/gem/store/status_variables.hpp"
namespace secsgem::gem {
namespace s2 = secsgem::secs2;
// Composite over the seven focused stores. Each store is independently
// testable and independently usable — the application can keep a reference
// to just `alarms` and ignore the rest if that's all it needs. Variable
// lookups span SVIDs and DVIDs through this composite (the host's view
// per E30 §6.11).
struct EquipmentDataModel {
StatusVariableStore svids;
DataVariableStore dvids;
EquipmentConstantStore ecids;
EventReportSubscriptions events;
AlarmRegistry alarms;
RecipeStore recipes;
Clock clock;
HostCommandRegistry commands;
// ---- Status / data / equipment-constant variables ------------------------
// Convenience: VID -> value lookup spanning SVIDs and DVIDs.
std::optional<s2::Item> vid_value(uint32_t vid) const {
if (auto v = svids.value(vid)) return v;
if (auto v = dvids.value(vid)) return v;
return std::nullopt;
}
bool vid_exists(uint32_t vid) const {
return svids.has(vid) || dvids.has(vid);
}
struct StatusVariable {
uint32_t id;
std::string name;
std::string units;
s2::Item value;
};
struct DataVariable {
uint32_t id;
std::string name;
std::string units;
s2::Item value;
};
struct EquipmentConstant {
uint32_t id;
std::string name;
std::string units;
s2::Item value;
s2::Item def_value;
std::string min_str; // optional, ASCII for S2F30 ECMIN/ECMAX
std::string max_str;
};
// ---- Event reports -------------------------------------------------------
struct CollectionEvent {
uint32_t id;
std::string name;
};
// One <CPNAME, CPVAL> entry on S2F41. Defined here (not in the generated
// messages.hpp) so the data model can name it in its public API; the codegen
// references it as an external struct.
struct CommandParameter {
std::string name;
s2::Item value;
};
struct Report {
uint32_t id;
std::vector<uint32_t> vids;
};
// One report's worth of data at emission time: the RPTID and the values for
// each VID, in declaration order.
struct ReportData {
uint32_t rptid;
std::vector<s2::Item> values;
};
// ---- Alarms --------------------------------------------------------------
struct Alarm {
uint32_t id;
std::string text;
// Lower 7 bits of ALCD: alarm severity category (1=personal safety,
// 2=equipment safety, 4=parameter control error, ...). Bit 7 is the
// set/cleared flag, applied at emit time.
uint8_t severity_category;
};
// ---- Ack codes for the new SxFy ------------------------------------------
// S2F30 carries no ack; S2F34 / S2F36 / S2F38 do.
enum class DefineReportAck : uint8_t {
Accept = 0,
InsufficientSpace = 1,
InvalidFormat = 2,
RptidAlreadyDefined = 3,
InvalidVid = 5,
};
enum class LinkEventAck : uint8_t {
Accept = 0,
InsufficientSpace = 1,
InvalidFormat = 2,
UnknownCeid = 3,
UnknownRptid = 4,
CeidAlreadyLinked = 5,
};
enum class EnableEventAck : uint8_t {
Accept = 0,
UnknownCeid = 1,
};
// S5F2 / S5F4 alarm ack.
enum class AlarmAck : uint8_t {
Accept = 0,
Error = 1,
};
// S6F12 event-report ack.
enum class EventReportAck : uint8_t {
Accept = 0,
Denied = 1,
};
// S7F4 process-program ack.
enum class ProcessProgramAck : uint8_t {
Accept = 0,
PermissionNotGranted = 1,
LengthError = 2,
MatrixOverflow = 3,
PpidNotFound = 4,
ModeUnsupported = 5,
PerformanceError = 6,
};
// ---- Existing ack enums from earlier batches -----------------------------
enum class EquipmentAck : uint8_t {
Accept = 0,
Denied_UnknownEcid = 1,
Denied_Busy = 3,
Denied_OutOfRange = 4,
};
enum class TimeAck : uint8_t {
Accept = 0,
Error = 1,
NotDoneNotEmpty = 2,
};
enum class HostCmdAck : uint8_t {
Accept = 0,
InvalidCommand = 1,
CannotDoNow = 2,
ParameterInvalid = 3,
AcceptedWillFinishLater = 4,
Rejected = 5,
InvalidObject = 6,
};
enum class TerminalAck : uint8_t {
Accepted = 0,
WillNotDisplay = 1,
TerminalNotAvailable = 2,
};
// The in-memory equipment data dictionary. Owns SVIDs, DVIDs, ECIDs, the
// dynamic event-report subscription state (reports + links + enabled set),
// the alarm table, and a small process-program registry. Single-threaded;
// all access happens on the Asio executor.
class EquipmentDataModel {
public:
using CommandParam = CommandParameter; // back-compat alias
// Declarative host-command effect, loaded from YAML. The server
// dispatches a command by looking up the spec and (optionally) firing
// a CEID emit / setting an alarm after the S2F42 reply is sent.
struct CommandSpec {
HostCmdAck ack = HostCmdAck::Accept;
std::optional<uint32_t> emit_ceid;
std::optional<uint32_t> set_alarm;
};
struct CommandResult {
HostCmdAck ack = HostCmdAck::InvalidCommand;
std::optional<uint32_t> emit_ceid;
std::optional<uint32_t> set_alarm;
};
// --- SVID ---------------------------------------------------------------
void add_status_variable(StatusVariable sv);
std::optional<StatusVariable> status_variable(uint32_t id) const;
std::vector<StatusVariable> all_status_variables() const;
void set_status_value(uint32_t id, s2::Item value);
// --- DVID ---------------------------------------------------------------
void add_data_variable(DataVariable dv);
std::optional<DataVariable> data_variable(uint32_t id) const;
std::vector<DataVariable> all_data_variables() const;
void set_data_value(uint32_t id, s2::Item value);
// VID lookup that searches SVIDs then DVIDs (E30 §6.5 — VIDs share a
// namespace from the host's point of view).
std::optional<s2::Item> vid_value(uint32_t vid) const;
bool vid_exists(uint32_t vid) const;
// --- ECID ---------------------------------------------------------------
void add_equipment_constant(EquipmentConstant ec);
std::optional<EquipmentConstant> equipment_constant(uint32_t id) const;
std::vector<EquipmentConstant> all_equipment_constants() const;
EquipmentAck set_equipment_constant_value(uint32_t id, s2::Item value);
// --- Clock --------------------------------------------------------------
std::string current_time_string() const;
TimeAck set_time_string(const std::string& time_str);
// --- Host commands ------------------------------------------------------
void register_command(const std::string& rcmd, CommandSpec spec);
CommandResult dispatch_command(const std::string& rcmd,
const std::vector<CommandParam>& params) const;
bool has_command(const std::string& rcmd) const;
// --- Collection events --------------------------------------------------
void register_event(CollectionEvent ce);
bool has_event(uint32_t ceid) const;
std::vector<CollectionEvent> all_events() const;
// S2F33: define reports. `reports` maps RPTID -> VID list. An empty
// vector deletes all reports (and clears all links). An empty VID list
// for a given RPTID deletes that report.
// Sugar that adapts the EventReportSubscriptions API to the host-supplied
// VID list shape (matches what parse_s2f33 / parse_s2f35 yield).
DefineReportAck define_reports(
const std::vector<std::pair<uint32_t, std::vector<uint32_t>>>& reports);
// S2F35: link CEIDs to RPTID lists. `links` maps CEID -> RPTID list.
// An empty RPTID list for a CEID clears its links. An empty outer list
// clears all links.
const std::vector<std::pair<uint32_t, std::vector<uint32_t>>>& rows) {
return events.define_reports(rows, [this](uint32_t vid) { return vid_exists(vid); });
}
LinkEventAck link_event_reports(
const std::vector<std::pair<uint32_t, std::vector<uint32_t>>>& links);
// S2F37: enable/disable events. Empty CEID list applies to *all*
// registered events.
EnableEventAck enable_events(bool enable, const std::vector<uint32_t>& ceids);
bool is_event_enabled(uint32_t ceid) const;
// Compose the report data for a CEID emission (S6F11 body's report list).
std::vector<ReportData> compose_reports_for(uint32_t ceid) const;
std::vector<Report> all_reports() const;
// --- Alarms -------------------------------------------------------------
void add_alarm(Alarm a);
std::optional<Alarm> alarm(uint32_t alid) const;
std::vector<Alarm> all_alarms() const;
AlarmAck set_alarm_enabled(uint32_t alid, bool enable);
bool alarm_enabled(uint32_t alid) const;
// Returns the ALCD byte to put on the wire (bit 7 indicates set/cleared).
// Returns nullopt for unknown alarms.
std::optional<uint8_t> alarm_set(uint32_t alid);
std::optional<uint8_t> alarm_clear(uint32_t alid);
bool alarm_active(uint32_t alid) const;
// --- Process programs ---------------------------------------------------
void add_process_program(std::string ppid, std::string ppbody);
std::optional<std::string> process_program(const std::string& ppid) const;
std::vector<std::string> process_program_list() const;
ProcessProgramAck delete_process_program(const std::string& ppid);
private:
std::map<uint32_t, StatusVariable> svids_;
std::map<uint32_t, DataVariable> dvids_;
std::map<uint32_t, EquipmentConstant> ecids_;
std::int64_t time_offset_seconds_ = 0;
std::map<std::string, CommandSpec> commands_;
std::map<uint32_t, CollectionEvent> ceids_;
std::map<uint32_t, Report> reports_;
std::map<uint32_t, std::vector<uint32_t>> ce_links_; // CEID -> RPTID list
std::set<uint32_t> events_enabled_;
std::map<uint32_t, Alarm> alarms_;
std::set<uint32_t> alarms_enabled_;
std::set<uint32_t> alarms_active_;
std::map<std::string, std::string> process_programs_;
const std::vector<std::pair<uint32_t, std::vector<uint32_t>>>& rows) {
return events.link_event_reports(rows);
}
EnableEventAck enable_events(bool enable, const std::vector<uint32_t>& ceids) {
return events.enable_events(enable, ceids);
}
bool is_event_enabled(uint32_t ceid) const { return events.is_enabled(ceid); }
std::vector<ReportData> compose_reports_for(uint32_t ceid) const {
return events.compose_for(ceid, [this](uint32_t vid) { return vid_value(vid); });
}
};
} // namespace secsgem::gem