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;
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()
*/
.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();
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();
// }
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();
// }
}
......
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":
......
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.*;
......@@ -11,17 +17,23 @@ 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());
}
......@@ -29,4 +41,40 @@ public class CoAPServer {
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;
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();
......
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,63 +10,63 @@ 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() {
this.conn = this.connect();
}
private Connection connect(){
// Lade Datenbankverbindungsdaten aus MySQL.properties.
try {
conn =
DriverManager.getConnection("jdbc:mysql://"+MySQL.DB_SERVER+":"+MySQL.DB_PORT+"/"+MySQL.DB_DBNAME+"?" +
"user="+MySQL.DB_USER+"&password="+MySQL.DB_PASS);
Properties prop = new Properties();
InputStream input = null;
this.statement = (Statement) this.conn.createStatement();
return conn;
// Do something with the Connection
input = new FileInputStream("MySQL.properties");
prop.load(input);
// INSERT
//Statement st = (Statement) conn.createStatement();
//st.executeUpdate("INSERT INTO tabelle1(test1,test2,test3) " + "VALUES (11, 22, 33)");
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");
// SELECT
//String query = "SELECT * FROM tabelle1";
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");
// create the java statement
//Statement st = conn.createStatement();
} catch (IOException ex) {
ex.printStackTrace();
}
// execute the query, and get a java resultset
//ResultSet rs = st.executeQuery(query);
this.conn = this.connect();
}
// while (rs.next())
// {
// int test1 = rs.getInt("test1");
//int test2 = rs.getInt("test2");
//int test3 = rs.getInt("test3");
//String firstName = rs.getString("first_name");
private Connection connect(){
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
//System.out.format("%s, %s, %s\n", test1, test2, test3);
//}
this.statement = (Statement) this.conn.createStatement();
return conn;
} catch (SQLException ex) {
// handle any errors
......@@ -77,7 +80,7 @@ public class MySQL {
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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment