Files
secs-gem/include/secsgem/gem/store/equipment_constants.hpp
T
raphael 711ee1b40f #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>
2026-06-02 09:51:54 +02:00

104 lines
3.7 KiB
C++

#pragma once
#include <cstdint>
#include <map>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "secsgem/secs2/item.hpp"
namespace secsgem::gem {
namespace s2 = secsgem::secs2;
struct EquipmentConstant {
uint32_t id;
std::string name;
std::string units;
s2::Item value;
s2::Item def_value;
std::string min_str; // bounds for S2F30 + EAC validation; "" disables check
std::string max_str;
};
enum class EquipmentAck : uint8_t {
Accept = 0,
Denied_UnknownEcid = 1,
Denied_Busy = 3,
Denied_OutOfRange = 4,
};
class EquipmentConstantStore {
public:
void add(EquipmentConstant ec) { by_id_.insert_or_assign(ec.id, std::move(ec)); }
std::optional<EquipmentConstant> get(uint32_t id) const {
auto it = by_id_.find(id);
if (it == by_id_.end()) return std::nullopt;
return it->second;
}
std::vector<EquipmentConstant> all() const {
std::vector<EquipmentConstant> out;
out.reserve(by_id_.size());
for (const auto& [_, ec] : by_id_) out.push_back(ec);
return out;
}
bool has(uint32_t id) const { return by_id_.count(id) > 0; }
// Validates against min/max for numeric formats (only when min_str and
// max_str parse cleanly). Returns Denied_UnknownEcid / Denied_OutOfRange
// per S2F16 EAC. This is the rule that closes the COMPLIANCE.md gap
// about "EC range validation against min/max".
EquipmentAck set_value(uint32_t id, s2::Item value) {
auto it = by_id_.find(id);
if (it == by_id_.end()) return EquipmentAck::Denied_UnknownEcid;
if (!in_range(it->second, value)) return EquipmentAck::Denied_OutOfRange;
it->second.value = std::move(value);
return EquipmentAck::Accept;
}
private:
// For numeric formats, parse min_str / max_str as integers / doubles and
// compare against the first value of the array (we don't support setting
// multi-element ECs in this implementation).
static bool in_range(const EquipmentConstant& ec, const s2::Item& value) {
if (ec.min_str.empty() && ec.max_str.empty()) return true;
auto parse_d = [](const std::string& s, double& out) {
if (s.empty()) return false;
try { out = std::stod(s); return true; } catch (...) { return false; }
};
double v;
if (!extract_number(value, v)) return true; // unknown format: skip
double lo = -1e300, hi = 1e300;
parse_d(ec.min_str, lo);
parse_d(ec.max_str, hi);
return v >= lo && v <= hi;
}
static bool extract_number(const s2::Item& item, double& out) {
switch (item.format()) {
case s2::Format::U1: out = std::get<std::vector<uint8_t>>(item.storage()).front(); return true;
case s2::Format::U2: out = std::get<std::vector<uint16_t>>(item.storage()).front(); return true;
case s2::Format::U4: out = std::get<std::vector<uint32_t>>(item.storage()).front(); return true;
case s2::Format::U8: out = static_cast<double>(std::get<std::vector<uint64_t>>(item.storage()).front()); return true;
case s2::Format::I1: out = std::get<std::vector<int8_t>>(item.storage()).front(); return true;
case s2::Format::I2: out = std::get<std::vector<int16_t>>(item.storage()).front(); return true;
case s2::Format::I4: out = std::get<std::vector<int32_t>>(item.storage()).front(); return true;
case s2::Format::I8: out = static_cast<double>(std::get<std::vector<int64_t>>(item.storage()).front()); return true;
case s2::Format::F4: out = std::get<std::vector<float>>(item.storage()).front(); return true;
case s2::Format::F8: out = std::get<std::vector<double>>(item.storage()).front(); return true;
default: return false;
}
}
std::map<uint32_t, EquipmentConstant> by_id_;
};
} // namespace secsgem::gem