Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed Sep 30, 2023
1 parent 837bdfe commit 24ccb16
Show file tree
Hide file tree
Showing 19 changed files with 745 additions and 156 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ add_subdirectory(tools)

include_directories(modules)

add_subdirectory(modules/HydrogenClient)
if(NOT WIN32)
add_subdirectory(modules/HydrogenServer)
endif()
add_subdirectory(modules/libridge)

add_subdirectory(drivers)

add_subdirectory(${lithium_src_dir}/core)
Expand Down
21 changes: 7 additions & 14 deletions src/LithiumApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace Lithium
std::shared_ptr<LithiumApp> MyApp = nullptr;
LithiumApp::LithiumApp()
{
LOG_F(INFO, "Loading Lithium App and preparing ...");
try
{
m_ConfigManager = Config::ConfigManager::createShared();
Expand All @@ -61,11 +60,10 @@ namespace Lithium
m_MessageBus->StartProcessingThread<IStringProperty>();
m_MessageBus->StartProcessingThread<IBoolProperty>();
m_MessageBus->StartProcessingThread<INumberProperty>();
LOG_F(INFO, "Lithium App Loaded.");
}
catch (const std::exception &e)
{
LOG_F(ERROR, "Failed to load Lithium App , error : %s", e.what());
LOG_F(ERROR, _("Failed to load Lithium App , error : {}"), e.what());
throw std::runtime_error("Failed to load Lithium App");
}
}
Expand All @@ -77,13 +75,13 @@ namespace Lithium

nlohmann::json LithiumApp::GetConfig(const std::string &key_path) const
{
LOG_F(INFO, "Get value : %s", key_path.c_str());
DLOG_F(INFO, _("Get config value: {}"), key_path);
return m_ConfigManager->getValue(key_path);
}

void LithiumApp::SetConfig(const std::string &key_path, const nlohmann::json &value)
{
LOG_F(INFO, "Set %s to %s", key_path.c_str(), value.dump().c_str());
DLOG_F(INFO, _("Set {} to {}"), key_path, value.dump());
m_ConfigManager->setValue(key_path, value);
}

Expand Down Expand Up @@ -257,11 +255,6 @@ namespace Lithium
m_ThreadManager->joinThreadByName(name);
}

bool LithiumApp::sleepThreadByName(const std::string &name, int seconds)
{
return m_ThreadManager->sleepThreadByName(name, seconds);
}

bool LithiumApp::isThreadRunning(const std::string &name)
{
return m_ThreadManager->isThreadRunning(name);
Expand All @@ -275,7 +268,7 @@ namespace Lithium
}
else
{
LOG_F(ERROR, "Failed to run chai command : %s", command.c_str());
LOG_F(ERROR, _("Failed to run chai command : {}"), command);
}
return false;
}
Expand All @@ -293,7 +286,7 @@ namespace Lithium
{
result += str + "\n";
}
LOG_F(ERROR, "Failed to run chai multi command %s", result.c_str());
LOG_F(ERROR, _("Failed to run chai multi command {}"), result);
}
return true;
}
Expand All @@ -306,7 +299,7 @@ namespace Lithium
}
else
{
LOG_F(ERROR, "Failed to load chaiscript file %s", filename.c_str());
LOG_F(ERROR, _("Failed to load chaiscript file {}"), filename);
return false;
}
}
Expand All @@ -319,7 +312,7 @@ namespace Lithium
}
else
{
LOG_F(ERROR, "Failed to run chai script %s", filename.c_str());
LOG_F(ERROR, _("Failed to run chai script {}"), filename);
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/LithiumApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ namespace Lithium
void addThread(std::function<void()> func, const std::string &name);
void joinAllThreads();
void joinThreadByName(const std::string &name);
bool sleepThreadByName(const std::string &name, int seconds);
bool isThreadRunning(const std::string &name);

public:
Expand Down
32 changes: 31 additions & 1 deletion src/modules/device/device_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Description: Device Manager

#include "core/camera_utils.hpp"

#define LOGURU_USE_FMTLIB
#include "device_utils.hpp"

#include "loguru/loguru.hpp"

#ifdef __cpp_lib_format
Expand All @@ -57,6 +58,10 @@ Description: Device Manager
#include "indi_device.hpp"
#include "indidevice_manager.hpp"

#ifndef _WIN32
#include "deviceloader/hydrogen_server.hpp"
#endif

// For DEVICE_FUNC

#define CHECK_MAIN_CAMERA \
Expand Down Expand Up @@ -1295,4 +1300,29 @@ namespace Lithium
{
return true;
}

bool DeviceManager::runHydrogenServer(const nlohmann::json &m_params);
{
#ifdef _WIN32

#else

#endif
}
bool DeviceManager::startHydrogenDriver(const nlohmann::json &m_params)
{
#ifdef _WIN32

#else

#endif
}
bool DeviceManager::stopHydrogenDriver(const nlohmann::json &m_params)
{
#ifdef _WIN32

#else

#endif
}
}
9 changes: 8 additions & 1 deletion src/modules/device/device_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,14 @@ namespace Lithium

std::shared_ptr<INDIManager> m_indimanager;
std::shared_ptr<INDIDriverCollection> m_indicollection;
// std::shared_ptr<HydrogenManager> m_hydrogenmanager;

// For Hydrogen Inside Server
public:
bool runHydrogenServer(const nlohmann::json &m_params);
bool startHydrogenDriver(const nlohmann::json &m_params);
bool stopHydrogenDriver(const nlohmann::json &m_params);
private:
std::jthread m_hydrogen_server_thread;
};

}
104 changes: 96 additions & 8 deletions src/modules/device/device_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,10 @@ Description: Device Utilities

#include "device_utils.hpp"

#ifdef _WIN32
#include <windows.h>
#else

#endif
#include <iostream>
#include <array>
#include <memory>
#include <string>
#include <stdexcept>

#include <sstream>

#ifdef _WIN32
Expand All @@ -51,6 +44,13 @@ Description: Device Utilities
#include <sys/types.h>
#include <sys/wait.h>
#endif
#include <regex>
#include <iostream>
#include <array>
#include <memory>
#include <stdexcept>
#include <iomanip>
#include <sstream>

#ifdef _WIN32
std::string execute_command(const std::string &cmd)
Expand Down Expand Up @@ -96,4 +96,92 @@ std::string execute_command(const std::string &cmd)
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return result;
}
}
bool checkTimeFormat(const std::string &str)
{
std::regex timeRegex(R"(\d{1,2}(:\d{1,2}){0,2})");
return std::regex_match(str, timeRegex);
}

std::string convertToTimeFormat(int num)
{
int hours = num / 3600;
int minutes = (num % 3600) / 60;
int seconds = num % 60;
std::ostringstream oss;
oss << std::setw(2) << std::setfill('0') << hours << ":"
<< std::setw(2) << std::setfill('0') << minutes << ":"
<< std::setw(2) << std::setfill('0') << seconds;
return oss.str();
}

bool checkDigits(const std::string &str)
{
for (char c : str)
{
if (!std::isdigit(c))
{
return false;
}
}
return true;
}
#else
std::string execute_command(const std::string &cmd)
{
std::array<char, 128> buffer;
std::string result = "";

int pipeOut[2];
if (pipe(pipeOut) == -1)
{
throw std::runtime_error("Failed to create pipe!");
}

pid_t childPid = fork();
if (childPid == -1)
{
throw std::runtime_error("Failed to fork process!");
}
else if (childPid == 0)
{
// Child process
close(pipeOut[0]); // Close unused read end of the pipe

// Redirect stdout and stderr to the write end of the pipe
if (dup2(pipeOut[1], STDOUT_FILENO) == -1 || dup2(pipeOut[1], STDERR_FILENO) == -1)
{
throw std::runtime_error("Failed to redirect output!");
}
close(pipeOut[1]); // Close the write end of the pipe

// Execute the command
execl("/bin/sh", "sh", "-c", cmd.c_str(), NULL);

// This point is reached only if execl fails
throw std::runtime_error("Failed to execute command!");
}
else
{
// Parent process
close(pipeOut[1]); // Close unused write end of the pipe

ssize_t bytesRead;
while ((bytesRead = read(pipeOut[0], buffer.data(), buffer.size())) > 0)
{
result.append(buffer.data(), bytesRead);
}
close(pipeOut[0]); // Close the read end of the pipe

int status;
waitpid(childPid, &status, 0); // Wait for the child process to exit

// Handle any error status
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
{
throw std::runtime_error("Command execution failed with non-zero exit status!");
}
}

return result;
}
1 change: 1 addition & 0 deletions src/modules/deviceloader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ else()
add_executable(${PROJECT_NAME} ${server_SRC} hydrogen_server.cpp)

target_compile_definitions(${PROJECT_NAME} PRIVATE USE_LIBUV=1)
target_compile_definitions(${PROJECT_NAME} PRIVATE MAIN_FUNC=1)

target_link_libraries(hydrogenserver hydrogendriverstatic ${CMAKE_THREAD_LIBS_INIT} ${LIBEV_LIBRARIES})
target_include_directories(hydrogenserver SYSTEM PRIVATE ${LIBEV_INCLUDE_DIRS})
Expand Down
Loading

0 comments on commit 24ccb16

Please sign in to comment.