XORDE  v1.0
eXtensible Operational Robotic Development Environment
ModuleBaseONB.h
1 #ifndef MODULEBASEONB_H
2 #define MODULEBASEONB_H
3 
4 #include "tronostools_global.h"
5 #include "ComponentBase.h"
6 #include <QIcon>
7 #include <QJsonObject>
8 #include <QJsonArray>
9 #include <QElapsedTimer>
10 #include <QObject>
11 #include <QDebug>
12 
45 
59 class TRONOSTOOLSSHARED_EXPORT ModuleBaseONB : public QObject
60 {
61  Q_OBJECT
62 
63 public:
65  ModuleBaseONB(QObject* parent = nullptr);
66 
68  ~ModuleBaseONB();
69 
76  template<class T> void declareComponent(T *component)
77  {
78  ComponentBase *comp = static_cast<ComponentBase*>(component);
79  m_classes << comp->classID;
80  m_classInfo[comp->classID] = comp;
81  m_creators[comp->classID] = [=](QString name)
82  {
83  ComponentBase *c = new T;
84  if (!name.isEmpty())
85  c->m_instanceName = name;
86  return c;
87  };
88  }
89 
93  void setIcon(QImage icon);
94 
98  void setIcon(QString filename);
99 
103  ComponentBase *component(unsigned short id) {return m_componentsMap.value(id, nullptr);}
104 
106  void log(QString message);
107 
109  QList<QJsonObject> getClassesInfo() const;
111  QJsonArray getComponentsConfigList() const;
112 
113  virtual bool start() = 0;
114  virtual bool isActive() = 0;
115  virtual bool work() = 0;
116 
117 // void connectToComponents();
118 // void disconnectFromComponents();
119 
121  virtual QJsonObject getJsonConfig() const;
122 
123 private:
124  QList<ComponentBase*> m_components;
125  QMap<unsigned short, ComponentBase*> m_componentsMap;
126  QList<unsigned long> m_classes;
127  QHash<unsigned long, ComponentBase*> m_classInfo;
128  QHash<unsigned long, std::function<ComponentBase*(QString)> > m_creators;
129 
130  QByteArray m_iconData;
131  QElapsedTimer m_timestampTimer;
132 
133  bool isComponentExists(QString name);
134 
135  void parsePacket(const ONBPacket &packet);
136 
137 // void sendModuleID();
138  void sendFail(const ONBPacket &packet);
139  void sendKillComponent(unsigned short compID);
140  void sendClassList();
141 
142  // maybe make these protected?
143  bool newComponent(unsigned long classID, QString name);
144  bool deleteComponent(unsigned short compID);
145 
146 signals:
148  void componentInitialized(QJsonObject config);
149 
152  void componentCreated(unsigned short id);
153 
154 private slots:
155  void sendClassInfoPacket(const ONBPacket &packet);
156 protected slots:
159  virtual void sendPacket(const ONBPacket &packet) = 0;
160 
161 public slots:
164  void receivePacket(const ONBPacket &packet);
165 };
166 
167 #endif // MODULEBASE_H
ComponentBase * component(unsigned short id)
Get the component by id.
Definition: ModuleBaseONB.h:103
void declareComponent(T *component)
Declare the ONB component.
Definition: ModuleBaseONB.h:76
The base for the ONB component.
Definition: ComponentBase.h:97
unsigned long classID
ClassID of the component. Class identifier, must be unique. There is classification table (must be so...
Definition: ComponentBase.h:135
The base class for XORDE module.
Definition: ModuleBaseONB.h:59