libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Peer.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef PEER_H_
32 #define PEER_H_
33 
34 #include "../DeviceDescription/HomegearDevice.h"
35 #include "../Database/DatabaseTypes.h"
36 #include "../Sockets/RpcClientInfo.h"
37 #include "../ScriptEngine/ScriptInfo.h"
38 #include "ServiceMessages.h"
39 #include "../DeviceDescription/DeviceTranslations.h"
40 #include "Role.h"
41 
42 #include <string>
43 #include <unordered_map>
44 #include <vector>
45 #include <memory>
46 
47 using namespace BaseLib::DeviceDescription;
48 
49 namespace BaseLib {
50 
51 namespace Rpc {
52 class IWebserverEventSink;
53 }
54 
55 namespace Systems {
56 class ICentral;
57 class Peer;
58 
60  public:
64  uint64_t databaseId = 0;
65 
69  int32_t specialType = 0;
70 
75 
80 
84  void lock() noexcept;
85 
89  void unlock() noexcept;
90 
95  std::vector<uint8_t>::size_type getBinaryDataSize() noexcept;
96 
101  std::vector<uint8_t> getBinaryData() noexcept;
102 
107  std::vector<uint8_t> &getBinaryDataReference() noexcept;
108 
113  void setBinaryData(std::vector<uint8_t> &value) noexcept;
114 
119  std::vector<uint8_t> getPartialBinaryData() noexcept;
120 
125  std::vector<uint8_t> &getPartialBinaryDataReference() noexcept;
126 
131  void setPartialBinaryData(std::vector<uint8_t> &value) noexcept;
132 
137  BaseLib::PVariable getLogicalData() noexcept;
138 
143  void setLogicalData(PVariable value) noexcept;
144 
149  bool equals(std::vector<uint8_t> &value) noexcept;
150 
151  bool hasCategory(uint64_t id) {
152  std::lock_guard<std::mutex> categoriesGuard(_categoriesMutex);
153  return _categories.find(id) != _categories.end();
154  }
155  void addCategory(uint64_t id) {
156  std::lock_guard<std::mutex> categoriesGuard(_categoriesMutex);
157  _categories.emplace(id);
158  }
159  void removeCategory(uint64_t id) {
160  std::lock_guard<std::mutex> categoriesGuard(_categoriesMutex);
161  _categories.erase(id);
162  }
163  std::set<uint64_t> getCategories() {
164  std::lock_guard<std::mutex> categoriesGuard(_categoriesMutex);
165  return _categories;
166  }
167  std::string getCategoryString();
168  bool hasCategories() {
169  std::lock_guard<std::mutex> categoriesGuard(_categoriesMutex);
170  return !_categories.empty();
171  }
172 
173  bool hasRole(uint64_t id) {
174  std::lock_guard<std::mutex> rolesGuard(_rolesMutex);
175  return _roles.find(id) != _roles.end();
176  }
177  bool hasServiceRole() {
178  std::lock_guard<std::mutex> rolesGuard(_rolesMutex);
179  for (auto &role: _roles) {
180  auto hexRole = BaseLib::Math::getNumber(std::to_string(role.first), true);
181  if (((hexRole & 0xFF0000)) == 0x800000) return true;
182  }
183  return false;
184  }
185  void addRole(const Role &role);
186  void addRole(uint64_t id, RoleDirection direction, bool invert, bool scale, RoleScaleInfo scaleInfo);
187  void removeRole(uint64_t id);
188  Role getRole(uint64_t id) {
189  std::lock_guard<std::mutex> rolesGuard(_rolesMutex);
190  auto rolesIterator = _roles.find(id);
191  if (rolesIterator != _roles.end()) return rolesIterator->second; else return Role();
192  }
193  std::unordered_map<uint64_t, Role> getRoles() {
194  std::lock_guard<std::mutex> rolesGuard(_rolesMutex);
195  return _roles;
196  }
197  std::string getRoleString();
198  bool hasRoles() {
199  std::lock_guard<std::mutex> rolesGuard(_rolesMutex);
200  return !_roles.empty();
201  }
202  bool invert();
203  bool scale();
204  Role mainRole();
205 
206  uint64_t getRoom() {
207  return _room;
208  }
209  void setRoom(uint64_t id) {
210  _room = id;
211  }
212 
213  uint64_t getBuildingPart() {
214  return _buildingPart;
215  }
216  void setBuildingPart(uint64_t id) {
217  _buildingPart = id;
218  }
219  private:
220  std::mutex _logicalDataMutex;
221  BaseLib::PVariable _logicalData;
222  std::mutex _binaryDataMutex;
223  std::vector<uint8_t> _binaryData;
224  std::vector<uint8_t> _partialBinaryData;
225  std::mutex _categoriesMutex;
226  std::set<uint64_t> _categories;
227  std::mutex _rolesMutex;
228 
232  bool _invert = false;
233  bool _scale = false;
234  Role _mainRole;
235  std::unordered_map<uint64_t, Role> _roles;
236  std::atomic<uint64_t> _room{0};
237  std::atomic<uint64_t> _buildingPart{0};
238 };
239 
241  public:
243  ConfigDataBlock(ConfigDataBlock const &rhs);
244  virtual ~ConfigDataBlock() {}
245  ConfigDataBlock &operator=(const ConfigDataBlock &rhs);
246 
250  void lock() noexcept;
251 
255  void unlock() noexcept;
256 
261  std::vector<uint8_t>::size_type getBinaryDataSize() noexcept;
262 
267  std::vector<uint8_t> getBinaryData() noexcept;
268 
273  std::vector<uint8_t> &getBinaryDataReference() noexcept;
274 
279  void setBinaryData(std::vector<uint8_t> &value) noexcept;
280 
285  bool equals(std::vector<uint8_t> &value) noexcept;
286 
290  uint64_t databaseId = 0;
291  private:
292  std::mutex _binaryDataMutex;
293  std::vector<uint8_t> _binaryData;
294 };
295 
296 class BasicPeer {
297  public:
299  BasicPeer(int32_t address, std::string serial, bool isVirtual) {
300  this->address = address;
301  serialNumber = serial;
302  this->isVirtual = isVirtual;
303  }
304  virtual ~BasicPeer() {}
305 
306  std::shared_ptr<Peer> peer;
307  bool hasSender = false; //Todo: Can be removed in future versions
308  bool isSender = false;
309  uint64_t id = 0;
310  int32_t address = 0;
311  std::string serialNumber;
312  int32_t channel = 0;
313  int32_t physicalIndexOffset = 0;
314  bool isVirtual = false;
315  std::string linkName;
316  std::string linkDescription;
317  std::vector<uint8_t> data;
318  int32_t configEEPROMAddress = -1;
319 };
320 
322  public:
323  //Event handling
325  public:
326  //Hooks
327  virtual void onAddWebserverEventHandler(BaseLib::Rpc::IWebserverEventSink *eventHandler, std::map<int32_t, PEventHandler> &eventHandlers) = 0;
328  virtual void onRemoveWebserverEventHandler(std::map<int32_t, PEventHandler> &eventHandlers) = 0;
329 
330  virtual void onRPCEvent(std::string &source, uint64_t id, int32_t channel, std::string &deviceAddress, std::shared_ptr<std::vector<std::string>> &valueKeys, std::shared_ptr<std::vector<PVariable>> &values) = 0;
331  virtual void onRPCUpdateDevice(uint64_t id, int32_t channel, std::string address, int32_t hint) = 0;
332  virtual void onEvent(std::string &source, uint64_t peerID, int32_t channel, std::shared_ptr<std::vector<std::string>> &variables, std::shared_ptr<std::vector<PVariable>> &values) = 0;
333  virtual void onServiceMessageEvent(const PServiceMessage &serviceMessage) = 0;
334  virtual void onRunScript(ScriptEngine::PScriptInfo &scriptInfo, bool wait) = 0;
335  virtual BaseLib::PVariable onInvokeRpc(std::string &methodName, BaseLib::PArray &parameters) = 0;
336  };
337  //End event handling
338 
339  static const uint64_t kMaximumPeerId = 0x3FFFFFFF;
340  static constexpr std::pair<uint64_t, uint64_t> kVirtualPeerIdRange{0x40000000, 0x4FFFFFFF};
341  static constexpr std::pair<uint64_t, uint64_t> kUiPeerIdRange{0x50000000, 0x5FFFFFFF};
342 
343  std::atomic_bool deleting; //Needed, so the peer gets not saved in central's worker thread while being deleted
344 
345  void setRpcDevice(std::shared_ptr<HomegearDevice> value) {
346  _rpcDevice = value;
347  initializeTypeString();
348  }
349  std::shared_ptr<HomegearDevice> getRpcDevice() { return _rpcDevice; }
350 
351  std::unordered_map<uint32_t, ConfigDataBlock> binaryConfig;
352  std::unordered_map<uint32_t, std::unordered_map<std::string, RpcConfigurationParameter>> configCentral;
353  std::unordered_map<uint32_t, std::unordered_map<std::string, RpcConfigurationParameter>> valuesCentral;
354  std::unordered_map<uint32_t, std::unordered_map<int32_t, std::unordered_map<uint32_t, std::unordered_map<std::string, RpcConfigurationParameter>>>> linksCentral;
355  std::shared_ptr<ServiceMessages> serviceMessages;
356 
357  Peer(BaseLib::SharedObjects *baseLib, uint32_t parentId, IPeerEventSink *eventHandler);
358  Peer(BaseLib::SharedObjects *baseLib, uint64_t id, int32_t address, std::string serialNumber, uint32_t parentId, IPeerEventSink *eventHandler);
359  virtual ~Peer();
360  virtual void dispose();
361 
362  //Features
363  virtual bool wireless() = 0;
364  //End features
365 
366  //In table peers:
367  virtual uint32_t getParentID() { return _parentID; }
368  virtual int32_t getAddress() { return _address; }
369  virtual uint64_t getID() { return _peerID; }
370  virtual void setID(uint64_t id);
371  virtual void setAddress(int32_t value) {
372  _address = value;
373  if (_peerID > 0) save(true, false, false);
374  }
375  virtual std::string getSerialNumber() { return _serialNumber; }
376  virtual void setSerialNumber(std::string serialNumber);
377  //End
378 
379  //In table variables:
380  virtual int32_t getFirmwareVersion() { return _firmwareVersion; }
381  virtual void setFirmwareVersion(int32_t value) {
382  _firmwareVersion = value;
383  saveVariable(1001, value);
384  }
385  virtual std::string getFirmwareVersionString() { return _firmwareVersionString; }
386  virtual void setFirmwareVersionString(std::string value) {
387  _firmwareVersionString = value;
388  saveVariable(1003, value);
389  }
390  virtual uint64_t getDeviceType() { return _deviceType; }
391  virtual void setDeviceType(uint64_t value) {
392  _deviceType = value;
393  saveVariable(1002, (int64_t)_deviceType);
394  initializeTypeString();
395  }
396  virtual std::string getName() { return getName(-1); }
397  virtual std::string getName(int32_t channel);
398  virtual void setName(std::string value) { setName(-1, value); }
399  virtual void setName(int32_t channel, std::string value);
400  virtual std::string getIp() { return _ip; }
401  virtual void setIp(std::string value) {
402  _ip = value;
403  saveVariable(1004, value);
404  }
405  virtual std::string getIdString() { return _idString; }
406  virtual void setIdString(std::string value) {
407  _idString = value;
408  saveVariable(1005, value);
409  }
410  virtual std::string getTypeString() { return _typeString; }
411  virtual void setTypeString(std::string value) {
412  _typeString = value;
413  saveVariable(1006, value);
414  }
415  virtual std::set<int32_t> getChannelsInRoom(uint64_t roomId);
416  virtual uint64_t getRoom(int32_t channel);
417  virtual bool hasRoomInChannels(uint64_t roomId);
418  bool roomsSet();
419  virtual bool setRoom(int32_t channel, uint64_t value);
420  virtual std::set<int32_t> getChannelsInBuildingPart(uint64_t buildingPartId);
421  virtual uint64_t getBuildingPart(int32_t channel);
422  virtual bool hasBuildingPartInChannels(uint64_t buildingPartId);
423  bool buildingPartsSet();
424  virtual bool setBuildingPart(int32_t channel, uint64_t value);
425  virtual std::unordered_map<int32_t, std::set<uint64_t>> getCategories();
426  virtual std::set<uint64_t> getCategories(int32_t channel);
427  virtual std::set<int32_t> getChannelsInCategory(uint64_t categoryId);
428  virtual bool hasCategories();
429  virtual bool hasCategories(int32_t channel);
430  virtual bool hasCategory(int32_t channel, uint64_t id);
431  virtual bool hasCategoryInChannels(uint64_t categoryId);
432  virtual bool addCategory(int32_t channel, uint64_t id);
433  virtual bool removeCategory(int32_t channel, uint64_t id);
434  //End
435 
436  virtual std::string getRpcTypeString() { return _rpcTypeString; }
437 
438  virtual std::string handleCliCommand(std::string command) = 0;
439  virtual HomegearDevice::ReceiveModes::Enum getRXModes();
440  virtual bool isTeam() { return false; }
441  virtual void setLastPacketReceived();
442  virtual int64_t getLastPacketReceived() { return _lastPacketReceived; }
443  virtual bool pendingQueuesEmpty() { return true; }
444  virtual void enqueuePendingQueues() {}
445  virtual int32_t getChannelGroupedWith(int32_t channel) = 0;
446  virtual int32_t getNewFirmwareVersion() = 0;
447  virtual std::string getFirmwareVersionString(int32_t firmwareVersion) = 0;
448  virtual bool firmwareUpdateAvailable() = 0;
449 
450  virtual bool setVariableRoom(int32_t channel, std::string &variableName, uint64_t roomId);
451  virtual void removeRoomFromVariables(uint64_t roomId);
452  virtual uint64_t getVariableRoom(int32_t channel, const std::string &variableName);
453  virtual bool setVariableBuildingPart(int32_t channel, std::string &variableName, uint64_t buildingPartId);
454  virtual void removeBuildingPartFromVariables(uint64_t buildingPartId);
455  virtual uint64_t getVariableBuildingPart(int32_t channel, const std::string &variableName);
456  virtual bool addCategoryToVariable(int32_t channel, std::string &variableName, uint64_t categoryId);
457  virtual bool removeCategoryFromVariable(int32_t channel, std::string &variableName, uint64_t categoryId);
458  virtual void removeCategoryFromVariables(uint64_t categoryId);
459  virtual std::set<uint64_t> getVariableCategories(int32_t channel, std::string &variableName);
460  virtual bool variableHasCategory(int32_t channel, const std::string &variableName, uint64_t categoryId);
461  virtual bool variableHasCategories(int32_t channel, const std::string &variableName);
462  virtual bool addRoleToVariable(int32_t channel, std::string &variableName, uint64_t roleId, RoleDirection direction, bool invert, bool scale, RoleScaleInfo scaleInfo);
463  virtual bool removeRoleFromVariable(int32_t channel, std::string &variableName, uint64_t roleId);
464  virtual void removeRoleFromVariables(uint64_t roleId);
465  virtual std::unordered_map<uint64_t, Role> getVariableRoles(int32_t channel, std::string &variableName);
466  virtual bool variableHasRole(int32_t channel, const std::string &variableName, uint64_t roleId);
467  virtual bool variableHasRoles(int32_t channel, const std::string &variableName);
468 
469  virtual bool load(ICentral *central) { return false; }
470  virtual void save(bool savePeer, bool saveVariables, bool saveCentralConfig);
471  virtual void loadConfig();
472  virtual void saveConfig();
473 
485  virtual uint64_t createParameter(ParameterGroup::Type::Enum parameterSetType, uint32_t channel, const std::string &parameterName, std::vector<uint8_t> &value, int32_t remoteAddress = 0, uint32_t remoteChannel = 0);
486  virtual void saveParameter(uint32_t parameterID, ParameterGroup::Type::Enum parameterSetType, uint32_t channel, const std::string &parameterName, std::vector<uint8_t> &value, int32_t remoteAddress = 0, uint32_t remoteChannel = 0);
487  virtual void saveSpecialTypeParameter(uint32_t parameterID,
488  ParameterGroup::Type::Enum parameterSetType,
489  uint32_t channel,
490  const std::string &parameterName,
491  std::vector<uint8_t> &value,
492  int32_t specialType,
493  const BaseLib::PVariable &metadata,
494  const std::string &roles);
495  virtual void saveParameter(uint32_t parameterID, uint32_t address, std::vector<uint8_t> &value);
496  virtual void saveParameter(uint32_t parameterID, std::vector<uint8_t> &value);
497  virtual void loadVariables(ICentral *central, std::shared_ptr<BaseLib::Database::DataTable> &rows);
498  virtual void saveVariables();
499  virtual void saveVariable(uint32_t index, int32_t intValue);
500  virtual void saveVariable(uint32_t index, int64_t intValue);
501  virtual void saveVariable(uint32_t index, std::string &stringValue);
502  virtual void saveVariable(uint32_t index, std::vector<char> &binaryValue);
503  virtual void saveVariable(uint32_t index, std::vector<uint8_t> &binaryValue);
504  virtual void deleteFromDatabase();
505  virtual void savePeers() = 0;
506 
507  /*
508  * Executed when Homegear is fully started.
509  */
510  virtual void homegearStarted();
511 
512  /*
513  * Executed before Homegear starts shutting down.
514  */
515  virtual void homegearShuttingDown();
516 
517  /*
518  * Initializes the MASTER and VALUES config parameter sets by inserting all missing RPC parameters into configCentral and valuesCentral and creating the database entries. If a parameter does not exist yet, it is initialized with the default value defined in the device's XML file.
519  *
520  * @see configCentral
521  * @see valuesCentral
522  */
523  virtual void initializeCentralConfig();
524 
525  virtual std::unordered_map<int32_t, std::vector<std::shared_ptr<BasicPeer>>> getPeers();
526  virtual std::shared_ptr<BasicPeer> getPeer(int32_t channel, int32_t address, int32_t remoteChannel = -1);
527  virtual std::shared_ptr<BasicPeer> getPeer(int32_t channel, uint64_t id, int32_t remoteChannel = -1);
528  virtual std::shared_ptr<BasicPeer> getPeer(int32_t channel, std::string serialNumber, int32_t remoteChannel = -1);
529  virtual void updatePeer(uint64_t oldId, uint64_t newId);
530  virtual void updatePeer(const std::string &old_serial_number, const std::string &new_serial_number);
531 
532  virtual std::shared_ptr<ICentral> getCentral() = 0;
533 
534  //RPC methods
535  virtual PVariable activateLinkParamset(PRpcClientInfo clientInfo, int32_t channel, uint64_t remoteID, int32_t remoteChannel, bool longPress) { return Variable::createError(-32601, "Method not implemented by this device family."); }
536  virtual PVariable forceConfigUpdate(PRpcClientInfo clientInfo) { return Variable::createError(-32601, "Method not implemented for this peer."); }
537  virtual PVariable getAllConfig(PRpcClientInfo clientInfo);
538  virtual PVariable getAllValues(PRpcClientInfo clientInfo, bool returnWriteOnly, bool checkAcls);
539  virtual PVariable getConfigParameter(PRpcClientInfo clientInfo, uint32_t channel, std::string name);
540  virtual std::shared_ptr<std::vector<PVariable>> getDeviceDescriptions(PRpcClientInfo clientInfo, bool channels, std::map<std::string, bool> fields);
541  virtual PVariable getDeviceDescription(PRpcClientInfo clientInfo, int32_t channel, std::map<std::string, bool> fields);
542  virtual PVariable getDeviceInfo(PRpcClientInfo clientInfo, std::map<std::string, bool> fields);
543  virtual PVariable getLink(PRpcClientInfo clientInfo, int32_t channel, int32_t flags, bool avoidDuplicates);
544  virtual PVariable getLinkInfo(PRpcClientInfo clientInfo, int32_t senderChannel, uint64_t receiverID, int32_t receiverChannel);
545  virtual PVariable setLinkInfo(PRpcClientInfo clientInfo, int32_t senderChannel, uint64_t receiverID, int32_t receiverChannel, std::string name, std::string description);
546  virtual PVariable getLinkPeers(PRpcClientInfo clientInfo, int32_t channel, bool returnID);
547  virtual PVariable getParamset(PRpcClientInfo clientInfo, int32_t channel, ParameterGroup::Type::Enum type, uint64_t remoteID, int32_t remoteChannel, bool checkAcls);
548  virtual PVariable getParamsetDescription(PRpcClientInfo clientInfo, int32_t channel, PParameterGroup parameterSet, bool checkAcls);
549  virtual PVariable getParamsetDescription(PRpcClientInfo clientInfo, int32_t channel, ParameterGroup::Type::Enum type, uint64_t remoteID, int32_t remoteChannel, bool checkAcls);
550  virtual PVariable getParamsetId(PRpcClientInfo clientInfo, uint32_t channel, ParameterGroup::Type::Enum type, uint64_t remoteID, int32_t remoteChannel);
551  virtual PVariable getRolesInDevice(PRpcClientInfo clientInfo, bool checkAcls);
552  virtual PVariable getRolesInRoom(PRpcClientInfo clientInfo, uint64_t roomId, bool checkAcls);
553  virtual PVariable getServiceMessages(PRpcClientInfo clientInfo, bool returnID, const std::string &language);
554  virtual PVariable getValue(PRpcClientInfo clientInfo, uint32_t channel, std::string valueKey, bool requestFromDevice, bool asynchronous);
555  virtual PVariable getVariableDescription(PRpcClientInfo clientInfo, uint32_t channel, std::string valueKey, const std::unordered_set<std::string> &fields);
556  virtual PVariable getVariablesInCategory(PRpcClientInfo clientInfo, uint64_t categoryId, bool checkAcls);
557  virtual PVariable getVariablesInRole(PRpcClientInfo clientInfo, uint64_t roleId, bool checkAcls);
558  virtual PVariable getVariablesInRoom(PRpcClientInfo clientInfo, uint64_t roomId, bool returnDeviceAssigned, bool checkAcls);
559  virtual PVariable getVariablesInBuildingPart(PRpcClientInfo clientInfo, uint64_t buildingPartId, bool returnDeviceAssigned, bool checkAcls);
560  virtual PVariable putParamset(PRpcClientInfo clientInfo, int32_t channel, ParameterGroup::Type::Enum type, uint64_t remoteID, int32_t remoteChannel, PVariable variables, bool checkAcls, bool onlyPushing = false) = 0;
561  virtual PVariable reportValueUsage(PRpcClientInfo clientInfo);
562  virtual PVariable rssiInfo(PRpcClientInfo clientInfo);
563 
571  virtual PVariable setId(PRpcClientInfo clientInfo, uint64_t newPeerID);
572  virtual PVariable setSerialNumber(PRpcClientInfo clientInfo, const std::string &new_serial_number);
573  virtual PVariable setInterface(PRpcClientInfo clientInfo, std::string interfaceID) { return Variable::createError(-32601, "Method not implemented for this Peer."); }
574  virtual PVariable setValue(PRpcClientInfo clientInfo, uint32_t channel, std::string valueKey, PVariable value, bool wait);
575  //End RPC methods
576  protected:
577  BaseLib::SharedObjects *_bl = nullptr;
578  std::shared_ptr<HomegearDevice> _rpcDevice;
579  std::map<uint32_t, uint32_t> _variableDatabaseIDs;
580  std::shared_ptr<ICentral> _central;
581 
582  //In table peers:
583  uint64_t _peerID = 0;
584  uint32_t _parentID = 0;
585  int32_t _address = 0;
586  std::string _serialNumber;
587  //End
588 
589  //In table variables:
590  int32_t _firmwareVersion = 0;
592  uint64_t _deviceType = 0;
593  std::mutex _peersMutex;
594  std::unordered_map<int32_t, std::vector<std::shared_ptr<BasicPeer>>> _peers;
595  std::mutex _namesMutex;
596  std::unordered_map<int32_t, std::string> _names;
597  std::string _ip;
598  std::string _idString;
599 
600  /*
601  * Stores a manually set type string. Overrides _rpcTypeString.
602  * @see _rpcTypeString
603  */
604  std::string _typeString;
605 
606  std::mutex _roomMutex;
607  std::unordered_map<int32_t, uint64_t> _rooms;
608  std::mutex _buildingPartMutex;
609  std::unordered_map<int32_t, uint64_t> _buildingParts;
610  std::mutex _categoriesMutex;
611  std::unordered_map<int32_t, std::set<uint64_t>> _categories;
612  //End
613 
614  /*
615  * Stores the type string defined in the device's XML file. Can be overridden by _typeString.
616  * @see _typeString
617  */
618  std::string _rpcTypeString;
619  HomegearDevice::ReceiveModes::Enum _rxModes = HomegearDevice::ReceiveModes::Enum::none;
620 
621  bool _disposing = false;
622  bool _saveTeam = false;
623  std::atomic<int64_t> _lastPacketReceived{0};
624 
625  // {{{ Event handling
626  //Hooks
627  std::map<int32_t, PEventHandler> _webserverEventHandlers;
628  virtual void raiseAddWebserverEventHandler(Rpc::IWebserverEventSink *eventHandler);
629  virtual void raiseRemoveWebserverEventHandler();
630 
631  virtual void raiseRPCEvent(std::string &source, uint64_t id, int32_t channel, std::string &deviceAddress, std::shared_ptr<std::vector<std::string>> &valueKeys, std::shared_ptr<std::vector<PVariable>> &values);
632  virtual void raiseRPCUpdateDevice(uint64_t id, int32_t channel, std::string address, int32_t hint);
633  virtual void raiseEvent(std::string &source, uint64_t peerID, int32_t channel, std::shared_ptr<std::vector<std::string>> &variables, std::shared_ptr<std::vector<PVariable>> &values);
634  void raiseServiceMessageEvent(const PServiceMessage &serviceMessage);
635  virtual void raiseRunScript(ScriptEngine::PScriptInfo &scriptInfo, bool wait);
636  virtual BaseLib::PVariable raiseInvokeRpc(std::string &methodName, BaseLib::PArray &parameters);
637  // }}}
638 
639  //ServiceMessages event handling
640  virtual void onConfigPending(bool configPending);
641 
642  void onEvent(std::string &source, uint64_t peerId, int32_t channel, std::shared_ptr<std::vector<std::string>> &variables, std::shared_ptr<std::vector<PVariable>> &values) override;
643  void onRPCEvent(std::string &source, uint64_t peerId, int32_t channel, std::string &deviceAddress, std::shared_ptr<std::vector<std::string>> &valueKeys, std::shared_ptr<std::vector<PVariable>> &values) override;
644  void onServiceMessageEvent(const PServiceMessage &serviceMessage) override;
645  void onSaveParameter(std::string name, uint32_t channel, std::vector<uint8_t> &data) override;
646  std::shared_ptr<Database::DataTable> onGetServiceMessages() override;
647  void onSaveServiceMessage(Database::DataRow &data) override;
648  void onDeleteServiceMessage(uint64_t databaseID) override;
649  void onEnqueuePendingQueues() override;
650  //End ServiceMessages event handling
651 
659  BaseLib::DeviceDescription::PParameter createRoleRpcParameter(BaseLib::PVariable &variableInfo, const std::string &baseVariableName, const PParameterGroup &parameterGroup);
660 
670  virtual PVariable getValueFromDevice(std::shared_ptr<Parameter> &parameter, int32_t channel, bool asynchronous) { return Variable::createError(-32601, "Method not implemented for this device family."); }
671 
679  virtual PParameterGroup getParameterSet(int32_t channel, ParameterGroup::Type::Enum type) = 0;
680 
681  virtual PVariable getVariableDescription(PRpcClientInfo clientInfo, const PParameter &parameter, int32_t channel, ParameterGroup::Type::Enum type, int32_t index, const std::unordered_set<std::string> &fields);
682 
689  virtual void setDefaultValue(RpcConfigurationParameter &parameter);
690 
696  virtual void initializeMasterSet(int32_t channel, std::shared_ptr<ConfigParameters> masterSet);
697 
703  virtual void initializeValueSet(int32_t channel, std::shared_ptr<Variables> valueSet);
704 
710  virtual void initializeTypeString();
711 
712  // {{{ Hooks
713  /*
714  * This hook is executed for each parameter in getAllConfig after the initial checks if the parameter exists etc. It can be used for example to set the value of the parameter.
715  *
716  * @param parameter The current parameter.
717  * @param channel The channel of the parameter.
718  * @param parameters The parameters struct which will be returned by getAllConfig.
719  * @return "true" means the function handled the parameter completely and there is nothing to do anymore in getAllConfig.
720  */
721  virtual bool getAllConfigHook2(PRpcClientInfo clientInfo, PParameter parameter, uint32_t channel, PVariable parameters) { return false; }
722 
723  /*
724  * This hook is executed for each parameter in getAllValues after the initial checks if the parameter exists etc. It can be used for example to set the value of the parameter.
725  *
726  * @param parameter The current parameter.
727  * @param channel The channel of the parameter.
728  * @param parameters The parameters struct which will be returned by getAllValues.
729  * @return "true" means the function handled the parameter completely and there is nothing to do anymore in getAllValues.
730  */
731  virtual bool getAllValuesHook2(PRpcClientInfo clientInfo, PParameter parameter, uint32_t channel, PVariable parameters) { return false; }
732 
733  /*
734  * This hook is executed for each parameter in getParamset after the initial checks if the parameter exists etc. It can be used for example to set the value of the parameter.
735  *
736  * @param parameter The current parameter.
737  * @param channel The channel of the parameter.
738  * @param parameters The parameters struct which will be returned by getParamset.
739  * @return "true" means the function handled the parameter completely and there is nothing to do anymore in getParamset.
740  */
741  virtual bool getParamsetHook2(PRpcClientInfo clientInfo, PParameter parameter, uint32_t channel, PVariable parameters) { return false; }
742 
743  /*
744  * This hook is executed every time "convertFromPacket" is called in case custom conversions are used.
745  *
746  * @param parameter The current parameter.
747  * @param data The data to convert.
748  * @param result The conversion result.
749  * @return "true" means the function converted the parameter.
750  */
751  virtual bool convertFromPacketHook(RpcConfigurationParameter &parameter, std::vector<uint8_t> &data, PVariable &result) { return false; }
752 
753  /*
754  * This hook is executed every time "convertToPacket" is called in case custom conversions are used.
755  *
756  * @param parameter The current parameter.
757  * @param data The data to convert.
758  * @param result The conversion result.
759  * @return "true" means the function converted the parameter.
760  */
761  virtual bool convertToPacketHook(RpcConfigurationParameter &parameter, PVariable &data, std::vector<uint8_t> &result) { return false; }
762  // }}}
763 };
764 
765 }
766 }
767 #endif
Definition: Peer.h:296
virtual bool load(ICentral *central)
Definition: Peer.h:469
Definition: Peer.h:240
std::unordered_map< uint32_t, std::unordered_map< std::string, RpcConfigurationParameter > > configCentral
Definition: Peer.h:352
Definition: BinaryPayload.cpp:36
Definition: Role.h:26
virtual std::string getIp()
Definition: Peer.h:400
std::unordered_map< uint64_t, Role > getRoles()
Definition: Peer.h:193
virtual void setDeviceType(uint64_t value)
Definition: Peer.h:391
virtual void setFirmwareVersionString(std::string value)
Definition: Peer.h:386
std::shared_ptr< ServiceMessages > serviceMessages
Definition: Peer.h:355
virtual PVariable activateLinkParamset(PRpcClientInfo clientInfo, int32_t channel, uint64_t remoteID, int32_t remoteChannel, bool longPress)
Definition: Peer.h:535
virtual ~RpcConfigurationParameter()
Definition: Peer.h:78
bool hasCategory(uint64_t id)
Definition: Peer.h:151
std::mutex _roomMutex
Definition: Peer.h:606
Definition: IEvents.h:72
virtual int32_t getAddress()
Definition: Peer.h:368
This is the base library main class.
Definition: BaseLib.h:95
virtual void setAddress(int32_t value)
Definition: Peer.h:371
static int32_t getNumber(const std::string &s, bool isHex=false)
Converts a string (decimal or hexadecimal) to an integer.
Definition: Math.cpp:226
std::shared_ptr< ICentral > _central
Definition: Peer.h:580
virtual int32_t getFirmwareVersion()
Definition: Peer.h:380
int32_t id
Definition: FileDescriptorManager.h:53
std::vector< uint8_t > data
Definition: Peer.h:317
virtual std::string getRpcTypeString()
Definition: Peer.h:436
Definition: BaseLib.cpp:34
std::shared_ptr< HomegearDevice > getRpcDevice()
Definition: Peer.h:349
std::unordered_map< uint32_t, std::unordered_map< int32_t, std::unordered_map< uint32_t, std::unordered_map< std::string, RpcConfigurationParameter > > > > linksCentral
Definition: Peer.h:354
std::shared_ptr< Array > PArray
Definition: Variable.h:72
std::shared_ptr< RpcClientInfo > PRpcClientInfo
Definition: ScriptInfo.h:47
virtual int64_t getLastPacketReceived()
Definition: Peer.h:442
virtual bool pendingQueuesEmpty()
Definition: Peer.h:443
ConfigDataBlock()
Definition: Peer.h:242
virtual uint64_t getDeviceType()
Definition: Peer.h:390
virtual bool getAllValuesHook2(PRpcClientInfo clientInfo, PParameter parameter, uint32_t channel, PVariable parameters)
Definition: Peer.h:731
std::shared_ptr< HomegearDevice > _rpcDevice
Definition: Peer.h:578
virtual ~ConfigDataBlock()
Definition: Peer.h:244
std::shared_ptr< ScriptInfo > PScriptInfo
Definition: ScriptInfo.h:56
std::unordered_map< int32_t, std::vector< std::shared_ptr< BasicPeer > > > _peers
Definition: Peer.h:594
RpcConfigurationParameter()
Definition: Peer.h:76
PVariable value
Definition: UiElements.h:217
std::shared_ptr< ParameterGroup > PParameterGroup
Definition: Parameter.h:52
std::unordered_map< int32_t, uint64_t > _buildingParts
Definition: Peer.h:609
BasicPeer()
Definition: Peer.h:298
virtual bool convertToPacketHook(RpcConfigurationParameter &parameter, PVariable &data, std::vector< uint8_t > &result)
Definition: Peer.h:761
std::shared_ptr< Variable > PVariable
Definition: PhysicalInterfaceSettings.h:41
virtual std::string getFirmwareVersionString()
Definition: Peer.h:385
bool hasRoles()
Definition: Peer.h:198
int32_t channel
Definition: UiElements.h:215
virtual std::string getIdString()
Definition: Peer.h:405
std::string linkDescription
Definition: Peer.h:316
Definition: Role.h:34
std::unordered_map< uint32_t, std::unordered_map< std::string, RpcConfigurationParameter > > valuesCentral
Definition: Peer.h:353
uint64_t getBuildingPart()
Definition: Peer.h:213
void setRoom(uint64_t id)
Definition: Peer.h:209
virtual bool getParamsetHook2(PRpcClientInfo clientInfo, PParameter parameter, uint32_t channel, PVariable parameters)
Definition: Peer.h:741
void addCategory(uint64_t id)
Definition: Peer.h:155
std::unordered_map< int32_t, uint64_t > _rooms
Definition: Peer.h:607
bool hasServiceRole()
Definition: Peer.h:177
virtual PVariable setInterface(PRpcClientInfo clientInfo, std::string interfaceID)
Definition: Peer.h:573
virtual void setFirmwareVersion(int32_t value)
Definition: Peer.h:381
Role getRole(uint64_t id)
Definition: Peer.h:188
Definition: Peer.h:321
std::unordered_map< uint32_t, ConfigDataBlock > binaryConfig
Definition: Peer.h:351
bool hasRole(uint64_t id)
Definition: Peer.h:173
Definition: ICentral.h:49
std::string linkName
Definition: Peer.h:315
uint64_t peerId
Definition: UiElements.h:214
virtual ~BasicPeer()
Definition: Peer.h:304
virtual std::string getSerialNumber()
Definition: Peer.h:375
static PVariable createError(int32_t faultCode, const std::string &faultString, bool retry=false)
Definition: Variable.cpp:224
virtual uint32_t getParentID()
Definition: Peer.h:367
void removeCategory(uint64_t id)
Definition: Peer.h:159
std::string _serialNumber
Definition: Peer.h:586
virtual void setIdString(std::string value)
Definition: Peer.h:406
std::map< int32_t, PEventHandler > _webserverEventHandlers
Definition: Peer.h:627
virtual PVariable forceConfigUpdate(PRpcClientInfo clientInfo)
Definition: Peer.h:536
virtual PVariable getValueFromDevice(std::shared_ptr< Parameter > &parameter, int32_t channel, bool asynchronous)
Gets a variable value directly from the device.
Definition: Peer.h:670
virtual void enqueuePendingQueues()
Definition: Peer.h:444
std::unordered_map< int32_t, std::string > _names
Definition: Peer.h:596
DeviceDescription::PParameter rpcParameter
The RPC parameter as defined in the XML file.
Definition: Peer.h:74
std::set< uint64_t > getCategories()
Definition: Peer.h:163
std::shared_ptr< Peer > peer
Definition: Peer.h:306
std::string _typeString
Definition: Peer.h:604
RoleDirection
Definition: Role.h:19
std::map< uint32_t, uint32_t > _variableDatabaseIDs
Definition: Peer.h:579
virtual std::string getName()
Definition: Peer.h:396
virtual void setTypeString(std::string value)
Definition: Peer.h:411
std::atomic_bool deleting
Definition: Peer.h:343
virtual void setName(std::string value)
Definition: Peer.h:398
std::mutex _categoriesMutex
Definition: Peer.h:610
This class provides hooks into the web server so get and post requests can be passed into family modu...
Definition: IWebserverEventSink.h:43
virtual bool isTeam()
Definition: Peer.h:440
virtual std::string getTypeString()
Definition: Peer.h:410
virtual uint64_t getID()
Definition: Peer.h:369
std::string name
Definition: UiElements.h:216
std::unordered_map< int32_t, std::set< uint64_t > > _categories
Definition: Peer.h:611
std::string _firmwareVersionString
Definition: Peer.h:591
virtual bool convertFromPacketHook(RpcConfigurationParameter &parameter, std::vector< uint8_t > &data, PVariable &result)
Definition: Peer.h:751
std::string serialNumber
Definition: Peer.h:311
bool hasCategories()
Definition: Peer.h:168
std::mutex _buildingPartMutex
Definition: Peer.h:608
BasicPeer(int32_t address, std::string serial, bool isVirtual)
Definition: Peer.h:299
std::mutex _peersMutex
Definition: Peer.h:593
virtual bool getAllConfigHook2(PRpcClientInfo clientInfo, PParameter parameter, uint32_t channel, PVariable parameters)
Definition: Peer.h:721
std::shared_ptr< Parameter > PParameter
Definition: Parameter.h:55
void setRpcDevice(std::shared_ptr< HomegearDevice > value)
Definition: Peer.h:345
std::string _idString
Definition: Peer.h:598
std::string _rpcTypeString
Definition: Peer.h:618
std::string _ip
Definition: Peer.h:597
uint64_t getRoom()
Definition: Peer.h:206
Definition: IEvents.h:66
void setBuildingPart(uint64_t id)
Definition: Peer.h:216
virtual void setIp(std::string value)
Definition: Peer.h:401
std::shared_ptr< ServiceMessage > PServiceMessage
Definition: ServiceMessage.h:74
std::deque< std::shared_ptr< DataColumn > > DataRow
Type definition to easily create a DataRow.
Definition: DatabaseTypes.h:181
std::mutex _namesMutex
Definition: Peer.h:595