Skip to content

Commit 63d9a41

Browse files
committed
refactor: reference paths to headers explicitly
1 parent 1045e06 commit 63d9a41

File tree

5 files changed

+121
-118
lines changed

5 files changed

+121
-118
lines changed
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
#ifndef THERMOMETER_H
22
#define THERMOMETER_H
33

4-
#include "lab_component.h"
4+
#include "stateOfLab_s/lab_component.h"
55
#include <iostream>
66

77
class Thermometer : public LabComponent<double> {
88
public:
9-
// Constructor
10-
explicit Thermometer(const std::string& id, double initial_angle = 0.0)
11-
: LabComponent(id), angle(initial_angle) {}
9+
// Constructor
10+
explicit Thermometer(const std::string &id, double initial_angle = 0.0)
11+
: LabComponent(id), angle(initial_angle) {}
1212

13-
// Override the updateComponent method
14-
void updateComponent(const double& new_angle) override {
15-
angle = new_angle;
16-
std::cout << "Thermometer " << getID() << " updated to angle: " << angle << std::endl;
17-
}
13+
// Override the updateComponent method
14+
void updateComponent(const double &new_angle) override {
15+
angle = new_angle;
16+
std::cout << "Thermometer " << getID() << " updated to angle: " << angle
17+
<< std::endl;
18+
}
1819

19-
// Override the getComponentCurrently method
20-
double getComponentCurrently() const override {
21-
return angle;
22-
}
20+
// Override the getComponentCurrently method
21+
double getComponentCurrently() const override { return angle; }
2322

2423
private:
25-
double angle; // Angle representing the state of the thermometer
24+
double angle; // Angle representing the state of the thermometer
2625
};
2726

28-
#endif // THERMOMETER_H
27+
#endif // THERMOMETER_H

include/stateOfLab_s/state_of_lab.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
#ifndef STATE_OF_LABS_H
22
#define STATE_OF_LABS_H
33

4-
#include "lab_component.h"
5-
#include "thermometer.h"
4+
#include "stateOfLab_s/components/thermometer.h"
5+
#include "stateOfLab_s/lab_component.h"
66
#include <map>
7-
#include <string>
87
#include <memory>
9-
#include <tuple>
108
#include <mutex>
119
#include <stdexcept>
10+
#include <string>
11+
#include <tuple>
1212

1313
// StateOfLabs class for managing lab components
1414
class StateOfLabs {
1515
public:
16-
// Constructor
17-
explicit StateOfLabs(const std::string& preset);
16+
// Constructor
17+
explicit StateOfLabs(const std::string &preset);
1818

19-
// Method to update a component's value (thread-safe)
20-
void updateComponent(const std::string& id, double value);
19+
// Method to update a component's value (thread-safe)
20+
void updateComponent(const std::string &id, double value);
2121

22-
// Method to get a component's current value (thread-safe)
23-
double getComponent(const std::string& id) const;
22+
// Method to get a component's current value (thread-safe)
23+
double getComponent(const std::string &id) const;
2424

25-
// Method to get the class name of a component
26-
std::string getComponentClassName(const std::string& id) const;
25+
// Method to get the class name of a component
26+
std::string getComponentClassName(const std::string &id) const;
2727

2828
private:
29-
// Map of component ID to a tuple containing additional information
30-
std::map<std::string, std::tuple<std::string, std::shared_ptr<LabComponentBase>, std::string>> lab_components;
29+
// Map of component ID to a tuple containing additional information
30+
std::map<
31+
std::string,
32+
std::tuple<std::string, std::shared_ptr<LabComponentBase>, std::string>>
33+
lab_components;
3134

32-
mutable std::mutex mtx; // Mutex for thread-safe access
35+
mutable std::mutex mtx; // Mutex for thread-safe access
3336
};
3437

3538
#endif // STATE_OF_LABS_H

src/openCV_s/process_image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "process_image.h"
1+
#include "openCV_f/process_image.h"
22
#include <iostream>
33

44
ImageProcessor::ImageProcessor(const std::string &imagePath)

src/stateOfLab_s/state_of_lab.cpp

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,70 @@
1-
#include "state_of_lab.h"
1+
#include "stateOfLab_s/state_of_lab.h"
22
#include <iostream>
33

4-
StateOfLabs::StateOfLabs(const std::string& preset) {
5-
if (preset.empty()) {
6-
throw std::invalid_argument("Error: You need a preset to construct StateOfLabs.");
7-
}
4+
StateOfLabs::StateOfLabs(const std::string &preset) {
5+
if (preset.empty()) {
6+
throw std::invalid_argument(
7+
"Error: You need a preset to construct StateOfLabs.");
8+
}
89

9-
if (preset == "frankhertz1") {
10-
std::cout << "Initializing StateOfLabs with preset: frankhertz1" << std::endl;
10+
if (preset == "frankhertz1") {
11+
std::cout << "Initializing StateOfLabs with preset: frankhertz1"
12+
<< std::endl;
1113

12-
// Add a thermometer as an example component
13-
lab_components["thermometer_1"] = std::make_tuple(
14-
"Thermometer 1",
15-
std::make_shared<Thermometer>("thermometer_1", 0.0),
16-
"Thermometer"
17-
);
18-
} else {
19-
throw std::invalid_argument("Error: Unknown preset provided.");
20-
}
14+
// Add a thermometer as an example component
15+
lab_components["thermometer_1"] = std::make_tuple(
16+
"Thermometer 1", std::make_shared<Thermometer>("thermometer_1", 0.0),
17+
"Thermometer");
18+
} else {
19+
throw std::invalid_argument("Error: Unknown preset provided.");
20+
}
2121
}
2222

23-
void StateOfLabs::updateComponent(const std::string& id, double value) {
24-
std::lock_guard<std::mutex> lock(mtx);
25-
auto it = lab_components.find(id);
26-
if (it != lab_components.end()) {
27-
auto& [name, component, className] = it->second;
28-
if (className == "Thermometer") {
29-
auto thermometer = std::dynamic_pointer_cast<Thermometer>(component);
30-
if (thermometer) {
31-
thermometer->updateComponent(value);
32-
return;
33-
}
34-
}
35-
// Add other types as needed
36-
throw std::runtime_error("Unsupported or mismatched component type: " + className);
37-
} else {
38-
throw std::runtime_error("Component not found.");
23+
void StateOfLabs::updateComponent(const std::string &id, double value) {
24+
std::lock_guard<std::mutex> lock(mtx);
25+
auto it = lab_components.find(id);
26+
if (it != lab_components.end()) {
27+
auto &[name, component, className] = it->second;
28+
if (className == "Thermometer") {
29+
auto thermometer = std::dynamic_pointer_cast<Thermometer>(component);
30+
if (thermometer) {
31+
thermometer->updateComponent(value);
32+
return;
33+
}
3934
}
35+
// Add other types as needed
36+
throw std::runtime_error("Unsupported or mismatched component type: " +
37+
className);
38+
} else {
39+
throw std::runtime_error("Component not found.");
40+
}
4041
}
4142

42-
double StateOfLabs::getComponent(const std::string& id) const {
43-
std::lock_guard<std::mutex> lock(mtx);
44-
auto it = lab_components.find(id);
45-
if (it != lab_components.end()) {
46-
auto& [name, component, className] = it->second;
47-
if (className == "Thermometer") {
48-
auto thermometer = std::dynamic_pointer_cast<Thermometer>(component);
49-
if (thermometer) {
50-
return thermometer->getComponentCurrently();
51-
}
52-
}
53-
// Add handling for other types if needed
54-
throw std::runtime_error("Unsupported or mismatched component type: " + className);
55-
} else {
56-
throw std::runtime_error("Component not found.");
43+
double StateOfLabs::getComponent(const std::string &id) const {
44+
std::lock_guard<std::mutex> lock(mtx);
45+
auto it = lab_components.find(id);
46+
if (it != lab_components.end()) {
47+
auto &[name, component, className] = it->second;
48+
if (className == "Thermometer") {
49+
auto thermometer = std::dynamic_pointer_cast<Thermometer>(component);
50+
if (thermometer) {
51+
return thermometer->getComponentCurrently();
52+
}
5753
}
54+
// Add handling for other types if needed
55+
throw std::runtime_error("Unsupported or mismatched component type: " +
56+
className);
57+
} else {
58+
throw std::runtime_error("Component not found.");
59+
}
5860
}
5961

60-
61-
std::string StateOfLabs::getComponentClassName(const std::string& id) const {
62-
std::lock_guard<std::mutex> lock(mtx);
63-
auto it = lab_components.find(id);
64-
if (it != lab_components.end()) {
65-
return std::get<2>(it->second);
66-
} else {
67-
throw std::runtime_error("Component not found.");
68-
}
62+
std::string StateOfLabs::getComponentClassName(const std::string &id) const {
63+
std::lock_guard<std::mutex> lock(mtx);
64+
auto it = lab_components.find(id);
65+
if (it != lab_components.end()) {
66+
return std::get<2>(it->second);
67+
} else {
68+
throw std::runtime_error("Component not found.");
69+
}
6970
}

src/websocket_s/websocket_server.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#include "message.grpc.pb.h"
2-
#include "process_image.h"
2+
#include "openCV_f/process_image.h"
3+
#include "stateOfLab_s/state_of_lab.h"
34
#include <boost/asio.hpp>
45
#include <boost/beast.hpp>
56
#include <cstdlib>
67
#include <grpcpp/grpcpp.h>
78
#include <iostream>
9+
#include <nlohmann/json.hpp>
810
#include <opencv2/opencv.hpp>
911
#include <sstream>
1012
#include <string>
11-
#include "message.grpc.pb.h"
12-
#include "state_of_lab.h"
13-
#include <nlohmann/json.hpp>
1413

1514
// Namespace declarations
1615
namespace asio = boost::asio;
@@ -35,22 +34,22 @@ class MessageServiceImpl final : public MessageService::Service {
3534

3635
// Function to run the gRPC server
3736
void RunGrpcServer() {
38-
std::string serverAddress("192.168.2.136:50053");
39-
MessageServiceImpl service;
40-
41-
grpc::ServerBuilder builder;
42-
builder.AddListeningPort(serverAddress, grpc::InsecureServerCredentials());
43-
builder.RegisterService(&service);
44-
45-
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
46-
if (server) {
47-
std::cout << "gRPC server listening on " << serverAddress << std::endl;
48-
} else {
49-
std::cerr << "Failed to start gRPC server!" << std::endl;
50-
return;
51-
}
37+
std::string serverAddress("192.168.2.136:50053");
38+
MessageServiceImpl service;
39+
40+
grpc::ServerBuilder builder;
41+
builder.AddListeningPort(serverAddress, grpc::InsecureServerCredentials());
42+
builder.RegisterService(&service);
43+
44+
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
45+
if (server) {
46+
std::cout << "gRPC server listening on " << serverAddress << std::endl;
47+
} else {
48+
std::cerr << "Failed to start gRPC server!" << std::endl;
49+
return;
50+
}
5251

53-
server->Wait();
52+
server->Wait();
5453
}
5554

5655
// WebSocket session function
@@ -65,16 +64,19 @@ void do_session(tcp::socket socket) {
6564

6665
// Read WebSocket message
6766
ws.read(buffer);
68-
std::string receivedMessage = boost::beast::buffers_to_string(buffer.data());
67+
std::string receivedMessage =
68+
boost::beast::buffers_to_string(buffer.data());
6969

70-
std::cout << "WebSocket received message: " << receivedMessage << std::endl;
70+
std::cout << "WebSocket received message: " << receivedMessage
71+
<< std::endl;
7172

7273
// Parse the received JSON message
7374
json request;
7475
try {
7576
request = json::parse(receivedMessage);
7677
} catch (const json::exception &e) {
77-
std::string errorResponse = R"({"type":"error","payload":{"message":"Invalid JSON format"}})";
78+
std::string errorResponse =
79+
R"({"type":"error","payload":{"message":"Invalid JSON format"}})";
7880
ws.write(asio::buffer(errorResponse));
7981
continue;
8082
}
@@ -91,14 +93,12 @@ void do_session(tcp::socket socket) {
9193
std::string id = payload.value("id", "");
9294
double angle = 45.0; // Replace with actual logic to fetch the angle
9395

94-
response = {
95-
{"type", "circular_thermometer_angle"},
96-
{"payload", {{"id", id}, {"angle", angle}}}};
96+
response = {{"type", "circular_thermometer_angle"},
97+
{"payload", {{"id", id}, {"angle", angle}}}};
9798
} else {
9899
// Handle unknown request types
99-
response = {
100-
{"type", "error"},
101-
{"payload", {{"message", "Unknown request type"}}}};
100+
response = {{"type", "error"},
101+
{"payload", {{"message", "Unknown request type"}}}};
102102
}
103103

104104
// Send response back to the client
@@ -117,7 +117,8 @@ int main() {
117117

118118
// Start WebSocket server
119119
asio::io_context ioc;
120-
tcp::acceptor acceptor(ioc, tcp::endpoint(asio::ip::address_v4::any(), 8080));
120+
tcp::acceptor acceptor(ioc,
121+
tcp::endpoint(asio::ip::address_v4::any(), 8080));
121122

122123
std::cout << "WebSocket server listening on port 8080..." << std::endl;
123124

@@ -128,8 +129,7 @@ int main() {
128129
// Pass the socket and shared labs instance to the session thread
129130
std::thread(&do_session, std::move(socket), labs).detach();
130131
}
131-
} catch (const std::exception& e) {
132+
} catch (const std::exception &e) {
132133
std::cout << "Server Error: " << e.what() << std::endl;
133134
}
134135
}
135-

0 commit comments

Comments
 (0)