Skip to content
Snippets Groups Projects
Commit b4735122 authored by Philip's avatar Philip
Browse files

small changes - addes CoAP Client with CBOR support

parent a84c03db
Branches
No related tags found
No related merge requests found
package boarderGateway;
import java.io.ByteArrayOutputStream;
import org.eclipse.californium.core.CoapClient;
import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException;
import boarderGateway.CoAPRessources.*;
public class CoAPClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
new CborEncoder(baos).encode(new CborBuilder()
// .add("MyClient Name")
// .addArray() // add array
// .add("Para11")
// .add("string")
// .end()
// .addArray()
// .add("Para22")
// .add("integer")
// .end()
.add("My String value")
.add(1234)
.build());
} catch (CborException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] encodedBytes = baos.toByteArray();
CoapClient client = new CoapClient("coap://localhost/data");
client.post(encodedBytes, 0);
}
}
...@@ -39,15 +39,14 @@ public class CoAPRessources { ...@@ -39,15 +39,14 @@ public class CoAPRessources {
@Override @Override
public void handlePOST(CoapExchange exchange) { public void handlePOST(CoapExchange exchange) {
List<String> allowedTypes = Arrays.asList("string", "integer", "float");
List<String> allowedTypes = Arrays.asList("string", "integer");
boolean errorOccurred = false; boolean errorOccurred = false;
// TODO Auto-generated method stub // TODO Auto-generated method stub
CBORFactory f = new CBORFactory(); CBORFactory f = new CBORFactory();
String source_address = exchange.getSourceAddress().getHostAddress(); String source_address = exchange.getSourceAddress().getHostAddress();
System.out.println("GOT A PUT from "+source_address); System.out.println("Received POST on /register from "+source_address);
// Check, if source IP is allowed to // Check, if source IP is allowed to
if (database.isClientAllowed(source_address)){ if (database.isClientAllowed(source_address)){
try { try {
...@@ -131,13 +130,14 @@ public class CoAPRessources { ...@@ -131,13 +130,14 @@ public class CoAPRessources {
@Override @Override
public void handlePOST(CoapExchange exchange) { public void handlePUT(CoapExchange exchange) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
System.out.println("DATA POST!");
// Ermittle letzte registrierte ID zur Source Address // Ermittle letzte registrierte ID zur Source Address
CBORFactory f = new CBORFactory(); CBORFactory f = new CBORFactory();
String source_address = exchange.getSourceAddress().getHostAddress(); String source_address = exchange.getSourceAddress().getHostAddress();
System.out.println("Received PUT on /data from "+source_address);
List<String> values = new ArrayList<String>(); List<String> values = new ArrayList<String>();
if (database.doesClientAddressExist(source_address)){ if (database.doesClientAddressExist(source_address)){
try { try {
...@@ -146,7 +146,6 @@ public class CoAPRessources { ...@@ -146,7 +146,6 @@ public class CoAPRessources {
// Erhalte Reihenfolge von String & Integer // Erhalte Reihenfolge von String & Integer
List<String> types = database.getClientParamsTypeList(source_address); List<String> types = database.getClientParamsTypeList(source_address);
System.out.println("#Types: "+types.size());
for (int i = 0; i < types.size();i++){ for (int i = 0; i < types.size();i++){
System.out.println(types.get(i)); System.out.println(types.get(i));
} }
...@@ -154,18 +153,15 @@ public class CoAPRessources { ...@@ -154,18 +153,15 @@ public class CoAPRessources {
switch (type){ switch (type){
case "string": case "string":
String paraString = myCborParser.nextTextValue(); String paraString = myCborParser.nextTextValue();
System.out.println("String! : "+paraString);
values.add(paraString); values.add(paraString);
break; break;
case "integer": case "integer":
int paraInt = myCborParser.nextIntValue(0); int paraInt = myCborParser.nextIntValue(0);
System.out.println("Int! : "+paraInt);
values.add(Integer.toString(paraInt)); values.add(Integer.toString(paraInt));
break; break;
case "float": case "float":
myCborParser.nextToken(); myCborParser.nextToken();
float paraFloat = myCborParser.getFloatValue(); float paraFloat = myCborParser.getFloatValue();
Float.toString(paraFloat);
values.add(Float.toString(paraFloat)); values.add(Float.toString(paraFloat));
break; break;
default: default:
...@@ -184,7 +180,7 @@ public class CoAPRessources { ...@@ -184,7 +180,7 @@ public class CoAPRessources {
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR);
} }
}else{ }else{
...@@ -192,41 +188,5 @@ public class CoAPRessources { ...@@ -192,41 +188,5 @@ public class CoAPRessources {
} }
} }
/*@Override
public void handlePUT(CoapExchange exchange) {
try {
// Parse empfangenen String als JSON. Bei Fehler: BAD_REQUEST
JSONObject json = new JSONObject(exchange.getRequestText());
int clientID = json.getInt("id");
// Prüfe ob Client ID registriert ist. Bei Fehler: UNAUTHORIZED
if (database.doesClientIDExist(clientID)){
// Parse JSONArray in Java List
JSONArray values = json.getJSONArray("values");
List<String> list = new ArrayList<String>();
for(int i = 0; i < values.length(); i++){
list.add(values.get(i).toString());
}
// Füge Daten in Datenbank hinzu. Bei Fehler: INTERNAL_SERVER_ERROR
if (database.addData(clientID, list)){
exchange.respond(ResponseCode.CHANGED);
}else{
exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR);
}
}else{
exchange.respond(ResponseCode.UNAUTHORIZED);
}
}catch(JSONException e){
exchange.respond(ResponseCode.BAD_REQUEST);
}
}*/
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment