1+ #pragma once
2+
3+ #include < string>
4+ #include < vector>
5+ #include < CSAPI/DataModels/ControlStream.h>
6+ #include < CSAPI/DataModels/Command.h>
7+ #include < CSAPI/Query/CommandsOfControlStreamQuery.h>
8+
9+ namespace OSHConnect {
10+ class OSHSystem ;
11+
12+ class OSHControlStream {
13+ private:
14+ OSHSystem* parentSystem;
15+ ConnectedSystemsAPI::DataModels::ControlStream controlStreamResource;
16+
17+ friend class OSHSystem ;
18+
19+ public:
20+ OSHControlStream (OSHSystem* parentSystem, const ConnectedSystemsAPI::DataModels::ControlStream& controlStreamResource)
21+ : parentSystem(parentSystem), controlStreamResource(controlStreamResource) {
22+ }
23+
24+ // / <summary>
25+ // / Query the node for the latest commands of this control stream with the specified parameters.
26+ // / </summary>
27+ std::vector<ConnectedSystemsAPI::DataModels::Command> fetchCommands (std::string query = " " ) {
28+ auto response = getConSysAPI ().getCommandsAPI ().fetchCommandsOfControlStream (getId (), query);
29+ std::cout << " fetchCommands: " << response.getResponseBody () << std::endl;
30+ if (response.isSuccessful () && !response.getItems ().empty ()) {
31+ return response.getItems ();
32+ }
33+ return {};
34+ }
35+
36+ // / <summary>
37+ // / Query the node for the latest commands of this control stream with the specified parameters.
38+ // / </summary>
39+ std::vector<ConnectedSystemsAPI::DataModels::Command> fetchCommands (const ConnectedSystemsAPI::Query::CommandsOfControlStreamQuery& query) {
40+ return fetchCommands (query.toString ());
41+ }
42+
43+ // / <summary>
44+ // / Query the node for a specific command of this control stream by its ID.
45+ // / </summary>
46+ // / <param name="commandId">The ID of the command to fetch.</param>
47+ // / <returns>The command with the specified ID, or an empty command if not found.</returns>
48+ ConnectedSystemsAPI::DataModels::Command fetchCommandById (const std::string& commandId) {
49+ auto response = getConSysAPI ().getCommandsAPI ().fetchCommandById (commandId);
50+ if (response.isSuccessful ()) {
51+ return response.getItem ();
52+ }
53+ return ConnectedSystemsAPI::DataModels::Command{};
54+ }
55+
56+ // / <summary>
57+ // / Refresh the control stream properties from the server.
58+ // / </summary>
59+ // / <returns>True if the refresh was successful, false otherwise.</returns>
60+ bool refreshControlStream () {
61+ auto response = getConSysAPI ().getControlStreamsAPI ().getControlStreamById (getId ());
62+ if (response.isSuccessful ()) {
63+ controlStreamResource = response.getItem ();
64+ return true ;
65+ }
66+ return false ;
67+ }
68+
69+ // / <summary>
70+ // / Create a new command for this control stream on the OpenSensorHub node using the provided command resource.
71+ // / </summary>
72+ // / <param name="command">The command resource to create.</param>
73+ // / <returns>The created command if successful, or std::nullopt if creation failed.</returns>
74+ std::string createCommand (const ConnectedSystemsAPI::DataModels::Command& command) {
75+ auto response = getConSysAPI ().getCommandsAPI ().createCommand (getId (), command);
76+ for (const auto & [headerKey, headerValues] : response.getHeaders ()) {
77+ for (const auto & headerValue : headerValues) {
78+ if (headerKey == " Location" ) {
79+ // Extract command ID from Location header if available.
80+ // The Location header is in the format: /commands/{commandId}
81+ std::string location = headerValue;
82+ size_t lastSlashPos = location.find_last_of (' /' );
83+ if (lastSlashPos != std::string::npos && lastSlashPos + 1 < location.size ()) {
84+ std::string newCommandId = location.substr (lastSlashPos + 1 );
85+ return newCommandId;
86+ }
87+ }
88+ }
89+ }
90+ return " " ;
91+ }
92+
93+ // / <summary>
94+ // / Update an existing observation of this data stream on the OpenSensorHub node using the provided observation resource.
95+ // / </summary>
96+ // / <param name="observationId">The ID of the observation to update.</param>
97+ // / <param name="updatedCommand">The updated command resource.</param>
98+ // / <returns>True if the update was successful, false otherwise.</returns>
99+ bool updateCommand (const std::string& commandId, const ConnectedSystemsAPI::DataModels::Command& updatedCommand) {
100+ auto response = getConSysAPI ().getCommandsAPI ().updateCommand (commandId, updatedCommand);
101+ return response.isSuccessful ();
102+ }
103+
104+ // / <summary>
105+ // / Delete an existing command of this control stream on the OpenSensorHub node by its ID.
106+ // / </summary>
107+ // / <param name="commandId">The ID of the command to delete.</param>
108+ // / <returns>True if the deletion was successful, false otherwise.</returns>
109+ bool deleteCommand (const std::string& commandId) {
110+ auto response = getConSysAPI ().getCommandsAPI ().deleteCommand (commandId);
111+ return response.isSuccessful ();
112+ }
113+
114+ // / <summary>
115+ // / The ID of the control stream. Same as controlStreamResource.getId().
116+ // / </summary>
117+ const std::string& getId () const { return controlStreamResource.getId ().value (); }
118+
119+ // / <summary>
120+ // / The control stream resource representing this control stream.
121+ // / </summary>
122+ ConnectedSystemsAPI::DataModels::ControlStream getControlStreamResource () const { return controlStreamResource; }
123+
124+ // / <summary>
125+ // / The ConnectedSystemsAPI instance used to communicate with the server.
126+ // / </summary>
127+ ConnectedSystemsAPI::ConSysAPI& getConSysAPI ();
128+
129+ OSHSystem* getParentSystem () const { return parentSystem; }
130+ };
131+ }
0 commit comments