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

MySQL properties file

parent 7b29f12c
Branches
No related tags found
No related merge requests found
...@@ -4,51 +4,67 @@ import java.io.ByteArrayOutputStream; ...@@ -4,51 +4,67 @@ import java.io.ByteArrayOutputStream;
import org.eclipse.californium.core.CoapClient; import org.eclipse.californium.core.CoapClient;
import co.nstant.in.cbor.CborBuilder; import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.CborException;
import boarderGateway.CoAPRessources.*;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import com.sun.xml.internal.fastinfoset.algorithm.HexadecimalEncodingAlgorithm;
public class CoAPClient { public class CoAPClient {
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { byte[] encodedBytes = null;
new CborEncoder(baos).encode(new CborBuilder() CoapClient client = null;
.addArray()
/*.add("Neuer Test") // try {
.addArray() // add array // new CborEncoder(baos).encode(new CborBuilder()
.add("Parameter 1") // .addArray()
.add("float") // .add("Neuer Test")
.end() // .addArray() // add array
.addArray() // .add("Parameter 1")
.add("String Parmeter 2") // .add("float")
.add("string") // .end()
.end() // .addArray()
*/ // .add("String Parmeter 2")
// .add("string")
// .end()
.add("1") // .end()
.add(2300) // .build());
//
.end() // System.out.println("Byte: "+Hex.encodeHexString(baos.toByteArray()));
.build()); // encodedBytes = baos.toByteArray();
} catch (CborException e) { //
// TODO Auto-generated catch block // client = new CoapClient("coap://localhost/register");
e.printStackTrace(); // client.post(encodedBytes, 0);
} //
//System.out.println("String: "+baos.toString()); // } catch (CborException e) {
System.out.println("Byte: "+Hex.encodeHexString(baos.toByteArray())); // // TODO Auto-generated catch block
byte[] encodedBytes = baos.toByteArray(); // e.printStackTrace();
// }
CoapClient client = new CoapClient("coap://localhost/data"); // try {
client.put(encodedBytes, 0); // 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();
// }
} }
......
package boarderGateway; package boarderGateway;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -9,21 +8,13 @@ import java.util.List; ...@@ -9,21 +8,13 @@ import java.util.List;
import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.CoAP.ResponseCode; import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.server.resources.CoapExchange; 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.JsonParseException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonMappingException; 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.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser; 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 { public class CoAPRessources {
...@@ -157,9 +148,7 @@ public class CoAPRessources { ...@@ -157,9 +148,7 @@ 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);
for (int i = 0; i < types.size();i++){
System.out.println(types.get(i));
}
for (String type : types) { for (String type : types) {
switch (type){ switch (type){
case "string": case "string":
......
package boarderGateway; 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 org.eclipse.californium.core.CoapServer;
import boarderGateway.CoAPRessources.*; import boarderGateway.CoAPRessources.*;
...@@ -11,17 +17,23 @@ public class CoAPServer { ...@@ -11,17 +17,23 @@ public class CoAPServer {
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
CoapServer server = new CoapServer(); CoapServer server = new CoapServer();
if (args.length > 0){ if (args.length > 0){
// Starte Testserver wenn erstes Argument "test" ist. Ansonsten normaler Start.
if (args[0].equals("test")){ if (args[0].equals("test")){
server.add(new RegisterRessourceTest()); server.add(new RegisterRessourceTest());
server.add(new DataRessourceTest()); server.add(new DataRessourceTest());
}else{ }else{
createProperties();
server.add(new RegisterRessource()); server.add(new RegisterRessource());
server.add(new DataRessource()); server.add(new DataRessource());
} }
}else{ }else{
// Normaler Start wenn kein Argument gegeben
createProperties();
server.add(new RegisterRessource()); server.add(new RegisterRessource());
server.add(new DataRessource()); server.add(new DataRessource());
} }
...@@ -29,4 +41,40 @@ public class CoAPServer { ...@@ -29,4 +41,40 @@ public class CoAPServer {
server.start(); 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());
}
}
} }
package boarderGateway; package boarderGateway;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -9,21 +8,12 @@ import java.util.List; ...@@ -9,21 +8,12 @@ import java.util.List;
import org.eclipse.californium.core.CoapResource; import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.CoAP.ResponseCode; import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.server.resources.CoapExchange; 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.JsonParseException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonMappingException; 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.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser; 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 { public class CoAPTestRessources {
...@@ -132,9 +122,6 @@ public class CoAPTestRessources { ...@@ -132,9 +122,6 @@ public class CoAPTestRessources {
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); System.out.println("Received PUT on /data from "+source_address);
boolean errorOccurred = false;
List<String> values = new ArrayList<String>();
try { try {
byte payload[] = exchange.getRequestPayload(); byte payload[] = exchange.getRequestPayload();
......
package boarderGateway; package boarderGateway;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -7,63 +10,63 @@ import java.sql.SQLException; ...@@ -7,63 +10,63 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
public class MySQL { public class MySQL {
public static final String DB_SERVER = "localhost"; private String db_server;
public static final String DB_PORT = "3306"; private String db_port;
public static final String DB_USER = "root"; private String db_user;
public static final String DB_DBNAME = "iot"; private String db_dbname;
public static final String DB_PASS = ""; private String db_pass;
public static final String TBL_REGISTER = "tbl_register"; private String tbl_register;
public static final String TBL_PARAMS = "tbl_parameter"; private String tbl_params;
public static final String TBL_DATA = "tbl_data"; private String tbl_data;
public static final String TBL_ADDRESSES = "tbl_addresses"; private String tbl_addresses;
public static final String TBL_UNREGISTERED = "tbl_unregistered"; private String tbl_unregistered;
private Connection conn = null; private Connection conn = null;
private Statement statement = null; private Statement statement = null;
public MySQL() { public MySQL() {
this.conn = this.connect();
}
private Connection connect(){
// Lade Datenbankverbindungsdaten aus MySQL.properties.
try { try {
conn = Properties prop = new Properties();
DriverManager.getConnection("jdbc:mysql://"+MySQL.DB_SERVER+":"+MySQL.DB_PORT+"/"+MySQL.DB_DBNAME+"?" + InputStream input = null;
"user="+MySQL.DB_USER+"&password="+MySQL.DB_PASS);
this.statement = (Statement) this.conn.createStatement(); input = new FileInputStream("MySQL.properties");
return conn; prop.load(input);
// Do something with the Connection
// INSERT this.db_server = prop.getProperty("db_server");
//Statement st = (Statement) conn.createStatement(); this.db_port = prop.getProperty("db_port");
//st.executeUpdate("INSERT INTO tabelle1(test1,test2,test3) " + "VALUES (11, 22, 33)"); this.db_user = prop.getProperty("db_user");
this.db_dbname = prop.getProperty("db_dbname");
this.db_pass = prop.getProperty("db_pass");
// SELECT this.tbl_register = prop.getProperty("tbl_register");
//String query = "SELECT * FROM tabelle1"; 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");
// create the java statement } catch (IOException ex) {
//Statement st = conn.createStatement(); ex.printStackTrace();
}
// execute the query, and get a java resultset this.conn = this.connect();
//ResultSet rs = st.executeQuery(query); }
// while (rs.next()) private Connection connect(){
// {
// int test1 = rs.getInt("test1");
//int test2 = rs.getInt("test2");
//int test3 = rs.getInt("test3");
//String firstName = rs.getString("first_name");
try {
conn =
DriverManager.getConnection("jdbc:mysql://"+this.db_server+":"+this.db_port+"/"+this.db_dbname+"?" +
"user="+this.db_user+"&password="+this.db_pass);
// print the results this.statement = (Statement) this.conn.createStatement();
//System.out.format("%s, %s, %s\n", test1, test2, test3); return conn;
//}
} catch (SQLException ex) { } catch (SQLException ex) {
// handle any errors // handle any errors
...@@ -77,7 +80,7 @@ public class MySQL { ...@@ -77,7 +80,7 @@ public class MySQL {
public boolean isClientAllowed(String source_address){ public boolean isClientAllowed(String source_address){
String query = ""; String query = "";
query = "SELECT * FROM "+MySQL.TBL_ADDRESSES+" WHERE address = '"+source_address+"'"; query = "SELECT * FROM "+this.tbl_addresses+" WHERE address = '"+source_address+"'";
ResultSet rs; ResultSet rs;
try { try {
rs = this.statement.executeQuery(query); rs = this.statement.executeQuery(query);
...@@ -93,14 +96,14 @@ public class MySQL { ...@@ -93,14 +96,14 @@ public class MySQL {
// TODO: Prüfe ob Client bereits registriert? // TODO: Prüfe ob Client bereits registriert?
String query = ""; String query = "";
try { 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); System.out.println(query);
this.statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS); this.statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
int lastID = this.getLastInsertedID(this.statement.getGeneratedKeys()); int lastID = this.getLastInsertedID(this.statement.getGeneratedKeys());
System.out.println("Last ID: "+lastID); System.out.println("Last ID: "+lastID);
if (lastID > 0){ if (lastID > 0){
for ( int i = 0; i<params.size(); i++){ 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); System.out.println(query);
this.statement.executeUpdate(query); this.statement.executeUpdate(query);
} }
...@@ -124,14 +127,14 @@ public class MySQL { ...@@ -124,14 +127,14 @@ public class MySQL {
try { try {
// Erhalte aktuellsten Primary Key der entsprechenden clientID // 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); ResultSet rs = this.statement.executeQuery(query);
rs.next(); rs.next();
int id = rs.getInt(1); int id = rs.getInt(1);
// Erhalte ParameterID-Liste zur entsprechenden Client ID // Erhalte ParameterID-Liste zur entsprechenden Client ID
List<Integer> parameterIDs = new ArrayList<Integer>(); 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); rs = this.statement.executeQuery(query);
while (rs.next()){ while (rs.next()){
parameterIDs.add(rs.getInt("id")); parameterIDs.add(rs.getInt("id"));
...@@ -140,7 +143,7 @@ public class MySQL { ...@@ -140,7 +143,7 @@ public class MySQL {
// Wenn Parameter- und Werteliste gleich groß, füge in Datenbank ein // Wenn Parameter- und Werteliste gleich groß, füge in Datenbank ein
if (values.size() == parameterIDs.size()){ if (values.size() == parameterIDs.size()){
for (int i = 0; i < values.size(); i++){ 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); this.statement.executeUpdate(query);
} }
}else{ }else{
...@@ -160,7 +163,7 @@ public class MySQL { ...@@ -160,7 +163,7 @@ public class MySQL {
} }
public boolean doesClientAddressExist(String clientAddress){ 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 { try {
ResultSet rs = this.statement.executeQuery(query); ResultSet rs = this.statement.executeQuery(query);
// True if not empty // True if not empty
...@@ -174,7 +177,7 @@ public class MySQL { ...@@ -174,7 +177,7 @@ public class MySQL {
public List<String> getClientParamsTypeList(String clientAddress){ public List<String> getClientParamsTypeList(String clientAddress){
List<String> parameters = new ArrayList<String>(); 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; ResultSet rs;
String clientID = ""; String clientID = "";
try { try {
...@@ -183,7 +186,7 @@ public class MySQL { ...@@ -183,7 +186,7 @@ public class MySQL {
clientID = Integer.toString(rs.getInt("id")); 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); rs = this.statement.executeQuery(query);
while (rs.next()){ while (rs.next()){
parameters.add(rs.getString("type")); parameters.add(rs.getString("type"));
...@@ -197,12 +200,12 @@ public class MySQL { ...@@ -197,12 +200,12 @@ public class MySQL {
} }
public void storeUnregisteredClient(String clientAddress){ 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 { try {
ResultSet rs = this.statement.executeQuery(query); ResultSet rs = this.statement.executeQuery(query);
// True if not empty // True if not empty
if (!rs.next()){ 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); this.statement.executeUpdate(query);
} }
} catch (SQLException e) { } catch (SQLException e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment