Browse Source

RPN: check relay status before triggering (#2268)

* rpn: check status to avoid rescheduling

* keep existing op, add name hint
mcspr-patch-1
Max Prokhorov 4 years ago
committed by GitHub
parent
commit
5f90c7c3bc
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 9 deletions
  1. +5
    -0
      code/espurna/relay.cpp
  2. +5
    -0
      code/espurna/relay.h
  3. +24
    -9
      code/espurna/rpnrules.cpp

+ 5
- 0
code/espurna/relay.cpp View File

@ -619,6 +619,11 @@ bool relayStatus(unsigned char id) {
}
bool relayStatusTarget(unsigned char id) {
if (id >= _relays.size()) return false;
return _relays[id].target_status;
}
void relaySync(unsigned char id) {
// No sync if none or only one relay


+ 5
- 0
code/espurna/relay.h View File

@ -19,7 +19,12 @@ PayloadStatus relayParsePayload(const char * payload);
bool relayStatus(unsigned char id, bool status, bool report, bool group_report);
bool relayStatus(unsigned char id, bool status);
// gets either current or target status, where current is the status that we are
// actually in and target is the status we would be, eventually, unless
// relayStatus(id, relayStatus()) is called
bool relayStatus(unsigned char id);
bool relayStatusTarget(unsigned char id);
void relayToggle(unsigned char id, bool report, bool group_report);
void relayToggle(unsigned char id);


+ 24
- 9
code/espurna/rpnrules.cpp View File

@ -139,6 +139,23 @@ bool _rpnNtpFunc(rpn_context & ctxt, int (*func)(time_t)) {
#endif
#if RELAY_SUPPORT
bool _rpnRelayStatus(rpn_context & ctxt, bool force) {
float status, id;
rpn_stack_pop(ctxt, id);
rpn_stack_pop(ctxt, status);
if (int(status) == 2) {
relayToggle(int(id));
} else if (force || (relayStatusTarget(int(id)) != (int(status) == 1))) {
relayStatus(int(id), int(status) == 1);
}
return true;
}
#endif
void _rpnDump() {
float value;
DEBUG_MSG_P(PSTR("[RPN] Stack:\n"));
@ -239,16 +256,14 @@ void _rpnInit() {
// Accept relay number and numeric API status value (0, 1 and 2)
#if RELAY_SUPPORT
// apply status and reset timers when called
rpn_operator_set(_rpn_ctxt, "relay_reset", 2, [](rpn_context & ctxt) {
return _rpnRelayStatus(ctxt, true);
});
// only update status when target status differs, keep running timers
rpn_operator_set(_rpn_ctxt, "relay", 2, [](rpn_context & ctxt) {
float status, id;
rpn_stack_pop(ctxt, id);
rpn_stack_pop(ctxt, status);
if (int(status) == 2) {
relayToggle(int(id));
} else {
relayStatus(int(id), int(status) == 1);
}
return true;
return _rpnRelayStatus(ctxt, false);
});
#endif // RELAY_SUPPORT == 1


Loading…
Cancel
Save