# An IoT-Based Framework for Smart Water Supply Systems Management
## 5 Material and Methods
### Middleware IoT Layer  
This provides services for storage, analysis, processing, and orchestrating the communication between the Physical and Application layers and among the sensors and actuators in the IoT infrastructure. The implementation uses the open-source FIWARE platform, which offers a rich set of APIs (The algorithms and APIs used to create the Framework is available at [[https://github.com/telefonicaid](https://github.com/telefonicaid)]([https://github.com/telefonicaid](https://github.com/telefonicaid))) [22] for the development of smart applications [23]. This layer has three main components:  
* Orion Context Broker: it receives data from sensors in the IoT infrastructure; the CEP engine processes the data; as soon as the CEP engine recognizes a pattern of interest, it notifies the Orion, which updates the monitoring interface in the Application layer; Orion uses Cygnus to keep historical data persistent.
* Perseo: whenever an IoT agent produces an event, the Perseo-CEP processes the event, applying predefined rules to identify a pattern of interest. As soon as the Perseo-CEP identifies a patter, it notifies the system operator sitting in the Application layer. For instance, every 2 min the CEP rule in Figure 7 checks if the average pressure in 12 s is greater than the critical limit. It recognizes the pattern when the condition is true. Then, it emits an alert message, which is placed in the message queue to be consumed by registered consumers in the Application layer.  
Figure 9: REFlex Water architecture.  
* Cygnus: is responsible for maintaining the historical data managed by the Orion Context Broker over time. Historical data maintained by Cygnus are useful for analyzing trends, for instance.
* STH-Comet: retrieves historical data from the system and updates trend graphs the monitoring interface in the Application layer.
* MongoDB: this component records all data collected in the Physical layer. It also controls the access to this information, offering homogeneous access to data.  
The Orion Context Broker's Publish/Subscribe allows the connection between the FIWARE components. Each FIWARE component signs up the Orion Context Broker to receive a notification whenever a sensor sends some data. Figure 10 shows two JSON files with the signature of two components: Cygnus (Figure 9(a)) and Perseo (Figure 9(b)). Table 2 details each field in the JSON file. The component must execute the command POST /v2/subscriptions to save the signatures to the FIWARE database (MongoDB [24]). In addition to the signatures, the MongoDB component records the CEP rules and the data collected by the sensors. The STH-Comet accesses the MongoDB to generate the information displayed by the monitoring and control graphics.  
Figure 11 shows a JSON code with a rule created in Perseo. Table 3 details the code.  
\begin{table}
\begin{tabular}{} \end{tabular}
\end{table}
Table 2: Description of the subscription fields.  
\begin{table}
\begin{tabular}{} \end{tabular}
\end{table}
Table 3: Details of the Perseo rule.  
Figure 10: Example subscription: (**a**) Cygnus; (**b**) Perseo.  
Table 4 shows the operations available at REFlex Water, which adopts the HTTP protocol through the following address: http://\(<\)host-address\(>\)/operation.