Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
parameter_cache.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <expected>
5#include <filesystem>
6#include <nlohmann/json_fwd.hpp>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <vector>
11
13
14namespace mm::node {
15
23inline constexpr uint32_t kSynapticonVendorId = 0x000022D2;
24
30 false;
31 std::string directory;
32 bool enabled = true;
33};
34
56 public:
60 static constexpr int kFormatVersion = 1;
61
62 ParameterCache() = default;
63 explicit ParameterCache(ParameterCacheConfig config);
64
67
72 bool enabledForVendor(uint32_t vendorId) const;
73
82 std::optional<std::vector<DeviceParameter>> load(uint32_t vendorId, uint32_t productCode,
83 uint32_t revisionNumber) const;
84
90 void store(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber,
91 const std::vector<DeviceParameter>& parameters) const;
92
94 struct CacheEntry {
95 std::string id;
96 uint32_t vendorId = 0;
97 uint32_t productCode = 0;
98 uint32_t revisionNumber = 0;
99 uint32_t parameterCount = 0;
100 std::uintmax_t sizeBytes = 0;
101 };
102
106 static std::string makeId(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber);
107
114 std::vector<CacheEntry> list() const;
115
122 std::expected<std::vector<uint8_t>, std::string> readRaw(std::string_view id) const;
123
126 std::expected<void, std::string> remove(std::string_view id) const;
127
128 private:
132 std::filesystem::path resolveDir() const;
133
136 std::filesystem::path pathFor(uint32_t vendorId, uint32_t productCode,
137 uint32_t revisionNumber) const;
138
139 ParameterCacheConfig config_;
140};
141
144void to_json(nlohmann::json& j, const ParameterCache::CacheEntry& e);
145
146} // namespace mm::node
On-disk cache of CoE parameter definitions, keyed by device identity.
Definition parameter_cache.h:55
bool enabledForVendor(uint32_t vendorId) const
Whether the cache is active for vendorId under the current policy.
Definition parameter_cache.cc:137
static constexpr int kFormatVersion
Bumped when the on-disk field set / meaning changes in a way that invalidates older files....
Definition parameter_cache.h:60
void store(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber, const std::vector< DeviceParameter > &parameters) const
Persists parameter definitions for an identity (atomic temp-file + rename).
Definition parameter_cache.cc:218
std::expected< void, std::string > remove(std::string_view id) const
Deletes one cache file addressed by its id. Policy-independent.
Definition parameter_cache.cc:305
static std::string makeId(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber)
Builds the opaque id a client uses to address one cache file, from an identity triple....
Definition parameter_cache.cc:171
std::optional< std::vector< DeviceParameter > > load(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber) const
Loads the cached parameter definitions for an identity.
Definition parameter_cache.cc:182
std::expected< std::vector< uint8_t >, std::string > readRaw(std::string_view id) const
Reads the raw JSON bytes of one cache file, addressed by its id (for download).
Definition parameter_cache.cc:291
void configure(ParameterCacheConfig config)
Replaces the active policy/location. Called by DeviceManager::init.
Definition parameter_cache.cc:135
std::vector< CacheEntry > list() const
Lists every valid cache file in the cache directory.
Definition parameter_cache.cc:251
Definition http_server.h:16
void to_json(nlohmann::json &j, const Device &d)
Serialises a Device to JSON.
Definition device.cc:915
constexpr uint32_t kSynapticonVendorId
EtherCAT vendor ID of Synapticon (SOMANET drives).
Definition parameter_cache.h:23
Policy and location for the on-disk parameter cache.
Definition parameter_cache.h:28
std::string directory
"" = a standard per-user cache directory (see resolveDir).
Definition parameter_cache.h:31
bool cacheAllVendors
false: cache Synapticon (0x22D2) only; true: cache every vendor.
Definition parameter_cache.h:29
bool enabled
Master switch for the whole cache (false disables it entirely).
Definition parameter_cache.h:32
Summary of one cache file on disk, for the management UI.
Definition parameter_cache.h:94
uint32_t revisionNumber
Revision number from the file header.
Definition parameter_cache.h:98
std::uintmax_t sizeBytes
File size on disk, in bytes.
Definition parameter_cache.h:100
std::string id
Opaque "<vendor>-<product>-<revision>" key (see makeId).
Definition parameter_cache.h:95
uint32_t vendorId
Vendor ID from the file header.
Definition parameter_cache.h:96
uint32_t productCode
Product code from the file header.
Definition parameter_cache.h:97
uint32_t parameterCount
Number of cached parameter definitions in the file.
Definition parameter_cache.h:99