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
* ->So we implemented `TemperatureResource` which extends `BasicCoapResource` with:
* A constructor to initialize the resource and disallow POST, PUT and DELETE requests and
* Two get() methods:
```java
CoapData get(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
#### 2. Instantiate a new ResourceServer (Server.java, FIXME 1):
* Need a CoapResourceServer to maintain resources
```java
CoapResourceServer resourceServer = new CoapResourceServer();
```
#### 3. Instantiate a new TemperatureResource (Server.java, FIXME 2):
* Resources are created like normal objects and added to the server
```java
CoapResource resource = new CoapResource();
```
#### 4. Add the TemperatureResource to the ResourceServer (Server.java, FIXME 3):
* Resources are created like normal objects and added to the server
```java
resourceServer.createResource(resource);
```
#### 5. Run the ResourceServer (Server.java FIXME 4):
```java
resourceServer.start(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
#### 1. Establish a connection to the Server using the ChannelManager (Client.java, FIXME 5-6):
* A client must implement CoapClient interface
```java
public class Client implements CoapClient {...}
```
* A CoapChannelManager is used to manage different connections and to establish a connection to a server
```java
channelManager = BasicCoapChannelManager.getInstance();
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
#### 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
```java
Boolean reliable = false;
CoapRequestCode reqCode = CoapRequestCode.GET;
......@@ -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):
```java
clientChannel.sendMessage(request);
```
#### 4. Wait for CoapResponse & Print the CoapResponse on the console
* A client has some callbacks that are invoked, when the corresponding event occurs
```java
public void onConnectionFailed(...);
public void onResponse(...); // = Unicast
......@@ -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 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`
```java
// add a (mandatory) token to match outgoing multicast message and incoming unicast messages
request.setToken("MCToken".getBytes());
......@@ -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):
* Mark the TemperatureResource as observable
```java
resource.setObservable(true);
```
2. Let server notify clients every 5 s about changed TemperatureResource (Server.java, TODO 11):
* indicate a change for resource every 5 seconds
```java
while (true) {
try {Thread.sleep(5000);}
......@@ -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):
* Change exitAfterResponse to false (Client.java, TODO 12)
* Add the observe-option to your CoAP-GET request (Client.java, TODO 13)
```java
request.setObserveOption(0);
```
* add a BasicCoapResource to the ResourceServer (Server.java, TODO 14)
```java
resourceServer.createResource(newBasicCoapResource(/ACControl,off,CoapMediaType.text_plain));
```
* send PUT request (Client.java, TODO 15)
```java
CoapRequest request = clientChannel.createRequest(true, CoapRequestCode.PUT);
request.setUriPath(/ACControl);
request.setContentType(CoapMediaType.text_plain);
```
Depending on the received temperature, set the payload to high, medium, low or off
```sh
```
high 28 <= Temperature
medium 25 <= Temperature < 28
low 21 <= Temperature < 25
off Temperature < 21
```
```java
request.setPayload(medium.getBytes());
clientChannel.sendMessage(request);
......
......