use threads in wifi handler sim to simulate a bit of delay in the notifications like on hardware.

This commit is contained in:
MatthewColvin 2023-10-18 19:09:09 -05:00
parent 45160ceac5
commit 29f58d406f
2 changed files with 38 additions and 17 deletions

View file

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

View file

@ -1,24 +1,25 @@
#pragma once
#include "Notification.hpp"
#include "wifiHandlerInterface.h"
#include <atomic>
#include <memory>
#include <thread>
class wifiHandlerSim : public wifiHandlerInterface {
public:
wifiHandlerSim();
/**
* @brief Connect to the wifi using the provided credetials
*/
void begin() override;
void scan() override;
void connect(std::string ssid, std::string password) override;
/**
* @brief function to trigger asynchronous scan for wifi networks
*/
void scan();
bool isAvailable();
void begin();
wifiStatus GetStatus() override { return mCurrentStatus; };
private:
wifiStatus status = wifiStatus(true, "172.0.0.1", "FakeNet");
// Since they have not started consider them "done"
std::atomic<bool> mIsScanThreadDone = true;
std::atomic<bool> mIsStatusThreadDone = true;
std::thread mFakeScanThread;
std::thread mFakeStatusThread;
wifiStatus mCurrentStatus = wifiStatus(true, "172.0.0.1", "FakeNet");
};