|
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 |
|
@@ -102,86 +107,89 @@ class ArduinoIoTCloudClass {
|
102 | 107 |
|
103 | 108 | static unsigned long const DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS = 500; /* Data rate throttled to 2 Hz */
|
104 | 109 |
|
| 110 | + 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) { |
| 111 | + Permission permission = Permission::ReadWrite; |
| 112 | + if (permission_type == READ ) permission = Permission::Read; |
| 113 | + else if(permission_type == WRITE) permission = Permission::Write; |
| 114 | + else permission = Permission::ReadWrite; |
105 | 115 |
|
106 |
| - |
107 |
| - template<typename T, typename N = T> |
108 |
| - 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) { |
109 |
| - Permission permission = Permission::ReadWrite; |
110 |
| - if (permission_type == READ) { |
111 |
| - permission = Permission::Read; |
112 |
| - } else if (permission_type == WRITE) { |
113 |
| - permission = Permission::Write; |
114 |
| - } else { |
115 |
| - permission = Permission::ReadWrite; |
116 |
| - } |
117 |
| - |
118 |
| - if (seconds == ON_CHANGE) { |
119 |
| - Thing.addPropertyReal(property, name, permission).publishOnChange((T)minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn); |
120 |
| - } else { |
121 |
| - Thing.addPropertyReal(property, name, permission).publishEvery(seconds).onUpdate(fn).onSync(synFn); |
122 |
| - } |
123 |
| - } |
124 |
| - |
125 |
| - template <typename T> |
126 |
| - ArduinoCloudProperty<T> addPropertyReal(T & property, String const & name, Permission const permission) { |
127 |
| - return Thing.addPropertyReal(property, name, permission); |
128 |
| - } |
129 |
| - |
130 |
| - void connectionCheck(); |
131 |
| - String getBrokerAddress() { |
132 |
| - return _brokerAddress; |
133 |
| - } |
134 |
| - uint16_t getBrokerPort() { |
135 |
| - return _brokerPort; |
136 |
| - } |
137 |
| - void printDebugInfo(); |
138 |
| - |
139 |
| - protected: |
140 |
| - friend class CloudSerialClass; |
141 |
| - int writeStdout(const byte data[], int length); |
142 |
| - int writeProperties(const byte data[], int length); |
143 |
| - int writeShadowOut(const byte data[], int length); |
144 |
| - |
145 |
| - // Used to initialize MQTTClient |
146 |
| - void mqttClientBegin(); |
147 |
| - // Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout) |
148 |
| - bool mqttReconnect(int const maxRetries, int const timeout); |
149 |
| - // Used to retrieve last values from _shadowTopicIn |
150 |
| - void requestLastValue(); |
151 |
| - |
152 |
| - ArduinoIoTConnectionStatus getIoTStatus() { |
153 |
| - return iotStatus; |
| 116 | + if(seconds == ON_CHANGE) { |
| 117 | + Thing.addPropertyReal(property, name, permission).publishOnChange(minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn); |
| 118 | + } else { |
| 119 | + Thing.addPropertyReal(property, name, permission).publishEvery(seconds).onUpdate(fn).onSync(synFn); |
154 | 120 | }
|
155 |
| - void setIoTConnectionState(ArduinoIoTConnectionStatus _newState); |
156 |
| - private: |
157 |
| - ArduinoIoTConnectionStatus iotStatus = ArduinoIoTConnectionStatus::IDLE; |
158 |
| - ConnectionManager *connection; |
159 |
| - static void onMessage(int length); |
160 |
| - void handleMessage(int length); |
161 |
| - ArduinoIoTSynchronizationStatus _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED; |
162 |
| - |
163 |
| - void sendPropertiesToCloud(); |
164 |
| - |
165 |
| - |
166 |
| - String _device_id, |
167 |
| - _thing_id, |
168 |
| - _brokerAddress; |
169 |
| - uint16_t _brokerPort; |
170 |
| - ArduinoCloudThing Thing; |
171 |
| - BearSSLClient* _bearSslClient; |
172 |
| - MqttClient* _mqttClient; |
173 |
| - int _lastSyncRequestTickTime; |
174 |
| - |
175 |
| - |
176 |
| - // Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload |
177 |
| - String _stdinTopic; |
178 |
| - String _stdoutTopic; |
179 |
| - String _shadowTopicOut; |
180 |
| - String _shadowTopicIn; |
181 |
| - String _dataTopicOut; |
182 |
| - String _dataTopicIn; |
183 |
| - String _otaTopic; |
184 |
| - Client *_net; |
| 121 | + } |
| 122 | + |
| 123 | + ArduinoCloudProperty& addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission) { |
| 124 | + return Thing.addPropertyReal(property, name, permission); |
| 125 | + } |
| 126 | + |
| 127 | + 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) { |
| 128 | + ArduinoCloudProperty *p = new CloudWrapperBool(property); |
| 129 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 130 | + } |
| 131 | + 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) { |
| 132 | + ArduinoCloudProperty *p = new CloudWrapperFloat(property); |
| 133 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 134 | + } |
| 135 | + 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) { |
| 136 | + ArduinoCloudProperty *p = new CloudWrapperInt(property); |
| 137 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 138 | + } |
| 139 | + 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) { |
| 140 | + ArduinoCloudProperty *p = new CloudWrapperString(property); |
| 141 | + addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn); |
| 142 | + } |
| 143 | + |
| 144 | + void connectionCheck(); |
| 145 | + String getBrokerAddress(){ return _brokerAddress; } |
| 146 | + uint16_t getBrokerPort() { return _brokerPort; } |
| 147 | + void printDebugInfo(); |
| 148 | + |
| 149 | +protected: |
| 150 | + friend class CloudSerialClass; |
| 151 | + int writeStdout(const byte data[], int length); |
| 152 | + int writeProperties(const byte data[], int length); |
| 153 | + int writeShadowOut(const byte data[], int length); |
| 154 | + |
| 155 | + // Used to initialize MQTTClient |
| 156 | + void mqttClientBegin(); |
| 157 | + // Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout) |
| 158 | + bool mqttReconnect(int const maxRetries, int const timeout); |
| 159 | + // Used to retrieve last values from _shadowTopicIn |
| 160 | + void requestLastValue(); |
| 161 | + |
| 162 | + ArduinoIoTConnectionStatus getIoTStatus() { return iotStatus; } |
| 163 | + void setIoTConnectionState(ArduinoIoTConnectionStatus _newState); |
| 164 | +private: |
| 165 | + ArduinoIoTConnectionStatus iotStatus = ArduinoIoTConnectionStatus::IDLE; |
| 166 | + ConnectionManager *connection; |
| 167 | + static void onMessage(int length); |
| 168 | + void handleMessage(int length); |
| 169 | + ArduinoIoTSynchronizationStatus _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED; |
| 170 | + |
| 171 | + void sendPropertiesToCloud(); |
| 172 | + |
| 173 | + |
| 174 | + String _device_id, |
| 175 | + _thing_id, |
| 176 | + _brokerAddress; |
| 177 | + uint16_t _brokerPort; |
| 178 | + ArduinoCloudThing Thing; |
| 179 | + BearSSLClient* _bearSslClient; |
| 180 | + MqttClient* _mqttClient; |
| 181 | + int _lastSyncRequestTickTime; |
| 182 | + |
| 183 | + |
| 184 | + // Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload |
| 185 | + String _stdinTopic; |
| 186 | + String _stdoutTopic; |
| 187 | + String _shadowTopicOut; |
| 188 | + String _shadowTopicIn; |
| 189 | + String _dataTopicOut; |
| 190 | + String _dataTopicIn; |
| 191 | + String _otaTopic; |
| 192 | + Client *_net; |
185 | 193 | };
|
186 | 194 |
|
187 | 195 | extern ArduinoIoTCloudClass ArduinoCloud;
|
|
0 commit comments