Skip to content

Commit

Permalink
--Define CustomSensorAttributes to be held within a subconfig similar…
Browse files Browse the repository at this point in the history
… to user_defined.
  • Loading branch information
jturner65 committed Nov 25, 2024
1 parent bfe1bc0 commit a2bbbe5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
21 changes: 16 additions & 5 deletions src/esp/metadata/attributes/CustomSensorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,38 @@ namespace attributes {

CustomSensorAttributes::CustomSensorAttributes(const std::string& handle)
: AbstractSensorAttributes("CustomSensorAttributes", handle) {
addOrEditSubgroup<Configuration>("custom_attributes");
} // CustomSensorAttributes ctor

void CustomSensorAttributes::populateWithSensorSpec(
const sensor::SensorSpec::ptr& spec) {
// Call Base class version
AbstractSensorAttributes::populateWithSensorSpec(spec);
// TODO handle setting custom_attributes subconfig with any values in spec
// beyond base values.
// std::shared_ptr<Configuration> custAttrs = editCustomAttrFields();
// put values in custAttrs directly
} // CustomSensorAttributes::populateWithSensorSpec
std::string CustomSensorAttributes::getObjectInfoHeaderInternal() const {
const std::vector<std::string> sortedKeys = getKeys(true);
std::string CustomSensorAttributes::getAbstractSensorInfoHeaderInternal()
const {
// get every key from custom attributes
const std::vector<std::string> sortedKeys =
getCustomAttrFieldsView()->getKeys(true);
std::string res = "";
for (const std::string& key : sortedKeys) {
Cr::Utility::formatInto(res, res.size(), "{},", key);
}
return res;
} // CustomSensorAttributes::getObjectInfoHeaderInternal

std::string CustomSensorAttributes::getObjectInfoInternal() const {
const std::vector<std::string> sortedKeys = getKeys(true);
std::string CustomSensorAttributes::getAbstractSensorInfoInternal() const {
std::shared_ptr<const Configuration> custAttrs = getCustomAttrFieldsView();
// get every key from custom attributes
const std::vector<std::string> sortedKeys = custAttrs->getKeys(true);
std::string res = "";
for (const std::string& key : sortedKeys) {
Cr::Utility::formatInto(res, res.size(), "{},", getAsString(key));
Cr::Utility::formatInto(res, res.size(), "{},",
custAttrs->getAsString(key));
}
return res;
} // CustomSensorAttributes::getObjectInfoInternal
Expand Down
70 changes: 58 additions & 12 deletions src/esp/metadata/attributes/CustomSensorAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,73 @@ class CustomSensorAttributes : public AbstractSensorAttributes {
*/
void populateWithSensorSpec(const sensor::SensorSpec::ptr& spec) override;

protected:
/**
* @brief Write CustomSensorAttributes data to JSON
* @brief Gets a smart pointer reference to a copy of the custom attributes
* configuration data from config file. Habitat does not parse or process this
* data, but it will be available to the user via python bindings for each
* object.
*/
std::shared_ptr<Configuration> getCustomAttrFields() const {
return getSubconfigCopy<Configuration>("custom_attributes");
}

/**
* @brief Gets a const smart pointer reference to a view of the custom
* attributes configuration data from config file. Habitat does not parse or
* process this data, but it will be available to the user via python bindings
* for each object.
*/
std::shared_ptr<const Configuration> getCustomAttrFieldsView() const {
return getSubconfigView("custom_attributes");
}

/**
* @brief Gets a smart pointer reference to the actual custom attributes
* configuration data from config file. Habitat does not parse or process this
* data, but it will be available to the user via python bindings for each
* object. This method is for editing the configuration.
*/
std::shared_ptr<Configuration> editCustomAttrFields() {
return editSubconfig<Configuration>("custom_attributes");
}

/**
* @brief Move an existing custom_attributes subconfiguration into this
* configuration, overwriting the existing copy if it exists. Habitat does not
* parse or process this data, but it will be available to the user via python
* bindings for each object. This method is for editing the configuration.
*/
void setCustomAttrFields(std::shared_ptr<Configuration>& custAttrs) {
setSubconfigPtr("custom_attributes", custAttrs);
}

/**
* @brief Returns the number of custom attributes values (within the
* "custom_attributes" sub-Configuration) this attributes has.
*/
void writeValuesToJson(io::JsonGenericValue& jsonObj,
io::JsonAllocator& allocator) const override {
Configuration::writeValuesToJson(jsonObj, allocator);
};
int getNumCustomAttrFields() const {
return getSubconfigNumEntries("custom_attributes");
}

/**
* @brief Retrieve a comma-separated string holding the header values for the
* info returned for this managed object, type-specific.
* @brief Returns the number of custom attributes values and subconfig values
* (recursive) (within the "custom_attributes" sub-Configuration) this
* attributes has in its entire tree.
*/
int getTotalNumCustomAttrFields() const {
return getSubconfigTreeNumEntries("custom_attributes");
}

std::string getObjectInfoHeaderInternal() const override;
protected:
/**
* @brief get AbstractSensorAttributes-specific info header
*/
std::string getAbstractSensorInfoHeaderInternal() const override;

/**
* @brief Retrieve a comma-separated informational string about the contents
* of this managed object.
* @brief get AbstractSensorAttributes specific info for csv string
*/
std::string getObjectInfoInternal() const override;
std::string getAbstractSensorInfoInternal() const override;

public:
ESP_SMART_POINTERS(CustomSensorAttributes)
Expand Down

0 comments on commit a2bbbe5

Please sign in to comment.