|
22 | 22 | #include <ArduinoIoTCloudBearSSL.h> |
23 | 23 | #include <ArduinoCloudThing.h> |
24 | 24 | #include "ConnectionManager.h" |
| 25 | +#include "valuetypes/CloudWrapperBool.h" |
| 26 | +#include "valuetypes/CloudWrapperFloat.h" |
| 27 | +#include "valuetypes/CloudWrapperInt.h" |
| 28 | +#include "valuetypes/CloudWrapperString.h" |
| 29 | + |
25 | 30 |
|
26 | 31 | #include "CloudSerial.h" |
27 | 32 |
|
@@ -113,95 +118,96 @@ class ArduinoIoTCloudClass { |
113 | 118 |
|
114 | 119 | static unsigned long const DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS = 500; /* Data rate throttled to 2 Hz */ |
115 | 120 |
|
| 121 | + void addPropertyReal(ArduinoCloudProperty& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty& property) = CLOUD_WINS) { |
| 122 | + Permission permission = Permission::ReadWrite; |
| 123 | + if (permission_type == READ ) permission = Permission::Read; |
| 124 | + else if(permission_type == WRITE) permission = Permission::Write; |
| 125 | + else permission = Permission::ReadWrite; |
116 | 126 |
|
117 | | - |
118 | | - template<typename T, typename N = T> |
119 | | - void addPropertyReal(T & property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, N minDelta = N(0), void(*synFn)(ArduinoCloudProperty<T> property) = CLOUD_WINS) { |
120 | | - Permission permission = Permission::ReadWrite; |
121 | | - if (permission_type == READ) { |
122 | | - permission = Permission::Read; |
123 | | - } else if (permission_type == WRITE) { |
124 | | - permission = Permission::Write; |
125 | | - } else { |
126 | | - permission = Permission::ReadWrite; |
127 | | - } |
128 | | - |
129 | | - if (seconds == ON_CHANGE) { |
130 | | - Thing.addPropertyReal(property, name, permission).publishOnChange((T)minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn); |
131 | | - } else { |
132 | | - Thing.addPropertyReal(property, name, permission).publishEvery(seconds).onUpdate(fn).onSync(synFn); |
133 | | - } |
134 | | - } |
135 | | - |
136 | | - template <typename T> |
137 | | - ArduinoCloudProperty<T> addPropertyReal(T & property, String const & name, Permission const permission) { |
138 | | - return Thing.addPropertyReal(property, name, permission); |
139 | | - } |
140 | | - |
141 | | - void connectionCheck(); |
142 | | - String getBrokerAddress() { |
143 | | - return _brokerAddress; |
144 | | - } |
145 | | - uint16_t getBrokerPort() { |
146 | | - return _brokerPort; |
147 | | - } |
148 | | - void printDebugInfo(); |
149 | | - |
150 | | - void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback); |
151 | | - |
152 | | - protected: |
153 | | - friend class CloudSerialClass; |
154 | | - int writeStdout(const byte data[], int length); |
155 | | - int writeProperties(const byte data[], int length); |
156 | | - int writeShadowOut(const byte data[], int length); |
157 | | - |
158 | | - // Used to initialize MQTTClient |
159 | | - void mqttClientBegin(); |
160 | | - // Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout) |
161 | | - bool mqttReconnect(int const maxRetries, int const timeout); |
162 | | - // Used to retrieve last values from _shadowTopicIn |
163 | | - void requestLastValue(); |
164 | | - |
165 | | - ArduinoIoTConnectionStatus getIoTStatus() { |
166 | | - return iotStatus; |
| 127 | + if(seconds == ON_CHANGE) { |
| 128 | + Thing.addPropertyReal(property, name, permission).publishOnChange(minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn); |
| 129 | + } else { |
| 130 | + Thing.addPropertyReal(property, name, permission).publishEvery(seconds).onUpdate(fn).onSync(synFn); |
167 | 131 | } |
168 | | - void setIoTConnectionState(ArduinoIoTConnectionStatus _newState); |
169 | | - private: |
170 | | - ArduinoIoTConnectionStatus iotStatus = ArduinoIoTConnectionStatus::IDLE; |
171 | | - ConnectionManager *connection; |
172 | | - static void onMessage(int length); |
173 | | - void handleMessage(int length); |
174 | | - ArduinoIoTSynchronizationStatus _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED; |
175 | | - |
176 | | - void sendPropertiesToCloud(); |
177 | | - |
178 | | - |
179 | | - String _device_id, |
180 | | - _thing_id, |
181 | | - _brokerAddress; |
182 | | - uint16_t _brokerPort; |
183 | | - ArduinoCloudThing Thing; |
184 | | - BearSSLClient* _bearSslClient; |
185 | | - MqttClient* _mqttClient; |
186 | | - int _lastSyncRequestTickTime; |
187 | | - |
188 | | - |
189 | | - // Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload |
190 | | - String _stdinTopic; |
191 | | - String _stdoutTopic; |
192 | | - String _shadowTopicOut; |
193 | | - String _shadowTopicIn; |
194 | | - String _dataTopicOut; |
195 | | - String _dataTopicIn; |
196 | | - String _otaTopic; |
197 | | - Client *_net; |
198 | | - |
199 | | - OnCloudEventCallback _on_sync_event_callback, |
200 | | - _on_connect_event_callback, |
201 | | - _on_disconnect_event_callback; |
202 | | - |
203 | | - static void execCloudEventCallback(OnCloudEventCallback & callback, void * callback_arg); |
204 | | - |
| 132 | + } |
| 133 | + |
| 134 | + ArduinoCloudProperty& addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission) { |
| 135 | + return Thing.addPropertyReal(property, name, permission); |
| 136 | + } |
| 137 | + |
| 138 | + void addPropertyReal(bool& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) { |
| 139 | + ArduinoCloudProperty *p = new CloudWrapperBool(property); |
| 140 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 141 | + } |
| 142 | + void addPropertyReal(float& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) { |
| 143 | + ArduinoCloudProperty *p = new CloudWrapperFloat(property); |
| 144 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 145 | + } |
| 146 | + void addPropertyReal(int& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) { |
| 147 | + ArduinoCloudProperty *p = new CloudWrapperInt(property); |
| 148 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 149 | + } |
| 150 | + void addPropertyReal(String& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) { |
| 151 | + ArduinoCloudProperty *p = new CloudWrapperString(property); |
| 152 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 153 | + } |
| 154 | + |
| 155 | + void connectionCheck(); |
| 156 | + String getBrokerAddress(){ return _brokerAddress; } |
| 157 | + uint16_t getBrokerPort() { return _brokerPort; } |
| 158 | + void printDebugInfo(); |
| 159 | + void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback); |
| 160 | + |
| 161 | +protected: |
| 162 | + friend class CloudSerialClass; |
| 163 | + int writeStdout(const byte data[], int length); |
| 164 | + int writeProperties(const byte data[], int length); |
| 165 | + int writeShadowOut(const byte data[], int length); |
| 166 | + |
| 167 | + // Used to initialize MQTTClient |
| 168 | + void mqttClientBegin(); |
| 169 | + // Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout) |
| 170 | + bool mqttReconnect(int const maxRetries, int const timeout); |
| 171 | + // Used to retrieve last values from _shadowTopicIn |
| 172 | + void requestLastValue(); |
| 173 | + |
| 174 | + ArduinoIoTConnectionStatus getIoTStatus() { return iotStatus; } |
| 175 | + void setIoTConnectionState(ArduinoIoTConnectionStatus _newState); |
| 176 | +private: |
| 177 | + ArduinoIoTConnectionStatus iotStatus = ArduinoIoTConnectionStatus::IDLE; |
| 178 | + ConnectionManager *connection; |
| 179 | + static void onMessage(int length); |
| 180 | + void handleMessage(int length); |
| 181 | + ArduinoIoTSynchronizationStatus _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED; |
| 182 | + |
| 183 | + void sendPropertiesToCloud(); |
| 184 | + |
| 185 | + |
| 186 | + String _device_id, |
| 187 | + _thing_id, |
| 188 | + _brokerAddress; |
| 189 | + uint16_t _brokerPort; |
| 190 | + ArduinoCloudThing Thing; |
| 191 | + BearSSLClient* _bearSslClient; |
| 192 | + MqttClient* _mqttClient; |
| 193 | + int _lastSyncRequestTickTime; |
| 194 | + |
| 195 | + |
| 196 | + // Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload |
| 197 | + String _stdinTopic; |
| 198 | + String _stdoutTopic; |
| 199 | + String _shadowTopicOut; |
| 200 | + String _shadowTopicIn; |
| 201 | + String _dataTopicOut; |
| 202 | + String _dataTopicIn; |
| 203 | + String _otaTopic; |
| 204 | + Client *_net; |
| 205 | + |
| 206 | + OnCloudEventCallback _on_sync_event_callback, |
| 207 | + _on_connect_event_callback, |
| 208 | + _on_disconnect_event_callback; |
| 209 | + |
| 210 | + static void execCloudEventCallback(OnCloudEventCallback & callback, void * callback_arg); |
205 | 211 | }; |
206 | 212 |
|
207 | 213 | extern ArduinoIoTCloudClass ArduinoCloud; |
|
0 commit comments