Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
border-gateway
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
philjak
border-gateway
Commits
b4735122
Commit
b4735122
authored
8 years ago
by
Philip
Browse files
Options
Downloads
Patches
Plain Diff
small changes - addes CoAP Client with CBOR support
parent
a84c03db
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/boarderGateway/CoAPClient.java
+45
-0
45 additions, 0 deletions
src/boarderGateway/CoAPClient.java
src/boarderGateway/CoAPRessources.java
+7
-47
7 additions, 47 deletions
src/boarderGateway/CoAPRessources.java
with
52 additions
and
47 deletions
src/boarderGateway/CoAPClient.java
0 → 100644
+
45
−
0
View file @
b4735122
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
);
}
}
This diff is collapsed.
Click to expand it.
src/boarderGateway/CoAPRessources.java
+
7
−
47
View file @
b4735122
...
...
@@ -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
handleP
OS
T
(
CoapExchange
exchange
)
{
public
void
handleP
U
T
(
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
(
);
e
xchange
.
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);
}
}*/
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment