Skip to content

Commit

Permalink
新增 GPIO、Focuser、USB 和 Telescope 中间件,重构 CMake 配置,删除不再使用的内置任务功能
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed Nov 10, 2024
1 parent 7fac5c6 commit b509219
Show file tree
Hide file tree
Showing 21 changed files with 2,683 additions and 2,198 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ set(debug_module
)

set(device_module
${lithium_src_dir}/device/manager.cpp
${lithium_src_dir}/device/template/device.cpp
)

Expand Down
121 changes: 121 additions & 0 deletions src/atom/system/crontab.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "crontab.hpp"

#include <fstream>

#include "atom/type/json.hpp"

using json = nlohmann::json;

auto CronJob::toJson() const -> json {
return json{{"time", time_}, {"command", command_}};
}

auto CronJob::fromJson(const json& jsonObj) -> CronJob {
return CronJob{jsonObj.at("time").get<std::string>(),
jsonObj.at("command").get<std::string>()};
}

auto CronManager::createCronJob(const CronJob& job) -> bool {
std::string command = "crontab -l 2>/dev/null | { cat; echo \"" +
job.time_ + " " + job.command_ + "\"; } | crontab -";
if (system(command.c_str()) == 0) {
jobs_.push_back(job);
return true;
}
return false;
}

auto CronManager::deleteCronJob(const std::string& command) -> bool {
std::string jobToDelete = " " + command;
std::string cmd =
"crontab -l | grep -v \"" + jobToDelete + "\" | crontab -";
if (system(cmd.c_str()) == 0) {
jobs_.erase(std::remove_if(jobs_.begin(), jobs_.end(),
[&](const CronJob& job) {
return job.command_ == command;
}),
jobs_.end());
return true;
}
return false;
}

auto CronManager::listCronJobs() -> std::vector<CronJob> {
std::vector<CronJob> currentJobs;
std::string cmd = "crontab -l";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe)
return currentJobs;

constexpr size_t bufferSize = 128;
char buffer[bufferSize];
while (fgets(buffer, sizeof buffer, pipe) != nullptr) {
std::string line(buffer);
size_t spacePos = line.find(' ');
if (spacePos != std::string::npos) {
currentJobs.push_back(
{line.substr(0, spacePos), line.substr(spacePos + 1)});
}
}
pclose(pipe);
return currentJobs;
}

auto CronManager::exportToJSON(const std::string& filename) -> bool {
json jsonObj;
for (const auto& job : jobs_) {
jsonObj.push_back(job.toJson());
}
std::ofstream file(filename);
if (file.is_open()) {
file << jsonObj.dump(4);
return true;
}
return false;
}

auto CronManager::importFromJSON(const std::string& filename) -> bool {
std::ifstream file(filename);
if (!file.is_open()) {
return false;
}

json jsonObj;
file >> jsonObj;

for (const auto& jobJson : jsonObj) {
CronJob job = CronJob::fromJson(jobJson);
createCronJob(job);
}
return true;
}

auto CronManager::updateCronJob(const std::string& oldCommand,
const CronJob& newJob) -> bool {
if (deleteCronJob(oldCommand)) {
return createCronJob(newJob);
}
return false;
}

auto CronManager::viewCronJob(const std::string& command) -> CronJob {
auto iterator = std::find_if(
jobs_.begin(), jobs_.end(),
[&](const CronJob& job) { return job.command_ == command; });
return (iterator != jobs_.end()) ? *iterator
: CronJob{"", ""}; // 返回一个空的任务
}

auto CronManager::searchCronJobs(const std::string& query)
-> std::vector<CronJob> {
std::vector<CronJob> foundJobs;
for (const auto& job : jobs_) {
if (job.command_.find(query) != std::string::npos ||
job.time_.find(query) != std::string::npos) {
foundJobs.push_back(job);
}
}
return foundJobs;
}

auto CronManager::statistics() -> int { return static_cast<int>(jobs_.size()); }
101 changes: 101 additions & 0 deletions src/atom/system/crontab.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifndef CRONJOB_H
#define CRONJOB_H

#include <string>
#include "atom/type/json_fwd.hpp"

/**
* @brief Represents a Cron job with a scheduled time and command.
*/
struct alignas(64) CronJob {
public:
std::string time_; ///< Scheduled time for the Cron job.
std::string command_; ///< Command to be executed by the Cron job.

/**
* @brief Converts the CronJob object to a JSON representation.
* @return JSON representation of the CronJob object.
*/
[[nodiscard]] auto toJson() const -> nlohmann::json;

/**
* @brief Creates a CronJob object from a JSON representation.
* @param jsonObj JSON object representing a CronJob.
* @return CronJob object created from the JSON representation.
*/
static auto fromJson(const nlohmann::json& jsonObj) -> CronJob;
};

/**
* @brief Manages a collection of Cron jobs.
*/
class CronManager {
public:
/**
* @brief Adds a new Cron job.
* @param job The CronJob object to be added.
* @return True if the job was added successfully, false otherwise.
*/
auto createCronJob(const CronJob& job) -> bool;

/**
* @brief Deletes a Cron job with the specified command.
* @param command The command of the Cron job to be deleted.
* @return True if the job was deleted successfully, false otherwise.
*/
auto deleteCronJob(const std::string& command) -> bool;

/**
* @brief Lists all current Cron jobs.
* @return A vector of all current CronJob objects.
*/
auto listCronJobs() -> std::vector<CronJob>;

/**
* @brief Exports all Cron jobs to a JSON file.
* @param filename The name of the file to export to.
* @return True if the export was successful, false otherwise.
*/
auto exportToJSON(const std::string& filename) -> bool;

/**
* @brief Imports Cron jobs from a JSON file.
* @param filename The name of the file to import from.
* @return True if the import was successful, false otherwise.
*/
auto importFromJSON(const std::string& filename) -> bool;

/**
* @brief Updates an existing Cron job.
* @param oldCommand The command of the Cron job to be updated.
* @param newJob The new CronJob object to replace the old one.
* @return True if the job was updated successfully, false otherwise.
*/
auto updateCronJob(const std::string& oldCommand,
const CronJob& newJob) -> bool;

/**
* @brief Views the details of a Cron job with the specified command.
* @param command The command of the Cron job to view.
* @return The CronJob object with the specified command.
*/
auto viewCronJob(const std::string& command) -> CronJob;

/**
* @brief Searches for Cron jobs that match the specified query.
* @param query The query string to search for.
* @return A vector of CronJob objects that match the query.
*/
auto searchCronJobs(const std::string& query) -> std::vector<CronJob>;

/**
* @brief Gets statistics about the current Cron jobs.
* @return An integer representing the statistics.
*/
auto statistics() -> int;

private:
std::vector<CronJob> jobs_; ///< List of Cron jobs.
};

#endif // CRONJOB_H
1 change: 1 addition & 0 deletions src/device/template/telescope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class AtomTelescope : public AtomDriver {
virtual auto setTelescopeTrackEnable(bool enable) -> bool = 0;

virtual auto setTelescopeAbortMotion() -> bool = 0;
virtual auto getTelescopeStatus() -> std::optional<std::string> = 0;

virtual auto setTelescopeParkOption(ParkOptions option) -> bool = 0;

Expand Down
Empty file.
8 changes: 8 additions & 0 deletions src/server/middleware/focuser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef LITHIUM_SERVER_MIDDLEWARE_FOCUSER_HPP
#define LITHIUM_SERVER_MIDDLEWARE_FOCUSER_HPP

#include <string>

namespace lithium::middleware {} // namespace lithium::middleware

#endif // LITHIUM_SERVER_MIDDLEWARE_FOCUSER_HPP
70 changes: 70 additions & 0 deletions src/server/middleware/gpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "gpio.hpp"

#include "atom/async/message_bus.hpp"
#include "atom/function/global_ptr.hpp"
#include "atom/log/loguru.hpp"
#include "atom/system/gpio.hpp"
#include "atom/utils/print.hpp"
#include "utils/constant.hpp"

#define GPIO_PIN_1 "516"
#define GPIO_PIN_2 "527"

namespace lithium::middleware {
void getGPIOsStatus() {
LOG_F(INFO, "getGPIOsStatus: Entering function");

std::shared_ptr<atom::async::MessageBus> messageBusPtr;
GET_OR_CREATE_PTR(messageBusPtr, atom::async::MessageBus,
Constants::MESSAGE_BUS)

const std::vector<std::pair<int, std::string>> gpioPins = {{1, GPIO_PIN_1},
{2, GPIO_PIN_2}};

for (const auto& [id, pin] : gpioPins) {
LOG_F(INFO, "getGPIOsStatus: Processing GPIO pin: {} with ID: %d",
pin.c_str(), id);
atom::system::GPIO gpio(pin);
int value = static_cast<int>(gpio.getValue());
LOG_F(INFO, "getGPIOsStatus: GPIO pin: {} has value: %d", pin.c_str(),
value);
messageBusPtr->publish("main",
"OutPutPowerStatus:{}:{}"_fmt(id, value));
}

LOG_F(INFO, "getGPIOsStatus: Exiting function");
}

void switchOutPutPower(int id) {
LOG_F(INFO, "switchOutPutPower: Entering function with ID: %d", id);

std::shared_ptr<atom::async::MessageBus> messageBusPtr;
GET_OR_CREATE_PTR(messageBusPtr, atom::async::MessageBus,
Constants::MESSAGE_BUS)

const std::vector<std::pair<int, std::string>> gpioPins = {{1, GPIO_PIN_1},
{2, GPIO_PIN_2}};

auto it = std::find_if(gpioPins.begin(), gpioPins.end(),
[id](const auto& pair) { return pair.first == id; });

if (it != gpioPins.end()) {
LOG_F(INFO, "switchOutPutPower: Found GPIO pin: {} for ID: %d",
it->second.c_str(), id);
atom::system::GPIO gpio(it->second);
bool newValue = !gpio.getValue();
LOG_F(INFO, "switchOutPutPower: Setting GPIO pin: {} to new value: %d",
it->second.c_str(), newValue);
gpio.setValue(newValue);
messageBusPtr->publish("main",
"OutPutPowerStatus:{}:{}"_fmt(id, newValue));
} else {
LOG_F(WARNING, "switchOutPutPower: No GPIO pin found for ID: %d", id);
}

LOG_F(INFO, "switchOutPutPower: Exiting function");
}
} // namespace lithium::middleware

#undef GPIO_PIN_1
#undef GPIO_PIN_2
9 changes: 9 additions & 0 deletions src/server/middleware/gpio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef LITHIUM_SERVER_MIDDLEWARE_GPIO_HPP
#define LITHIUM_SERVER_MIDDLEWARE_GPIO_HPP

namespace lithium::middleware {
void getGPIOsStatus();
void switchOutPutPower(int id);
} // namespace lithium::middleware

#endif
Empty file added src/server/middleware/image.cpp
Empty file.
Empty file added src/server/middleware/image.hpp
Empty file.
Loading

0 comments on commit b509219

Please sign in to comment.