BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
TestingCenter.h
1 #pragma once
2 
3 #include <functional>
4 #include <list>
5 #include <map>
6 #include <vector>
7 
8 #include <Poco/AtomicCounter.h>
9 #include <Poco/SharedPtr.h>
10 
11 #include "core/CommandHandler.h"
12 #include "core/CommandSender.h"
13 #include "credentials/FileCredentialsStorage.h"
14 #include "io/Console.h"
15 #include "loop/StoppableRunnable.h"
16 #include "model/DeviceID.h"
17 #include "model/DeviceDescription.h"
18 #include "model/ModuleID.h"
19 #include "util/CryptoConfig.h"
20 #include "util/Loggable.h"
21 
22 namespace BeeeOn {
23 
25  public CommandSender,
26  public CommandHandler,
27  public StoppableRunnable,
28  protected Loggable {
29 public:
30  typedef std::map<ModuleID, double> DeviceData;
31 
32  struct ActionContext {
33  ConsoleSession &console;
34  std::map<DeviceID, DeviceData> &devices;
35  Poco::Mutex &mutex;
36  CommandSender &sender;
37  const std::vector<std::string> args;
38  Poco::SharedPtr<FileCredentialsStorage> credentialsStorage;
39  Poco::SharedPtr<CryptoConfig> cryptoConfig;
40  std::list<DeviceID> &newDevices;
41  std::set<DeviceID> &acceptedDevices;
42  std::map<DeviceID, DeviceDescription> &seenDevices;
43  };
44 
48  typedef std::function<void(ActionContext &context)> Action;
49 
50  struct ActionRecord {
51  std::string description;
52  Action action;
53  };
54 
55  TestingCenter();
56 
57  bool accept(const Command::Ptr cmd) override;
58  void handle(Command::Ptr cmd, Answer::Ptr answer) override;
59 
60  void run() override;
61  void stop() override;
62 
63  void setConsole(Poco::SharedPtr<Console> console);
64  void setPairedDevices(const std::list<std::string> &ids);
65  Poco::SharedPtr<Console> console() const;
66  void setCredentialsStorage(Poco::SharedPtr<FileCredentialsStorage> storage);
67  void setCryptoConfig(Poco::SharedPtr<CryptoConfig> config);
68 
69 protected:
70  void registerAction(
71  const std::string &name,
72  const Action action,
73  const std::string &description);
74  void printHelp(ConsoleSession &session);
75  void processLine(ConsoleSession &session, const std::string &line);
76 
77 private:
78  Poco::SharedPtr<Console> m_console;
79  std::list<DeviceID> m_newDevices;
80  Poco::AtomicCounter m_stop;
81  std::map<std::string, ActionRecord> m_action;
82  std::map<DeviceID, DeviceData> m_devices;
83  Poco::Mutex m_mutex;
84  Poco::SharedPtr<FileCredentialsStorage> m_credentialsStorage;
85  Poco::SharedPtr<CryptoConfig> m_cryptoConfig;
86  std::map<DeviceID, DeviceDescription> m_seenDevices;
87  std::set<DeviceID> m_acceptedDevices;
88 };
89 
90 }
void stop() override
Definition: TestingCenter.cpp:685
Definition: TestingCenter.h:24
Definition: TestingCenter.h:50
Definition: TestingCenter.h:32
Definition: CommandHandler.h:18
Definition: CommandSender.h:20
Definition: Loggable.h:19
Definition: StoppableRunnable.h:8
std::function< void(ActionContext &context)> Action
Definition: TestingCenter.h:48
Definition: Console.h:70