SpaIot Library
SpaIot::SpaClient

SpaClient class. More...

Public Member Functions

 SpaClient ()
 Construct a new default SpaClient object. More...
 
virtual ~SpaClient ()
 Destructor.
 
 SpaClient (std::initializer_list< Event::Type > subscribedEvents)
 Construct a new SpaClient object with the specified subscribed events. More...
 
 SpaClient (const std::set< Event::Type > &subscribedEvents)
 Construct a new SpaClient object with the specified subscribed events. More...
 
 SpaClient (const SpaClient &other)
 Copy Constructor.
 
 SpaClient (SpaClient &&other)
 Move Constructor after the move, other is null, other.clear() must be called if it is to be used again.
 
SpaClientoperator= (const SpaClient &other)
 Sets the SpaClient object to be equal to other.
 
SpaClientoperator= (SpaClient &&other)
 Move assignment after the move, other is null, other.clear() must be called if it is to be used again.
 
virtual void clear ()
 Clears the SpaClient object After the call to this function, the SpaClient object is the same as one that has been default constructed.
 
bool isNull () const
 Returns true if the object is null (i.e. it has not been initialized, d_ptr is null)
 
virtual bool isEmpty () const
 Checks if all attributes are set to their default values (same as the default constructor)
 
virtual bool operator== (const SpaClient &other) const
 
virtual bool operator!= (const SpaClient &other) const
 
void subscribe (std::initializer_list< Event::Type > subscribedEvents)
 Add subscribed events. More...
 
void subscribe (std::set< Event::Type > subscribedEvents)
 Add subscribed events. More...
 
void subscribe (Event::Type event)
 Add subscribed event. More...
 
const std::set< Event::Type > & subscribedEvents () const
 Get the subscribed events. More...
 
bool isSubscribed (const Event::Type &event) const
 Check if the client is subscribed to the event. More...
 
bool write (const Event &event)
 Write an event to the client. More...
 
Event read ()
 Reads the next event from the client. More...
 
Event read (Event::Type type)
 Reads the next event of the specified type from the client. More...
 
int available () const
 Returns the number of events to be read.
 
bool begin ()
 Start the client. More...
 
virtual bool isOpen () const
 Check if the client is open. More...
 
virtual void end ()
 Stop the client. More...
 
virtual bool handle ()
 Process the client. More...
 
virtual const String & className () const
 Returns the class name This allows to know the class name of the object at runtime when only a pointer to the base class is available It should be note that typeid().name() is not available on ESP32/ESP8266 because RTTI is not enabled.
 

Protected Member Functions

SpaClient Protected API

Used by client-derived classes to communicate the outside world to the spa

 SpaClient (const char *className)
 Construct a new SpaClient object with the specified class name.
 
void pushToSpa (const Event &event)
 This client push an event to the spa. More...
 
bool pullFromSpa (Event &event)
 This client pull an event from the spa. More...
 
virtual bool pollSpaEvents ()
 This client poll events from the spa. More...
 
uint16_t spaValue (Event::Type type) const
 Get a spa value from the internal variables of the client object. More...
 

Detailed Description

SpaClient class.

This class is used to interface the spa with the external world (MQTT, HTTP, Alexa, ...). It's a base class that must be derived to implement the protocol with an external library (PubSubClient, SinricPro...).

  • When an event occurs at the spa level, it is sent to all connected clients using write(), this event is stored in a FIFO queue,
  • the client retrieves the event using pullFromSpa() and transmits it outside the spa (MQTT, HTTP, Alexa, ...).
  • When the user sends a command from outside the spa (MQTT, HTTP, Alexa, ...), the client transmits the command to the spa using pushToSpa(), this event is stored in a FIFO queue,
  • the spa retrieves the event using read() and executes it.

Here is a schematic representation of the communication between the spa and the clients:

+-----------+               FiFo                     +-----------+
|           | write() --> OOOOOOOO --> pullFromSpa() |           |
| SpaServer |                                        | Protected | <---> External world (MQTT, HTTP, Alexa, ...)
|   (SPA)   | read()  <-- OOOOOOOO <---- pushToSpa() |    API    |       implements the protocol with an external library
+-----------+               FiFo                     +-----------+
            [ ----------------   SpaClient   ----------------   ] [ --- External library (PubSubClient, SinricPro...) ---]

The communication between server and clients is asynchronous, clients can be connected or disconnected at any time. When the spa receives a command from a client, it executes it, then if the command has modified the state of the spa, it sends an event to all connected clients.

Warning
Thus a client must not block its process to wait for an event from the spa or the system will be blocked.

Constructor & Destructor Documentation

◆ SpaClient() [1/3]

SpaIot::SpaClient::SpaClient ( )

Construct a new default SpaClient object.

The subscribed events list is empty, all events may be sent to the client or the spa

◆ SpaClient() [2/3]

SpaIot::SpaClient::SpaClient ( std::initializer_list< Event::Type subscribedEvents)

Construct a new SpaClient object with the specified subscribed events.

Parameters
subscribedEventsThe events subscribed by the client, only the events in the list will be sent to the client or the spa

◆ SpaClient() [3/3]

SpaIot::SpaClient::SpaClient ( const std::set< Event::Type > &  subscribedEvents)

Construct a new SpaClient object with the specified subscribed events.

Parameters
subscribedEventsThe events subscribed by the client, only the events in the list will be sent to the client or the spa

Member Function Documentation

◆ begin()

bool SpaIot::SpaClient::begin ( )

Start the client.

This method must be called in the setup() function before begin(). It will be call by SpaServer::begin() to start the client services. The default implementation sets isOpen() to true and returns true. It must be reimplemented in derived classes to start the client services, and called by the derived class begin() method.

Returns
true if the client has been started, false otherwise

◆ end()

virtual void SpaIot::SpaClient::end ( )
virtual

Stop the client.

This method closes the client and stops the client services. It may be reimplemented in derived classes to stop the client services, and called by the derived class end() method. The default implementation sets isOpen() to false.

◆ handle()

virtual bool SpaIot::SpaClient::handle ( )
virtual

Process the client.

This method must be called periodically in the main loop. It will be call by SpaServer::handle() to process the client tasks. The default implementation call pollSpaEvents() and returns true if events have been polled. It should be reimplemented in derived classes to process the client tasks.

Returns
true if events have been processed from spa, false otherwise

◆ isOpen()

virtual bool SpaIot::SpaClient::isOpen ( ) const
virtual

Check if the client is open.

Returns
true if begin() has been called and the client is open, false otherwise

◆ isSubscribed()

bool SpaIot::SpaClient::isSubscribed ( const Event::Type event) const

Check if the client is subscribed to the event.

Parameters
eventThe event
Returns
true if the client is subscribed to the event, false otherwise

◆ operator!=()

virtual bool SpaIot::SpaClient::operator!= ( const SpaClient other) const
virtual

The function returns true if the two objects are not equal

Parameters
otherthe other SpaClient object to compare to
Returns
The return value is a boolean value. It is true if the two objects are not equal, and false if they are equal.

◆ operator==()

virtual bool SpaIot::SpaClient::operator== ( const SpaClient other) const
virtual

The function returns true if the two objects are equal

Parameters
otherthe other SpaClient object to compare to
Returns
The return value is a boolean value. It is true if the two objects are equal, and false if they are not equal.

◆ pollSpaEvents()

virtual bool SpaIot::SpaClient::pollSpaEvents ( )
protectedvirtual

This client poll events from the spa.

Poll events from the spa using pullFromSpa() and update the internal variables of the client object. These variables are accessible by the spaValue() method.

Returns
true if events have been polled, false otherwise

◆ pullFromSpa()

bool SpaIot::SpaClient::pullFromSpa ( Event event)
protected

This client pull an event from the spa.

Parameters
eventThe event to be filled
Returns
true if the event has been filled, false otherwise

◆ pushToSpa()

void SpaIot::SpaClient::pushToSpa ( const Event event)
protected

This client push an event to the spa.

Parameters
eventThe event to be pushed

◆ read() [1/2]

Event SpaIot::SpaClient::read ( )

Reads the next event from the client.

Returns
event The event, type is Event::NoEvent if the queue is empty

◆ read() [2/2]

Event SpaIot::SpaClient::read ( Event::Type  type)

Reads the next event of the specified type from the client.

Parameters
typeThe event type that is expected
Returns
Event The event, type is Event::NoEvent if the queue is empty or the event type is not the expected one

◆ spaValue()

uint16_t SpaIot::SpaClient::spaValue ( Event::Type  type) const
protected

Get a spa value from the internal variables of the client object.

These variables are updated by the pollSpaEvents() method.

Parameters
typeThe event type
Returns
uint16_t The spa value, UnsetValue16 or UnsetValue8 (for boolean) if no value is available

◆ subscribe() [1/3]

void SpaIot::SpaClient::subscribe ( Event::Type  event)

Add subscribed event.

Parameters
eventThe event subscribed by the client

◆ subscribe() [2/3]

void SpaIot::SpaClient::subscribe ( std::initializer_list< Event::Type subscribedEvents)

Add subscribed events.

Parameters
subscribedEventsThe events subscribed by the client

◆ subscribe() [3/3]

void SpaIot::SpaClient::subscribe ( std::set< Event::Type subscribedEvents)

Add subscribed events.

Parameters
subscribedEventsThe events subscribed by the client

◆ subscribedEvents()

const std::set<Event::Type>& SpaIot::SpaClient::subscribedEvents ( ) const

Get the subscribed events.

Returns
subscribedEvents The events subscribed by the client

◆ write()

bool SpaIot::SpaClient::write ( const Event event)

Write an event to the client.

Parameters
eventThe event to be sent