Skip to content

Commit

Permalink
Extracting constants and device sync functions to modules where they …
Browse files Browse the repository at this point in the history
…belong better than to ArduinoCloudThing
  • Loading branch information
aentinger committed Jul 2, 2020
1 parent 8c1da19 commit c754099
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 39 deletions.
1 change: 1 addition & 0 deletions extras/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(testArduinoIoTCloud)
##########################################################################

include_directories(include)
include_directories(../../src)
include_directories(../../src/cbor)
include_directories(../../src/property)
include_directories(../../src/utility/ota)
Expand Down
1 change: 1 addition & 0 deletions extras/test/src/test_publishEvery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <catch.hpp>

#include <util/CBORTestUtil.h>
#include <AIoTC_Const.h>
#include <ArduinoCloudThing.h>

/**************************************************************************************
Expand Down
34 changes: 34 additions & 0 deletions src/AIoTC_Const.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
This file is part of ArduinoIoTCloud.
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

#ifndef ARDUINO_AIOTC_CONST_H_
#define ARDUINO_AIOTC_CONST_H_

/******************************************************************************
CONSTANTS
******************************************************************************/

static bool const ON = true;
static bool const OFF = false;

static long const ON_CHANGE = -1;
static long const SECONDS = 1;
static long const MINUTES = 60;
static long const HOURS = 3600;
static long const DAYS = 86400;

#endif /* ARDUINO_AIOTC_CONST_H_ */
3 changes: 3 additions & 0 deletions src/ArduinoIoTCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
#include <Arduino_ConnectionHandler.h>
#include <Arduino_DebugUtils.h>

#include "AIoTC_Const.h"

#include "cbor/ArduinoCloudThing.h"

#include "property/Property.h"
#include "property/PropertyContainer.h"
#include "property/types/CloudWrapperBool.h"
#include "property/types/CloudWrapperFloat.h"
Expand Down
15 changes: 0 additions & 15 deletions src/cbor/ArduinoCloudThing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,3 @@ double ArduinoCloudThing::convertCborHalfFloatToDouble(uint16_t const half_val)
}
return half_val & 0x8000 ? -val : val;
}

void onAutoSync(Property & property) {
if (property.getLastCloudChangeTimestamp() > property.getLastLocalChangeTimestamp()) {
property.fromCloudToLocal();
property.execCallbackOnChange();
}
}

void onForceCloudSync(Property & property) {
property.fromCloudToLocal();
property.execCallbackOnChange();
}

void onForceDeviceSync(Property & /* property */) {
}
24 changes: 0 additions & 24 deletions src/cbor/ArduinoCloudThing.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@
#include "../property/types/automation/CloudTemperature.h"
#include "../property/types/automation/CloudTelevision.h"

/******************************************************************************
CONSTANTS
******************************************************************************/

static bool const ON = true;
static bool const OFF = false;

static long const ON_CHANGE = -1;
static long const SECONDS = 1;
static long const MINUTES = 60;
static long const HOURS = 3600;
static long const DAYS = 86400;

/******************************************************************************
SYNCHRONIZATION CALLBACKS
******************************************************************************/

void onAutoSync(Property & property);
#define MOST_RECENT_WINS onAutoSync
void onForceCloudSync(Property & property);
#define CLOUD_WINS onForceCloudSync
void onForceDeviceSync(Property & property);
#define DEVICE_WINS onForceDeviceSync // The device property value is already the correct one. The cloud property value will be synchronized at the next update cycle.

/******************************************************************************
CLASS DECLARATION
******************************************************************************/
Expand Down
20 changes: 20 additions & 0 deletions src/property/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,23 @@ unsigned long Property::getLastLocalChangeTimestamp() {
void Property::setIdentifier(int identifier) {
_identifier = identifier;
}

/******************************************************************************
SYNCHRONIZATION CALLBACKS
******************************************************************************/

void onAutoSync(Property & property) {
if (property.getLastCloudChangeTimestamp() > property.getLastLocalChangeTimestamp()) {
property.fromCloudToLocal();
property.execCallbackOnChange();
}
}

void onForceCloudSync(Property & property) {
property.fromCloudToLocal();
property.execCallbackOnChange();
}

void onForceDeviceSync(Property & /* property */) {

}
11 changes: 11 additions & 0 deletions src/property/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,15 @@ inline bool operator == (Property const & lhs, Property const & rhs) {
return (lhs.name() == rhs.name());
}

/******************************************************************************
SYNCHRONIZATION CALLBACKS
******************************************************************************/

void onAutoSync(Property & property);
#define MOST_RECENT_WINS onAutoSync
void onForceCloudSync(Property & property);
#define CLOUD_WINS onForceCloudSync
void onForceDeviceSync(Property & property);
#define DEVICE_WINS onForceDeviceSync // The device property value is already the correct one. The cloud property value will be synchronized at the next update cycle.

#endif /* ARDUINO_CLOUD_PROPERTY_HPP_ */

0 comments on commit c754099

Please sign in to comment.