diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..6249558 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/src", + "${workspaceFolder}/iflytoplinuxsdk/src/", + ], + "compilerPath": "/usr/bin/g++", + "cStandard": "gnu11", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64", + "compileCommands": "${workspaceFolder}/build/compile_commands.json" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index e3a4e56..018a8d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,18 +50,20 @@ file( ) message("VAR_APP_SOURCE: ${VAR_APP_SOURCE}") - zadd_executable( TARGET app.out # SRC ${VAR_APP_SOURCE} - + LINK_DIRECTORIES + libs/libixwebsocket/lib LINK_LIBRARIES pthread + libixwebsocket.a # INCLUDE_DIRECTORIES src iflytoplinuxsdk/src/iflytop/core/spdlog/include/ iflytoplinuxsdk/src/ + libs/libixwebsocket/include ./) diff --git a/README.md b/README.md index e69de29..2a490d4 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,18 @@ + +# README + +``` +1. 功能点 + + FrontEndService + MainCtrlService + UnixCan + IflytopCmdServer + SqlService + UsrDB + SensorDataDB + DisinfectionRecordDB + DeviceConfigDB + + +``` \ No newline at end of file diff --git a/iflytoplinuxsdk b/iflytoplinuxsdk index 3edbad7..9fbd847 160000 --- a/iflytoplinuxsdk +++ b/iflytoplinuxsdk @@ -1 +1 @@ -Subproject commit 3edbad7ca097f108cef97f9e89e80da1310263e5 +Subproject commit 9fbd8473108b10c6d0e69ed663fa27fee5d4dfa0 diff --git a/init.sh b/init.sh old mode 100644 new mode 100755 index e69de29..394afe0 --- a/init.sh +++ b/init.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +LIBSDIR=$(pwd)/libs/ +mkdir -p ${LIBSDIR} + +function prepare_ifytop_librelease_libs() { + cd ${IFLYBUILD_TARGET_LIBS_DIR} + libname=$1 + linklibs=$2 + url=$3 + wget -c $3 + tarname=$(basename $3) + # unpackname=tarname 去掉.tar.gz, 例如iflytopv1.3.0.tar.gz -> iflytopv1.3.0 + unpackname=${tarname%.tar.gz} + tar -xvf $tarname >/dev/null + rm -rf ${LIBSDIR}/$libname + mv $unpackname ${LIBSDIR}/$libname + LINK_DIRECTORIES="${LINK_DIRECTORIES};${LIBSDIR}/$libname/lib" + INCLUDE_DIRECTORIES="${INCLUDE_DIRECTORIES};${LIBSDIR}/$libname/include" + LINK_LIBS="${LINK_LIBS};$linklibs" +} + +prepare_ifytop_librelease_libs libixwebsocket "ixwebsocket" "http://192.168.1.2:8021/zlibrelease/libixwebsocket/libixwebsocket_v11.4.3_aarch64_static_ubuntu18.04_0.tar.gz" + +echo "$LINK_DIRECTORIES" +echo "$INCLUDE_DIRECTORIES" +echo "$LINK_LIBS" + diff --git a/src/configs/gconfig.hpp b/src/configs/gconfig.hpp new file mode 100644 index 0000000..f5c3ae6 --- /dev/null +++ b/src/configs/gconfig.hpp @@ -0,0 +1,27 @@ + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytop/core/components/config_template/config_template.hpp" + +#define ConfigELEMENT_LIST(marco) \ + marco(string /* */, deviceId, "") /*设备ID*/ \ + marco(string /* */, iflytopSubDeviceCanIFName, "can0") /*子设备Can设备名称*/ \ + marco(int32_t /* */, iflytopSubDeviceCanBitrate, 500000) /*子设备Can设备波特率*/ \ + marco(string /* */, pipettingRobotCanIFName, "can1") /*移液臂Can设备名称*/ \ + marco(int32_t /* */, pipettingRobotCanBitrate, 500000) /*移液臂Can设备波特率*/ + +configTemplateDEFILE_CONFIG_SERVICE2( // + GConfig, // + ConfigELEMENT_LIST, // + "./configs/config.json", {}); diff --git a/src/configs/project_setting.hpp b/src/configs/project_setting.hpp new file mode 100644 index 0000000..fac8c71 --- /dev/null +++ b/src/configs/project_setting.hpp @@ -0,0 +1,10 @@ +#pragma once +#define LINUX_HARDWARE_ID (0x02) + +/******************************************************************************* + * LINUX_HARDWARE_REG * + *******************************************************************************/ + +#define REG_REBOOT_FLAG 0x0000 // 重启标识 +#define REG_DEVICE_ID 0x0001 // 重启标识 +#define REG_EXCEPTION_FLAG 0x0100 // 异常标志位 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index be837a4..d22cfec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,58 @@ -int main(int argc, char const *argv[]) -{ - - return 0; +#include "main.hpp" + #include +using namespace iflytop; +using namespace core; +using namespace std; +/******************************************************************************* + * MAIN => MAIN * + *******************************************************************************/ +Main *Main::g_main; +int main(int argc, char *argv[]) { + Main main; + Main::g_main = &main; + main.run(argc, argv); +} +void Main::onsignal(int signo) { exit(0); } +void Main::run(int argc, char *argv[]) { + // ::signal(SIGINT, Main::_onsignal); + thread.reset(new Thread("main", [&]() { exit(main(argc, argv)); })); + while (true) sleep(1000); +} +int Main::main(int argc, char *argv[]) { + /** + * @brief 解析命令行参数 + */ + // string device_id; + // auto cli = ((required("-device_id") & value("device_id", device_id)).doc("device_id")); + // if (!parse(argc, argv, cli)) { + // cout << make_man_page(cli, argv[0]); + // exit(-1); + // } + + /** + * @brief 系统初始化 + */ + logger->info("system setup start."); + spdlog::flush_on(spdlog::level::debug); + logger->info("#"); + logger->info("# company:{}", "ifytop"); + logger->info("# version:{}", VERSION); + logger->info("#"); + logger->info("build {}.....", "Config"); + // 构造GConfig + BUILD_AND_REG_SERRVICE(GConfig); + GET_SERVICE(GConfig)->initialize(); + + /** + * @brief + */ + // if (!device_id.empty()) GET_SERVICE(GConfig)->set_deviceId(device_id); + + auto config = GET_SERVICE(GConfig); + + // 构造MainControlService + BUILD_AND_REG_SERRVICE(MainControlService); + GET_SERVICE(MainControlService)->initialize(); + logger->info("system setup end."); + while (true) sleep(1000); } diff --git a/src/main.hpp b/src/main.hpp new file mode 100644 index 0000000..80eeae6 --- /dev/null +++ b/src/main.hpp @@ -0,0 +1,51 @@ +// #include +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// +#include "configs/gconfig.hpp" +#include "iflytop/core/spdlogfactory/logger.hpp" +#include "iflytop/core/thread/thread.hpp" +#include "version.hpp" +#include "zservice_container/zservice_container.hpp" +#include "iflytop/components/zclicpp/clipp.h" +// +#include "main_control_service.hpp" + +namespace iflytop { +using namespace iflytop; +using namespace core; +using namespace std; +using namespace clipp; +class Main { + ENABLE_LOGGER(Main); + + private: + unique_ptr thread; + + private: + int main(int argc, char *argv[]); + + public: + static Main *g_main; + static void _onsignal(int signo) { g_main->onsignal(signo); } + void onsignal(int signo); + + public: + Main(/* args */) {} + ~Main() {} + void run(int argc, char *argv[]); + void dosystem(string order, bool dump) { + if (dump) logger->info("{}", order); + system(order.c_str()); + } +}; +} // namespace iflytop diff --git a/src/main_control_service.cpp b/src/main_control_service.cpp new file mode 100644 index 0000000..0ce66af --- /dev/null +++ b/src/main_control_service.cpp @@ -0,0 +1,42 @@ +#include "main_control_service.hpp" + +#include "configs/project_setting.hpp" +#include "iflytop/core/components/stringutils.hpp" +#include "version.hpp" +using namespace iflytop; +using namespace core; +using namespace std; + +#define BIND + +void MainControlService::initialize() { + GET_TO_SERVICE(m_zconfig); + // m_iflytopCanProtocolControler.reset(new IflytopCanProtocolControler()); + // m_iflytopCanProtocolControler->initialize(m_zconfig->get_iflytopSubDeviceCanIFName(), m_zconfig->get_iflytopSubDeviceCanBitrate(), false); + // m_iflytopCanProtocolControler->start(); + + m_restfulServer.reset(new RestfulServer()); + m_restfulServer->regAPI("/hello_world", RESTFUL_SERVER_BIND(MainControlService::hello_world)); + m_restfulServer->regAPI("/api1/script_processer/doscript", RESTFUL_SERVER_BIND(MainControlService::doscript)); + m_restfulServer->regAPI("/api1/script_processer/stopscript", RESTFUL_SERVER_BIND(MainControlService::stopscript)); + m_restfulServer->start(20000, 20001, "0.0.0.0"); +}; + +HttpResponsePtr MainControlService::hello_world( // + HttpRequestPtr request, shared_ptr context, std::shared_ptr) { + return std::make_shared(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "hello_world"); +} + +HttpResponsePtr MainControlService::doscript(HttpRequestPtr httpreq, shared_ptr context, std::shared_ptr conn) { + // logger->info("do\n{}", httpreq->body); + // if (m_a8000_script_processer->isWorking()) { + // return std::make_shared(200, "FAIL", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script fail, script processer is running"); + // } + // m_a8000_script_processer->executeScript(httpreq->body); + return std::make_shared(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script success"); +} + +HttpResponsePtr MainControlService::stopscript(HttpRequestPtr, shared_ptr, std::shared_ptr) { + // m_a8000_script_processer->stopScript(); + return std::make_shared(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "stop script success"); +} diff --git a/src/main_control_service.hpp b/src/main_control_service.hpp new file mode 100644 index 0000000..1be99e3 --- /dev/null +++ b/src/main_control_service.hpp @@ -0,0 +1,74 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytop/core/spdlogfactory/logger.hpp" +#include "iflytop/components/restful_server/restful_server.hpp" +// +#include "configs/gconfig.hpp" +#include "zservice_container/zservice_container.hpp" + +// +#include "configs/gconfig.hpp" +#include "iflytop/core/components/jobs/work_queue.hpp" +#include "iflytop/core/components/timer/simple_timer.hpp" + +// #include "zlinuxcomponents/iflytop_can_protocol_stack/basic/iflytop_virtual_can_slave.hpp" +// #include "zlinuxcomponents/iflytop_can_protocol_stack/iflytop_can_protocol_controler.hpp" +// + +/** + * @brief + * + * service: MainControlService + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +using namespace std; +using namespace core; +class MainControlService : public enable_shared_from_this { + ENABLE_LOGGER(MainControlService); + + shared_ptr m_zconfig; + shared_ptr m_restfulServer; + + public: + MainControlService(){}; + void initialize(); + + private: + void dosystem(string order, bool dump) { + if (dump) logger->info("do:{}", order); + system(order.c_str()); + } + + private: + HttpResponsePtr reactionConfigCardInfo_read(HttpRequestPtr, shared_ptr, std::shared_ptr); + HttpResponsePtr reactionConfigCardInfo_trigger_once_report(HttpRequestPtr, shared_ptr, std::shared_ptr); + HttpResponsePtr reactionConfigCardInfo_read_state(HttpRequestPtr, shared_ptr, std::shared_ptr); + HttpResponsePtr hello_world(HttpRequestPtr, shared_ptr, std::shared_ptr); + // + HttpResponsePtr doscript(HttpRequestPtr, shared_ptr, std::shared_ptr); + HttpResponsePtr doscriptblock(HttpRequestPtr, shared_ptr, std::shared_ptr); + HttpResponsePtr stopscript(HttpRequestPtr, shared_ptr, std::shared_ptr); + + void createReactionConfigCardInfoReportAndSend(); +}; +} // namespace iflytop \ No newline at end of file diff --git a/deplib/.mark b/src/service/front_end_service.cpp similarity index 100% rename from deplib/.mark rename to src/service/front_end_service.cpp diff --git a/src/service/front_end_service.hpp b/src/service/front_end_service.hpp new file mode 100644 index 0000000..c2eb1c2 --- /dev/null +++ b/src/service/front_end_service.hpp @@ -0,0 +1,41 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytop/core/spdlogfactory/logger.hpp" +#include "iflytop/core/thread/thread.hpp" + +/** + * @brief + * + * service: FrontEndService + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +using namespace std; +using namespace core; +class FrontEndService : public enable_shared_from_this { + ENABLE_LOGGER(FrontEndService); + + public: + FrontEndService(){}; + void initialize(){}; +}; +} // namespace iflytop \ No newline at end of file diff --git a/src/version.hpp b/src/version.hpp new file mode 100644 index 0000000..025956d --- /dev/null +++ b/src/version.hpp @@ -0,0 +1,2 @@ +#pragma once +#define VERSION "0.1" \ No newline at end of file diff --git a/src/zservice_container/zservice_container.cpp b/src/zservice_container/zservice_container.cpp new file mode 100644 index 0000000..4f93ceb --- /dev/null +++ b/src/zservice_container/zservice_container.cpp @@ -0,0 +1,8 @@ +#include "zservice_container.hpp" +using namespace iflytop; +using namespace std; + +ServiceContrainer& ServiceContrainer::get() { + static ServiceContrainer serviceContrainer; + return serviceContrainer; +} \ No newline at end of file diff --git a/src/zservice_container/zservice_container.hpp b/src/zservice_container/zservice_container.hpp new file mode 100644 index 0000000..710aeab --- /dev/null +++ b/src/zservice_container/zservice_container.hpp @@ -0,0 +1,200 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief + * + * service: ServiceContrainer + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ +// std::shared_ptr buf(new Buffer()); +// ZserviceContainerAny any(buf); +// std::shared_ptr b = any.Get>(); +// assert(buf.get() == b.get()); +namespace iflytop { +using namespace std; + +class ZserviceContainerAny { + public: + ZserviceContainerAny() : content_(nullptr) {} + ~ZserviceContainerAny() { delete content_; } + + template + explicit ZserviceContainerAny(const ValueType& value) : content_(new Holder(value)) {} + + ZserviceContainerAny(const ZserviceContainerAny& other) + : content_(other.content_ ? other.content_->clone() : nullptr) {} + + public: + ZserviceContainerAny& swap(ZserviceContainerAny& rhs) { + std::swap(content_, rhs.content_); + return *this; + } + + template + ZserviceContainerAny& operator=(const ValueType& rhs) { + ZserviceContainerAny(rhs).swap(*this); + return *this; + } + + ZserviceContainerAny& operator=(const ZserviceContainerAny& rhs) { + ZserviceContainerAny(rhs).swap(*this); + return *this; + } + + bool IsEmpty() const { return !content_; } + + const std::type_info& GetType() const { return content_ ? content_->GetType() : typeid(void); } + + template + ValueType operator()() const { + return Get(); + } + + template + ValueType Get() const { + if (GetType() == typeid(ValueType)) { + return static_cast*>(content_)->held_; + } else { + return ValueType(); + } + } + + protected: + class ZPlaceHolder { + public: + virtual ~ZPlaceHolder() {} + + public: + virtual const std::type_info& GetType() const = 0; + virtual ZPlaceHolder* clone() const = 0; + }; + + template + class Holder : public ZPlaceHolder { + public: + Holder(const ValueType& value) : held_(value) {} + + virtual const std::type_info& GetType() const { return typeid(ValueType); } + + virtual ZPlaceHolder* clone() const { return new Holder(held_); } + + ValueType held_; + }; + + protected: + ZPlaceHolder* content_; + template + friend ValueType* zany_cast(ZserviceContainerAny*); +}; + +template +ValueType* zany_cast(ZserviceContainerAny* any) { + if (any && any->GetType() == typeid(ValueType)) { + return &(static_cast*>(any->content_)->held_); + } + + return nullptr; +} + +template +const ValueType* zany_cast(const ZserviceContainerAny* any) { + return zany_cast(const_cast(any)); +} + +template +ValueType zany_cast(const ZserviceContainerAny& any) { + const ValueType* result = zany_cast(&any); + assert(result); + + if (!result) { + return ValueType(); + } + + return *result; +} + +class ServiceContrainer { + map services; + ServiceContrainer(){}; + + public: + static ServiceContrainer& get(); + + template + void regAndInitializeService(shared_ptr object) { + if (object == nullptr) { + return; + } + object->initialize(); + type_index index = typeid(T); + services[index] = ZserviceContainerAny(object); + }; + template + void regService(shared_ptr object) { + if (object == nullptr) { + return; + } + type_index index = typeid(T); + services[index] = ZserviceContainerAny(object); + }; + template + shared_ptr getService() { + type_index index = typeid(T); + auto result = services.find(index); + if (result == services.end()) { + return nullptr; + } + return result->second.Get>(); + }; + + template + void getToService(shared_ptr& service) { + type_index index = typeid(T); + auto result = services.find(index); + if (result == services.end()) { + service = nullptr; + } + service = result->second.Get>(); + }; +}; + +#define GET_SERVICE(type) (ServiceContrainer::get().getService()) +#define GET_TO_SERVICE(value) \ + ServiceContrainer::get().getToService(value); \ + if (value == nullptr) { \ + logger->error("[{}:{}]GET_TO_SERVICE({}) fail", __FILE__, __LINE__, #value); \ + exit(-1); \ + }; + +#define BUILD_AND_REG_SERRVICE(type, ...) \ + logger->info("build {}.....", #type); \ + shared_ptr type##_val(new type(__VA_ARGS__)); \ + ServiceContrainer::get().regService(type##_val); + +#define BUILD_AND_REG_MOCK_SERRVICE(type, mocktype, ...) \ + logger->info("build {}.....", #type); \ + shared_ptr type##_val(new mocktype(__VA_ARGS__)); \ + ServiceContrainer::get().regService(type##_val); + +} // namespace iflytop \ No newline at end of file