diff --git a/java/src/boarderGateway/CoAPClient.java b/java/src/boarderGateway/CoAPClient.java index b9a067820ee60813ba770a0f288fc04fa7b5d9e9..ef233451249e594c006c9521b947cc94934e4404 100644 --- a/java/src/boarderGateway/CoAPClient.java +++ b/java/src/boarderGateway/CoAPClient.java @@ -4,51 +4,67 @@ import java.io.ByteArrayOutputStream; import org.eclipse.californium.core.CoapClient; import co.nstant.in.cbor.CborBuilder; -import co.nstant.in.cbor.CborDecoder; import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; -import boarderGateway.CoAPRessources.*; - import org.apache.commons.codec.binary.Hex; -import com.sun.xml.internal.fastinfoset.algorithm.HexadecimalEncodingAlgorithm; 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() - .addArray() - /*.add("Neuer Test") - .addArray() // add array - .add("Parameter 1") - .add("float") - .end() - .addArray() - .add("String Parmeter 2") - .add("string") - .end() - */ - + byte[] encodedBytes = null; + CoapClient client = null; + +// try { +// new CborEncoder(baos).encode(new CborBuilder() +// .addArray() +// .add("Neuer Test") +// .addArray() // add array +// .add("Parameter 1") +// .add("float") +// .end() +// .addArray() +// .add("String Parmeter 2") +// .add("string") +// .end() +// .end() +// .build()); +// +// System.out.println("Byte: "+Hex.encodeHexString(baos.toByteArray())); +// encodedBytes = baos.toByteArray(); +// +// client = new CoapClient("coap://localhost/register"); +// client.post(encodedBytes, 0); +// +// } catch (CborException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + + - .add("1") - .add(2300) - .end() - .build()); - } catch (CborException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - //System.out.println("String: "+baos.toString()); - System.out.println("Byte: "+Hex.encodeHexString(baos.toByteArray())); - byte[] encodedBytes = baos.toByteArray(); - CoapClient client = new CoapClient("coap://localhost/data"); - client.put(encodedBytes, 0); +// try { +// new CborEncoder(baos).encode(new CborBuilder() +// .addArray() +// .add(29.4) +// .add("My Value 2") +// .end() +// .build()); +// +// System.out.println("Byte: "+Hex.encodeHexString(baos.toByteArray())); +// encodedBytes = baos.toByteArray(); +// +// client = new CoapClient("coap://localhost/data"); +// client.put(encodedBytes, 0); +// +// } catch (CborException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } } diff --git a/java/src/boarderGateway/CoAPRessources.java b/java/src/boarderGateway/CoAPRessources.java index 6439e8e8e2f34d986c5ec82acfb21de6119a0bb0..1e214779dfba163a0ce8a9825374ee9e9fd89589 100644 --- a/java/src/boarderGateway/CoAPRessources.java +++ b/java/src/boarderGateway/CoAPRessources.java @@ -1,6 +1,5 @@ package boarderGateway; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -9,21 +8,13 @@ import java.util.List; import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.coap.CoAP.ResponseCode; import org.eclipse.californium.core.server.resources.CoapExchange; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; + import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.cbor.CBORConstants; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; import com.fasterxml.jackson.dataformat.cbor.CBORParser; -import co.nstant.in.cbor.*; -import co.nstant.in.cbor.decoder.UnicodeStringDecoder; -import co.nstant.in.cbor.model.*; public class CoAPRessources { @@ -157,9 +148,7 @@ public class CoAPRessources { // Erhalte Reihenfolge von String & Integer List<String> types = database.getClientParamsTypeList(source_address); - for (int i = 0; i < types.size();i++){ - System.out.println(types.get(i)); - } + for (String type : types) { switch (type){ case "string": diff --git a/java/src/boarderGateway/CoAPServer.java b/java/src/boarderGateway/CoAPServer.java index 116d779694e6cabb01e642439137ac979da0e74b..a9977601e8dbf62b0ea971729e86c56d92f94f64 100644 --- a/java/src/boarderGateway/CoAPServer.java +++ b/java/src/boarderGateway/CoAPServer.java @@ -1,5 +1,11 @@ package boarderGateway; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Properties; + import org.eclipse.californium.core.CoapServer; import boarderGateway.CoAPRessources.*; @@ -10,23 +16,65 @@ public class CoAPServer { public static void main(String[] args) { // TODO Auto-generated method stub + CoapServer server = new CoapServer(); + if (args.length > 0){ + // Starte Testserver wenn erstes Argument "test" ist. Ansonsten normaler Start. if (args[0].equals("test")){ server.add(new RegisterRessourceTest()); server.add(new DataRessourceTest()); }else{ + createProperties(); server.add(new RegisterRessource()); server.add(new DataRessource()); } }else{ + // Normaler Start wenn kein Argument gegeben + createProperties(); server.add(new RegisterRessource()); server.add(new DataRessource()); } server.start(); } + + // Überprüfe ob MySQL.properties existiert. Falls nein, erstelle ein Default-Set + private static void createProperties(){ + File f = new File("MySQL.properties"); + if(!f.exists()) { + System.out.println("Creating MySQL.properties"); + + try { + Properties prop = new Properties(); + OutputStream output = null; + + output = new FileOutputStream("MySQL.properties"); + + // set the properties value + prop.setProperty("db_server", "localhost"); + prop.setProperty("db_port", "3306"); + prop.setProperty("db_user", "root"); + prop.setProperty("db_dbname", "iot"); + prop.setProperty("db_pass", ""); + + prop.setProperty("tbl_register", "tbl_register"); + prop.setProperty("tbl_params", "tbl_parameter"); + prop.setProperty("tbl_data", "tbl_data"); + prop.setProperty("tbl_addresses", "tbl_addresses"); + prop.setProperty("tbl_unregistered", "tbl_unregistered"); + + // save properties to project root folder + prop.store(output, null); + + } catch (IOException io) { + io.printStackTrace(); + } + }else{ + System.out.println("Found MySQL Config at "+f.getAbsolutePath()); + } + } } diff --git a/java/src/boarderGateway/CoAPTestRessources.java b/java/src/boarderGateway/CoAPTestRessources.java index a6c42f9614827d0dc9f3e5d80449cc253f533acf..2d7ec3aeea010c2f6b1419f07468744fa9c59907 100644 --- a/java/src/boarderGateway/CoAPTestRessources.java +++ b/java/src/boarderGateway/CoAPTestRessources.java @@ -1,6 +1,5 @@ package boarderGateway; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -9,21 +8,12 @@ import java.util.List; import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.coap.CoAP.ResponseCode; import org.eclipse.californium.core.server.resources.CoapExchange; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.cbor.CBORConstants; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; import com.fasterxml.jackson.dataformat.cbor.CBORParser; -import co.nstant.in.cbor.*; -import co.nstant.in.cbor.decoder.UnicodeStringDecoder; -import co.nstant.in.cbor.model.*; public class CoAPTestRessources { @@ -132,9 +122,6 @@ public class CoAPTestRessources { CBORFactory f = new CBORFactory(); String source_address = exchange.getSourceAddress().getHostAddress(); System.out.println("Received PUT on /data from "+source_address); - boolean errorOccurred = false; - - List<String> values = new ArrayList<String>(); try { byte payload[] = exchange.getRequestPayload(); diff --git a/java/src/boarderGateway/MySQL.java b/java/src/boarderGateway/MySQL.java index 06c7b35db7c40dd2a9d260143b330ed0e75b3995..b7855b57a5e993a67c3698df8a0b90f55c3868c8 100644 --- a/java/src/boarderGateway/MySQL.java +++ b/java/src/boarderGateway/MySQL.java @@ -1,5 +1,8 @@ package boarderGateway; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; @@ -7,25 +10,51 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; +import java.util.Properties; public class MySQL { - public static final String DB_SERVER = "localhost"; - public static final String DB_PORT = "3306"; - public static final String DB_USER = "root"; - public static final String DB_DBNAME = "iot"; - public static final String DB_PASS = ""; + private String db_server; + private String db_port; + private String db_user; + private String db_dbname; + private String db_pass; - public static final String TBL_REGISTER = "tbl_register"; - public static final String TBL_PARAMS = "tbl_parameter"; - public static final String TBL_DATA = "tbl_data"; - public static final String TBL_ADDRESSES = "tbl_addresses"; - public static final String TBL_UNREGISTERED = "tbl_unregistered"; + private String tbl_register; + private String tbl_params; + private String tbl_data; + private String tbl_addresses; + private String tbl_unregistered; private Connection conn = null; private Statement statement = null; public MySQL() { + + // Lade Datenbankverbindungsdaten aus MySQL.properties. + try { + Properties prop = new Properties(); + InputStream input = null; + + input = new FileInputStream("MySQL.properties"); + prop.load(input); + + this.db_server = prop.getProperty("db_server"); + this.db_port = prop.getProperty("db_port"); + this.db_user = prop.getProperty("db_user"); + this.db_dbname = prop.getProperty("db_dbname"); + this.db_pass = prop.getProperty("db_pass"); + + this.tbl_register = prop.getProperty("tbl_register"); + this.tbl_params = prop.getProperty("tbl_params"); + this.tbl_data = prop.getProperty("tbl_data"); + this.tbl_addresses = prop.getProperty("tbl_addresses"); + this.tbl_unregistered = prop.getProperty("tbl_unregistered"); + + } catch (IOException ex) { + ex.printStackTrace(); + } + this.conn = this.connect(); } @@ -33,51 +62,25 @@ public class MySQL { try { conn = - DriverManager.getConnection("jdbc:mysql://"+MySQL.DB_SERVER+":"+MySQL.DB_PORT+"/"+MySQL.DB_DBNAME+"?" + - "user="+MySQL.DB_USER+"&password="+MySQL.DB_PASS); + DriverManager.getConnection("jdbc:mysql://"+this.db_server+":"+this.db_port+"/"+this.db_dbname+"?" + + "user="+this.db_user+"&password="+this.db_pass); this.statement = (Statement) this.conn.createStatement(); return conn; - // Do something with the Connection - - // INSERT - //Statement st = (Statement) conn.createStatement(); - //st.executeUpdate("INSERT INTO tabelle1(test1,test2,test3) " + "VALUES (11, 22, 33)"); - - // SELECT - //String query = "SELECT * FROM tabelle1"; - - // create the java statement - //Statement st = conn.createStatement(); - - // execute the query, and get a java resultset - //ResultSet rs = st.executeQuery(query); - - // while (rs.next()) - // { - // int test1 = rs.getInt("test1"); - //int test2 = rs.getInt("test2"); - //int test3 = rs.getInt("test3"); - //String firstName = rs.getString("first_name"); - - - // print the results - //System.out.format("%s, %s, %s\n", test1, test2, test3); - //} - - } catch (SQLException ex) { - // handle any errors - System.out.println("SQLException: " + ex.getMessage()); - System.out.println("SQLState: " + ex.getSQLState()); - System.out.println("VendorError: " + ex.getErrorCode()); - - return null; - } + + } catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + ex.getMessage()); + System.out.println("SQLState: " + ex.getSQLState()); + System.out.println("VendorError: " + ex.getErrorCode()); + + return null; + } } public boolean isClientAllowed(String source_address){ String query = ""; - query = "SELECT * FROM "+MySQL.TBL_ADDRESSES+" WHERE address = '"+source_address+"'"; + query = "SELECT * FROM "+this.tbl_addresses+" WHERE address = '"+source_address+"'"; ResultSet rs; try { rs = this.statement.executeQuery(query); @@ -93,14 +96,14 @@ public class MySQL { // TODO: Prüfe ob Client bereits registriert? String query = ""; try { - query = "INSERT INTO "+MySQL.TBL_REGISTER+"(clientAddress, name) VALUES('"+clientAddress+"', '"+name+"')"; + query = "INSERT INTO "+this.tbl_register+"(clientAddress, name) VALUES('"+clientAddress+"', '"+name+"')"; System.out.println(query); this.statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS); int lastID = this.getLastInsertedID(this.statement.getGeneratedKeys()); System.out.println("Last ID: "+lastID); if (lastID > 0){ for ( int i = 0; i<params.size(); i++){ - query = "INSERT INTO "+MySQL.TBL_PARAMS+"(fid_register, param, type) VALUES("+lastID+", '"+params.get(i)+"', '"+types.get(i)+"')"; + query = "INSERT INTO "+this.tbl_params+"(fid_register, param, type) VALUES("+lastID+", '"+params.get(i)+"', '"+types.get(i)+"')"; System.out.println(query); this.statement.executeUpdate(query); } @@ -124,14 +127,14 @@ public class MySQL { try { // Erhalte aktuellsten Primary Key der entsprechenden clientID - query = "SELECT * FROM "+MySQL.TBL_REGISTER+" WHERE clientAddress = '"+clientAddress+"' ORDER BY ID DESC LIMIT 1"; + query = "SELECT * FROM "+this.tbl_register+" WHERE clientAddress = '"+clientAddress+"' ORDER BY ID DESC LIMIT 1"; ResultSet rs = this.statement.executeQuery(query); rs.next(); int id = rs.getInt(1); // Erhalte ParameterID-Liste zur entsprechenden Client ID List<Integer> parameterIDs = new ArrayList<Integer>(); - query = "SELECT * FROM "+MySQL.TBL_PARAMS+" WHERE fid_register = "+id; + query = "SELECT * FROM "+this.tbl_params+" WHERE fid_register = "+id; rs = this.statement.executeQuery(query); while (rs.next()){ parameterIDs.add(rs.getInt("id")); @@ -140,7 +143,7 @@ public class MySQL { // Wenn Parameter- und Werteliste gleich groß, füge in Datenbank ein if (values.size() == parameterIDs.size()){ for (int i = 0; i < values.size(); i++){ - query = "INSERT INTO "+MySQL.TBL_DATA+"(fid_parameter, value) VALUES("+parameterIDs.get(i).toString()+", '"+values.get(i).toString()+"')"; + query = "INSERT INTO "+this.tbl_data+"(fid_parameter, value) VALUES("+parameterIDs.get(i).toString()+", '"+values.get(i).toString()+"')"; this.statement.executeUpdate(query); } }else{ @@ -160,7 +163,7 @@ public class MySQL { } public boolean doesClientAddressExist(String clientAddress){ - String query = "SELECT * FROM "+MySQL.TBL_REGISTER+" WHERE clientAddress = '"+clientAddress+"'"; + String query = "SELECT * FROM "+this.tbl_register+" WHERE clientAddress = '"+clientAddress+"'"; try { ResultSet rs = this.statement.executeQuery(query); // True if not empty @@ -174,7 +177,7 @@ public class MySQL { public List<String> getClientParamsTypeList(String clientAddress){ List<String> parameters = new ArrayList<String>(); - String query = "SELECT * FROM "+MySQL.TBL_REGISTER+" WHERE clientAddress = '"+clientAddress+"' ORDER BY id DESC LIMIT 1"; + String query = "SELECT * FROM "+this.tbl_register+" WHERE clientAddress = '"+clientAddress+"' ORDER BY id DESC LIMIT 1"; ResultSet rs; String clientID = ""; try { @@ -183,7 +186,7 @@ public class MySQL { clientID = Integer.toString(rs.getInt("id")); } - query = "SELECT * FROM "+MySQL.TBL_PARAMS+" WHERE fid_register = "+clientID; + query = "SELECT * FROM "+this.tbl_params+" WHERE fid_register = "+clientID; rs = this.statement.executeQuery(query); while (rs.next()){ parameters.add(rs.getString("type")); @@ -197,12 +200,12 @@ public class MySQL { } public void storeUnregisteredClient(String clientAddress){ - String query = "SELECT * FROM "+MySQL.TBL_UNREGISTERED+" WHERE clientAddress = '"+clientAddress+"'"; + String query = "SELECT * FROM "+this.tbl_unregistered+" WHERE clientAddress = '"+clientAddress+"'"; try { ResultSet rs = this.statement.executeQuery(query); // True if not empty if (!rs.next()){ - query = "INSERT INTO "+MySQL.TBL_UNREGISTERED+"(clientAddress) VALUES('"+clientAddress+"')"; + query = "INSERT INTO "+this.tbl_unregistered+"(clientAddress) VALUES('"+clientAddress+"')"; this.statement.executeUpdate(query); } } catch (SQLException e) {