Parcourir la source

* sensor node: now have two alert priorities, LO, HI

Scott Powell il y a 1 an
Parent
commit
797ab85283

+ 6 - 5
examples/simple_sensor/SensorMesh.cpp

@@ -332,13 +332,14 @@ void SensorMesh::applyContactPermissions(const uint8_t* pubkey, uint16_t perms)
   dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);   // trigger saveContacts()
 }
 
-void SensorMesh::sendAlert(const char* text) {
+void SensorMesh::sendAlert(AlertPriority pri, const char* text) {
   int text_len = strlen(text);
+  uint16_t pri_mask = (pri == HIGH_PRI_ALERT) ? PERM_RECV_ALERTS_HI : PERM_RECV_ALERTS_LO;
 
   // send text message to all contacts with RECV_ALERT permission
   for (int i = 0; i < num_contacts; i++) {
     auto c = &contacts[i];
-    if ((c->permissions & PERM_RECV_ALERTS) == 0) continue;   // contact does NOT want alerts
+    if ((c->permissions & pri_mask) == 0) continue;   // contact does NOT want alert
 
     uint8_t data[MAX_PACKET_PAYLOAD];
     uint32_t now = getRTCClock()->getCurrentTimeUnique();  // need different timestamp per packet
@@ -360,12 +361,12 @@ void SensorMesh::sendAlert(const char* text) {
   }
 }
 
-void SensorMesh::alertIf(bool condition, Trigger& t, const char* text) {
+void SensorMesh::alertIf(bool condition, Trigger& t, AlertPriority pri, const char* text) {
   if (condition) {
     if (!t.triggered) {
       t.triggered = true;
       t.time = getRTCClock()->getCurrentTime();
-      sendAlert(text);
+      sendAlert(pri, text);
     }
   } else {
     if (t.triggered) {
@@ -422,7 +423,7 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
   MESH_DEBUG_PRINTLN("Login success!");
   client->last_timestamp = sender_timestamp;
   client->last_activity = getRTCClock()->getCurrentTime();
-  client->permissions = PERM_IS_ADMIN | PERM_RECV_ALERTS;
+  client->permissions = PERM_IS_ADMIN | PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO;  // initially opt-in to receive alerts (can opt out)
   memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
 
   dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);

+ 5 - 4
examples/simple_sensor/SensorMesh.h

@@ -26,7 +26,8 @@
 #define PERM_IS_ADMIN          0x8000
 #define PERM_GET_TELEMETRY     0x0001
 #define PERM_GET_MIN_MAX_AVG   0x0002
-#define PERM_RECV_ALERTS       0x0100
+#define PERM_RECV_ALERTS_LO    0x0100   // low priority alerts
+#define PERM_RECV_ALERTS_HI    0x0200   // high priority alerts
 
 struct ContactInfo {
   mesh::Identity id;
@@ -104,8 +105,8 @@ protected:
 
     Trigger() { triggered = false; time = 0; }
   };
-
-  void alertIf(bool condition, Trigger& t, const char* text);
+  enum AlertPriority { LOW_PRI_ALERT, HIGH_PRI_ALERT };
+  void alertIf(bool condition, Trigger& t, AlertPriority pri, const char* text);
 
   virtual void onSensorDataRead() = 0;   // for app to implement
   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 @@ private:
   ContactInfo* putContact(const mesh::Identity& id);
   void applyContactPermissions(const uint8_t* pubkey, uint16_t perms);
 
-  void sendAlert(const char* text);
+  void sendAlert(AlertPriority pri, const char* text);
 
 };

+ 1 - 1
examples/simple_sensor/main.cpp

@@ -22,7 +22,7 @@ protected:
     float batt_voltage = getVoltage(TELEM_CHANNEL_SELF);
 
     battery_data.recordData(getRTCClock(), batt_voltage);   // record battery
-    alertIf(batt_voltage < 3.4f, low_batt, "Battery low!");
+    alertIf(batt_voltage < 3.4f, low_batt, HIGH_PRI_ALERT, "Battery low!");
   }
 
   int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) override {