Ver Fonte

采集程序初始

Signed-off-by: wlxuz <myxuan475@126.com>
wlxuz há 9 meses atrás
commit
d4e715a425
7 ficheiros alterados com 183 adições e 0 exclusões
  1. 12 0
      CMakeLists.txt
  2. 0 0
      README.md
  3. 22 0
      acq_task.cpp
  4. 24 0
      acq_task.h
  5. 74 0
      acq_vendor.cpp
  6. 31 0
      acq_vendor.h
  7. 20 0
      main.cpp

+ 12 - 0
CMakeLists.txt

@@ -0,0 +1,12 @@
+set(MODULE iot-acq)
+
+include_directories(./
+        )
+aux_source_directory(./ SRC)
+add_executable(${MODULE} ${SRC})
+# add_dependencies(${MODULE} vendor)
+target_link_directories(${MODULE} PUBLIC ${JSONCPP_LIBS_DIR})
+target_link_libraries(${MODULE} jsoncpp settings vendor leoyun)
+target_include_directories(${MODULE} PUBLIC ${JSONCPP_INCLUDE_DIR}
+    ${PRODUCT_ROOT_DIR}/foundation/webconfig/
+    ${PRODUCT_ROOT_DIR}/communications)

+ 0 - 0
README.md


+ 22 - 0
acq_task.cpp

@@ -0,0 +1,22 @@
+#include "acq_task.h"
+
+namespace iot_acq {
+void AcqTask::OnEvent(void* args) {}
+
+void AcqTask::OnTimer(void* args) {
+    for (auto &pair : mapRpc_) {
+        std::shared_ptr<leoyun::YunRpc> rpc = pair.second;
+        if (rpc == nullptr) {
+            HTELINK_LOG_INFO("sth error about %s", pair.first.c_str());
+            continue;
+        }
+        rpc->Run();
+    }
+}
+
+void AcqTask::OnShot(void* args) {}
+void AcqTask::PushRpc(const std::string& name,
+                      std::shared_ptr<leoyun::YunRpc> rpc) {
+    mapRpc_.insert(std::pair<std::string, std::shared_ptr<leoyun::YunRpc>>(name, rpc));
+}
+}  // namespace iot_acq

+ 24 - 0
acq_task.h

@@ -0,0 +1,24 @@
+#ifndef FOUNDATION_IOTACQ_TASK_H
+#define FOUNDATION_IOTACQ_TASK_H
+
+#include <vendor_global.h>
+#include <settings/config_parser.h>
+#include <leoyun/yun_rpc.h>
+#include <unordered_map>
+#include <uv.h>
+
+namespace iot_acq {
+class AcqTask : public vendor::VendorIf {
+public:
+    virtual void OnEvent(void *args);
+    virtual void OnTimer(void *args);
+    virtual void OnShot(void *args);
+
+    void PushRpc(const std::string &name, std::shared_ptr<leoyun::YunRpc> rpc);
+
+private:
+    std::unordered_map<std::string, std::shared_ptr<leoyun::YunRpc>> mapRpc_;
+};
+} // namespace iot_acq
+
+#endif // FOUNDATION_IOTACQ_TASK_H

+ 74 - 0
acq_vendor.cpp

@@ -0,0 +1,74 @@
+
+#include "acq_vendor.h"
+#include <communications/communication.h>
+#include <json/json.h>
+#include <settings/config_parser.h>
+
+static vendor::MyOpt myOpts_[] = {
+    {"help",     no_argument,       'h', "[options] [documents] [IPaddress][:port]..."       },
+    {"auth",     required_argument, 0,   "User and role configuration"                       },
+    {"debug",    no_argument,       0,   "Run in debug mode"                                 },
+    {"home",     required_argument, 0,   "Change to directory to run"                        },
+    {"log",      required_argument, 0,   "logFile:level, Log to file file at verbosity level"},
+    {"route",    required_argument, 0,   "route file"                                        },
+    {"port",     required_argument, 0,   "set port, default: 8880"                           },
+    {"delay",    required_argument, 0,   "delay secs to quit"                                },
+    {"document", required_argument, 0,   "document"                                          },
+    {"verbose",  required_argument, 0,   "Same as --log stderr:2"                            },
+    {"version",  no_argument,       0,   "version information"                               }
+};
+
+namespace iot_acq {
+AcqVendor::AcqVendor():
+    acqTask_(new AcqTask())
+{
+}
+
+AcqVendor::~AcqVendor()
+{
+}
+
+int AcqVendor::ParseCmdline(const std::string &optname, const std::string &optarg)
+{
+
+    return 0;
+}
+
+vendor::MyOpt *AcqVendor::GetOpts()
+{
+    return myOpts_;
+}
+
+int AcqVendor::GetOptSize()
+{
+    return sizeof(myOpts_) / sizeof(myOpts_[0]);
+}
+
+void sigHandler(int sig)
+{
+    HTELINK_LOG_DEBUG("sigHandler, %d", sig);
+}
+
+int AcqVendor::Run()
+{
+    std::vector<settings::ConfigParser::DataCenter> dataCenters = configParser_.GetDataCenter();
+    for(settings::ConfigParser::DataCenter &dc: dataCenters) {
+        std::shared_ptr<leoyun::YunRpc> rpc = leoyun::YunRpc::GetRpcInstance(dc.yunID);
+        rpc->SetDataCenter(dc);
+        acqTask_->PushRpc(dc.name, rpc);
+    }
+    timerCheck_ = AppendTimerEvent(100, acqTask_);
+    return 0;
+}
+
+std::string AcqVendor::Name()
+{
+    return "iot-acq";
+}
+
+void AcqVendor::Stop(int signal)
+{
+    HTELINK_LOG_INFO("acq exit, signal = %d", signal);
+    StopEvent(timerCheck_);
+}
+}  // namespace iot_acq

+ 31 - 0
acq_vendor.h

@@ -0,0 +1,31 @@
+#ifndef FOUNDATION_IOTACQ_ACQ_H
+#define FOUNDATION_IOTACQ_ACQ_H
+
+#include <vendor_global.h>
+#include <settings/config_parser.h>
+#include <leoyun/yun_rpc.h>
+#include <unordered_map>
+#include <uv.h>
+#include "acq_task.h"
+
+namespace iot_acq {
+class AcqVendor : public vendor::VendorBase {
+public:
+    AcqVendor();
+    virtual ~AcqVendor();
+
+    virtual int Run();
+    virtual std::string Name();
+    virtual void Stop(int signal);
+protected:
+    virtual int ParseCmdline(const std::string &optname, const std::string &optarg);
+    virtual vendor::MyOpt *GetOpts();
+    virtual int GetOptSize();
+
+private:
+    settings::ConfigParser configParser_;
+    uv_handle_t* timerCheck_ = nullptr;
+    AcqTask *acqTask_ = nullptr;
+};
+}
+#endif // FOUNDATION_IOTACQ_ACQ_H

+ 20 - 0
main.cpp

@@ -0,0 +1,20 @@
+#include "acq_vendor.h"
+
+int main(int argc, char *argv[])
+{
+    HTELINK_LOG_ENABLE(true);
+
+    HTELINK_LOG_INFO("check env set");
+    if (getenv(VENDOR_RUN_PATH_ENV) == nullptr) {
+        HTELINK_LOG_INFO("execute env not set");
+        std::string exe_dir = utils::get_executable_directory();
+        setenv(VENDOR_RUN_PATH_ENV, exe_dir.c_str(), 1);
+    }
+
+    HTELINK_LOG_INFO("execute directory is %s", getenv(VENDOR_RUN_PATH_ENV));
+    vendor::VendorBase *vdor = new iot_acq::AcqVendor();
+    if (vendor::VendorBase::ParseOptionArgs(vdor, argc, argv) != 0) {
+        exit(-1);
+    }
+    return vdor->Exec();
+}