XORDE  v1.0
eXtensible Operational Robotic Development Environment
ComponentBase.h
1 #ifndef COMPONENTBASE_H
2 #define COMPONENTBASE_H
3 
4 #include <QJsonArray>
5 #include <QJsonObject>
6 #include <QImage>
7 #include <QBuffer>
8 #include <QFile>
9 #include <QObject>
10 #include <QTimer>
11 #include <QElapsedTimer>
12 #include "Protocol/onbobject.h"
13 #include "Protocol/ONBPacket.h"
14 #include "tronostools_global.h"
15 #include <QDebug>
16 
97 class TRONOSTOOLSSHARED_EXPORT ComponentBase : public QObject
98 {
99  Q_OBJECT
100 
101 public:
103  explicit ComponentBase(QObject *parent = nullptr);
104 
106  ComponentBase(QString name, unsigned long classId, QString description = QString(), QObject *parent = nullptr);
107 
109  virtual ~ComponentBase();
110 
114  unsigned short id() const {return m_id;}
115 
120  unsigned long timestamp() const;
121 
123  QJsonObject getJsonConfig() const;
124 
125 signals:
129  void objectChanged(QString name);
130 
131 protected:
133  QString componentName;
135  unsigned long classID = 0;
137  QString description;
139  unsigned long serialNumber = 0;
141  unsigned char &version;
143  unsigned char &versionMinor;
145  QString releaseInfo;
147  QString hardwareInfo;
149  unsigned long burnCount = 0;
150 
154  void setIcon(QImage icon);
155 
159  void setIcon(QString filename);
160 
167  template <class T>
168  ONBObject<T> &createInput(QString name, T &var)
169  {
170  ONBObject<T> &obj = ObjectInfo::create(name, var, ObjectInfo::Input);
171  bindObject(obj);
172  return obj;
173  }
174 
181  template <class T>
182  ONBObject<T> &createOutput(QString name, T &var)
183  {
184  ONBObject<T> &obj = ObjectInfo::create(name, var, ObjectInfo::Output);
185  bindObject(obj);
186  return obj;
187  }
188 
195  template <class T>
196  ONBObject<T> &createSetting(QString name, T &var)
197  {
198  ONBObject<T> &obj = ObjectInfo::create(name, var, ObjectInfo::Setting);
199  bindObject(obj);
200  return obj;
201  }
202 
209  const ObjectInfo *object(QString name) const;
210 
217  template <class T>
218  bool rebindObject(QString name, T &var)
219  {
220  ObjectInfo *obj = m_objectMap.value(name, nullptr);
221  if (obj)
222  {
223  if (obj->rebind(var))
224  sendObjectInfo(obj->description().id);
225  return true;
226  }
227  return false;
228  }
229 
236  bool renameObject(QString oldName, QString newName);
237 
242  bool deleteObject(QString name);
243 
247  void sendObject(QString name);
248 
255  void touchOutput(QString name, bool notifyAnyway = false);
256 
260  virtual void onCreate() {}
261 
265  virtual void onDestroy() {}
266 
273  virtual void objectRequestEvent(QString name) {(void)name;}
274 
280  virtual void objectReceiveEvent(QString name) {(void)name;}
281 
286  virtual void objectChangeEvent(QString name) {emit objectChanged(name);}
287 
289  void log(QString message);
290 
291 private:
293  unsigned short m_id = 0;
295  QList<ObjectInfo*> m_objects;
297  QMap<QString, ObjectInfo*> m_objectMap;
299  QList<ObjectInfo*> m_svcObjects;
301  unsigned char m_objectCount;
302 
304  QMap<unsigned char, QTimer*> m_autoSendTimers;
305 
307  unsigned char bindSvcObject(ObjectInfo &obj);
308 
309  void parseServiceMessage(unsigned char oid, const QByteArray &data);
310  void parseGlobalServiceMessage(unsigned char aid);
311  void parseMessage(unsigned char oid, const QByteArray &data);
312  void sendServiceMessage(unsigned char oid, const QByteArray &data = QByteArray());
313  void sendMessage(unsigned char oid, const QByteArray &data);
314 
315  void sendSvcObject(unsigned char oid);
316  void sendObjectInfo(unsigned char oid);
317 
318  unsigned char bindObject(ObjectInfo &obj);
319  void setId(unsigned short id) {m_id = id;}
320 
321  bool m_created = false;
322  QString m_instanceName = "";
323  unsigned short m_version = 0;
324  unsigned char m_busType = 0;
325  QByteArray m_iconData;
326 
327  friend class ModuleBaseONB;
328 
329  QElapsedTimer *m_timestampTimer = nullptr;
330 
331 private slots:
332  void receiveData(const ONBPacket &packet);
333 
334 signals:
336  void newData(const ONBPacket &packet);
337 };
338 
339 
340 
341 #endif // COMPONENTBASE_H
Output is volatile.
Definition: ObjectInfo.h:37
ONBObject< T > & createInput(QString name, T &var)
Create an input of the component.
Definition: ComponentBase.h:168
bool rebindObject(QString name, T &var)
Rebind the existing object to another variable.
Definition: ComponentBase.h:218
virtual void objectRequestEvent(QString name)
Event of object request.
Definition: ComponentBase.h:273
The base for the ONB component.
Definition: ComponentBase.h:97
ONBObject< T > & createSetting(QString name, T &var)
Create a setting of the component.
Definition: ComponentBase.h:196
const ObjectDescription & description() const
Get description of the object.
Definition: ObjectInfo.h:299
QString releaseInfo
This field can be filled with arbitrary information about component release (e.g. build date and time...
Definition: ComponentBase.h:145
ONBObject< T > & createOutput(QString name, T &var)
Create an ouput of the component.
Definition: ComponentBase.h:182
virtual void onCreate()
Component creation event.
Definition: ComponentBase.h:260
Component-side ONB Object.
Definition: ObjectInfo.h:9
unsigned char & versionMinor
Minor version of the component.
Definition: ComponentBase.h:143
The base class for XORDE module.
Definition: ModuleBaseONB.h:59
virtual void objectChangeEvent(QString name)
Event of object change.
Definition: ComponentBase.h:286
Object with read-write access, its value stored in NVM.
Definition: ObjectInfo.h:39
ONB Object base interface.
Definition: ObjectInfo.h:16
virtual void objectReceiveEvent(QString name)
Event of object receive.
Definition: ComponentBase.h:280
QString componentName
The name of the component. Used to name the type, must be unique.
Definition: ComponentBase.h:133
QString hardwareInfo
This field commonly used in devices. Can contain CPU description or something else about hardware...
Definition: ComponentBase.h:147
unsigned short id() const
Get componentID of the component.
Definition: ComponentBase.h:114
Input is volatile object that can oly be written.
Definition: ObjectInfo.h:36
QString description
Human-readable description for the help.
Definition: ComponentBase.h:137
virtual void onDestroy()
Component deletion event.
Definition: ComponentBase.h:265
unsigned char & version
Major version of the component.
Definition: ComponentBase.h:141
unsigned char id
Object ID.
Definition: objectdescription.h:27