Branch data Line data Source code
1 : : #ifndef RESTAPIPROVIDER_H
2 : : #define RESTAPIPROVIDER_H
3 : :
4 : : #include <string>
5 : : #include <atomic>
6 : : #include <mutex>
7 : : #include <thread>
8 : : #include <optional>
9 : :
10 : : class RestApiProvider {
11 : : public:
12 : : static RestApiProvider& Get();
13 : :
14 : : // Trigger an asynchronous request for the steering range
15 : : void RequestSteeringRange(int port);
16 : :
17 : : // Get the latest successfully retrieved range in degrees
18 : : // Returns 0.0 if no value has been retrieved yet
19 : : float GetFallbackRangeDeg() const;
20 : :
21 : : // Is a request currently in flight?
22 : : bool IsRequesting() const;
23 : :
24 : : // Has a manufacturer been successfully retrieved?
25 : : bool HasManufacturer() const { return m_hasManufacturer; }
26 : :
27 : : // Trigger an asynchronous request for the car manufacturer
28 : : void RequestManufacturer(int port, const std::string& vehicleName);
29 : :
30 : : // Get the latest successfully retrieved manufacturer
31 : : std::string GetManufacturer() const;
32 : :
33 : : // Reset manufacturer when vehicle changes
34 : 115 : void ResetManufacturer() {
35 [ + - ]: 115 : std::lock_guard<std::mutex> lock(m_manufacturerMutex);
36 [ + - ]: 115 : m_manufacturer = "Unknown";
37 : 115 : m_hasManufacturer = false;
38 : 115 : }
39 : :
40 : 115 : void ResetSteeringRange() {
41 : 115 : m_fallbackRangeDeg.store(0.0f);
42 : 115 : }
43 : :
44 : : private:
45 [ + - ]: 3 : RestApiProvider() = default;
46 : : ~RestApiProvider();
47 : :
48 : : void PerformRequest(int port);
49 : : void PerformManufacturerRequest(int port, std::string vehicleName);
50 : : float ParseSteeringLock(const std::string& json);
51 : : std::string ParseManufacturer(const std::string& json, const std::string& vehicleName);
52 : :
53 : : friend class RestApiProviderTestAccess;
54 : :
55 : : std::atomic<bool> m_isRequesting{false};
56 : : std::atomic<float> m_fallbackRangeDeg{0.0f};
57 : :
58 : : mutable std::mutex m_manufacturerMutex;
59 : : std::string m_manufacturer = "Unknown";
60 : : bool m_hasManufacturer = false;
61 : :
62 : : mutable std::mutex m_threadMutex;
63 : : std::thread m_requestThread;
64 : : };
65 : :
66 : : #endif // RESTAPIPROVIDER_H
|