formatting of eclipse import tutorial authored by Björn Butzin's avatar Björn Butzin
...@@ -94,6 +94,7 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -94,6 +94,7 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
* ->So we implemented `TemperatureResource` which extends `BasicCoapResource` with: * ->So we implemented `TemperatureResource` which extends `BasicCoapResource` with:
* A constructor to initialize the resource and disallow POST, PUT and DELETE requests and * A constructor to initialize the resource and disallow POST, PUT and DELETE requests and
* Two get() methods: * Two get() methods:
```java ```java
CoapData get(List<CoapMediaType> mediaTypesAccepted); CoapData get(List<CoapMediaType> mediaTypesAccepted);
CoapData get(List<String> query, List<CoapMediaType> mediaTypesAccepted); CoapData get(List<String> query, List<CoapMediaType> mediaTypesAccepted);
...@@ -101,23 +102,27 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -101,23 +102,27 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
#### 2. Instantiate a new ResourceServer (Server.java, FIXME 1): #### 2. Instantiate a new ResourceServer (Server.java, FIXME 1):
* Need a CoapResourceServer to maintain resources * Need a CoapResourceServer to maintain resources
```java ```java
CoapResourceServer resourceServer = new CoapResourceServer(); CoapResourceServer resourceServer = new CoapResourceServer();
``` ```
#### 3. Instantiate a new TemperatureResource (Server.java, FIXME 2): #### 3. Instantiate a new TemperatureResource (Server.java, FIXME 2):
* Resources are created like normal objects and added to the server * Resources are created like normal objects and added to the server
```java ```java
CoapResource resource = new CoapResource(); CoapResource resource = new CoapResource();
``` ```
#### 4. Add the TemperatureResource to the ResourceServer (Server.java, FIXME 3): #### 4. Add the TemperatureResource to the ResourceServer (Server.java, FIXME 3):
* Resources are created like normal objects and added to the server * Resources are created like normal objects and added to the server
```java ```java
resourceServer.createResource(resource); resourceServer.createResource(resource);
``` ```
#### 5. Run the ResourceServer (Server.java FIXME 4): #### 5. Run the ResourceServer (Server.java FIXME 4):
```java ```java
resourceServer.start(port); resourceServer.start(port);
resourceServer.start(); // equals port = CoapConstants.COAP_DEFAULT_PORT resourceServer.start(); // equals port = CoapConstants.COAP_DEFAULT_PORT
...@@ -139,10 +144,12 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -139,10 +144,12 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
#### 1. Establish a connection to the Server using the ChannelManager (Client.java, FIXME 5-6): #### 1. Establish a connection to the Server using the ChannelManager (Client.java, FIXME 5-6):
* A client must implement CoapClient interface * A client must implement CoapClient interface
```java ```java
public class Client implements CoapClient {...} public class Client implements CoapClient {...}
``` ```
* A CoapChannelManager is used to manage different connections and to establish a connection to a server * A CoapChannelManager is used to manage different connections and to establish a connection to a server
```java ```java
channelManager = BasicCoapChannelManager.getInstance(); channelManager = BasicCoapChannelManager.getInstance();
clientChannel = channelManager.connect(CoapClient client,InetAddress serverIP, int serverPort); clientChannel = channelManager.connect(CoapClient client,InetAddress serverIP, int serverPort);
...@@ -150,6 +157,7 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -150,6 +157,7 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
#### 2. Create a CoapRequest & add some Options (Client.java, FIXME 7-9): #### 2. Create a CoapRequest & add some Options (Client.java, FIXME 7-9):
* A channel represents a single connection and is used to create and send requests * A channel represents a single connection and is used to create and send requests
```java ```java
Boolean reliable = false; Boolean reliable = false;
CoapRequestCode reqCode = CoapRequestCode.GET; CoapRequestCode reqCode = CoapRequestCode.GET;
...@@ -158,12 +166,14 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -158,12 +166,14 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
``` ```
#### 3. Send the CoapRequest (Client.java, FIXME 7-9): #### 3. Send the CoapRequest (Client.java, FIXME 7-9):
```java ```java
clientChannel.sendMessage(request); clientChannel.sendMessage(request);
``` ```
#### 4. Wait for CoapResponse & Print the CoapResponse on the console #### 4. Wait for CoapResponse & Print the CoapResponse on the console
* A client has some callbacks that are invoked, when the corresponding event occurs * A client has some callbacks that are invoked, when the corresponding event occurs
```java ```java
public void onConnectionFailed(...); public void onConnectionFailed(...);
public void onResponse(...); // = Unicast public void onResponse(...); // = Unicast
...@@ -177,6 +187,7 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -177,6 +187,7 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
* GET the `/.well-known/core` resource (it is generated automatically by the server) * GET the `/.well-known/core` resource (it is generated automatically by the server)
* GET the `/.well-known/core` resource using multicast (aka. Multicast Discovery) * GET the `/.well-known/core` resource using multicast (aka. Multicast Discovery)
* Multicast Adresses: `CoapConstants.COAP_ALL_NODES_IPV4_MC_ADDR`, `CoapConstants.COAP_ALL_NODES_IPV6_LL_MC_ADDR` and `CoapConstants.COAP_ALL_NODES_IPV6_SL_MC_ADDR` * Multicast Adresses: `CoapConstants.COAP_ALL_NODES_IPV4_MC_ADDR`, `CoapConstants.COAP_ALL_NODES_IPV6_LL_MC_ADDR` and `CoapConstants.COAP_ALL_NODES_IPV6_SL_MC_ADDR`
```java ```java
// add a (mandatory) token to match outgoing multicast message and incoming unicast messages // add a (mandatory) token to match outgoing multicast message and incoming unicast messages
request.setToken("MCToken".getBytes()); request.setToken("MCToken".getBytes());
...@@ -191,11 +202,13 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -191,11 +202,13 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
1. Use the eventing mechanism CoAP-Observe (Server.java, TODO 10): 1. Use the eventing mechanism CoAP-Observe (Server.java, TODO 10):
* Mark the TemperatureResource as observable * Mark the TemperatureResource as observable
```java ```java
resource.setObservable(true); resource.setObservable(true);
``` ```
2. Let server notify clients every 5 s about changed TemperatureResource (Server.java, TODO 11): 2. Let server notify clients every 5 s about changed TemperatureResource (Server.java, TODO 11):
* indicate a change for resource every 5 seconds * indicate a change for resource every 5 seconds
```java ```java
while (true) { while (true) {
try {Thread.sleep(5000);} try {Thread.sleep(5000);}
...@@ -206,26 +219,31 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn ...@@ -206,26 +219,31 @@ You can find the required files in our repository at [https://gitlab.amd.e-techn
3. Implement an Air Conditioner Resource with the path `/ACControl`, that can be set to `high`, `medium`, `low` or `off` (Client.java & Server.java, TODO 12-15): 3. Implement an Air Conditioner Resource with the path `/ACControl`, that can be set to `high`, `medium`, `low` or `off` (Client.java & Server.java, TODO 12-15):
* Change exitAfterResponse to false (Client.java, TODO 12) * Change exitAfterResponse to false (Client.java, TODO 12)
* Add the observe-option to your CoAP-GET request (Client.java, TODO 13) * Add the observe-option to your CoAP-GET request (Client.java, TODO 13)
```java ```java
request.setObserveOption(0); request.setObserveOption(0);
``` ```
* add a BasicCoapResource to the ResourceServer (Server.java, TODO 14) * add a BasicCoapResource to the ResourceServer (Server.java, TODO 14)
```java ```java
resourceServer.createResource(newBasicCoapResource(/ACControl,off,CoapMediaType.text_plain)); resourceServer.createResource(newBasicCoapResource(/ACControl,off,CoapMediaType.text_plain));
``` ```
* send PUT request (Client.java, TODO 15) * send PUT request (Client.java, TODO 15)
```java ```java
CoapRequest request = clientChannel.createRequest(true, CoapRequestCode.PUT); CoapRequest request = clientChannel.createRequest(true, CoapRequestCode.PUT);
request.setUriPath(/ACControl); request.setUriPath(/ACControl);
request.setContentType(CoapMediaType.text_plain); request.setContentType(CoapMediaType.text_plain);
``` ```
Depending on the received temperature, set the payload to high, medium, low or off Depending on the received temperature, set the payload to high, medium, low or off
```sh
```
high 28 <= Temperature high 28 <= Temperature
medium 25 <= Temperature < 28 medium 25 <= Temperature < 28
low 21 <= Temperature < 25 low 21 <= Temperature < 25
off Temperature < 21 off Temperature < 21
``` ```
```java ```java
request.setPayload(medium.getBytes()); request.setPayload(medium.getBytes());
clientChannel.sendMessage(request); clientChannel.sendMessage(request);
... ...
......