Program Listing for File EventsManager.hpp

Return to documentation for file (include/depthai/utility/EventsManager.hpp)

#pragma once

#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>

#include "depthai/pipeline/datatype/ADatatype.hpp"
#include "depthai/pipeline/datatype/EncodedFrame.hpp"
#include "depthai/pipeline/datatype/ImgFrame.hpp"
#include "depthai/pipeline/datatype/NNData.hpp"

namespace dai {
namespace proto {
namespace event {
class Event;
}  // namespace event
}  // namespace proto
namespace utility {
enum class EventDataType { DATA, FILE_URL, IMG_FRAME, ENCODED_FRAME, NN_DATA };
class EventData {
   public:
    EventData(const std::string& data, const std::string& fileName, const std::string& mimeType);
    explicit EventData(std::string fileUrl);
    explicit EventData(const std::shared_ptr<ImgFrame>& imgFrame, std::string fileName);
    explicit EventData(const std::shared_ptr<EncodedFrame>& encodedFrame, std::string fileName);
    explicit EventData(const std::shared_ptr<NNData>& nnData, std::string fileName);
    bool toFile(const std::string& path);

   private:
    std::string fileName;
    std::string mimeType;
    std::string data;
    EventDataType type;
    friend class EventsManager;
};
class EventsManager {
   public:
    explicit EventsManager(std::string url = "https://events-ingest.cloud.luxonis.com", bool uploadCachedOnStart = false, float publishInterval = 10.0);
    ~EventsManager();

    bool sendEvent(const std::string& name,
                   const std::shared_ptr<ImgFrame>& imgFrame = nullptr,
                   std::vector<std::shared_ptr<EventData>> data = {},
                   const std::vector<std::string>& tags = {},
                   const std::unordered_map<std::string, std::string>& extraData = {},
                   const std::string& deviceSerialNo = "");
    bool sendSnap(const std::string& name,
                  const std::shared_ptr<ImgFrame>& imgFrame = nullptr,
                  std::vector<std::shared_ptr<EventData>> data = {},
                  const std::vector<std::string>& tags = {},
                  const std::unordered_map<std::string, std::string>& extraData = {},
                  const std::string& deviceSerialNo = "");

    void setDeviceSerialNumber(const std::string& deviceSerialNumber);
    void setUrl(const std::string& url);
    void setSourceAppId(const std::string& sourceAppId);
    void setSourceAppIdentifier(const std::string& sourceAppIdentifier);
    void setToken(const std::string& token);
    void setQueueSize(uint64_t queuSize);
    void setLogResponse(bool logResponse);
    void setVerifySsl(bool verifySsl);

    bool checkConnection();

    void uploadCachedData();

    void setCacheDir(const std::string& cacheDir);

    void setCacheIfCannotSend(bool cacheIfCannotSend);

   private:
    struct EventMessage {
        std::shared_ptr<proto::event::Event> event;
        std::vector<std::shared_ptr<EventData>> data;
        std::string cachePath;
    };
    static std::string createUUID();
    void sendEventBuffer();
    void sendFile(const std::shared_ptr<EventData>& file, const std::string& url);
    void cacheEvents();
    bool checkForCachedData();
    std::string token;
    std::string deviceSerialNumber;
    std::string url;
    std::string sourceAppId;
    std::string sourceAppIdentifier;
    uint64_t queueSize;
    std::unique_ptr<std::thread> eventBufferThread;
    std::vector<std::shared_ptr<EventMessage>> eventBuffer;
    std::mutex eventBufferMutex;
    float publishInterval;
    bool logResponse;
    bool verifySsl;
    std::string cacheDir;
    bool cacheIfCannotSend;
    std::atomic<bool> stopEventBuffer;
    std::condition_variable eventBufferCondition;
    std::mutex eventBufferConditionMutex;
};
}  // namespace utility
}  // namespace dai