# 109 Lab4 - GPS tracking using OM2M, node-red and app inventor 2

###### tags: `109 物聯網核心網路技術`
---
[TOC]
---
## oneM2M 架構圖



Mca: the interface point between AE and CSE which provides the applications access to the common services.
Mcc: the reference point between two CSEs.
Mcn: the reference point between CSE and underlying NSE like transport and connectivity services.
Mcc’: the interface between two M2M service providers.
IN: Infrastructure Node
NoDN: Non-oneM2M Device Node
## A. Pre-work
* Start the OM2M IN-CSE
* Start the OM2M MN-CSE
* Start node-red
* Start App Inventor 2
:::danger
Lab4-1
:::
:::warning
因為 MN-CSE 可以透過 redirector(MCC) 到 IN-CSE
所以這邊 127.0.0.1:8282 可以存取到 IN-CSE 的資料
:::
## B. Create a MN-AE with Node-RED.
### 1. Application
:::info
在 mn-cse 上新增一個 Application resource
:::

#### Properties
* **Platform** : Destination CSE.

* **Application** : AE Id (application name).

* **Point of Access** : PoA URL.
* **Announce** : whether this resource is announced to other CSE or not.
* **Labels** : labels to describe this application.

#### om2m resource tree

---
### 2. Container
:::info
Creates a Container resource to store content instances.
:::

#### Properties
* **Platform** : Destination CSE.
(選剛剛新增的 **"mn-cse"**)
* **Application** : AE Id (application name)
(選剛剛新增的 **"LOCATION_GATEWAY_APPLICATION"**)
* **Container** : Name of the container (choose among DESCRIPTOR, DATA, or input your own container’s name).
<!--  -->
* 
#### om2m resource tree
* 
---
### 3. ContentInstance
:::info
Creates a ContentInstance resource to store application’s data.
:::

#### Properties
* Platform: Destination CSE.
(選剛剛新增的 **"mn-cse"**)
* Application: AE Id (application name).
(選剛剛新增的 **"LOCATION_GATEWAY_APPLICATION"**)
* Container: Name of the container.
(填入剛剛新增的 **"DATA"**)
* Labels: The data you want to store. You must describe three fields: Type (str, int, bool, etc.), Name (the name of the attribute), Value (the value of the attribute).
* 
#### om2m resource tree


---
### 4. NotificationHandler(用http in取代)
om2m 1.3 訂閱時會先確認要通知的位置是否存在,但這顆node不太好用
("NotificationHandler"會把port佔住,要重啟node-red才能進行後面的測試)
建議使用node-red內建的 **"http in"**,或者直接開啟http server
這邊用 **input ->"http in"** 來示範

1. 將第一顆node(http in)改成以下內容

2. 中間那顆是個http response,在訂閱時需要回傳訊息,讓om2m知道這個位置存在
---
### 5. Subscription
Creates a Subscription source.

* Platform: Destination CSE.
(選剛剛新增的 **"mn-cse"**)
* Application: AE Id (application name).
(選剛剛新增的 **"LOCATION_GATEWAY_APPLICATION"**)
* Container: Name of the container you want to subscribe to.
(填入剛剛新增的 **"DATA"**)
* Path: URL of the webservice that will handle the notification of this subscription.

* om2m resource tree


---
## C. 測試&處理Notification
:::warning
把剛才用來確認訂閱的flow稍微改一下

:::
* "switch" node

* 取出狀態
```javascript=
var data = msg.payload["m2m:sgn"].nev[0].rep[0]["m2m:cin"][0].con[0];
msg.payload = data
return msg;
```

* 設定object
```javascript=
var New = {latitude:null,longitude:null};
New.latitude = msg.payload.obj.str[0].$.val;
New.longitude = msg.payload.obj.str[1].$.val;
msg.payload = New;
return msg;
```

---
## D. 在MN-AE新增一個處理建立om2m resource任務的功能

* http in node

* set payload and headers

```javascript=
/*jshint multistr: true */
var data = '<m2m:cin xmlns:m2m="http://www.onem2m.org/xml/protocols">\
<cnf>message</cnf>\
<con>\
<obj>\
<str name="latitude" val="'+msg.payload.latitude+'"/>\
<str name="longitude" val="'+msg.payload.longitude+'"/>\
</obj>\
</con>\
</m2m:cin>';
msg.payload = data;
msg.headers = {};
msg.headers['X-M2M-Origin'] = 'admin:admin';
msg.headers['Content-Type'] = 'application/xml;ty=4';
return msg;
```
格式參考 : https://wiki.eclipse.org/OM2M/one/REST_API#Create_a_data_contentInstance
* post to mn-cse

---
## E. Sending Location Data to MN-AE using App inventor 2
1. Create a new project and draw the components in the GUI as shown below.

2. In the blocks area, draw the corresponding objects for:


---
:::danger
Lab4-2
:::
## F. Getting Location Data from IN-AE using App Inventor 2
1. Use node-red to create resources in in-cse
* create ae(application)





---
* create container




---
* create ContentInstance




---
* handle the request from app inventor (om2m 自行編譯版)

第四條 第二顆 node

第四條 第四顆 node

第四條 第六顆 node

* handle the request from app inventor (om2m 下載版)
* 在 `[get]/locations` node 後,新增一function node
```xml=
msg.headers = {};
msg.headers['X-M2M-Origin'] = 'admin:admin';
return msg;
```
* `get from in cse` node 取消勾選 Use authentication
2. create a new project and draw the components in the GUI as shown below.

3. In the blocks area, draw the corresponding objects for:


