update handler sim to make it a bit simpler. Thought the atomics fixed a bug but I don't think it did...

This commit is contained in:
MatthewColvin 2023-10-18 20:07:45 -05:00
parent 29f58d406f
commit c7a8987d3b
2 changed files with 14 additions and 26 deletions

View file

@ -7,19 +7,15 @@ wifiHandlerSim::wifiHandlerSim() {}
void wifiHandlerSim::begin() {} void wifiHandlerSim::begin() {}
void wifiHandlerSim::connect(std::string ssid, std::string password) { void wifiHandlerSim::connect(std::string ssid, std::string password) {
while (!mIsStatusThreadDone.load()) {
}
if (mFakeStatusThread.joinable()) { if (mFakeStatusThread.joinable()) {
mFakeStatusThread.join(); mFakeStatusThread.join();
}
mCurrentStatus.ssid = ssid; mCurrentStatus.ssid = ssid;
mCurrentStatus.isConnected = true; mCurrentStatus.isConnected = true;
mIsStatusThreadDone = false;
mFakeStatusThread = std::thread([this] { mFakeStatusThread = std::thread([this] {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
mStatusUpdate->notify(mCurrentStatus); mStatusUpdate->notify(mCurrentStatus);
mIsStatusThreadDone = true;
}); });
}
} }
static const WifiInfo wifis[] = { static const WifiInfo wifis[] = {
@ -27,17 +23,13 @@ static const WifiInfo wifis[] = {
WifiInfo("Low Signal Wifi", -65), WifiInfo("No Signal Wifi", -90)}; WifiInfo("Low Signal Wifi", -65), WifiInfo("No Signal Wifi", -90)};
void wifiHandlerSim::scan() { void wifiHandlerSim::scan() {
while (!mIsScanThreadDone.load()) {
}
if (mFakeScanThread.joinable()) { if (mFakeScanThread.joinable()) {
mFakeScanThread.join(); mFakeScanThread.join();
}
mIsScanThreadDone = false;
mFakeScanThread = std::thread([this] { mFakeScanThread = std::thread([this] {
std::vector<WifiInfo> info = std::vector<WifiInfo> info =
std::vector(std::begin(wifis), std::end(wifis)); std::vector(std::begin(wifis), std::end(wifis));
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
mScanNotification->notify(info); mScanNotification->notify(info);
mIsScanThreadDone = true;
}); });
}
} }

View file

@ -15,11 +15,7 @@ public:
wifiStatus GetStatus() override { return mCurrentStatus; }; wifiStatus GetStatus() override { return mCurrentStatus; };
private: private:
// Since they have not started consider them "done" std::thread mFakeScanThread = std::thread([] {});
std::atomic<bool> mIsScanThreadDone = true; std::thread mFakeStatusThread = std::thread([] {});
std::atomic<bool> mIsStatusThreadDone = true;
std::thread mFakeScanThread;
std::thread mFakeStatusThread;
wifiStatus mCurrentStatus = wifiStatus(true, "172.0.0.1", "FakeNet"); wifiStatus mCurrentStatus = wifiStatus(true, "172.0.0.1", "FakeNet");
}; };