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

Init

parents
Branches
No related tags found
No related merge requests found
Showing with 366 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_77]"/>
<classpathentry kind="lib" path="/Users/Philip/Downloads/californium-core-1.0.4.jar" sourcepath="/Users/Philip/.m2/repository/org/eclipse/californium/californium-core/1.0.4/californium-core-1.0.4-sources.jar"/>
<classpathentry kind="lib" path="/Users/Philip/Downloads/element-connector-1.0.4.jar"/>
<classpathentry kind="lib" path="/Users/Philip/Downloads/json-20160212.jar"/>
<classpathentry kind="lib" path="/Users/Philip/Downloads/mysql-connector-java-5.1.39.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
.project 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>boarderGateway</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
#Californium CoAP Properties file
#Wed Jun 15 09:30:38 CEST 2016
HTTP_SERVER_SOCKET_BUFFER_SIZE=8192
UDP_CONNECTOR_OUT_CAPACITY=2147483647
CONGESTION_CONTROL_ALGORITHM=Cocoa
USE_CONGESTION_CONTROL=false
ACK_TIMEOUT=2000
NOTIFICATION_CHECK_INTERVAL_COUNT=100
MAX_MESSAGE_SIZE=1024
DEDUPLICATOR=DEDUPLICATOR_MARK_AND_SWEEP
COAP_PORT=5683
ACK_TIMEOUT_SCALE=2.0
PREFERRED_BLOCK_SIZE=512
NETWORK_STAGE_RECEIVER_THREAD_COUNT=1
PROTOCOL_STAGE_THREAD_COUNT=8
MAX_TRANSMIT_WAIT=93000
UDP_CONNECTOR_RECEIVE_BUFFER=0
EXCHANGE_LIFETIME=247000
HTTP_SERVER_SOCKET_TIMEOUT=100000
CROP_ROTATION_PERIOD=2000
UDP_CONNECTOR_DATAGRAM_SIZE=2048
MAX_RETRANSMIT=4
NOTIFICATION_CHECK_INTERVAL=86400000
LEISURE=5000
HTTP_CACHE_RESPONSE_MAX_AGE=86400
BLOCKWISE_STATUS_LIFETIME=600000
UDP_CONNECTOR_SEND_BUFFER=0
HEALTH_STATUS_PRINT_LEVEL=FINEST
NETWORK_STAGE_SENDER_THREAD_COUNT=1
NON_LIFETIME=145000
TOKEN_SIZE_LIMIT=8
HTTP_PORT=8080
MARK_AND_SWEEP_INTERVAL=10000
HEALTH_STATUS_INTERVAL=60
ACK_RANDOM_FACTOR=1.5
NSTART=1
USE_RANDOM_MID_START=true
HTTP_CACHE_SIZE=32
PROBING_RATE=1.0
NOTIFICATION_REREGISTRATION_BACKOFF=2000
COAP_SECURE_PORT=5684
File added
File added
File added
File added
File added
File added
File added
File added
package boarderGateway;
import java.util.ArrayList;
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;
public class CoAPRessources {
public static class RegisterRessource extends CoapResource{
private MySQL database;
public RegisterRessource(){
super("register");
this.database = new MySQL();
getAttributes().setTitle("Client Registrierung");
}
@Override
public void handlePOST(CoapExchange exchange) {
try {
// Parse empfangenen String als JSON. Bei Fehler: BAD_REQUEST
JSONObject json = new JSONObject(exchange.getRequestText());
String name = json.getString("name");
int clientID = json.getInt("id");
// Parse JSONArray in Java List
JSONArray params = json.getJSONArray("parameter");
List<String> list = new ArrayList<String>();
for(int i = 0; i < params.length(); i++){
list.add(params.get(i).toString());
}
// Füge Client in Datenbank hinzu. Bei Fehler: INTERNAL_SERVER_ERROR
if (database.registerClient(name, clientID, list)){
exchange.respond(ResponseCode.CREATED);
}else{
exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR);
}
}catch(JSONException e){
exchange.respond(ResponseCode.BAD_REQUEST);
}
}
}
public static class DataRessource extends CoapResource{
private MySQL database;
public DataRessource(){
super("data");
this.database = new MySQL();
getAttributes().setTitle("Datenuebertragung");
}
@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);
}
}
}
}
package boarderGateway;
import org.eclipse.californium.core.CoapServer;
import boarderGateway.CoAPRessources.*;
public class CoAPServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
CoapServer server = new CoapServer();
server.add(new RegisterRessource());
server.add(new DataRessource());
server.start();
}
}
package boarderGateway;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
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 = "";
public static final String TBL_REGISTER = "tbl_register";
public static final String TBL_PARAMS = "tbl_parameter";
public static final String TBL_DATA = "tbl_data";
private Connection conn = null;
private Statement statement = null;
public MySQL() {
this.conn = this.connect();
}
private Connection connect(){
try {
conn =
DriverManager.getConnection("jdbc:mysql://"+MySQL.DB_SERVER+":"+MySQL.DB_PORT+"/"+MySQL.DB_DBNAME+"?" +
"user="+MySQL.DB_USER+"&password="+MySQL.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;
}
}
public boolean registerClient(String name, int clientID, List<String> params){
// TODO: Prüfe ob Client bereits registriert?
String query = "";
try {
query = "INSERT INTO "+MySQL.TBL_REGISTER+"(clientID, name) VALUES("+clientID+", '"+name+"')";
System.out.println(query);
this.statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
int lastID = this.getLastInsertedID(this.statement.getGeneratedKeys());
if (lastID > 0){
for ( String para : params){
query = "INSERT INTO "+MySQL.TBL_PARAMS+"(fid_register, param) VALUES("+lastID+", '"+para+"')";
System.out.println(query);
this.statement.executeUpdate(query);
}
return true;
}else{
return false;
}
} 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 false;
}
}
public boolean addData(int clientID, List<String> values){
String query = "";
try {
// Erhalte aktuellsten Primary Key der entsprechenden clientID
query = "SELECT * FROM "+MySQL.TBL_REGISTER+" WHERE clientID = "+clientID+" 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;
rs = this.statement.executeQuery(query);
while (rs.next()){
parameterIDs.add(rs.getInt("id"));
}
// 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()+"')";
this.statement.executeUpdate(query);
}
}else{
return false;
}
} 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 false;
}
return true;
}
public boolean doesClientIDExist(int clientID){
String query = "SELECT * FROM "+MySQL.TBL_REGISTER+" WHERE clientID = "+clientID;
try {
ResultSet rs = this.statement.executeQuery(query);
// True if not empty
return rs.next();
} catch (SQLException e) {
// TODO Auto-generated catch block
return false;
}
}
private int getLastInsertedID(ResultSet keys){
try {
keys.next();
return keys.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
return 0;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment