-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoAPtoMQTTResourceHandler.java
145 lines (131 loc) · 4.72 KB
/
CoAPtoMQTTResourceHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package handler;
import java.util.logging.Logger;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.server.resources.CoapExchange;
import handler.ICommClient;
public class CoapToMqttResourceHandler extends CoapResource {
// static
private static final Logger _Logger = Logger.getLogger(CoapToMqttResourceHandler.class.getName());
public static final int DEFAULT_QOS_LEVEL = 2;
private ICommClient _mqttClient;
// constructors
/**
* Constructor.
*
* @param name
* Basically, the path (or topic)
*/
public CoapToMqttResourceHandler(String name) {
super(name);
_mqttClient = new ICommClient(); // instantiates a new MQTT Client object
try {
System.out.println("into resource hander with " + name);
_mqttClient.connect(); // connects to the given MQTT broker address
} catch (Exception e) {
_Logger.warning("Failed to connect to MQTT broker.");
}
}
// public methods
/*
* (non-Javadoc)
*
* @see org.eclipse.californium.core.CoapResource#handleDELETE(org.eclipse.
* californium.core.server.resources.CoapExchange)
*/
@Override
public void handleDELETE(CoapExchange context) {
System.out.println("handling delete");// TODO: handle DELETE as appropriate
try {
context.accept();
String s = new String(context.getRequestPayload());
if (_mqttClient.sendMessage(super.getURI(), DEFAULT_QOS_LEVEL, s)) {// checks if that message is modified
// accordingly, send a corresponding
// message. Here it is DELETE
context.respond(ResponseCode.DELETED, "Deleted content.");
} else {
context.respond(ResponseCode.SERVICE_UNAVAILABLE, "Oops - can't delete content.");
_Logger.warning("Failed to publish message to MQTT broker.");
}
} catch (Exception e) {
_Logger.warning("Failed to handle DELETE!");
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.californium.core.CoapResource#handleGET(org.eclipse.californium.
* core.server.resources.CoapExchange)
*/
@Override
public void handleGET(CoapExchange context) {
// System.out.println("handling get");// TODO: handle GET as appropriate
String responseMsg = "This is a generic response to the GET request for path: " + super.getName();
context.accept(); // sends an acknowledgement to the client for successful reception of message
context.respond(ResponseCode.VALID, responseMsg); // responds with suitable response message
_Logger.info("Handling GET: " + responseMsg);
_Logger.info(context.getRequestCode().toString() + ": " + context.getRequestText());
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.californium.core.CoapResource#handlePOST(org.eclipse.californium.
* core.server.resources.CoapExchange)
*/
@Override
public void handlePOST(CoapExchange context) {
try {
context.accept();
System.out.println("handling post ");
// System.out.println(context.getRequestText().toString());
String s = new String(context.getRequestPayload()); // gets String for a byte payload
if (_mqttClient.sendMessage(context.getRequestOptions().getUriPathString(), DEFAULT_QOS_LEVEL, s)) {// checks
// if
// that
// message
// is
// modified
// accordingly, send a corresponding
// message. Here it is DELETE
context.respond(ResponseCode.CREATED, "Created content.");
} else {
context.respond(ResponseCode.SERVICE_UNAVAILABLE, "Oops - can't create content.");
_Logger.warning("Failed to publish message to MQTT broker in HandlePost.");
}
} catch (Exception e) {
_Logger.warning("Failed to handle POST!");
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.californium.core.CoapResource#handlePUT(org.eclipse.californium.
* core.server.resources.CoapExchange)
*/
@Override
public void handlePUT(CoapExchange context) {
System.out.println("IN HANDLEPUT");
// TODO: handle PUT as appropriate
try {
context.accept();
System.out.println(super.getPath() + context.getRequestText());
String s = new String(context.getRequestPayload());
if (_mqttClient.sendMessage(context.getRequestOptions().getUriPathString(), DEFAULT_QOS_LEVEL, s)) {
context.respond(ResponseCode.CREATED, "Changed content.");// checks if that message is modified
// accordingly, send a corresponding
// message. Here it is PUT
} else {
context.respond(ResponseCode.SERVICE_UNAVAILABLE, "Oops - can't change content.");
_Logger.warning("Failed to publish message to MQTT broker in HandlePut.");
}
} catch (Exception e) {
_Logger.warning("Failed to handle POST!");
}
}
public String toString() {
return null;
}
}