Generalized-Core-Counter 3.20
Particle-based generalized core counter firmware
Loading...
Searching...
No Matches
MyPersistentData.h
Go to the documentation of this file.
1
34
35#ifndef __MYPERSISTENTDATA_H
36#define __MYPERSISTENTDATA_H
37
38#include "Particle.h"
39#include "StorageHelperRK.h"
40// This way you can do "data.setup()" instead of "MyPersistentData::instance().setup()" as an example
41#define current currentStatusData::instance()
42#define sysStatus sysStatusData::instance()
43#define sensorConfig sensorConfigData::instance()
44
45// *************** Operating Mode Enumerations ***************
54 COUNTING = 0, // Count each detection event (interrupt-driven)
55 OCCUPANCY = 1, // Track occupied/unoccupied state with debounce (interrupt-driven)
56 SCHEDULED = 2 // Time-based polling (non-interrupt)
57};
58
67 CONNECTED = 0, // Always connected, frequent reporting
68 LOW_POWER = 1, // Disconnect and sleep between reports
69 DISCONNECTED = 2 // Stay offline unless manually overridden
70};
71
72// NOTE: TriggerMode has been deprecated in favor of using
73// CountingMode (COUNTING/OCCUPANCY/SCHEDULED) as the single
74// source of truth for behavioral mode. The legacy TriggerMode
75// enum and associated storage have been removed.
76
86
87// ******************* SysStatus Storage Object **********************
88//
89// ********************************************************************
90
91class sysStatusData : public StorageHelperRK::PersistentDataFile {
92public:
93
99 static sysStatusData &instance();
100
106 void setup();
107
113 void loop();
114
119 bool validate(size_t dataSize);
120
129 void initialize();
130
131
132
133 class SysData {
134 public:
135 // This structure must always begin with the header (16 bytes)
136 StorageHelperRK::PersistentDataBase::SavedDataHeader sysHeader;
137 // Your fields go here. Once you've added a field you cannot add fields
138 // (except at the end), insert fields, remove fields, change size of a field.
139 // Doing so will cause the data to be corrupted!
140 uint8_t structuresVersion; // Version of the data structures (system and data)
141 bool verboseMode; // Turns on extra messaging
142 bool solarPowerMode; // Powered by a solar panel or utility power
143 bool lowPowerMode; // Does the device need to run disconnected to save battery
144 bool lowBatteryMode; // Is the battery level so low that we can no longer connect
145 uint8_t resetCount; // reset count of device (0-256)
146 char timeZoneStr[39]; // String for the timezone - https://developer.ibm.com/technologies/systems/articles/au-aix-posix/
147 uint8_t openTime; // Hour the park opens (0-23)
148 uint8_t closeTime; // Hour the park closes (0-23)
149 time_t lastReport; // The last time we sent a webhook to the queue
150 time_t lastConnection; // Last time we successfully connected to Particle
151 time_t lastHookResponse; // Last time we got a valid Webhook response
152 uint16_t lastConnectionDuration; // How long - in seconds - did it take to last connect to the Particle cloud
153 uint8_t sensorType; // What is the sensor type - 0-Pressure Sensor, 1-PIR Sensor
154 bool updatesPending; // Has there been a change to the sysStatus that we need to effect
155 uint16_t reportingInterval; // How often do we report in to the Particle cloud - in seconds
156 bool disconnectedMode; // Are we in disconnected mode - this is used to prevent the device from trying to connect to the Particle cloud - for Development and testing purposes
157 bool serialConnected; // Is the serial port connected - used to determine if we should wait for a serial connection before starting the device
158 time_t lastDailyCleanup; // Last time dailyCleanup() successfully ran
159 time_t lastTimeSync; // Last time we synced time from Particle cloud
160
161 // ********** Operating Mode Configuration **********
162 uint8_t countingMode; // 0=COUNTING, 1=OCCUPANCY, 2=SCHEDULED (time-based)
163 uint8_t operatingMode; // 0=CONNECTED, 1=LOW_POWER, 2=DISCONNECTED
164 uint32_t occupancyDebounceMs; // Milliseconds to wait before marking space as unoccupied (occupancy mode only)
165 uint16_t connectedReportingIntervalSec; // Reporting interval in seconds when in CONNECTED mode
166 uint16_t lowPowerReportingIntervalSec; // Reporting interval in seconds when in LOW_POWER mode
167 uint16_t connectAttemptBudgetSec; // Max seconds to spend attempting a cloud connect per wake
168 uint16_t cloudDisconnectBudgetSec; // Max seconds to wait for cloud disconnect before error
169 uint16_t modemOffBudgetSec; // Max seconds to wait for modem power-down before error
170
171 };
172
174
175 // ******************* Get and Set Functions for each variable in the storage object ***********
176
187
198
199 uint8_t get_structuresVersion() const ;
200 void set_structuresVersion(uint8_t value);
201
202 bool get_verboseMode() const;
203 void set_verboseMode(bool value);
204
205 bool get_solarPowerMode() const;
206 void set_solarPowerMode(bool value);
207
208 bool get_lowPowerMode() const;
209 void set_lowPowerMode(bool value);
210
211 bool get_lowBatteryMode() const;
212 void set_lowBatteryMode(bool value);
213
214 uint8_t get_resetCount() const;
215 void set_resetCount(uint8_t value);
216
217 String get_timeZoneStr() const;
218 bool set_timeZoneStr(const char *str);
219
220 uint8_t get_openTime() const;
221 void set_openTime(uint8_t value);
222
223 uint8_t get_closeTime() const;
224 void set_closeTime(uint8_t value);
225
226 time_t get_lastReport() const;
227 void set_lastReport(time_t value);
228
229 time_t get_lastConnection() const;
230 void set_lastConnection(time_t value);
231
232 uint16_t get_lastConnectionDuration() const;
233 void set_lastConnectionDuration(uint16_t value);
234
235 time_t get_lastHookResponse() const;
236 void set_lastHookResponse(time_t value);
237
238 uint8_t get_alertCodeNode() const;
239 void set_alertCodeNode(uint8_t value);
240
242 void set_alertTimestampNode(time_t value);
243
244 uint8_t get_sensorType() const;
245 void set_sensorType(uint8_t value);
246
247 bool get_updatesPending() const;
248 void set_updatesPending(bool value);
249
250 uint16_t get_reportingInterval() const;
251 void set_reportingInterval(uint16_t value);
252
253 bool get_disconnectedMode() const;
254 void set_disconnectedMode(bool value);
255
256 bool get_serialConnected() const;
257 void set_serialConnected(bool value);
258
259 time_t get_lastDailyCleanup() const;
260 void set_lastDailyCleanup(time_t value);
261
262 time_t get_lastTimeSync() const;
263 void set_lastTimeSync(time_t value);
264
265 // ********** Operating Mode Configuration Get/Set Functions **********
266
267 uint8_t get_countingMode() const;
268 void set_countingMode(uint8_t value);
269
270 uint8_t get_operatingMode() const;
271 void set_operatingMode(uint8_t value);
272
273 uint32_t get_occupancyDebounceMs() const;
274 void set_occupancyDebounceMs(uint32_t value);
275
276 uint16_t get_connectedReportingIntervalSec() const;
277 void set_connectedReportingIntervalSec(uint16_t value);
278
279 uint16_t get_lowPowerReportingIntervalSec() const;
280 void set_lowPowerReportingIntervalSec(uint16_t value);
281
282 uint16_t get_connectAttemptBudgetSec() const;
283 void set_connectAttemptBudgetSec(uint16_t value);
284
285 uint16_t get_cloudDisconnectBudgetSec() const;
286 void set_cloudDisconnectBudgetSec(uint16_t value);
287
288 uint16_t get_modemOffBudgetSec() const;
289 void set_modemOffBudgetSec(uint16_t value);
290
291
292 //Members here are internal only and therefore protected
293protected:
300
304 virtual ~sysStatusData();
305
309 sysStatusData(const sysStatusData&) = delete;
310
315
322
323 //Since these variables are only used internally - They can be private.
324 static const uint32_t SYS_DATA_MAGIC = 0x20a15e75;
325 static const uint16_t SYS_DATA_VERSION = 3;
326
327}; // End of sysStatusData class
328
329// ***************** Sensor Config Storage Object *******************
330//
331// ********************************************************************
332
333class sensorConfigData : public StorageHelperRK::PersistentDataFile {
334public:
335
341 static sensorConfigData &instance();
342
348 void setup();
349
355 void loop();
356
361 void loadCurrentDefaults(); // Initilize the object values for new deployments
362
363
368 bool validate(size_t dataSize);
369
378 void initialize();
379
381 public:
382 // This structure must always begin with the header (16 bytes)
383 StorageHelperRK::PersistentDataBase::SavedDataHeader sensorHeader;
384 // Your fields go here. Once you've added a field you cannot add fields
385 // (except at the end), insert fields, remove fields, change size of a field.
386 // Doing so will cause the data to be corrupted!
387 // You may want to keep a version number in your data.
388 uint16_t threshold1; // Sensor-specific threshold 1 (e.g., confidence, distance)
389 uint16_t threshold2; // Sensor-specific threshold 2 (e.g., secondary parameter)
390 uint16_t pollingRate; // How often to poll the sensor in seconds - a value of zero means no polling
391 };
393
394 // ******************* Get and Set Functions for each variable in the storage object ***********
395
406
417
418
419 uint16_t get_threshold1() const;
420 void set_threshold1(uint16_t value);
421
422 uint16_t get_threshold2() const;
423 void set_threshold2(uint16_t value); uint16_t get_pollingRate() const;
424 void set_pollingRate(uint16_t value);
425
426 //Members here are internal only and therefore protected
427protected:
434
438 virtual ~sensorConfigData();
439
444
449
456
457 //Since these variables are only used internally - They can be private.
458 static const uint32_t SENSOR_DATA_MAGIC = 0x20a47e74;
459 static const uint16_t SENSOR_DATA_VERSION = 1;
460}; // End of sensorConfigData class
461
462
463// ***************** Current Status Storage Object *******************
464//
465// ********************************************************************
466
467class currentStatusData : public StorageHelperRK::PersistentDataFile {
468public:
469
475 static currentStatusData &instance();
476
482 void setup();
483
489 void loop();
490
495 void loadCurrentDefaults(); // Initilize the object values for new deployments
496
501 void resetEverything();
502
507 bool validate(size_t dataSize);
508
517 void initialize();
518
520 public:
521 // This structure must always begin with the header (16 bytes)
522 StorageHelperRK::PersistentDataBase::SavedDataHeader currentHeader;
523 // Your fields go here. Once you've added a field you cannot add fields
524 // (except at the end), insert fields, remove fields, change size of a field.
525 // Doing so will cause the data to be corrupted!
526 // You may want to keep a version number in your data.
527 uint16_t faceNumber; // number of faces counted
528 uint16_t faceScore; // Score of the face counted
529 uint16_t gestureType; // Gesure observed
530 uint16_t gestureScore; // faceNumber to the object in cm
531 time_t lastCountTime; // Last time a count was made
532 float internalTempC; // Enclosure temperature in degrees C
533 float externalTempC; // Temp Sensor at the ultrasonic device
534 uint8_t alertCode; // Current Alert Code
536 float stateOfCharge; // Battery charge level
537 uint8_t batteryState; // Stores the current battery state
538
539 // ********** Counting Mode Fields **********
540 uint16_t hourlyCount; // Events counted this hour
541 uint16_t dailyCount; // Events counted today
542
543 // ********** Occupancy Mode Fields **********
544 bool occupied; // Is the space currently occupied? (occupancy mode)
545 uint32_t lastOccupancyEvent; // Timestamp of last occupancy detection (millis) - uint32_t to match millis() return type
546 time_t occupancyStartTime; // When current occupancy session started (epoch time)
547 uint32_t totalOccupiedSeconds; // Total occupied time today (in seconds)
548 };
550
551 // ******************* Get and Set Functions for each variable in the storage object ***********
552
563
574
575 uint16_t get_faceNumber() const;
576 void set_faceNumber(uint16_t value);
577
578 uint16_t get_faceScore() const;
579 void set_faceScore(uint16_t value);
580
581 uint16_t get_gestureType() const;
582 void set_gestureType(uint16_t value);
583
584 uint16_t get_gestureScore() const;
585 void set_gestureScore(uint16_t value);
586
587 time_t get_lastCountTime() const;
588 void set_lastCountTime(time_t value);
589
590 float get_internalTempC() const ;
591 void set_internalTempC(float value);
592
593 float get_externalTempC() const ;
594 void set_externalTempC(float value);
595
596 int8_t get_alertCode() const;
597 void set_alertCode(int8_t value);
598
607 void raiseAlert(int8_t value);
608
609 time_t get_lastAlertTime() const;
610 void set_lastAlertTime(time_t value);
611
612 float get_stateOfCharge() const;
613 void set_stateOfCharge(float value);
614
615 uint8_t get_batteryState() const;
616 void set_batteryState(uint8_t value);
617
618 // ********** Counting Mode Get/Set Functions **********
619
620 uint16_t get_hourlyCount() const;
621 void set_hourlyCount(uint16_t value);
622
623 uint16_t get_dailyCount() const;
624 void set_dailyCount(uint16_t value);
625
626 // ********** Occupancy Mode Get/Set Functions **********
627
628 bool get_occupied() const;
629 void set_occupied(bool value);
630
631 uint32_t get_lastOccupancyEvent() const;
632 void set_lastOccupancyEvent(uint32_t value);
633
634 time_t get_occupancyStartTime() const;
635 void set_occupancyStartTime(time_t value);
636
637 uint32_t get_totalOccupiedSeconds() const;
638 void set_totalOccupiedSeconds(uint32_t value);
639
640
641 //Members here are internal only and therefore protected
642protected:
649
653 virtual ~currentStatusData();
654
659
664
671
672 //Since these variables are only used internally - They can be private.
673 static const uint32_t CURRENT_DATA_MAGIC = 0x20a99e74;
674 static const uint16_t CURRENT_DATA_VERSION = 1;
675};
676
677
678#endif /* __MYPERSISTENTDATA_H */
CountingMode
Counting mode defines how the device processes sensor events.
@ SCHEDULED
@ OCCUPANCY
@ COUNTING
OperatingMode
Operating mode defines power and connectivity behavior.
@ LOW_POWER
@ CONNECTED
@ DISCONNECTED
StorageHelperRK::PersistentDataBase::SavedDataHeader currentHeader
void set_gestureScore(uint16_t value)
time_t get_lastAlertTime() const
uint16_t get_gestureScore() const
uint32_t get_lastOccupancyEvent() const
float get_internalTempC() const
void setup()
Perform setup operations; call this from global application setup().
static currentStatusData & instance()
Gets the singleton instance of this class, allocating it if necessary.
void resetEverything()
Resets the current and hourly counts.
void set_internalTempC(float value)
void initialize()
Will reinitialize data if it is found not to be valid.
void raiseAlert(int8_t value)
Raise an alert, keeping the highest severity code when multiple occur.
uint32_t get_totalOccupiedSeconds() const
void set_faceNumber(uint16_t value)
void loadCurrentDefaults()
Load the appropriate system defaults - good ot initialize a system to "factory settings".
float get_stateOfCharge() const
uint16_t get_dailyCount() const
int8_t get_alertCode() const
uint16_t get_hourlyCount() const
time_t get_occupancyStartTime() const
void set_batteryState(uint8_t value)
static const uint16_t CURRENT_DATA_VERSION
void set_gestureType(uint16_t value)
currentStatusData()
The constructor is protected because the class is a singleton.
float get_externalTempC() const
uint8_t get_batteryState() const
void set_externalTempC(float value)
void set_alertCode(int8_t value)
currentStatusData(const currentStatusData &)=delete
This class is a singleton and cannot be copied.
static currentStatusData * _instance
Singleton instance of this class.
time_t get_lastCountTime() const
void set_lastCountTime(time_t value)
static const uint32_t CURRENT_DATA_MAGIC
void set_lastOccupancyEvent(uint32_t value)
void set_occupied(bool value)
bool validate(size_t dataSize)
Validates values and, if valid, checks that data is in the correct range.
void loop()
Perform application loop operations; call this from global application loop().
void set_hourlyCount(uint16_t value)
uint16_t get_faceScore() const
void set_dailyCount(uint16_t value)
virtual ~currentStatusData()
The destructor is protected because the class is a singleton and cannot be deleted.
void set_faceScore(uint16_t value)
void set_totalOccupiedSeconds(uint32_t value)
uint16_t get_gestureType() const
void set_stateOfCharge(float value)
uint16_t get_faceNumber() const
For the Get functions, used to retrieve the value of the variable.
void set_lastAlertTime(time_t value)
currentStatusData & operator=(const currentStatusData &)=delete
This class is a singleton and cannot be copied.
void set_occupancyStartTime(time_t value)
StorageHelperRK::PersistentDataBase::SavedDataHeader sensorHeader
static sensorConfigData & instance()
Gets the singleton instance of this class, allocating it if necessary.
uint16_t get_pollingRate() const
static sensorConfigData * _instance
Singleton instance of this class.
static const uint32_t SENSOR_DATA_MAGIC
virtual ~sensorConfigData()
The destructor is protected because the class is a singleton and cannot be deleted.
uint16_t get_threshold1() const
For the Get functions, used to retrieve the value of the variable.
void setup()
Perform setup operations; call this from global application setup().
sensorConfigData()
The constructor is protected because the class is a singleton.
bool validate(size_t dataSize)
Validates values and, if valid, checks that data is in the correct range.
sensorConfigData & operator=(const sensorConfigData &)=delete
This class is a singleton and cannot be copied.
void set_pollingRate(uint16_t value)
void set_threshold2(uint16_t value)
void loadCurrentDefaults()
Load the appropriate system defaults - good ot initialize a system to "factory settings".
void initialize()
Will reinitialize data if it is found not to be valid.
void set_threshold1(uint16_t value)
sensorConfigData(const sensorConfigData &)=delete
This class is a singleton and cannot be copied.
void loop()
Perform application loop operations; call this from global application loop().
static const uint16_t SENSOR_DATA_VERSION
uint16_t get_threshold2() const
StorageHelperRK::PersistentDataBase::SavedDataHeader sysHeader
time_t get_lastReport() const
uint16_t get_connectAttemptBudgetSec() const
void set_modemOffBudgetSec(uint16_t value)
bool get_solarPowerMode() const
sysStatusData & operator=(const sysStatusData &)=delete
This class is a singleton and cannot be copied.
time_t get_lastHookResponse() const
void set_lastConnectionDuration(uint16_t value)
uint8_t get_operatingMode() const
time_t get_lastDailyCleanup() const
bool set_timeZoneStr(const char *str)
uint8_t get_resetCount() const
void set_connectAttemptBudgetSec(uint16_t value)
bool get_lowPowerMode() const
void set_lastHookResponse(time_t value)
void set_cloudDisconnectBudgetSec(uint16_t value)
uint8_t get_structuresVersion() const
For the Get functions, used to retrieve the value of the variable.
uint16_t get_reportingInterval() const
uint8_t get_sensorType() const
void set_lastReport(time_t value)
void set_serialConnected(bool value)
static sysStatusData & instance()
Gets the singleton instance of this class, allocating it if necessary.
sysStatusData(const sysStatusData &)=delete
This class is a singleton and cannot be copied.
uint16_t get_lastConnectionDuration() const
void set_disconnectedMode(bool value)
void set_lowPowerReportingIntervalSec(uint16_t value)
void set_resetCount(uint8_t value)
void set_countingMode(uint8_t value)
void set_solarPowerMode(bool value)
void set_connectedReportingIntervalSec(uint16_t value)
void set_lowBatteryMode(bool value)
sysStatusData()
The constructor is protected because the class is a singleton.
void set_updatesPending(bool value)
void set_alertCodeNode(uint8_t value)
bool get_disconnectedMode() const
void set_closeTime(uint8_t value)
static sysStatusData * _instance
Singleton instance of this class.
uint32_t get_occupancyDebounceMs() const
static const uint32_t SYS_DATA_MAGIC
void set_openTime(uint8_t value)
uint16_t get_cloudDisconnectBudgetSec() const
void loop()
Perform application loop operations; call this from global application loop().
uint16_t get_modemOffBudgetSec() const
uint8_t get_alertCodeNode() const
String get_timeZoneStr() const
bool get_lowBatteryMode() const
void set_structuresVersion(uint8_t value)
void initialize()
Will reinitialize data if it is found not to be valid.
uint16_t get_connectedReportingIntervalSec() const
void set_sensorType(uint8_t value)
void set_lowPowerMode(bool value)
void setup()
Perform setup operations; call this from global application setup().
void set_alertTimestampNode(time_t value)
void set_reportingInterval(uint16_t value)
uint8_t get_closeTime() const
static const uint16_t SYS_DATA_VERSION
bool get_serialConnected() const
virtual ~sysStatusData()
The destructor is protected because the class is a singleton and cannot be deleted.
void set_occupancyDebounceMs(uint32_t value)
void set_verboseMode(bool value)
bool get_updatesPending() const
bool get_verboseMode() const
void set_lastDailyCleanup(time_t value)
uint8_t get_openTime() const
void set_operatingMode(uint8_t value)
time_t get_lastTimeSync() const
time_t get_lastConnection() const
void set_lastConnection(time_t value)
void set_lastTimeSync(time_t value)
time_t get_alertTimestampNode() const
bool validate(size_t dataSize)
Validates values and, if valid, checks that data is in the correct range.
uint8_t get_countingMode() const
uint16_t get_lowPowerReportingIntervalSec() const