-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增 GPIO、Focuser、USB 和 Telescope 中间件,重构 CMake 配置,删除不再使用的内置任务功能
- Loading branch information
Showing
21 changed files
with
2,683 additions
and
2,198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
Oops, something went wrong.