Skip to content

Commit 797ab85

Browse files
author
Scott Powell
committed
* sensor node: now have two alert priorities, LO, HI
1 parent 91b9113 commit 797ab85

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

examples/simple_sensor/SensorMesh.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,14 @@ void SensorMesh::applyContactPermissions(const uint8_t* pubkey, uint16_t perms)
332332
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger saveContacts()
333333
}
334334

335-
void SensorMesh::sendAlert(const char* text) {
335+
void SensorMesh::sendAlert(AlertPriority pri, const char* text) {
336336
int text_len = strlen(text);
337+
uint16_t pri_mask = (pri == HIGH_PRI_ALERT) ? PERM_RECV_ALERTS_HI : PERM_RECV_ALERTS_LO;
337338

338339
// send text message to all contacts with RECV_ALERT permission
339340
for (int i = 0; i < num_contacts; i++) {
340341
auto c = &contacts[i];
341-
if ((c->permissions & PERM_RECV_ALERTS) == 0) continue; // contact does NOT want alerts
342+
if ((c->permissions & pri_mask) == 0) continue; // contact does NOT want alert
342343

343344
uint8_t data[MAX_PACKET_PAYLOAD];
344345
uint32_t now = getRTCClock()->getCurrentTimeUnique(); // need different timestamp per packet
@@ -360,12 +361,12 @@ void SensorMesh::sendAlert(const char* text) {
360361
}
361362
}
362363

363-
void SensorMesh::alertIf(bool condition, Trigger& t, const char* text) {
364+
void SensorMesh::alertIf(bool condition, Trigger& t, AlertPriority pri, const char* text) {
364365
if (condition) {
365366
if (!t.triggered) {
366367
t.triggered = true;
367368
t.time = getRTCClock()->getCurrentTime();
368-
sendAlert(text);
369+
sendAlert(pri, text);
369370
}
370371
} else {
371372
if (t.triggered) {
@@ -422,7 +423,7 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
422423
MESH_DEBUG_PRINTLN("Login success!");
423424
client->last_timestamp = sender_timestamp;
424425
client->last_activity = getRTCClock()->getCurrentTime();
425-
client->permissions = PERM_IS_ADMIN | PERM_RECV_ALERTS;
426+
client->permissions = PERM_IS_ADMIN | PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO; // initially opt-in to receive alerts (can opt out)
426427
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
427428

428429
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);

examples/simple_sensor/SensorMesh.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
#define PERM_IS_ADMIN 0x8000
2727
#define PERM_GET_TELEMETRY 0x0001
2828
#define PERM_GET_MIN_MAX_AVG 0x0002
29-
#define PERM_RECV_ALERTS 0x0100
29+
#define PERM_RECV_ALERTS_LO 0x0100 // low priority alerts
30+
#define PERM_RECV_ALERTS_HI 0x0200 // high priority alerts
3031

3132
struct ContactInfo {
3233
mesh::Identity id;
@@ -104,8 +105,8 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
104105

105106
Trigger() { triggered = false; time = 0; }
106107
};
107-
108-
void alertIf(bool condition, Trigger& t, const char* text);
108+
enum AlertPriority { LOW_PRI_ALERT, HIGH_PRI_ALERT };
109+
void alertIf(bool condition, Trigger& t, AlertPriority pri, const char* text);
109110

110111
virtual void onSensorDataRead() = 0; // for app to implement
111112
virtual int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) = 0; // for app to implement
@@ -145,6 +146,6 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
145146
ContactInfo* putContact(const mesh::Identity& id);
146147
void applyContactPermissions(const uint8_t* pubkey, uint16_t perms);
147148

148-
void sendAlert(const char* text);
149+
void sendAlert(AlertPriority pri, const char* text);
149150

150151
};

examples/simple_sensor/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MyMesh : public SensorMesh {
2222
float batt_voltage = getVoltage(TELEM_CHANNEL_SELF);
2323

2424
battery_data.recordData(getRTCClock(), batt_voltage); // record battery
25-
alertIf(batt_voltage < 3.4f, low_batt, "Battery low!");
25+
alertIf(batt_voltage < 3.4f, low_batt, HIGH_PRI_ALERT, "Battery low!");
2626
}
2727

2828
int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) override {

0 commit comments

Comments
 (0)