Skip to content

Commit d7911f6

Browse files
Add Control Stream, Command Schema
1 parent 1ebdfc7 commit d7911f6

22 files changed

Lines changed: 788 additions & 92 deletions

.idea/workspace.xml

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CSAPI-lib/APIRequest.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ namespace ConnectedSystemsAPI {
131131
std::string resourceId;
132132
std::string subResourcePath;
133133
std::string subResourceId;
134+
std::string queryString;
134135

135136
public:
136137
Builder& setApiRoot(const std::string& root) {
@@ -178,6 +179,11 @@ namespace ConnectedSystemsAPI {
178179
return *this;
179180
}
180181

182+
Builder& setQueryString(const std::string& queryString) {
183+
this->queryString = queryString;
184+
return *this;
185+
}
186+
181187
APIRequest build() {
182188
if (apiRoot.empty()) {
183189
throw std::invalid_argument("API root must be set");
@@ -191,6 +197,7 @@ namespace ConnectedSystemsAPI {
191197
endpoint = appendPath(endpoint, resourceId);
192198
endpoint = appendPath(endpoint, subResourcePath);
193199
endpoint = appendPath(endpoint, subResourceId);
200+
endpoint += queryString;
194201

195202
return APIRequest(apiRoot, endpoint, requestMethod, headers, body);
196203
}

CSAPI-lib/CSAPI-lib.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
<ClInclude Include="APIRequest.h" />
136136
<ClInclude Include="APIResponse.h" />
137137
<ClInclude Include="ConnectedSystemsAPI.h" />
138+
<ClInclude Include="ControlStreamsAPI.h" />
139+
<ClInclude Include="DataModels\CommandSchema.h" />
138140
<ClInclude Include="DataModels\Component\Boolean.h" />
139141
<ClInclude Include="DataModels\Component\BooleanBuilder.h" />
140142
<ClInclude Include="DataModels\Component\Category.h" />
@@ -158,6 +160,8 @@
158160
<ClInclude Include="DataModels\Component\TimeRange.h" />
159161
<ClInclude Include="DataModels\Component\UnitOfMeasure.h" />
160162
<ClInclude Include="DataModels\Component\Vector.h" />
163+
<ClInclude Include="DataModels\ControlledProperty.h" />
164+
<ClInclude Include="DataModels\ControlStream.h" />
161165
<ClInclude Include="DataModels\DataStream.h" />
162166
<ClInclude Include="DataModels\DataStreamBuilder.h" />
163167
<ClInclude Include="DataModels\Data\DataBlock.h" />

CSAPI-lib/CSAPI-lib.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,17 @@
201201
<ClInclude Include="Query\ObservationsOfDataStreamQuery.h">
202202
<Filter>Header Files\Query</Filter>
203203
</ClInclude>
204+
<ClInclude Include="ControlStreamsAPI.h">
205+
<Filter>Header Files</Filter>
206+
</ClInclude>
207+
<ClInclude Include="DataModels\ControlStream.h">
208+
<Filter>Header Files\DataModels</Filter>
209+
</ClInclude>
210+
<ClInclude Include="DataModels\ControlledProperty.h">
211+
<Filter>Header Files\DataModels</Filter>
212+
</ClInclude>
213+
<ClInclude Include="DataModels\CommandSchema.h">
214+
<Filter>Header Files\DataModels</Filter>
215+
</ClInclude>
204216
</ItemGroup>
205217
</Project>

CSAPI-lib/ConnectedSystemsAPI.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "DataStreamsAPI.h"
66
#include "ObservationsAPI.h"
77
#include "SystemsAPI.h"
8+
#include "ControlStreamsAPI.h"
89

910
namespace ConnectedSystemsAPI {
1011
class ConSysAPI {
@@ -14,6 +15,7 @@ namespace ConnectedSystemsAPI {
1415
SystemsAPI systemsAPI;
1516
DataStreamsAPI dataStreamsAPI;
1617
ObservationsAPI observationsAPI;
18+
ControlStreamsAPI controlStreamsAPI;
1719

1820
public:
1921
/// <param name="apiRoot">e.g. "localhost:8181/sensorhub/api"</param>
@@ -30,6 +32,7 @@ namespace ConnectedSystemsAPI {
3032
systemsAPI = SystemsAPI(apiRoot, authHeader);
3133
dataStreamsAPI = DataStreamsAPI(apiRoot, authHeader);
3234
observationsAPI = ObservationsAPI(apiRoot, authHeader);
35+
controlStreamsAPI = ControlStreamsAPI(apiRoot, authHeader);
3336
}
3437

3538
// Overload to accept C-style string literals to avoid list-initialization narrowing issues
@@ -48,7 +51,8 @@ namespace ConnectedSystemsAPI {
4851
const std::string& getAuthHeader() const { return authHeader; }
4952
SystemsAPI& getSystemsAPI() { return systemsAPI; }
5053
DataStreamsAPI& getDataStreamsAPI() { return dataStreamsAPI; }
51-
ObservationsAPI getObservationsAPI() { return observationsAPI; }
54+
ObservationsAPI& getObservationsAPI() { return observationsAPI; }
55+
ControlStreamsAPI& getControlStreamsAPI() { return controlStreamsAPI; }
5256
private:
5357
std::string base64_encode(const std::string& in) {
5458
static const std::string base64_chars =

CSAPI-lib/ControlStreamsAPI.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "APIRequest.h"
6+
#include "APIResponse.h"
7+
#include "DataModels/ControlStream.h"
8+
#include "DataModels/CommandSchema.h"
9+
10+
namespace ConnectedSystemsAPI {
11+
class ControlStreamsAPI {
12+
private:
13+
std::string apiRoot;
14+
std::string authHeader;
15+
16+
public:
17+
ControlStreamsAPI() = default;
18+
ControlStreamsAPI(const std::string& apiRoot, const std::string& authHeader)
19+
: apiRoot(apiRoot), authHeader(authHeader) {
20+
}
21+
22+
APIResponse<DataModels::ControlStream> getControlStreams(std::string queryString = "") {
23+
auto response = APIRequest::Builder()
24+
.setApiRoot(apiRoot)
25+
.setMethod("GET")
26+
.setAuthHeader(authHeader)
27+
.setResourcePath("controlstreams")
28+
.setQueryString(queryString)
29+
.build()
30+
.execute<DataModels::ControlStream>();
31+
return response;
32+
}
33+
34+
APIResponse<DataModels::ControlStream> getControlStreamsOfSystem(const std::string& systemId, std::string queryString = "") {
35+
auto response = APIRequest::Builder()
36+
.setApiRoot(apiRoot)
37+
.setMethod("GET")
38+
.setAuthHeader(authHeader)
39+
.setResourcePath("systems")
40+
.setResourceId(systemId)
41+
.setSubResourcePath("controlstreams")
42+
.setQueryString(queryString)
43+
.build()
44+
.execute<DataModels::ControlStream>();
45+
return response;
46+
}
47+
48+
APIResponse<DataModels::ControlStream> getControlStreamById(const std::string& controlStreamId) {
49+
auto response = APIRequest::Builder()
50+
.setApiRoot(apiRoot)
51+
.setMethod("GET")
52+
.setAuthHeader(authHeader)
53+
.setResourcePath("controlstreams")
54+
.setResourceId(controlStreamId)
55+
.build()
56+
.execute<DataModels::ControlStream>();
57+
return response;
58+
}
59+
60+
APIResponse<DataModels::CommandSchema> getControlStreamSchema(const std::string& controlStreamId) {
61+
auto response = APIRequest::Builder()
62+
.setApiRoot(apiRoot)
63+
.setMethod("GET")
64+
.setAuthHeader(authHeader)
65+
.setResourcePath("controlstreams")
66+
.setResourceId(controlStreamId)
67+
.setSubResourcePath("schema")
68+
.build()
69+
.execute<DataModels::CommandSchema>();
70+
return response;
71+
}
72+
};
73+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)