Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
PiCoapClient.java
Go to the documentation of this file.
1 /* Copyright 2013 Eric Ptak - trouch.com
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13 */
14 
15 package com.trouch.webiopi.client;
16 
17 import com.trouch.coap.client.CoapClient;
18 import com.trouch.coap.messages.CoapRequest;
19 import com.trouch.coap.messages.CoapResponse;
20 import com.trouch.coap.methods.CoapGet;
21 import com.trouch.coap.methods.CoapPost;
22 
23 
24 public class PiCoapClient extends PiClient {
25  public final static int DEFAULT_PORT = 5683;
26  private CoapClient client;
27 
28  public PiCoapClient(String host) {
29  super("coap", host, DEFAULT_PORT);
30  client = new CoapClient();
31  }
32 
33  public PiCoapClient(String host, int port) {
34  super("coap", host, port);
35  client = new CoapClient();
36  }
37 
38  @Override
39  public String sendRequest(String method, String path) throws Exception {
40  CoapRequest request;
41  if (method == "GET") {
42  request = new CoapGet(this.urlBase + path);
43  }
44  else if (method == "POST") {
45  request = new CoapPost(this.urlBase + path);
46  }
47  else throw new Exception("Method not supported: " + method);
48 
49  CoapResponse response = client.sendRequest(request);
50  if (response != null) {
51  return response.getPayload();
52  }
53 
54  return null;
55  }
56 
57 }
tuple response
Definition: coap-client.py:9
String sendRequest(String method, String path)