diff --git a/src/boarderGateway/CoAPClient.java b/src/boarderGateway/CoAPClient.java new file mode 100644 index 0000000000000000000000000000000000000000..8d4adf4ff4a054e1429d669deaac2e712b445d20 --- /dev/null +++ b/src/boarderGateway/CoAPClient.java @@ -0,0 +1,45 @@ +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); + + } + +} diff --git a/src/boarderGateway/CoAPRessources.java b/src/boarderGateway/CoAPRessources.java index 78ca4e1db225362397f61a966b68b3a736db3782..611e8724bca0dce49b2024067bca197b595535ac 100644 --- a/src/boarderGateway/CoAPRessources.java +++ b/src/boarderGateway/CoAPRessources.java @@ -39,15 +39,14 @@ public class CoAPRessources { @Override - public void handlePOST(CoapExchange exchange) { - - List<String> allowedTypes = Arrays.asList("string", "integer"); + public void handlePOST(CoapExchange exchange) { + List<String> allowedTypes = Arrays.asList("string", "integer", "float"); boolean errorOccurred = false; // TODO Auto-generated method stub CBORFactory f = new CBORFactory(); 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 if (database.isClientAllowed(source_address)){ try { @@ -131,13 +130,14 @@ public class CoAPRessources { @Override - public void handlePOST(CoapExchange exchange) { + public void handlePUT(CoapExchange exchange) { // TODO Auto-generated method stub - System.out.println("DATA POST!"); // Ermittle letzte registrierte ID zur Source Address CBORFactory f = new CBORFactory(); String source_address = exchange.getSourceAddress().getHostAddress(); + System.out.println("Received PUT on /data from "+source_address); + List<String> values = new ArrayList<String>(); if (database.doesClientAddressExist(source_address)){ try { @@ -146,7 +146,6 @@ public class CoAPRessources { // Erhalte Reihenfolge von String & Integer List<String> types = database.getClientParamsTypeList(source_address); - System.out.println("#Types: "+types.size()); for (int i = 0; i < types.size();i++){ System.out.println(types.get(i)); } @@ -154,18 +153,15 @@ public class CoAPRessources { switch (type){ case "string": String paraString = myCborParser.nextTextValue(); - System.out.println("String! : "+paraString); values.add(paraString); break; case "integer": int paraInt = myCborParser.nextIntValue(0); - System.out.println("Int! : "+paraInt); values.add(Integer.toString(paraInt)); break; case "float": myCborParser.nextToken(); float paraFloat = myCborParser.getFloatValue(); - Float.toString(paraFloat); values.add(Float.toString(paraFloat)); break; default: @@ -184,7 +180,7 @@ public class CoAPRessources { } catch (IOException e) { // TODO Auto-generated catch block - e.printStackTrace(); + exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR); } }else{ @@ -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); - } - }*/ - } }