# Enhancing Sustainable IoT Systems Through a Goal-Oriented Requirements Analysis Framework
## 4 Goal-Oriented Requirements Analysis Approach for IoT Software Systems
### Application Example: IoT System for Monitoring Air Quality
#### 4.2.5 QVT for Model-to-Model Transformations
Finally, scalability is achieved by structuring the Python code to support seamless cloud integration and the addition of new sensors with minimal modifications, ensuring adaptability to evolving IoT requirements. The Acceleo template is presented next. The proposed transformation yields Python class implementations corresponding to each conceptual element of the model: actors (including User, MobileApp, and IoTSystem classes); goals (represented as class attributes such as air quality monitoring parameters); tasks and services (implemented as methods like get_sensor_data()); and resources (encapsulated as sensor classes including HumiditySensor, CO2 Sensor, and other relevant sensor types). This object-oriented map ping preserves the structural and behavioral semantics of the original model while providing executable implementations that maintain the intended system functionality (see Listing 7). [comment encoding = UTF-8 /]

[module generate('[[http://gora.org/pythonmodel](http://gora.org/pythonmodel)]([http://gora.org/pythonmodel](http://gora.org/pythonmodel))')/]

[template public generateSystem(p : PythonSystem)]

import paho.mqtt.client as mqtt

import time, psutil, json,datetime

from influxdb import InfluxDBClient

client = mqtt.Client()

client.connect(\"localhost\", 1883)

influx = InfluxDBClient('localhost', 8086, '', ', ', '

iot_metrics')

[for (n : PythonNode | p.nodes)]

[if (n.nodeType ='sensor')]

def [n.handler]() :

val = simulate_sensor()

ts = time.time()

client.publish(\"[n.topic]\", json.dumps({\"value\": val, \"ts\":

ts}))

influx.write_points([{

\"measurement\": \"sensor_data\",

\"fields\": {\"value\": val, \"ts\": ts}

])

[/if]

[if (n.nodeType = 'actuator')]

def [n.handler](msg) :

ts_received = time.time()

# Process actuation

influx.write_points([{

\"measurement\": \"latency\",

\"fields\": {\"latency\": ts_received - json.loads(msg. payload)[\"ts\"]}

])

[/if]

[if (n.nodeType = 'logger')]

def log_metrics():

cpu = psutil.cpu_percent()

mem = psutil.virtual_memory().percent

influx.write_points([{

\"measurement\": \"cpu_metrics\",

\"fields\": {\"cpu\": cpu, \"memory\": mem}

[/if]

[/for]

# Start application loop or bind callbacks as needed

The Python source (except from the Acceleo template output) code for the IoT system is detailed below (see Listing 8). ```
import paho.mqtt.client asmqtt import time, psutil, json,datetime from influxdb import InfluxDBClient client=mqtt.Client() client.connect(\"localhost\", 1883) influx=InfluxDBClient('localhost', 8086, '', '', '', '' \(\hookrightarrow\) iot_metrics') defread_air_sensor(): val=simulate_sensor() ts=time.time() client.publish(\"sensors/air\", json.dumps((\"value\": val, \"ts\": \(\hookrightarrow\) ts))) influx.write_points([{ \"measurement\": \"sensor_data\", \"fields\": {\"value\": val, \"ts\": ts} ]}) deftrigger_purifier(msg): ts_received=time.time() influx.write_points([{  \"measurement\": \"latency\",  \"fields\": {\"latency\": ts_received-json.loads(msg. \(\hookrightarrow\) payload)[\"ts\"]} ]} deflog_metrics(): cpu=psutil.cpu_percent() mem=psutil.virtual_memory().percent influx.write_points([{  \"measurement\": \"cpu_metrics\",  \"fields\": {\"cpu\": cpu, \"memory\": mem} }])
```

The Python source code produced through the GORA framework is the result of the MDD pipeline guided by goal-oriented requirements analysis. The code is designed to operate within an IoT system architecture, where it processes sensor data, evaluates environmental conditions, and triggers actuation responses based on declarative goals captured in the requirements model. The generated code exhibits an event-driven modular structure that includes clearly defined components for sensor data acquisition, condition evaluation, actuation logic, and log. MQTT is used as the communication middleware, enabling low-latency asynchronous message exchange between simulated sensors, broker, and actuator logic. The program is initialized by subscribing to predefined MQTT topics corresponding to environmental sensor readings.