# How to develop an ETSI MEC application to exploit both ETSI MEC Sandbox and oneM2M platforms, based on ETSI GS MEC 046
This tutorial introduces the step by step procedure to create a basic MEC application following ETSI MEC standards and oneM2M standards to manage devices/sensors/actuators... (MEC 046).
This tutorial introduces the step by step procedure to create a basic MEC application following ETSI MEC standards and oneM2M standards to manage devices/sensors/actuators... (MEC 033, MEC 046).
It uses two platforms:
1. The ETSI MEC Sandbox and,
2. a oneM2M platform.
In this tutorial, we will use the [ACME oneM2M platform](https://acmecse.net/) with some changes to register to the ETSI MEC platform.
<divclass="alert alert-block alert-danger">
<b>Note:</b> These source code examples are simplified and ignore return codes and error checks to a large extent. We do this to highlight how to use the MEC Sandbox API and the different MEC satndards and reduce unrelated code.
A real-world application will of course properly check every return value and exit correctly at the first serious error.
</div>
%% Cell type:markdown id: tags:
Here is the list of the questions and topics to clarify from the ETSI MEC perspective to support an efficient support of oneM2M features
<divclass="alert alert-block alert-danger">
<b>Note:</b> FIXMEs
- How to register MEC/oneM2M platforms together (currently the oneM2M platform is registering to the ETSI MEC sandbox platform)
- How to setup the testbed for the tutorial
- How to link oneM2M resources (AE, container, flex-container...) and SAREF onthologies with existing MEC standards
-
</div>
%% Cell type:markdown id: tags:
## What is a ETSI MEC application
For more details on how to develop an ETSI MEC application, please refer to [The Wiki MEC web site](https://www.etsi.org/technologies/multi-access-edge-computing) and the [ETSI MEC application tutorial](http://mec-platform2.etsi.org:9999/notebooks/work/notebook/MEC%20application.ipynb).
%% Cell type:markdown id: tags:
## The basics to develop an ETSI MEC application
All the code below are helpers functions required to start the development our ETSI MEC application.
For more details, please refer to the[ETSI MEC application tutorial](http://mec-platform2.etsi.org:9999/notebooks/work/notebook/MEC%20application.ipynb).
<divclass="alert alert-warning"role="alert">
<b>Note:</b> The sub-paragraph 'Putting everything together' is a specific paragraph where all the newly features introduced in the main paragraph are put together to create an executable block of code. It is possible to skip this block of code by removing the comment character (#) on first line of this block of code.
</div>
%% Cell type:code id: tags:
``` python
importos
os.chdir(os.path.join(os.getcwd(),'../mecapp'))
print(os.getcwd())
from__future__importdivision# Import floating-point division (1/4=0.25) instead of Euclidian division (1/4=0)
logger.error('Failed to delete the application instance identifier')
else:
# Wait for the MEC services are terminated
time.sleep(STABLE_TIME_OUT)
# Deactivate a network scenario based on a list of criterias (hard coded!!!)
ifdeactivate_network_scenario(sandbox_name)==-1:
logger.error('Failed to deactivate network scenario')
else:
# Wait for the MEC services are terminated
time.sleep(2*STABLE_TIME_OUT)
# Logout
process_logout(sandbox_name)
# End of function mec_app_termination
```
%% Cell type:markdown id: tags:
# Create our first ETSI MEC application: the skeleton of our MEC application
The purpose of this section is to create a first skeloton of our final MEC application by retrieving the oneM2M platform identifier following the procedure described in MEC 033.
## Accessing the ETSI MEC Sandbox
The cell code above include a section where the ETSI MEC Sandbox setting are already filled to make this tutorial running well (see constants MEC_SANDBOX_URL and MEC_SANDBOX_API_URL).
## Hosting the oneM2M platform
All the details about the oneM2M platform are provided during the registration to the ETSI MEC Sanbox registration process.
The tricky point is that the oneM2M platform shall address the same ETSI MEC Sandbox configured in the cell code above (see constants MEC_SANDBOX_URL and MEC_SANDBOX_API_URL).
## Wait for the oneM2M platform to register to the ETSI MEC platform
Here, we just going to poll every 5 seconds to verify if the oneM2M platform is registered to the ETSI MEC platform.
After a minutes, this function returns with than error.
logger.error('Exception when calling call_api: %s\n'%e)
returnNone
# End of function om2m_discover_devices
```
%% Cell type:markdown id: tags:
## Preconditions
To be efficient, some sensors and devices shall be registered on the oneM2M platform.
Here is a little project simulating a basic smart garden based on ESP32. To use it, you do not need any material. This project uses the well known Wokwi playground to simulate the ESP32. It provides you a free license to play with it and this is enough for our needs.
You are just required to use Visual Code Studio or Cursor IDE with PlatformIO extention installed. Next, follow the instructions from the README file to build, configure and execute this project.
The references below will point you to the project code
- The [ESP32 project](https://gitlab.com/garcia.yann/lolin32esp32project-01.git)
- The [wokwi web site](https://wokwi.com/)
Assuming this ESP32 project is deployed, the preconditions() function below is just (for 30 seconds) waiting for you to execute it for 30 seconds.
%% Cell type:code id: tags:
``` python
defpreconditions():
"""
This function performs a discovery of all available sensors and devices based on type
"""
globalOM2M_REGISTRATION
logger.debug('=======================> Please run script to register sensors & devices')
time.sleep(OM2M_REGISTRATION)# Wait
# End of function preconditions
```
%% Cell type:markdown id: tags:
## Putting everything together
It is time now to create the our third iteration of our MEC application.
The sequence is the followin
- Create an ETSI MEC application instance
- Wait for a oneM2M platform to register to the ETSI MEC platform (1 minute max.)
- Discover the list of sensors and devices of type
- Display the result of the discovery
- Terminate the ETSI MEC application instance
g:
%% Cell type:code id: tags:
``` python
%%scriptechoskipping
# Uncomment the line above to skip execution of this cell
defprocess_main():
"""
This is the second sprint of our skeleton of our MEC application:
- Create an ETSI MEC application instance
- Wait for a oneM2M platform to register to the ETSI MEC platform (1 minute max.)
- Discover the list of sensors and devices of type
- Display the result of the discovery
- Terminate the ETSI MEC application instance
"""
globallogger,nw_scenarios
logger.debug('Starting at '+time.strftime('%Y%m%d-%H%M%S'))
logger.debug('\t pwd= '+os.getcwd())
# Setup the MEC application
(sandbox_name,app_inst_id,sub_id)=mec_app_setup()
# Wait for a oneM2M platform to register to the ETSI MEC platform
logger.debug('Stopped at '+time.strftime('%Y%m%d-%H%M%S'))
# End of function process_main
if__name__=='__main__':
process_main()
```
%% Cell type:markdown id: tags:
# Fourth round: How to use subscriptions
The purpose here is to use subscription on sensors/devices to be notified when a event occurs, such as an update of the sensor/device, a new measurement...
According to ETSI GS MEC 046 V3.1.1 (2024-04) Clause 5.3 Sequence diagrams, there are several kind of subscription:
- Sensor Status subscription (clause 5.3.5 Sensor Status Subscription);
- Sensor Data subscription (clause 5.3.7 Sensor Data Subscription).
In any case, the mechanism is the same:
1. Our MEC application shall create a subscription request to get a subscription identifier
2. Our MEC application receives notification (see next section below)
3. Our Our MEC application cancel the subscription when terminating
The cell below provides the code to create our subscription.
%% Cell type:markdown id: tags:
#### Notification support
To recieve notification, our MEC application is required to support an HTTP listenener to recieve POST requests from the MEC Sandbox and reply to them: this is the notification mechanism.
The class HTTPRequestHandler (see cell below) provides the suport of such mechanism.
logger.debug('Stopped at '+time.strftime('%Y%m%d-%H%M%S'))
# End of function process_main
if__name__=='__main__':
process_main()
```
%% Cell type:markdown id: tags:
# Fith round: Update sensor value
%% Cell type:markdown id: tags:
# Annexes
## Annex A: How to use an existing MEC sandbox instance
This case is used when the MEC Sandbox API is not used. The procedure is the following:
- Log to the MEC Sandbox using a WEB browser
- Select a network scenario
- Create a new application instance
When it is done, the newly created application instance is used by your application when required. This application instance is usually passed to your application in the command line or using a configuration file
%% Cell type:markdown id: tags:
### Bibliography
1. ETSI GS MEC 002 (V2.2.1) (01-2022): "Multi-access Edge Computing (MEC); Phase 2: Use Cases and Requirements".