Converted sprintfNotify and eventNotify to use HapOut
Successfully tested with MultiTest script
This commit is contained in:
parent
20fe4ab6fd
commit
00944c1f2f
34
src/HAP.cpp
34
src/HAP.cpp
|
|
@ -1286,32 +1286,26 @@ void HAPClient::checkTimedWrites(){
|
|||
|
||||
void HAPClient::eventNotify(SpanBuf *pObj, int nObj, int ignoreClient){
|
||||
|
||||
for(int cNum=0;cNum<homeSpan.maxConnections;cNum++){ // loop over all connection slots
|
||||
if(hap[cNum]->client && cNum!=ignoreClient){ // if there is a client connected to this slot and it is NOT flagged to be ignored (in cases where it is the client making a PUT request)
|
||||
for(int cNum=0;cNum<homeSpan.maxConnections;cNum++){ // loop over all connection slots
|
||||
if(hap[cNum]->client && cNum!=ignoreClient){ // if there is a client connected to this slot and it is NOT flagged to be ignored (in cases where it is the client making a PUT request)
|
||||
|
||||
int nBytes=homeSpan.sprintfNotify(pObj,nObj,NULL,cNum); // get JSON response for notifications to client cNum - includes terminating null (will be recast to uint8_t* below)
|
||||
homeSpan.printfNotify(pObj,nObj,cNum); // create JSON (which may be of zero length if there are no applicable notifications for this cNum)
|
||||
size_t nBytes=hapOut.getSize();
|
||||
hapOut.flush();
|
||||
|
||||
if(nBytes>0){ // if there are notifications to send to client cNum
|
||||
TempBuffer<char> jsonBuf(nBytes+1);
|
||||
homeSpan.sprintfNotify(pObj,nObj,jsonBuf,cNum);
|
||||
if(nBytes>0){ // if there ARE notifications to send to client cNum
|
||||
|
||||
char *body;
|
||||
asprintf(&body,"EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",nBytes);
|
||||
LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",hap[cNum]->client.remoteIP().toString().c_str());
|
||||
|
||||
LOG2("\n>>>>>>>>>> ");
|
||||
LOG2(hap[cNum]->client.remoteIP());
|
||||
LOG2(" >>>>>>>>>>\n");
|
||||
LOG2(body);
|
||||
LOG2(jsonBuf.get());
|
||||
LOG2("\n");
|
||||
hapOut.setLogLevel(2).setHapClient(hap[cNum]);
|
||||
hapOut << "EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: " << nBytes << "\r\n\r\n";
|
||||
homeSpan.printfNotify(pObj,nObj,cNum);
|
||||
hapOut.flush();
|
||||
|
||||
hap[cNum]->sendEncrypted(body,(uint8_t *)jsonBuf.get(),nBytes); // note recasting of jsonBuf into uint8_t*
|
||||
free(body);
|
||||
|
||||
} // if there are characteristic updates to notify client cNum
|
||||
} // if client exists
|
||||
LOG2("\n-------- SENT ENCRYPTED! --------\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
|||
|
|
@ -1489,32 +1489,29 @@ void Span::clearNotify(int slotNum){
|
|||
|
||||
///////////////////////////////
|
||||
|
||||
int Span::sprintfNotify(SpanBuf *pObj, int nObj, char *cBuf, int conNum){
|
||||
void Span::printfNotify(SpanBuf *pObj, int nObj, int conNum){
|
||||
|
||||
int nChars=0;
|
||||
boolean notifyFlag=false;
|
||||
|
||||
nChars+=snprintf(cBuf,cBuf?64:0,"{\"characteristics\":[");
|
||||
|
||||
for(int i=0;i<nObj;i++){ // loop over all objects
|
||||
for(int i=0;i<nObj;i++){ // loop over all objects
|
||||
|
||||
if(pObj[i].status==StatusCode::OK && pObj[i].val){ // characteristic was successfully updated with a new value (i.e. not just an EV request)
|
||||
|
||||
if(pObj[i].characteristic->ev[conNum]){ // if notifications requested for this characteristic by specified connection number
|
||||
if(pObj[i].characteristic->ev[conNum]){ // if notifications requested for this characteristic by specified connection number
|
||||
|
||||
if(notifyFlag) // already printed at least one other characteristic
|
||||
nChars+=snprintf(cBuf?(cBuf+nChars):NULL,cBuf?64:0,","); // add preceeding comma before printing next characteristic
|
||||
if(!notifyFlag) // this is first notification for any characteristic
|
||||
hapOut << "{\"characteristics\":["; // print start of JSON array
|
||||
else // else already printed at least one other characteristic
|
||||
hapOut << ","; // add preceeding comma before printing this characteristic
|
||||
|
||||
nChars+=pObj[i].characteristic->sprintfAttributes(cBuf?(cBuf+nChars):NULL,GET_VALUE|GET_AID|GET_NV); // get JSON attributes for characteristic
|
||||
pObj[i].characteristic->printfAttributes(GET_VALUE|GET_AID|GET_NV); // print JSON attributes for this characteristic
|
||||
notifyFlag=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // notification requested
|
||||
} // characteristic updated
|
||||
} // loop over all objects
|
||||
|
||||
nChars+=snprintf(cBuf?(cBuf+nChars):NULL,cBuf?64:0,"]}");
|
||||
|
||||
return(notifyFlag?nChars:0); // if notifyFlag is not set, return 0, else return number of characters printed to cBuf
|
||||
if(notifyFlag)
|
||||
hapOut << "]}";
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
|
|
@ -1872,7 +1869,7 @@ void SpanCharacteristic::printfAttributes(int flags){
|
|||
hapOut << ",\"format\":\"" << formatCodes[format] << "\"";
|
||||
|
||||
if(customRange && (flags&GET_META)){
|
||||
hapOut << ",\"minValue\":" << uvPrint(minValue).c_str() << "\"maxValue\":" << uvPrint(maxValue).c_str();
|
||||
hapOut << ",\"minValue\":" << uvPrint(minValue).c_str() << ",\"maxValue\":" << uvPrint(maxValue).c_str();
|
||||
|
||||
if(uvGet<float>(stepValue)>0)
|
||||
hapOut << ",\"minStep\":" << uvPrint(stepValue).c_str();
|
||||
|
|
|
|||
|
|
@ -294,7 +294,7 @@ class Span{
|
|||
int sprintfAttributes(SpanBuf *pObj, int nObj, char *cBuf); // prints SpanBuf object into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL
|
||||
int sprintfAttributes(char **ids, int numIDs, int flags, char *cBuf); // prints accessory.characteristic ids into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL
|
||||
void clearNotify(int slotNum); // set ev notification flags for connection 'slotNum' to false across all characteristics
|
||||
int sprintfNotify(SpanBuf *pObj, int nObj, char *cBuf, int conNum); // prints notification JSON into buf based on SpanBuf objects and specified connection number
|
||||
void printfNotify(SpanBuf *pObj, int nObj, int conNum); // writes notification JSON to hapOut stream based on SpanBuf objects and specified connection number
|
||||
|
||||
static boolean invalidUUID(const char *uuid, boolean isCustom){
|
||||
int x=0;
|
||||
|
|
@ -506,7 +506,6 @@ class SpanCharacteristic{
|
|||
UVal newValue; // the updated value requested by PUT /characteristic
|
||||
SpanService *service=NULL; // pointer to Service containing this Characteristic
|
||||
|
||||
|
||||
int sprintfAttributes(char *val, int flags); // prints Characteristic JSON records into buf; return number of characters printed, excluding null terminator
|
||||
void printfAttributes(int flags); // writes Characteristic JSON to hapOut stream
|
||||
StatusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intitial HAP status code (checks to see if characteristic is found, is writable, etc.)
|
||||
|
|
@ -688,7 +687,7 @@ class SpanCharacteristic{
|
|||
sb.characteristic=this; // set characteristic
|
||||
sb.status=StatusCode::OK; // set status
|
||||
char dummy[]="";
|
||||
sb.val=dummy; // set dummy "val" so that sprintfNotify knows to consider this "update"
|
||||
sb.val=dummy; // set dummy "val" so that printfNotify knows to consider this "update"
|
||||
homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector
|
||||
|
||||
if(nvsKey){
|
||||
|
|
@ -775,7 +774,7 @@ class SpanCharacteristic{
|
|||
sb.characteristic=this; // set characteristic
|
||||
sb.status=StatusCode::OK; // set status
|
||||
char dummy[]="";
|
||||
sb.val=dummy; // set dummy "val" so that sprintfNotify knows to consider this "update"
|
||||
sb.val=dummy; // set dummy "val" so that printfNotify knows to consider this "update"
|
||||
homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector
|
||||
|
||||
if(nvsKey){
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ void setup() {
|
|||
|
||||
Serial.begin(115200);
|
||||
|
||||
homeSpan.setLogLevel(0);
|
||||
homeSpan.setLogLevel(2);
|
||||
homeSpan.enableWebLog(200);
|
||||
|
||||
homeSpan.begin(Category::Lighting,"HomeSpan Max");
|
||||
|
|
|
|||
Loading…
Reference in New Issue