Created
March 17, 2018 14:28
-
-
Save gbl08ma/19aa524f1fb5bfe2ce1fdcc2559db364 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Helper class so we can send AP scan results in an easier way | |
class JsonObjectAsyncStream : public JsonObjectStream | |
{ | |
public: | |
virtual ~JsonObjectAsyncStream(); | |
virtual uint16_t readMemoryBlock(char* data, int bufSize); | |
virtual bool isFinished(); | |
void setCanSend(); | |
private: | |
bool can_send = false; | |
}; | |
uint16_t JsonObjectAsyncStream::readMemoryBlock(char* data, int bufSize) { | |
Debug.println("read mem block"); | |
if(can_send) return JsonObjectStream::readMemoryBlock(data, bufSize); | |
else return MemoryDataStream::readMemoryBlock(data, bufSize); | |
} | |
bool JsonObjectAsyncStream::isFinished() { | |
if(can_send) return JsonObjectStream::isFinished(); | |
else return false; | |
} | |
void JsonObjectAsyncStream::setCanSend() { | |
can_send = true; | |
} | |
JsonObjectAsyncStream::~JsonObjectAsyncStream() { | |
if (can_send) Debug.println("async stream destroyed after can_send"); | |
else Debug.println("async stream destroyed before can_send"); | |
} | |
// HOW IT IS USED | |
// scanreq_stream is a field of the WebUI class | |
void WebUI::onScanAP(HttpRequest &request, HttpResponse &response) { | |
if(scanreq_stream == nullptr) { | |
scanreq_stream = new JsonObjectAsyncStream(); | |
WifiStation.startScan(ScanCompletedDelegate(&WebUI::onAPscanned, this)); | |
} | |
response.sendDataStream(scanreq_stream, MIME_JSON); | |
} | |
void WebUI::onAPscanned(bool succeeded, BssList list) { | |
// serialize the network list | |
JsonObject& json = scanreq_stream->getRoot(); | |
JsonArray& array = json.createNestedArray("networks"); | |
for (int i = 0; i < list.count(); i++) | |
{ | |
JsonObject& ap = array.createNestedObject(); | |
ap["ssid"] = list[i].ssid; | |
ap["channel"] = list[i].channel; | |
ap["rssi"] = list[i].rssi; | |
ap["auth"] = list[i].getAuthorizationMethodName(); | |
ap["hidden"] = list[i].hidden; | |
} | |
scanreq_stream->setCanSend(); | |
scanreq_stream = nullptr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment