libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
ServiceMessages.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 SERVICEMESSAGES_H_
32 #define SERVICEMESSAGES_H_
33 
34 #include "../Variable.h"
35 #include "../Encoding/BinaryEncoder.h"
36 #include "../Encoding/BinaryDecoder.h"
37 #include "../IEvents.h"
38 #include "../Database/DatabaseTypes.h"
39 #include "../Sockets/RpcClientInfo.h"
40 #include "ServiceMessage.h"
41 
42 namespace BaseLib {
43 
44 class SharedObjects;
45 
46 namespace Systems {
47 class ServiceMessages : public IEvents {
48  public:
49  //Event handling
51  public:
52  virtual void onConfigPending(bool configPending) = 0;
53 
54  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<std::shared_ptr<Variable>>> &values) = 0;
55  virtual 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<std::shared_ptr<Variable>>> &values) = 0;
56  virtual void onServiceMessageEvent(const PServiceMessage &serviceMessage) = 0;
57  virtual void onSaveParameter(std::string name, uint32_t channel, std::vector<uint8_t> &data) = 0;
58  virtual std::shared_ptr<Database::DataTable> onGetServiceMessages() = 0;
59  virtual void onSaveServiceMessage(Database::DataRow &data) = 0;
60  virtual void onDeleteServiceMessage(uint64_t databaseID) = 0;
61  virtual void onEnqueuePendingQueues() = 0;
62  };
63  //End event handling
64 
65  ServiceMessages(BaseLib::SharedObjects *baseLib, uint64_t peerId, std::string peerSerial, IServiceEventSink *eventHandler);
66  virtual ~ServiceMessages();
67 
68  virtual void setPeerId(uint64_t peerId) { _peerId = peerId; }
69  virtual void setPeerSerial(std::string peerSerial) { _peerSerial = peerSerial; }
70 
71  virtual void load();
72  virtual void save(ServiceMessagePriority priority, int64_t timestamp, uint32_t index, bool value);
73  virtual void save(ServiceMessagePriority priority, int64_t timestamp, int32_t channel, std::string id, uint8_t value);
74  virtual bool set(std::string id, bool value);
75  virtual void set(std::string id, uint8_t value, uint32_t channel);
76  virtual std::shared_ptr<Variable> get(const PRpcClientInfo &clientInfo, bool returnId, const std::string &language);
77 
78  virtual void setConfigPending(bool value);
79  virtual bool getConfigPending() { return _configPending; }
80  virtual void resetConfigPendingSetTime();
81  virtual int64_t getConfigPendingSetTime() { return _configPendingSetTime; }
82 
83  virtual void setUnreach(bool value, bool requeue);
84  virtual bool getUnreach() { return _unreach; }
85  virtual void checkUnreach(int32_t cyclicTimeout, int64_t lastPacketReceived);
86  virtual void endUnreach();
87 
88  virtual bool getLowbat() { return _lowbat; }
89  protected:
90  struct ErrorInfo {
91  int64_t timestamp = 0;
92  uint8_t value = 0;
93  };
94 
96  std::map<uint32_t, uint32_t> _variableDatabaseIDs;
97  uint64_t _peerId = 0;
98  std::string _peerSerial;
99  bool _disposing = false;
100  bool _configPending = false;
101  int64_t _configPendingTime = 0;
104  bool _unreach = false;
105  int64_t _unreachTime = 0;
107  bool _stickyUnreach = false;
108  int64_t _stickyUnreachTime = 0;
109  bool _lowbat = false;
110  int64_t _lowbatTime = 0;
111 
112  std::mutex _errorMutex;
113  std::map<uint32_t, std::map<std::string, ErrorInfo>> _errors;
114 
115  //Event handling
116  virtual void raiseConfigPending(bool configPending);
117 
118  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<std::shared_ptr<BaseLib::Variable>>> &values);
119  virtual void raiseRPCEvent(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<std::shared_ptr<Variable>>> &values);
120  virtual void raiseServiceMessageEvent(const PServiceMessage &serviceMessage);
121  virtual void raiseSaveParameter(std::string name, uint32_t channel, std::vector<uint8_t> &data);
122  virtual std::shared_ptr<Database::DataTable> raiseGetServiceMessages();
123  virtual void raiseSaveServiceMessage(Database::DataRow &data);
124  virtual void raiseDeleteServiceMessage(uint64_t databaseID);
125  virtual void raiseEnqueuePendingQueues();
126  //End event handling
127 };
128 
129 }
130 }
131 #endif /* SERVICEMESSAGES_H_ */
virtual void save(ServiceMessagePriority priority, int64_t timestamp, uint32_t index, bool value)
Definition: ServiceMessages.cpp:144
bool _lowbat
Definition: ServiceMessages.h:109
virtual std::shared_ptr< Database::DataTable > onGetServiceMessages()=0
virtual void raiseRPCEvent(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< std::shared_ptr< Variable >>> &values)
Definition: ServiceMessages.cpp:57
virtual void load()
Definition: ServiceMessages.cpp:88
std::string _peerSerial
Definition: ServiceMessages.h:98
Definition: IEvents.h:72
This is the base library main class.
Definition: BaseLib.h:95
bool _stickyUnreach
Definition: ServiceMessages.h:107
int32_t id
Definition: FileDescriptorManager.h:53
ServiceMessages(BaseLib::SharedObjects *baseLib, uint64_t peerId, std::string peerSerial, IServiceEventSink *eventHandler)
Definition: ServiceMessages.cpp:36
Definition: BaseLib.cpp:34
int64_t _stickyUnreachTime
Definition: ServiceMessages.h:108
virtual void onConfigPending(bool configPending)=0
virtual void onSaveServiceMessage(Database::DataRow &data)=0
virtual void resetConfigPendingSetTime()
Definition: ServiceMessages.cpp:617
std::shared_ptr< RpcClientInfo > PRpcClientInfo
Definition: ScriptInfo.h:47
virtual void setUnreach(bool value, bool requeue)
Definition: ServiceMessages.cpp:652
virtual std::shared_ptr< Database::DataTable > raiseGetServiceMessages()
Definition: ServiceMessages.cpp:69
virtual ~ServiceMessages()
Definition: ServiceMessages.cpp:44
bool _configPending
Definition: ServiceMessages.h:100
int64_t _configPendingTime
Definition: ServiceMessages.h:101
virtual void raiseDeleteServiceMessage(uint64_t databaseID)
Definition: ServiceMessages.cpp:79
int32_t _unreachResendCounter
Definition: ServiceMessages.h:103
Definition: ServiceMessages.h:47
PVariable value
Definition: UiElements.h:217
virtual void raiseSaveServiceMessage(Database::DataRow &data)
Definition: ServiceMessages.cpp:74
virtual int64_t getConfigPendingSetTime()
Definition: ServiceMessages.h:81
int32_t channel
Definition: UiElements.h:215
BaseLib::SharedObjects * _bl
Definition: ServiceMessages.h:95
std::map< uint32_t, std::map< std::string, ErrorInfo > > _errors
Definition: ServiceMessages.h:113
std::map< uint32_t, uint32_t > _variableDatabaseIDs
Definition: ServiceMessages.h:96
virtual void raiseServiceMessageEvent(const PServiceMessage &serviceMessage)
Definition: ServiceMessages.cpp:61
virtual void setConfigPending(bool value)
Definition: ServiceMessages.cpp:626
Definition: ServiceMessages.h:90
uint64_t peerId
Definition: UiElements.h:214
virtual void raiseConfigPending(bool configPending)
Definition: ServiceMessages.cpp:49
int64_t last_unreach_event_
Definition: ServiceMessages.h:106
virtual void setPeerId(uint64_t peerId)
Definition: ServiceMessages.h:68
int64_t _unreachTime
Definition: ServiceMessages.h:105
bool _unreach
Definition: ServiceMessages.h:104
bool _disposing
Definition: ServiceMessages.h:99
virtual void onDeleteServiceMessage(uint64_t databaseID)=0
static int64_t getTime()
Gets the current unix time stamp in milliseconds.
Definition: HelperFunctions.cpp:73
ServiceMessagePriority
Definition: ServiceMessage.h:45
virtual void endUnreach()
Definition: ServiceMessages.cpp:577
virtual bool getLowbat()
Definition: ServiceMessages.h:88
virtual void raiseEnqueuePendingQueues()
Definition: ServiceMessages.cpp:83
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< std::shared_ptr< Variable >>> &values)=0
virtual void raiseSaveParameter(std::string name, uint32_t channel, std::vector< uint8_t > &data)
Definition: ServiceMessages.cpp:65
virtual void onSaveParameter(std::string name, uint32_t channel, std::vector< uint8_t > &data)=0
virtual void checkUnreach(int32_t cyclicTimeout, int64_t lastPacketReceived)
Definition: ServiceMessages.cpp:527
std::string name
Definition: UiElements.h:216
int64_t _lowbatTime
Definition: ServiceMessages.h:110
virtual void onServiceMessageEvent(const PServiceMessage &serviceMessage)=0
virtual void setPeerSerial(std::string peerSerial)
Definition: ServiceMessages.h:69
std::mutex _errorMutex
Definition: ServiceMessages.h:112
int64_t _configPendingSetTime
Definition: ServiceMessages.h:102
virtual 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< std::shared_ptr< Variable >>> &values)=0
uint64_t _peerId
Definition: ServiceMessages.h:97
virtual bool getUnreach()
Definition: ServiceMessages.h:84
Definition: IEvents.h:66
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< std::shared_ptr< BaseLib::Variable >>> &values)
Definition: ServiceMessages.cpp:53
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
virtual bool getConfigPending()
Definition: ServiceMessages.h:79