#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
+1 -1
View File
@@ -222,7 +222,7 @@ int main(int argc, char** argv) {
// 9. Host command START -> equipment fires CEID kCeidProcessStarted.
seq->steps.push_back([conn, logfn, fail, pause_then](auto next) {
std::vector<gem::EquipmentDataModel::CommandParam> params = {
std::vector<gem::CommandParameter> params = {
{"LOTID", s2::Item::ascii("LOT-42")},
{"PPID", s2::Item::ascii("RECIPE-A")},
};
+26 -26
View File
@@ -36,8 +36,8 @@ constexpr uint32_t kSvidControlState = 1;
constexpr uint32_t kSvidClock = 2;
void refresh(gem::EquipmentDataModel& m, const gem::ControlStateMachine& sm) {
m.set_status_value(kSvidControlState, s2::Item::ascii(gem::control_state_name(sm.state())));
m.set_status_value(kSvidClock, s2::Item::ascii(m.current_time_string()));
m.svids.set_value(kSvidControlState, s2::Item::ascii(gem::control_state_name(sm.state())));
m.svids.set_value(kSvidClock, s2::Item::ascii(m.clock.current_time_string()));
}
} // namespace
@@ -59,10 +59,10 @@ int main(int argc, char** argv) {
std::cerr << "[equip] config error: " << e.what() << std::endl;
return 1;
}
logfn("loaded " + std::to_string(model->all_status_variables().size()) + " SVIDs, " +
std::to_string(model->all_equipment_constants().size()) + " ECIDs, " +
std::to_string(model->all_events().size()) + " CEIDs, " +
std::to_string(model->all_alarms().size()) + " alarms");
logfn("loaded " + std::to_string(model->svids.size()) + " SVIDs, " +
std::to_string(model->ecids.all().size()) + " ECIDs, " +
std::to_string(model->events.all_events().size()) + " CEIDs, " +
std::to_string(model->alarms.all().size()) + " alarms");
auto sm = std::make_shared<gem::ControlStateMachine>(sm_cfg.table, sm_cfg.initial);
@@ -93,11 +93,11 @@ int main(int argc, char** argv) {
asio::post(io, [active_conn, model, logfn, emit_event, alid]() {
auto conn = active_conn->lock();
if (!conn) return;
auto alarm = model->alarm(alid);
auto alarm = model->alarms.get(alid);
if (!alarm) return;
auto alcd = model->alarm_set(alid);
auto alcd = model->alarms.set_active(alid);
if (!alcd) return;
if (model->alarm_enabled(alid)) {
if (model->alarms.enabled(alid)) {
logfn("emit S5F1 alarm set ALID=" + std::to_string(alid));
conn->send_request(gem::s5f1_alarm_report(*alcd, alid, alarm->text),
[](std::error_code, const s2::Message&) {});
@@ -128,10 +128,10 @@ int main(int argc, char** argv) {
if (!svids) return s2::Message(1, 0, false);
std::vector<std::optional<s2::Item>> values;
if (svids->empty()) {
for (const auto& sv : model->all_status_variables()) values.push_back(sv.value);
for (const auto& sv : model->svids.all()) values.push_back(sv.value);
} else {
for (auto id : *svids) {
auto sv = model->status_variable(id);
auto sv = model->svids.get(id);
values.push_back(sv ? std::optional<s2::Item>(sv->value) : std::nullopt);
}
}
@@ -140,7 +140,7 @@ int main(int argc, char** argv) {
});
router.on(1, 11, [model, logfn](const s2::Message&) {
std::vector<gem::StatusName> rows;
for (const auto& sv : model->all_status_variables())
for (const auto& sv : model->svids.all())
rows.push_back({sv.id, sv.name, sv.units});
logfn("S1F11 -> S1F12 (namelist, " + std::to_string(rows.size()) + ")");
return gem::s1f12_status_namelist_data(rows);
@@ -166,7 +166,7 @@ int main(int argc, char** argv) {
if (!ids) return s2::Message(2, 0, false);
std::vector<s2::Item> values;
for (auto id : *ids) {
auto ec = model->equipment_constant(id);
auto ec = model->ecids.get(id);
values.push_back(ec ? ec->value : s2::Item::list({}));
}
logfn("S2F13 -> S2F14 (" + std::to_string(values.size()) + " values)");
@@ -178,7 +178,7 @@ int main(int argc, char** argv) {
if (!sets) eac = gem::EquipmentAck::Denied_OutOfRange;
else
for (const auto& s : *sets) {
auto r = model->set_equipment_constant_value(s.ecid, s.value);
auto r = model->ecids.set_value(s.ecid, s.value);
if (r != gem::EquipmentAck::Accept) eac = r;
}
logfn("S2F15 -> S2F16 EAC=" + std::to_string(static_cast<int>(eac)));
@@ -186,15 +186,15 @@ int main(int argc, char** argv) {
});
router.on(2, 17, [model, logfn](const s2::Message&) {
logfn("S2F17 -> S2F18 (clock)");
return gem::s2f18_date_time_data(model->current_time_string());
return gem::s2f18_date_time_data(model->clock.current_time_string());
});
router.on(2, 29, [model, logfn](const s2::Message& msg) {
auto ids = gem::parse_u4_list_body(msg);
std::vector<gem::EquipmentConstant> ecs;
if (ids && ids->empty()) ecs = model->all_equipment_constants();
if (ids && ids->empty()) ecs = model->ecids.all();
else if (ids)
for (auto id : *ids) {
auto ec = model->equipment_constant(id);
auto ec = model->ecids.get(id);
if (ec) ecs.push_back(*ec);
}
std::vector<gem::EcNameRow> rows;
@@ -205,7 +205,7 @@ int main(int argc, char** argv) {
});
router.on(2, 31, [model, logfn](const s2::Message& msg) {
auto t = gem::parse_s2f31(msg);
auto ack = t ? model->set_time_string(*t) : gem::TimeAck::Error;
auto ack = t ? model->clock.set_time_string(*t) : gem::TimeAck::Error;
logfn("S2F31 -> S2F32 TIACK=" + std::to_string(static_cast<int>(ack)));
return gem::s2f32_date_time_ack(ack);
});
@@ -244,7 +244,7 @@ int main(int argc, char** argv) {
router.on(2, 41, [model, logfn, emit_event, emit_alarm_set](const s2::Message& msg) {
auto cmd = gem::parse_s2f41(msg);
if (!cmd) return gem::s2f42_host_command_ack(gem::HostCmdAck::ParameterInvalid, {});
auto result = model->dispatch_command(cmd->rcmd, cmd->params);
auto result = model->commands.dispatch(cmd->rcmd, cmd->params);
logfn("S2F41 RCMD=" + cmd->rcmd + " -> S2F42 HCACK=" +
std::to_string(static_cast<int>(result.ack)));
if (result.ack == gem::HostCmdAck::Accept) {
@@ -256,7 +256,7 @@ int main(int argc, char** argv) {
router.on(5, 3, [model, logfn](const s2::Message& msg) {
auto req = gem::parse_s5f3(msg);
auto ack = req ? model->set_alarm_enabled(req->alid, (req->aled & 0x80) != 0)
auto ack = req ? model->alarms.set_enabled(req->alid, (req->aled & 0x80) != 0)
: gem::AlarmAck::Error;
logfn(std::string("S5F3 -> S5F4 ACKC5=") + std::to_string(static_cast<int>(ack)));
return gem::s5f4_enable_alarm_ack(ack);
@@ -264,33 +264,33 @@ int main(int argc, char** argv) {
router.on(5, 5, [model, logfn](const s2::Message& msg) {
auto ids = gem::parse_u4_list_body(msg);
std::vector<gem::Alarm> alarms;
if (ids && ids->empty()) alarms = model->all_alarms();
if (ids && ids->empty()) alarms = model->alarms.all();
else if (ids)
for (auto id : *ids) {
auto a = model->alarm(id);
auto a = model->alarms.get(id);
if (a) alarms.push_back(*a);
}
logfn("S5F5 -> S5F6 (" + std::to_string(alarms.size()) + " alarms)");
return gem::s5f6_list_alarms_data(
alarms, [model](uint32_t id) { return model->alarm_active(id); });
alarms, [model](uint32_t id) { return model->alarms.active(id); });
});
router.on(7, 3, [model, logfn](const s2::Message& msg) {
auto pp = gem::parse_s7f3(msg);
if (!pp) return gem::s7f4_process_program_ack(gem::ProcessProgramAck::LengthError);
model->add_process_program(pp->ppid, pp->ppbody);
model->recipes.add(pp->ppid, pp->ppbody);
logfn("S7F3 PPID=" + pp->ppid + " -> S7F4 (Accept)");
return gem::s7f4_process_program_ack(gem::ProcessProgramAck::Accept);
});
router.on(7, 5, [model, logfn](const s2::Message& msg) {
auto ppid = gem::parse_s7f5(msg);
if (!ppid) return gem::s7f6_process_program_data("", "");
auto body = model->process_program(*ppid);
auto body = model->recipes.get(*ppid);
logfn("S7F5 PPID=" + *ppid + " -> S7F6");
return gem::s7f6_process_program_data(*ppid, body ? *body : "");
});
router.on(7, 19, [model, logfn](const s2::Message&) {
auto list = model->process_program_list();
auto list = model->recipes.list();
logfn("S7F19 -> S7F20 (" + std::to_string(list.size()) + " PPIDs)");
return gem::s7f20_current_eppd_data(list);
});