Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock relay status on boot #1705

Merged
merged 11 commits into from
Aug 12, 2019
6 changes: 6 additions & 0 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
#define RELAY_BOOT_ON 1
#define RELAY_BOOT_SAME 2
#define RELAY_BOOT_TOGGLE 3
#define RELAY_BOOT_LOCKED_OFF 4
#define RELAY_BOOT_LOCKED_ON 5

#define RELAY_TYPE_NORMAL 0
#define RELAY_TYPE_INVERSE 1
Expand All @@ -101,6 +103,10 @@
#define RELAY_GROUP_SYNC_INVERSE 1
#define RELAY_GROUP_SYNC_RECEIVEONLY 2

#define RELAY_LOCK_OFF 0
#define RELAY_LOCK_ON 1
#define RELAY_LOCK_DISABLED 2

//------------------------------------------------------------------------------
// UDP SYSLOG
//------------------------------------------------------------------------------
Expand Down
Binary file modified code/espurna/data/index.all.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.light.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.lightfox.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfbridge.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfm69.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.sensor.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.small.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.thermostat.html.gz
Binary file not shown.
42 changes: 39 additions & 3 deletions code/espurna/relay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct {

bool current_status; // Holds the current (physical) status of the relay
bool target_status; // Holds the target status
unsigned char lock; // Holds the value of target status, that cannot be changed afterwards. (0 for false, 1 for true, 2 to disable)
unsigned long fw_start; // Flood window start time
unsigned char fw_count; // Number of changes within the current flood window
unsigned long change_time; // Scheduled time to change
Expand Down Expand Up @@ -174,6 +175,23 @@ void _relayProcess(bool mode) {
// Only process the relays we have to change to the requested mode
if (target != mode) continue;

// Only process the relays that can be changed
switch (_relays[id].lock) {
case RELAY_LOCK_ON:
case RELAY_LOCK_OFF:
{
bool lock = _relays[id].lock == 1;
if (lock != _relays[id].target_status) {
_relays[id].target_status = lock;
continue;
}
break;
}
case RELAY_LOCK_DISABLED:
default:
break;
}

// Only process if the change_time has arrived
if (current_time < _relays[id].change_time) continue;

Expand Down Expand Up @@ -521,13 +539,15 @@ void _relayBoot() {
auto mask = std::bitset<RELAY_SAVE_MASK_MAX>(stored_mask);

// Walk the relays
unsigned char lock;
bool status;
for (unsigned char i=0; i<relayCount(); ++i) {

unsigned char boot_mode = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt();
DEBUG_MSG_P(PSTR("[RELAY] Relay #%u boot mode %u\n"), i, boot_mode);

status = false;
lock = RELAY_LOCK_DISABLED;
switch (boot_mode) {
case RELAY_BOOT_SAME:
if (i < 8) {
Expand All @@ -541,6 +561,13 @@ void _relayBoot() {
trigger_save = true;
}
break;
case RELAY_BOOT_LOCKED_ON:
status = true;
lock = RELAY_LOCK_ON;
break;
case RELAY_BOOT_LOCKED_OFF:
lock = RELAY_LOCK_OFF;
break;
case RELAY_BOOT_ON:
status = true;
break;
Expand All @@ -556,7 +583,10 @@ void _relayBoot() {
#else
_relays[i].change_time = millis();
#endif
}

_relays[i].lock = lock;

}

// Save if there is any relay in the RELAY_BOOT_TOGGLE mode
if (trigger_save) {
Expand Down Expand Up @@ -599,9 +629,15 @@ bool _relayWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
}

void _relayWebSocketUpdate(JsonObject& root) {
JsonArray& relay = root.createNestedArray("relayStatus");
JsonObject& state = root.createNestedObject("relayState");
state["size"] = relayCount();

JsonArray& status = state.createNestedArray("status");
JsonArray& lock = state.createNestedArray("lock");

for (unsigned char i=0; i<relayCount(); i++) {
relay.add<uint8_t>(_relays[i].target_status);
status.add<uint8_t>(_relays[i].target_status);
lock.add(_relays[i].lock);
}
}

Expand Down
6,469 changes: 3,236 additions & 3,233 deletions code/espurna/static/index.all.html.gz.h

Large diffs are not rendered by default.

4,403 changes: 2,203 additions & 2,200 deletions code/espurna/static/index.light.html.gz.h

Large diffs are not rendered by default.

3,683 changes: 1,843 additions & 1,840 deletions code/espurna/static/index.lightfox.html.gz.h

Large diffs are not rendered by default.

3,717 changes: 1,860 additions & 1,857 deletions code/espurna/static/index.rfbridge.html.gz.h

Large diffs are not rendered by default.

6,439 changes: 3,221 additions & 3,218 deletions code/espurna/static/index.rfm69.html.gz.h

Large diffs are not rendered by default.

3,772 changes: 1,887 additions & 1,885 deletions code/espurna/static/index.sensor.html.gz.h

Large diffs are not rendered by default.

3,683 changes: 1,843 additions & 1,840 deletions code/espurna/static/index.small.html.gz.h

Large diffs are not rendered by default.

5,319 changes: 2,661 additions & 2,658 deletions code/espurna/static/index.thermostat.html.gz.h

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,15 @@ function initRelays(data) {

}

function updateRelays(data) {
var size = data.size;
for (var i=0; i<size; ++i) {
var elem = $("input[name='relay'][data='" + i + "']");
elem.prop("checked", data.status[i]);
elem.prop("disabled", data.lock[i] < 2); // RELAY_LOCK_DISABLED=2
}
}

function createCheckboxes() {

$("input[type='checkbox']").each(function() {
Expand Down Expand Up @@ -1565,11 +1574,9 @@ function processData(data) {
// Relays
// ---------------------------------------------------------------------

if ("relayStatus" === key) {
initRelays(value);
for (i in value) {
$("input[name='relay'][data='" + i + "']").prop("checked", value[i]);
}
if ("relayState" === key) {
initRelays(value.status);
updateRelays(value);
return;
}

Expand Down
2 changes: 2 additions & 0 deletions code/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,8 @@ <h2>
<option value="1">Always ON</option>
<option value="2">Same as before</option>
<option value="3">Toggle before</option>
<option value="4">Locked OFF</option>
<option value="5">Locked ON</option>
</select>
</div>
<div class="pure-g">
Expand Down