1+ #pragma once
2+
3+ #include < string>
4+ #include < nlohmann/json.hpp>
5+
6+ #include " Component/DataComponent.h"
7+ #include " Component/DataComponentRegistry.h"
8+ #include " Component/DataRecord.h"
9+
10+ namespace ConnectedSystemsAPI {
11+ namespace DataModels {
12+ class CommandSchema {
13+ private:
14+ std::string commandFormat;
15+ std::unique_ptr<Component::DataComponent> parametersSchema;
16+ std::unique_ptr<Component::DataComponent> resultSchema;
17+ // todo: feasibilityResultSchema
18+
19+ public:
20+ CommandSchema () = default ;
21+ CommandSchema (const std::string& commandFormat,
22+ std::unique_ptr<Component::DataComponent> parametersSchema,
23+ std::unique_ptr<Component::DataComponent> resultSchema)
24+ : commandFormat(commandFormat), resultSchema(std::move(resultSchema)) {
25+ }
26+
27+ CommandSchema (const CommandSchema&) = delete ;
28+ CommandSchema& operator =(const CommandSchema&) = delete ;
29+ CommandSchema (CommandSchema&&) noexcept = default ;
30+ CommandSchema& operator =(CommandSchema&&) noexcept = default ;
31+
32+ // / <summary>
33+ // / Encoding format of the command.
34+ // / </summary>
35+ const std::string& getCommandFormat () const { return commandFormat; }
36+ // / <summary>
37+ // / Record schema for the command parameters property. If omitted, parameters are not included in the datastream.
38+ // / </summary>
39+ const Component::DataComponent* getParametersSchema () const { return parametersSchema.get (); }
40+ // / <summary>
41+ // / Schema for the command result property.
42+ // / this describes the observed properties included in the result
43+ // / and how they are structured if the result is a record, a vector quantity or a coverage.
44+ // / </summary>
45+ const Component::DataComponent* getResultSchema () const { return resultSchema.get (); }
46+
47+ friend void from_json (const nlohmann::json& j, CommandSchema& v);
48+ friend void to_json (nlohmann::ordered_json& j, const CommandSchema& v);
49+ friend std::ostream& operator <<(std::ostream& os, const CommandSchema& v);
50+ };
51+
52+ inline void from_json (const nlohmann::json& j, CommandSchema& v) {
53+ // Print the json for debugging
54+ std::cout << " Deserializing CommandSchema from JSON: " << j.dump (2 ) << std::endl;
55+ v.commandFormat = j.at (" commandFormat" ).get <std::string>();
56+ v.parametersSchema = Component::DataComponentRegistry::createDataComponent (j.at (" paramsSchema" ));
57+ if (j.contains (" resultSchema" ))
58+ v.resultSchema = Component::DataComponentRegistry::createDataComponent (j.at (" resultSchema" ));
59+ }
60+
61+ inline void to_json (nlohmann::ordered_json& j, const CommandSchema& v) {
62+ j = nlohmann::ordered_json::object ();
63+
64+ j[" commandFormat" ] = v.commandFormat ;
65+ if (v.parametersSchema ) j[" paramsSchema" ] = v.getParametersSchema ()->toJson ();
66+ if (v.resultSchema ) j[" resultSchema" ] = v.getResultSchema ()->toJson ();
67+ }
68+
69+ inline std::ostream& operator <<(std::ostream& os, const CommandSchema& v) {
70+ nlohmann::ordered_json j;
71+ to_json (j, v);
72+ return os << j.dump (2 );
73+ }
74+ }
75+ }
0 commit comments