Skip to content
Base_DAQ.h 6.83 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed
#pragma once
Valerio Pastore's avatar
Valerio Pastore committed

#include <Base_Monitor.h>
#include <Base_Archiver.h>
#include <Base_Provider.h>
Valerio Pastore's avatar
Valerio Pastore committed
#include <Base_Packet.h>
Valerio Pastore's avatar
Valerio Pastore committed
#include <Base_Configurator.h>
#include <Base_Receiver.h>
Valerio Pastore's avatar
Valerio Pastore committed

// FORWARD DECLARE
namespace inaf::oasbo::DAQ_observers {
class BaseDAQ_Observer;
}

Valerio Pastore's avatar
Valerio Pastore committed
/**
 * @brief The BaseDAQ class represents the base class for a Data Acquisition (DAQ) system.
 * 
 * This class provides a common interface and functionality for a DAQ system. The DAQ system is imagined as a state machine. 
 * This super class defines the states, sets the receiver, archiver, provider, monitor, and packet objects, and
 * manages the observers and configurations. It also provides pure virtual functions for
 * starting, stopping, switching states, delivering packets, and getting the state as a string.
 * 
 * @note This class is meant to be inherited from and should not be instantiated directly.
 */
Valerio Pastore's avatar
Valerio Pastore committed
namespace inaf::oasbo::DAQ {
class BaseDAQ {
Valerio Pastore's avatar
Valerio Pastore committed
public:
	enum Status {
		INIT = -2, STOP = -1, READY = 0, IDLE = 1, RUN = 2
Valerio Pastore's avatar
Valerio Pastore committed

protected:
Valerio Pastore's avatar
Valerio Pastore committed
	Status currentState; /**< The current state of the DAQ system. */
	Status nextState; /**< The next state of the DAQ system. */
	bool changeStateFlag = false; /**< Flag indicating if the state has changed. */
	Receivers::BaseReceiver *receiver = nullptr; /**< Pointer to the receiver object. */
	Archivers::BaseArchiver *archiver = nullptr; /**< Pointer to the archiver object. */
	Providers::BaseProvider *provider = nullptr; /**< Pointer to the provider object. */
	PacketMonitors::BasePacketMonitor *monitor = nullptr; /**< Pointer to the packet monitor object. */
Valerio Pastore's avatar
.  
Valerio Pastore committed
	Packets::BasePacket *packet = nullptr; /**< Pointer to the packet object. */
Valerio Pastore's avatar
Valerio Pastore committed
	std::vector<inaf::oasbo::DAQ_observers::BaseDAQ_Observer*> observers; /**< Vector of observers. */
	std::vector<inaf::oasbo::Configurators::BaseConfigurator*> configurations; /**< Vector of configurations. */
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
public:
Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Sets the receiver object.
	 * 
	 * @param receiver The receiver object to set.
	 */
	void setReceiver(Receivers::BaseReceiver &receiver) {
		this->receiver = &receiver;
Valerio Pastore's avatar
Valerio Pastore committed
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the archiver object.
	 * 
	 * @param archiver The archiver object to set.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void setArchiver(Archivers::BaseArchiver &archiver) {
		this->archiver = &archiver;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the provider object.
	 * 
	 * @param provider The provider object to set.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void setProvider(Providers::BaseProvider &provider) {
		this->provider = &provider;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the packet monitor object.
	 * 
	 * @param monitor The packet monitor object to set.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void setMonitor(PacketMonitors::BasePacketMonitor &monitor) {
		this->monitor = &monitor;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the packet object.
	 * 
	 * @param packet The packet object to set.
	 */
Valerio Pastore's avatar
.  
Valerio Pastore committed
	void setPacket(Packets::BasePacket &packet) {
Valerio Pastore's avatar
Valerio Pastore committed
		this->packet = &packet;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the current state of the DAQ system.
	 * 
	 * @param currentState The current state to set.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void setCurrentState(Status currentState) {
		this->currentState = currentState;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the next state of the DAQ system.
	 * 
	 * @param nextState The next state to set.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void setNextState(Status nextState) {
		this->nextState = nextState;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Sets the flag indicating if the state has changed.
	 * 
	 * @param flag The flag value to set.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void setChangeStateFlag(bool flag) {
		this->changeStateFlag = flag;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Registers an observer to the DAQ system.
	 * 
	 * @param observer The observer to register.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void registerObserver(
			inaf::oasbo::DAQ_observers::BaseDAQ_Observer *observer) {
Valerio Pastore's avatar
Valerio Pastore committed
		observers.push_back(observer);
	}

Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Removes an observer from the DAQ system.
	 * 
	 * @param observer The observer to remove.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	void removeObserver(
			inaf::oasbo::DAQ_observers::BaseDAQ_Observer *observer) {
Valerio Pastore's avatar
Valerio Pastore committed
		observers.erase(
				std::remove(observers.begin(), observers.end(), observer),
				observers.end());
	}
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Gets a pointer to the receiver object.
	 * 
	 * @return Pointer to the receiver object.
	 */
	Receivers::BaseReceiver* getReceiverPtr() {
		return this->receiver;
Valerio Pastore's avatar
Valerio Pastore committed
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets a pointer to the archiver object.
	 * 
	 * @return Pointer to the archiver object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	Archivers::BaseArchiver* getArchiverPtr() {
		return this->archiver;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets a pointer to the provider object.
	 * 
	 * @return Pointer to the provider object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	Providers::BaseProvider* getProviderPtr() {
		return this->provider;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets a pointer to the packet monitor object.
	 * 
	 * @return Pointer to the packet monitor object.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	PacketMonitors::BasePacketMonitor* getMonitorPtr() {
		return this->monitor;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets a pointer to the packet object.
	 * 
	 * @return Pointer to the packet object.
	 */
Valerio Pastore's avatar
.  
Valerio Pastore committed
	Packets::BasePacket* getPacketPrt() {
Valerio Pastore's avatar
Valerio Pastore committed
		return this->packet;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets the current state of the DAQ system.
	 * 
	 * @return The current state of the DAQ system.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	Status getCurrentState() {
		return this->currentState;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets the next state of the DAQ system.
	 * 
	 * @return The next state of the DAQ system.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	Status getNextState() {
		return this->nextState;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Gets the flag indicating if the state has changed.
	 * 
	 * @return The flag indicating if the state has changed.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	int getChangeStateFlag() {
		return this->changeStateFlag;
	}
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Starts the DAQ system.
	 * 
	 * @note This is a pure virtual function and must be implemented by derived classes.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	virtual void start() = 0;
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Stops the DAQ system.
	 * 
	 * @note This is a pure virtual function and must be implemented by derived classes.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	virtual void stop() = 0;
Valerio Pastore's avatar
Valerio Pastore committed

	/**
	 * @brief Switches the state of the DAQ system.
	 * 
	 * @param state The state to switch to.
	 * 
	 * @note This is a pure virtual function and must be implemented by derived classes.
	 */
	virtual void switchState(const Status state) = 0;

	/**
	 * @brief Delivers a packet after received. It should probably call the provider to deliver the packet.
	 * 
	 * @return The result of the packet delivery.
	 * 
	 * @note This is a pure virtual function and must be implemented by derived classes.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	virtual int deliverPacket() = 0;
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	/**
	 * @brief Gets the string representation of a state.
	 * 
	 * @param state The state to get the string representation of.
	 * @return The string representation of the state.
	 * 
	 * @note This is a pure virtual function and must be implemented by derived classes.
	 */
	virtual std::string getStateStr(const Status state) = 0;

	/**
	 * @brief Destructor for the BaseDAQ class.
	 * 
	 * Deletes the receiver, provider, archiver, monitor, packet, and observers.
	 */
Valerio Pastore's avatar
Valerio Pastore committed
	virtual ~BaseDAQ() {
		delete receiver;
Valerio Pastore's avatar
Valerio Pastore committed
		delete provider;
		delete archiver;
		delete monitor;
Valerio Pastore's avatar
Valerio Pastore committed
		delete packet;
Valerio Pastore's avatar
Valerio Pastore committed
		for (auto ob : observers) {
			observers.erase(std::remove(observers.begin(), observers.end(), ob),
					observers.end());
		}
Valerio Pastore's avatar
Valerio Pastore committed
	}
};