Begin development of NVS Characteristic storage
Used method restore() to restore value. To do: Change this to a flag during instantiation of a new Characteristic instead of a separate method.
This commit is contained in:
parent
49e3786618
commit
40798b15cf
|
|
@ -26,7 +26,6 @@
|
||||||
********************************************************************************/
|
********************************************************************************/
|
||||||
|
|
||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
#include <nvs_flash.h>
|
|
||||||
#include <sodium.h>
|
#include <sodium.h>
|
||||||
#include <MD5Builder.h>
|
#include <MD5Builder.h>
|
||||||
|
|
||||||
|
|
@ -39,8 +38,6 @@ void HAPClient::init(){
|
||||||
|
|
||||||
size_t len; // not used but required to read blobs from NVS
|
size_t len; // not used but required to read blobs from NVS
|
||||||
|
|
||||||
nvs_flash_init(); // initialize non-volatile-storage partition in flash
|
|
||||||
|
|
||||||
nvs_open("WIFI",NVS_READWRITE,&wifiNVS); // open WIFI data namespace in NVS
|
nvs_open("WIFI",NVS_READWRITE,&wifiNVS); // open WIFI data namespace in NVS
|
||||||
nvs_open("SRP",NVS_READWRITE,&srpNVS); // open SRP data namespace in NVS
|
nvs_open("SRP",NVS_READWRITE,&srpNVS); // open SRP data namespace in NVS
|
||||||
nvs_open("HAP",NVS_READWRITE,&hapNVS); // open HAP data namespace in NVS
|
nvs_open("HAP",NVS_READWRITE,&hapNVS); // open HAP data namespace in NVS
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <nvs.h>
|
|
||||||
|
|
||||||
#include "HomeSpan.h"
|
#include "HomeSpan.h"
|
||||||
#include "TLV.h"
|
#include "TLV.h"
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,9 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
|
||||||
|
|
||||||
hapServer=new WiFiServer(tcpPortNum);
|
hapServer=new WiFiServer(tcpPortNum);
|
||||||
|
|
||||||
|
nvs_flash_init(); // initialize non-volatile-storage partition in flash
|
||||||
|
nvs_open("CHAR",NVS_READWRITE,&charNVS);
|
||||||
|
|
||||||
delay(2000);
|
delay(2000);
|
||||||
|
|
||||||
Serial.print("\n************************************************************\n"
|
Serial.print("\n************************************************************\n"
|
||||||
|
|
@ -1135,13 +1138,15 @@ int Span::updateCharacteristics(char *buf, SpanBuf *pObj){
|
||||||
LOG1(pObj[j].characteristic->aid);
|
LOG1(pObj[j].characteristic->aid);
|
||||||
LOG1(" iid=");
|
LOG1(" iid=");
|
||||||
LOG1(pObj[j].characteristic->iid);
|
LOG1(pObj[j].characteristic->iid);
|
||||||
if(status==StatusCode::OK){ // if status is okay
|
if(status==StatusCode::OK){ // if status is okay
|
||||||
pObj[j].characteristic->value
|
pObj[j].characteristic->value=pObj[j].characteristic->newValue; // update characteristic value with new value
|
||||||
=pObj[j].characteristic->newValue; // update characteristic value with new value
|
if(pObj[j].characteristic->nvsKey){ // if storage key found
|
||||||
|
nvs_set_blob(charNVS,pObj[j].characteristic->nvsKey,&(pObj[j].characteristic->value),sizeof(pObj[j].characteristic->value)); // store data
|
||||||
|
nvs_commit(charNVS);
|
||||||
|
}
|
||||||
LOG1(" (okay)\n");
|
LOG1(" (okay)\n");
|
||||||
} else { // if status not okay
|
} else { // if status not okay
|
||||||
pObj[j].characteristic->newValue
|
pObj[j].characteristic->newValue=pObj[j].characteristic->value; // replace characteristic new value with original value
|
||||||
=pObj[j].characteristic->value; // replace characteristic new value with original value
|
|
||||||
LOG1(" (failed)\n");
|
LOG1(" (failed)\n");
|
||||||
}
|
}
|
||||||
pObj[j].characteristic->isUpdated=false; // reset isUpdated flag for characteristic
|
pObj[j].characteristic->isUpdated=false; // reset isUpdated flag for characteristic
|
||||||
|
|
@ -1520,6 +1525,25 @@ SpanCharacteristic::SpanCharacteristic(HapChar *hapChar){
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
|
void SpanCharacteristic::restore(){
|
||||||
|
|
||||||
|
nvsKey=(char *)malloc(16);
|
||||||
|
uint16_t t;
|
||||||
|
sscanf(type,"%x",&t);
|
||||||
|
sprintf(nvsKey,"%04X%08X%03X",t,aid,iid&0xFFF);
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
if(!nvs_get_blob(homeSpan.charNVS,nvsKey,NULL,&len)){
|
||||||
|
nvs_get_blob(homeSpan.charNVS,nvsKey,&value,&len);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data
|
||||||
|
nvs_commit(homeSpan.charNVS); // commit to NVS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////
|
||||||
|
|
||||||
int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){
|
int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){
|
||||||
int nBytes=0;
|
int nBytes=0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <nvs.h>
|
||||||
|
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
|
|
@ -106,6 +107,7 @@ struct Span{
|
||||||
boolean isBridge=true; // flag indicating whether device is configured as a bridge (i.e. first Accessory contains nothing but AccessoryInformation and HAPProtocolInformation)
|
boolean isBridge=true; // flag indicating whether device is configured as a bridge (i.e. first Accessory contains nothing but AccessoryInformation and HAPProtocolInformation)
|
||||||
HapQR qrCode; // optional QR Code to use for pairing
|
HapQR qrCode; // optional QR Code to use for pairing
|
||||||
const char *sketchVersion="n/a"; // version of the sketch
|
const char *sketchVersion="n/a"; // version of the sketch
|
||||||
|
nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data
|
||||||
|
|
||||||
boolean connected=false; // WiFi connection status
|
boolean connected=false; // WiFi connection status
|
||||||
unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts
|
unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts
|
||||||
|
|
@ -252,6 +254,7 @@ struct SpanCharacteristic{
|
||||||
boolean staticRange; // Flag that indiates whether Range is static and cannot be changed with setRange()
|
boolean staticRange; // Flag that indiates whether Range is static and cannot be changed with setRange()
|
||||||
boolean customRange=false; // Flag for custom ranges
|
boolean customRange=false; // Flag for custom ranges
|
||||||
boolean *ev; // Characteristic Event Notify Enable (per-connection)
|
boolean *ev; // Characteristic Event Notify Enable (per-connection)
|
||||||
|
char *nvsKey=NULL; // key for NVS storage of Characteristic value
|
||||||
|
|
||||||
uint32_t aid=0; // Accessory ID - passed through from Service containing this Characteristic
|
uint32_t aid=0; // Accessory ID - passed through from Service containing this Characteristic
|
||||||
boolean isUpdated=false; // set to true when new value has been requested by PUT /characteristic
|
boolean isUpdated=false; // set to true when new value has been requested by PUT /characteristic
|
||||||
|
|
@ -263,6 +266,7 @@ struct SpanCharacteristic{
|
||||||
|
|
||||||
int sprintfAttributes(char *cBuf, int flags); // prints Characteristic JSON records into buf, according to flags mask; return number of characters printed, excluding null terminator
|
int sprintfAttributes(char *cBuf, int flags); // prints Characteristic JSON records into buf, according to flags mask; return number of characters printed, excluding null terminator
|
||||||
StatusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intiial HAP status code (checks to see if characteristic is found, is writable, etc.)
|
StatusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intiial HAP status code (checks to see if characteristic is found, is writable, etc.)
|
||||||
|
void restore(); // loads previous value of Characteristic from NVS (if found)
|
||||||
|
|
||||||
boolean updated(){return(isUpdated);} // returns isUpdated
|
boolean updated(){return(isUpdated);} // returns isUpdated
|
||||||
unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated
|
unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated
|
||||||
|
|
|
||||||
10
src/src.ino
10
src/src.ino
|
|
@ -39,13 +39,13 @@ void setup() {
|
||||||
new Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP
|
new Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP
|
||||||
|
|
||||||
new Service::LightBulb();
|
new Service::LightBulb();
|
||||||
new Characteristic::On();
|
(new Characteristic::On())->restore();
|
||||||
new Characteristic::Brightness();
|
(new Characteristic::Brightness())->restore();
|
||||||
new Characteristic::Name("Light 1");
|
new Characteristic::Name("Light 1");
|
||||||
new Characteristic::ColorTemperature(0);
|
new Characteristic::ColorTemperature();
|
||||||
new Service::LightBulb();
|
new Service::LightBulb();
|
||||||
new Characteristic::On(2);
|
(new Characteristic::On())->restore();
|
||||||
(new Characteristic::Brightness(50))->setRange(10,100,5);
|
(new Characteristic::Brightness(50))->setRange(10,100,5)->restore();
|
||||||
new Characteristic::Name("Light 2");
|
new Characteristic::Name("Light 2");
|
||||||
|
|
||||||
} // end of setup()
|
} // end of setup()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue