Skip to content

Commit

Permalink
Use Device if connect to WS server
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Apr 12, 2024
1 parent 5cd3989 commit 207b4fd
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 233 deletions.
7 changes: 1 addition & 6 deletions app/admin_panel/js/raspberry_pi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ function unselectRaspberryPi(_event, _args) {

function selectRaspberryPi(_event, args) {
const uuid = args.uuid;
if (websocket_server._raspberry_pi_map.has(uuid)) {
websocket_server._selected_raspberry_pi = { uuid: uuid, ws: websocket_server._raspberry_pi_map.get(uuid) };
return { success: true, message: `Connected to Raspberry Pi with UUID: ${uuid}` };
} else {
return { success: false, message: `Raspberry Pi with UUID: ${uuid} not found` };
}
return websocket_server.selectRaspberryPi(uuid);
}

function getRaspberryPiList(_event, _args) {
Expand Down
9 changes: 8 additions & 1 deletion app/admin_panel/js/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ class WebSocketServer {
*/
selectRaspberryPi(uuid) {
if (this._raspberry_pi_map.has(uuid)) {
this._selected_raspberry_pi = { uuid: uuid, ws: this._raspberry_pi_map.get(uuid) };
if (this._selected_raspberry_pi) {
const ws = this._selected_raspberry_pi.ws;
ws.send(JSON.stringify({ type: "selection", selected: false }));
this._selected_raspberry_pi = undefined;
}
const ws = this._raspberry_pi_map.get(uuid);
this._selected_raspberry_pi = { uuid: uuid, ws: ws };
ws.send(JSON.stringify({ type: "selection", selected: true }));
return { success: true, message: `Connected to Raspberry Pi with UUID: ${uuid}` };
} else {
return { success: false, message: `Raspberry Pi with UUID: ${uuid} not found` };
Expand Down
2 changes: 2 additions & 0 deletions app/admin_panel/src/lib/RaspberryPiSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
if (selected_uuid === "") {
return;
}
console.log(selected_uuid);
const selection_result = await api.selectRaspberryPi({
uuid: selected_uuid,
});
console.log(selection_result);
if (selection_result.success) {
selected_raspberry_pi_uuid.set(selected_uuid);
} else {
Expand Down
14 changes: 13 additions & 1 deletion app/rpi/common/include/car/system/device/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

using namespace car::configuration;

namespace car::system
{
class CarSystem;
}

namespace car::system::device
{
class DeviceManager {
Expand All @@ -35,14 +40,21 @@ namespace car::system::device
return this->lidar_device_.get();
}

void initialize();
const bool isRunning() const {
return this->is_running_;
}

void initialize(std::shared_ptr<system::CarSystem> car_system);
void start();
void update();
void stop();
void terminate();

private:
std::shared_ptr<car::system::CarSystem> car_system;

bool is_initialized_ = false;
bool is_running_ = false;

std::unique_ptr<lidar::LidarDevice> lidar_device_;
std::unique_ptr<CameraDevice> camera_device_;
Expand Down
12 changes: 6 additions & 6 deletions app/rpi/common/include/car/system/device/lidar/LidarScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ namespace car::system::device::lidar

void start() final override
{
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->running = true;
this->lidar_->start_motor();
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->scan_generator_ = this->lidar_->iter_scans();
};

void update() final override
{
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
if (this->running) {
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
const auto& scan_generator = std::get<std::function<std::vector<Measure>()>>(this->scan_generator_);
this->setScanData(scan_generator());
}
};

void stop() final override
{
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->running = false;
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->scan_generator_ = nullptr;
this->lidar_->stop();
this->lidar_->stop_motor();
Expand All @@ -69,10 +69,10 @@ namespace car::system::device::lidar

void disconnect() final override
{
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->running = false;
this->lidar_->disconnect();
std::lock_guard<std::mutex> lock(this->scan_data_mutex_);
this->scan_generator_ = nullptr;
this->lidar_->disconnect();
}

void terminate() final override
Expand All @@ -82,7 +82,7 @@ namespace car::system::device::lidar
}

private:
bool running = false;
std::atomic_bool running = false;

std::shared_ptr<configuration::Configuration> configuration_;

Expand Down
2 changes: 2 additions & 0 deletions app/rpi/common/include/car/system/messaging/MessagingSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace car::system::messaging
void setConfiguration(std::shared_ptr<configuration::Configuration> configuration);

nod::signal<void(const std::string, const rapidjson::Document&)>& getCommandSignal() { return this->command_signal_; }
nod::signal<void(const std::string, const rapidjson::Document&)>& getSelectionSignal() { return this->selection_signal_; }
nod::signal<void(const std::string)>& getMessageSignal() { return this->message_signal_; }
nod::signal<void(const std::string)>& getDisconnectSignal() { return this->on_disconnect_signal_; }

Expand All @@ -59,6 +60,7 @@ namespace car::system::messaging

nod::signal<void(const std::string)> message_signal_;
nod::signal<void(const std::string, const rapidjson::Document&)> command_signal_;
nod::signal<void(const std::string, const rapidjson::Document&)> selection_signal_;

private:
tl::expected<std::string, std::string> getFirstMessage();
Expand Down
6 changes: 2 additions & 4 deletions app/rpi/common/src/car/system/CarSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace car::system
{
assert(!this->initialized && "Car System is already initialized.");
this->messaging_system_->initialize(this->configuration_);
this->device_manager_->initialize();
this->device_manager_->initialize(shared_from_this());
this->movement_system_->initialize();
this->plugin_manager_->initialize(shared_from_this());
this->initialized = true;
Expand Down Expand Up @@ -78,7 +78,6 @@ namespace car::system
{
return tl::make_unexpected(messaging_system_result.error());
}
this->device_manager_->start();
return nullptr;
}

Expand All @@ -88,7 +87,6 @@ namespace car::system
assert(this->started && "Car System has not been started yet.");
//assert(this->messaging_system->isConnected() && "Car System is not connected to the WS Server."); The connect bool is set to false when it disconnects from the websocket
this->messaging_system_->stop();
this->device_manager_->stop();
}

/// <summary>
Expand All @@ -105,7 +103,7 @@ namespace car::system
void CarSystem::update()
{
this->device_manager_->update();
if (this->messaging_system_->isConnected())
if (this->messaging_system_->isConnected() && this->device_manager_->isRunning())
{
rapidjson::Document output_json;
output_json.SetObject();
Expand Down
22 changes: 20 additions & 2 deletions app/rpi/common/src/car/system/device/DeviceManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "car/system/device/DeviceManager.h"
#include "car/system/CarSystem.h"

namespace car::system::device {
tl::expected<std::unique_ptr<DeviceManager>, std::string> DeviceManager::create(std::shared_ptr<Configuration> configuration)
Expand All @@ -24,7 +25,7 @@ namespace car::system::device {
return std::make_unique<DeviceManager>(std::move(camera_device), std::move(lidar_device));
}

void DeviceManager::initialize()
void DeviceManager::initialize(std::shared_ptr<system::CarSystem> car_system)
{
assert(this->lidar_device_ != nullptr);
assert(this->camera_device_ != nullptr);
Expand All @@ -33,8 +34,23 @@ namespace car::system::device {
{
return;
}
this->car_system = car_system;
this->lidar_device_->initialize();
//this->camera_device_->initialize();
this->car_system->getMessagingSystem()->getSelectionSignal().connect([&](const std::string message, const rapidjson::Document& message_json) {
const bool selected = message_json["selected"].GetBool();
if (selected) {
this->start();
}
else {
this->stop();
}
}
);
this->car_system->getMessagingSystem()->getDisconnectSignal().connect([&](const std::string message) {
this->stop();
}
);
this->is_initialized_ = true;
}

Expand All @@ -45,13 +61,14 @@ namespace car::system::device {
assert(this->is_initialized_ && "The DeviceManager is not initialized");
this->lidar_device_->start();
this->camera_device_->start();
this->is_running_ = true;
}

void DeviceManager::update() {
assert(this->lidar_device_ != nullptr);
assert(this->camera_device_ != nullptr);
assert(this->is_initialized_ && "The DeviceManager is not initialized");
if (!this->is_initialized_)
if (!this->is_initialized_ || !this->is_running_)
{
return;
}
Expand Down Expand Up @@ -79,6 +96,7 @@ namespace car::system::device {
}
this->lidar_device_->stop();
this->camera_device_->stop();
this->is_running_ = false;
}

void DeviceManager::terminate()
Expand Down
Loading

0 comments on commit 207b4fd

Please sign in to comment.