Skip to content

Commit

Permalink
[Cache Controller] Allow to save sample time in local time
Browse files Browse the repository at this point in the history
Default is to save it using Unix Time, but that may be hard to use later if you need local time.
  • Loading branch information
TD-er committed Aug 9, 2021
1 parent 0d119d2 commit 961a69a
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 7 deletions.
22 changes: 20 additions & 2 deletions src/_C016.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

// #include <ArduinoJson.h>

bool C016_allowLocalSystemTime = false;

bool CPlugin_016(CPlugin::Function function, struct EventStruct *event, String& string)
{
bool success = false;
Expand All @@ -45,16 +47,21 @@ bool CPlugin_016(CPlugin::Function function, struct EventStruct *event, String&
{
Protocol[++protocolCount].Number = CPLUGIN_ID_016;
Protocol[protocolCount].usesMQTT = false;
Protocol[protocolCount].usesTemplate = true;
Protocol[protocolCount].usesTemplate = false;
Protocol[protocolCount].usesAccount = false;
Protocol[protocolCount].usesPassword = false;
Protocol[protocolCount].usesExtCreds = false;
Protocol[protocolCount].defaultPort = 80;
Protocol[protocolCount].usesID = false;
Protocol[protocolCount].usesHost = false;
Protocol[protocolCount].usesPort = false;
Protocol[protocolCount].usesQueue = false;
Protocol[protocolCount].usesCheckReply = false;
Protocol[protocolCount].usesTimeout = false;
Protocol[protocolCount].usesSampleSets = false;
Protocol[protocolCount].needsNetwork = false;
Protocol[protocolCount].allowsExpire = false;
Protocol[protocolCount].allowLocalSystemTime = true;
break;
}

Expand All @@ -66,6 +73,14 @@ bool CPlugin_016(CPlugin::Function function, struct EventStruct *event, String&

case CPlugin::Function::CPLUGIN_INIT:
{
{
MakeControllerSettings(ControllerSettings);

if (AllocatedControllerSettings()) {
LoadControllerSettings(event->ControllerIndex, ControllerSettings);
C016_allowLocalSystemTime = ControllerSettings.useLocalSystemTime();
}
}
success = init_c016_delay_queue(event->ControllerIndex);
ControllerCache.init();
break;
Expand Down Expand Up @@ -98,7 +113,10 @@ bool CPlugin_016(CPlugin::Function function, struct EventStruct *event, String&
{
// Collect the values at the same run, to make sure all are from the same sample
uint8_t valueCount = getValueCountForTask(event->TaskIndex);
C016_queue_element element(event, valueCount, node_time.getUnixTime());
C016_queue_element element(
event,
valueCount,
C016_allowLocalSystemTime ? node_time.now() : node_time.getUnixTime());
success = ControllerCache.write((uint8_t *)&element, sizeof(element));

/*
Expand Down
5 changes: 4 additions & 1 deletion src/src/ControllerQueue/ControllerDelayHandlerStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ struct ControllerDelayHandlerStruct {
max_retries(CONTROLLER_DELAY_QUEUE_RETRY_DFLT),
delete_oldest(false),
must_check_reply(false),
deduplicate(false) {}
deduplicate(false),
useLocalSystemTime(false) {}

void configureControllerSettings(const ControllerSettingsStruct& settings) {
minTimeBetweenMessages = settings.MinimalTimeBetweenMessages;
Expand All @@ -47,6 +48,7 @@ struct ControllerDelayHandlerStruct {
delete_oldest = settings.DeleteOldest;
must_check_reply = settings.MustCheckReply;
deduplicate = settings.deduplicate();
useLocalSystemTime = settings.useLocalSystemTime();
if (settings.allowExpire()) {
expire_timeout = max_queue_depth * max_retries * (minTimeBetweenMessages + settings.ClientTimeout);
if (expire_timeout < CONTROLLER_QUEUE_MINIMAL_EXPIRE_TIME) {
Expand Down Expand Up @@ -250,6 +252,7 @@ struct ControllerDelayHandlerStruct {
bool delete_oldest;
bool must_check_reply;
bool deduplicate;
bool useLocalSystemTime;
};


Expand Down
10 changes: 10 additions & 0 deletions src/src/DataStructs/ControllerSettingsStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,13 @@ void ControllerSettingsStruct::deduplicate(bool value)
{
bitWrite(VariousFlags, 10, value);
}

bool ControllerSettingsStruct::useLocalSystemTime() const
{
return bitRead(VariousFlags, 11);
}

void ControllerSettingsStruct::useLocalSystemTime(bool value)
{
bitWrite(VariousFlags, 11, value);
}
5 changes: 5 additions & 0 deletions src/src/DataStructs/ControllerSettingsStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct ControllerSettingsStruct
CONTROLLER_FULL_QUEUE_ACTION,
CONTROLLER_ALLOW_EXPIRE,
CONTROLLER_DEDUPLICATE,
CONTROLLER_USE_LOCAL_SYSTEM_TIME,
CONTROLLER_CHECK_REPLY,
CONTROLLER_CLIENT_ID,
CONTROLLER_UNIQUE_CLIENT_ID_RECONNECT,
Expand Down Expand Up @@ -142,6 +143,10 @@ struct ControllerSettingsStruct
bool deduplicate() const;
void deduplicate(bool value);

bool useLocalSystemTime() const;
void useLocalSystemTime(bool value);


boolean UseDNS;
uint8_t IP[4];
unsigned int Port;
Expand Down
2 changes: 1 addition & 1 deletion src/src/DataStructs/ProtocolStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ProtocolStruct::ProtocolStruct() :
defaultPort(0), Number(0), usesMQTT(false), usesAccount(false), usesPassword(false),
usesTemplate(false), usesID(false), Custom(false), usesHost(true), usesPort(true),
usesQueue(true), usesCheckReply(true), usesTimeout(true), usesSampleSets(false),
usesExtCreds(false), needsNetwork(true), allowsExpire(true) {}
usesExtCreds(false), needsNetwork(true), allowsExpire(true), allowLocalSystemTime(false) {}

bool ProtocolStruct::useCredentials() const {
return usesAccount || usesPassword;
Expand Down
1 change: 1 addition & 0 deletions src/src/DataStructs/ProtocolStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct ProtocolStruct
bool usesExtCreds : 1;
bool needsNetwork : 1;
bool allowsExpire : 1;
bool allowLocalSystemTime : 1;
};

typedef std::vector<ProtocolStruct> ProtocolVector;
Expand Down
8 changes: 7 additions & 1 deletion src/src/Helpers/_CPlugin_Helper_webform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const __FlashStringHelper * toString(ControllerSettingsStruct::VarType parameter
case ControllerSettingsStruct::CONTROLLER_FULL_QUEUE_ACTION: return F("Full Queue Action");
case ControllerSettingsStruct::CONTROLLER_ALLOW_EXPIRE: return F("Allow Expire");
case ControllerSettingsStruct::CONTROLLER_DEDUPLICATE: return F("De-duplicate");
case ControllerSettingsStruct::CONTROLLER_USE_LOCAL_SYSTEM_TIME: return F("Use Local System Time");

case ControllerSettingsStruct::CONTROLLER_CHECK_REPLY: return F("Check Reply");

Expand Down Expand Up @@ -206,6 +207,9 @@ void addControllerParameterForm(const ControllerSettingsStruct& ControllerSettin
case ControllerSettingsStruct::CONTROLLER_DEDUPLICATE:
addFormCheckBox(displayName, internalName, ControllerSettings.deduplicate());
break;
case ControllerSettingsStruct::CONTROLLER_USE_LOCAL_SYSTEM_TIME:
addFormCheckBox(displayName, internalName, ControllerSettings.useLocalSystemTime());
break;
case ControllerSettingsStruct::CONTROLLER_CHECK_REPLY:
{
const __FlashStringHelper * options[2];
Expand Down Expand Up @@ -333,10 +337,12 @@ void saveControllerParameterForm(ControllerSettingsStruct & ControllerSet
case ControllerSettingsStruct::CONTROLLER_DEDUPLICATE:
ControllerSettings.deduplicate(isFormItemChecked(internalName));
break;
case ControllerSettingsStruct::CONTROLLER_USE_LOCAL_SYSTEM_TIME:
ControllerSettings.useLocalSystemTime(isFormItemChecked(internalName));
break;
case ControllerSettingsStruct::CONTROLLER_CHECK_REPLY:
ControllerSettings.MustCheckReply = getFormItemInt(internalName, ControllerSettings.MustCheckReply);
break;

case ControllerSettingsStruct::CONTROLLER_CLIENT_ID:
strncpy_webserver_arg(ControllerSettings.ClientID, internalName);
break;
Expand Down
8 changes: 7 additions & 1 deletion src/src/WebServer/ControllerPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ void handle_controllers_ControllerSettingsPage(controllerIndex_t controllerindex
addControllerParameterForm(ControllerSettings, controllerindex, ControllerSettingsStruct::CONTROLLER_IP);
}
}
addControllerParameterForm(ControllerSettings, controllerindex, ControllerSettingsStruct::CONTROLLER_PORT);
if (Protocol[ProtocolIndex].usesPort) {
addControllerParameterForm(ControllerSettings, controllerindex, ControllerSettingsStruct::CONTROLLER_PORT);
}

if (Protocol[ProtocolIndex].usesQueue) {
addTableSeparator(F("Controller Queue"), 2, 3);
Expand All @@ -341,6 +343,10 @@ void handle_controllers_ControllerSettingsPage(controllerIndex_t controllerindex
if (Protocol[ProtocolIndex].usesSampleSets) {
addControllerParameterForm(ControllerSettings, controllerindex, ControllerSettingsStruct::CONTROLLER_SAMPLE_SET_INITIATOR);
}
if (Protocol[ProtocolIndex].allowLocalSystemTime) {
addControllerParameterForm(ControllerSettings, controllerindex, ControllerSettingsStruct::CONTROLLER_USE_LOCAL_SYSTEM_TIME);
}


if (Protocol[ProtocolIndex].useCredentials()) {
addTableSeparator(F("Credentials"), 2, 3);
Expand Down
2 changes: 1 addition & 1 deletion tools/pio/pre_custom_esp82xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
# "-DUSES_P106", # BME680
# "-DUSES_P107", # SI1145 UV index

# "-DUSES_C016", # Cache Controller
"-DUSES_C016", # Cache Controller
"-DUSES_C018", # TTN/RN2483
# "-DUSES_C015", # Blynk

Expand Down

0 comments on commit 961a69a

Please sign in to comment.