126 lines
3.9 KiB
C++
126 lines
3.9 KiB
C++
#include "sequence.hpp"
|
|
|
|
#include <map>
|
|
|
|
std::string toString(DaliCommandType type) {
|
|
static const std::map<DaliCommandType, std::string> map = {
|
|
{DaliCommandType::setBright, "setBright"},
|
|
{DaliCommandType::on, "on"},
|
|
{DaliCommandType::off, "off"},
|
|
{DaliCommandType::toScene, "toScene"},
|
|
{DaliCommandType::setScene, "setScene"},
|
|
{DaliCommandType::removeScene, "removeScene"},
|
|
{DaliCommandType::addToGroup, "addToGroup"},
|
|
{DaliCommandType::removeFromGroup, "removeFromGroup"},
|
|
{DaliCommandType::setFadeTime, "setFadeTime"},
|
|
{DaliCommandType::setFadeRate, "setFadeRate"},
|
|
{DaliCommandType::wait, "wait"},
|
|
{DaliCommandType::modifyShortAddress, "modifyShortAddress"},
|
|
{DaliCommandType::deleteShortAddress, "deleteShortAddress"},
|
|
};
|
|
|
|
const auto it = map.find(type);
|
|
if (it == map.end()) return "setBright";
|
|
return it->second;
|
|
}
|
|
|
|
DaliCommandType commandTypeFromString(const std::string& name, DaliCommandType fallback) {
|
|
static const std::map<std::string, DaliCommandType> map = {
|
|
{"setBright", DaliCommandType::setBright},
|
|
{"on", DaliCommandType::on},
|
|
{"off", DaliCommandType::off},
|
|
{"toScene", DaliCommandType::toScene},
|
|
{"setScene", DaliCommandType::setScene},
|
|
{"removeScene", DaliCommandType::removeScene},
|
|
{"addToGroup", DaliCommandType::addToGroup},
|
|
{"removeFromGroup", DaliCommandType::removeFromGroup},
|
|
{"setFadeTime", DaliCommandType::setFadeTime},
|
|
{"setFadeRate", DaliCommandType::setFadeRate},
|
|
{"wait", DaliCommandType::wait},
|
|
{"modifyShortAddress", DaliCommandType::modifyShortAddress},
|
|
{"deleteShortAddress", DaliCommandType::deleteShortAddress},
|
|
};
|
|
|
|
const auto it = map.find(name);
|
|
if (it == map.end()) return fallback;
|
|
return it->second;
|
|
}
|
|
|
|
int DaliCommandParams::getInt(const std::string& key, int def) const {
|
|
const auto* value = getObjectValue(data, key);
|
|
if (!value) return def;
|
|
return value->asInt().value_or(def);
|
|
}
|
|
|
|
DaliCommandParams DaliCommandParams::copy() const { return DaliCommandParams(data); }
|
|
|
|
DaliValue::Object DaliCommandParams::toJson() const { return data; }
|
|
|
|
DaliCommandParams DaliCommandParams::fromJson(const DaliValue::Object& json) {
|
|
return DaliCommandParams(json);
|
|
}
|
|
|
|
SequenceStep SequenceStep::copy() const {
|
|
SequenceStep s;
|
|
s.id = id;
|
|
s.remark = remark;
|
|
s.type = type;
|
|
s.params = params.copy();
|
|
return s;
|
|
}
|
|
|
|
DaliValue::Object SequenceStep::toJson() const {
|
|
DaliValue::Object out;
|
|
out["id"] = id;
|
|
if (remark.has_value()) out["remark"] = remark.value();
|
|
out["type"] = toString(type);
|
|
out["params"] = params.toJson();
|
|
return out;
|
|
}
|
|
|
|
SequenceStep SequenceStep::fromJson(const DaliValue::Object& json) {
|
|
SequenceStep s;
|
|
s.id = getObjectString(json, "id").value_or("");
|
|
s.remark = getObjectString(json, "remark");
|
|
s.type = commandTypeFromString(getObjectString(json, "type").value_or("setBright"));
|
|
if (const auto* paramsVal = getObjectValue(json, "params")) {
|
|
if (const auto* obj = paramsVal->asObject()) {
|
|
s.params = DaliCommandParams::fromJson(*obj);
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
|
|
DaliValue::Object CommandSequence::toJson() const {
|
|
DaliValue::Object out;
|
|
out["id"] = id;
|
|
out["name"] = name;
|
|
|
|
DaliValue::Array arr;
|
|
arr.reserve(steps.size());
|
|
for (const auto& s : steps) {
|
|
arr.emplace_back(s.toJson());
|
|
}
|
|
out["steps"] = std::move(arr);
|
|
return out;
|
|
}
|
|
|
|
CommandSequence CommandSequence::fromJson(const DaliValue::Object& json) {
|
|
CommandSequence seq;
|
|
seq.id = getObjectString(json, "id").value_or("");
|
|
seq.name = getObjectString(json, "name").value_or("");
|
|
|
|
if (const auto* stepsVal = getObjectValue(json, "steps")) {
|
|
if (const auto* arr = stepsVal->asArray()) {
|
|
seq.steps.reserve(arr->size());
|
|
for (const auto& v : *arr) {
|
|
if (const auto* stepObj = v.asObject()) {
|
|
seq.steps.push_back(SequenceStep::fromJson(*stepObj));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return seq;
|
|
}
|