Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "OM2M/one/App"
(Created page with "'''Before doing this tutorial you have to''' [https://wiki.eclipse.org/OM2M/Clone clone and build OM2M using Eclipse] __TOC__ = Develop standalone oneM2M applications = ==...") |
|||
Line 11: | Line 11: | ||
* Enter "org.eclipse.om2m" as Group Id and "org.eclipse.om2m.app" as "Artifact Id" then click on Finish | * Enter "org.eclipse.om2m" as Group Id and "org.eclipse.om2m.app" as "Artifact Id" then click on Finish | ||
* Create package "org.eclipse.om2m.app" inside the folder src/main/java | * Create package "org.eclipse.om2m.app" inside the folder src/main/java | ||
+ | |||
+ | == Configure project dependencies == | ||
* Add dependencies for HTTP and JSON to your project by adding the following section to your '''pom.xml''' file inside the '''<project>''' tag. | * Add dependencies for HTTP and JSON to your project by adding the following section to your '''pom.xml''' file inside the '''<project>''' tag. | ||
Line 33: | Line 35: | ||
</source> | </source> | ||
− | + | == RestHttpClient class == | |
− | + | ||
− | + | ||
<source lang="java"> | <source lang="java"> | ||
Line 105: | Line 105: | ||
</source> | </source> | ||
− | + | == HttpResponse class == | |
<source lang="java"> | <source lang="java"> | ||
package org.eclipse.om2m.app; | package org.eclipse.om2m.app; | ||
Line 127: | Line 127: | ||
public void setBody(String body) { | public void setBody(String body) { | ||
this.body = body; | this.body = body; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == MySensor Class == | ||
+ | <source lang="java"> | ||
+ | package org.eclipse.om2m.app; | ||
+ | |||
+ | import org.json.JSONObject; | ||
+ | |||
+ | public class MySensor { | ||
+ | |||
+ | public static int sensorValue; | ||
+ | |||
+ | private static String originator="admin:admin"; | ||
+ | private static String cseProtocol="http"; | ||
+ | private static String cseIp = "127.0.0.1"; | ||
+ | private static int csePort = 8282; | ||
+ | private static String cseId = "mn-cse"; | ||
+ | private static String cseName = "mn-name"; | ||
+ | |||
+ | private static String aeName = "mysensor"; | ||
+ | private static String cntName = "data"; | ||
+ | |||
+ | private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | JSONObject obj = new JSONObject(); | ||
+ | obj.put("rn", aeName); | ||
+ | obj.put("api", 12345); | ||
+ | obj.put("rr", false); | ||
+ | JSONObject resource = new JSONObject(); | ||
+ | resource.put("m2m:ae", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); | ||
+ | |||
+ | obj = new JSONObject(); | ||
+ | obj.put("rn", cntName); | ||
+ | resource = new JSONObject(); | ||
+ | resource.put("m2m:cnt", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 3); | ||
+ | |||
+ | while (true){ | ||
+ | |||
+ | obj = new JSONObject(); | ||
+ | obj.put("cnf", "application/text"); | ||
+ | obj.put("con", getSensorValue()); | ||
+ | resource = new JSONObject(); | ||
+ | resource.put("m2m:cin", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 4); | ||
+ | |||
+ | try { | ||
+ | Thread.sleep(10000); | ||
+ | } catch (InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static int getSensorValue(){ | ||
+ | sensorValue = (int)(Math.random()*500); | ||
+ | System.out.println("Sensor value = "+sensorValue); | ||
+ | return sensorValue; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == MyActuator Class == | ||
+ | <source lang="java"> | ||
+ | package org.eclipse.om2m.app; | ||
+ | |||
+ | import java.io.IOException; | ||
+ | import java.io.InputStream; | ||
+ | import java.io.OutputStream; | ||
+ | import java.net.InetSocketAddress; | ||
+ | import java.util.concurrent.Executors; | ||
+ | import org.json.JSONArray; | ||
+ | import org.json.JSONObject; | ||
+ | import com.sun.net.httpserver.HttpExchange; | ||
+ | import com.sun.net.httpserver.HttpHandler; | ||
+ | import com.sun.net.httpserver.HttpServer; | ||
+ | |||
+ | public class MyActuator { | ||
+ | |||
+ | private static boolean actuatorValue; | ||
+ | |||
+ | private static String originator="admin:admin"; | ||
+ | private static String cseProtocol="http"; | ||
+ | private static String cseIp = "127.0.0.1"; | ||
+ | private static int csePort = 8282; | ||
+ | private static String cseId = "mn-cse"; | ||
+ | private static String cseName = "mn-name"; | ||
+ | |||
+ | private static String aeName = "myactuator"; | ||
+ | private static String cntName = "data"; | ||
+ | |||
+ | private static String aeProtocol="http"; | ||
+ | private static String aeIp = "127.0.0.1"; | ||
+ | private static int aePort = 1400; | ||
+ | private static String subName="actuatorsub"; | ||
+ | |||
+ | private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; | ||
+ | private static String appPoa = aeProtocol+"://"+aeIp+":"+aePort; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | HttpServer server = null; | ||
+ | try { | ||
+ | server = HttpServer.create(new InetSocketAddress(aePort), 0); | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | server.createContext("/", new MyHandler()); | ||
+ | server.setExecutor(Executors.newCachedThreadPool()); | ||
+ | server.start(); | ||
+ | |||
+ | JSONArray array = new JSONArray(); | ||
+ | array.put(appPoa); | ||
+ | JSONObject obj = new JSONObject(); | ||
+ | obj.put("rn", aeName); | ||
+ | obj.put("api", 12345); | ||
+ | obj.put("rr", true); | ||
+ | obj.put("poa",array); | ||
+ | JSONObject resource = new JSONObject(); | ||
+ | resource.put("m2m:ae", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); | ||
+ | |||
+ | obj = new JSONObject(); | ||
+ | obj.put("rn", cntName); | ||
+ | resource = new JSONObject(); | ||
+ | resource.put("m2m:cnt", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 3); | ||
+ | |||
+ | array = new JSONArray(); | ||
+ | array.put("/"+cseId+"/"+cseName+"/"+aeName); | ||
+ | obj = new JSONObject(); | ||
+ | obj.put("nu", array); | ||
+ | obj.put("rn", subName); | ||
+ | obj.put("nct", 2); | ||
+ | resource = new JSONObject(); | ||
+ | resource.put("m2m:sub", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 23); | ||
+ | } | ||
+ | |||
+ | static class MyHandler implements HttpHandler { | ||
+ | |||
+ | public void handle(HttpExchange httpExchange) { | ||
+ | System.out.println("Event Recieved!"); | ||
+ | |||
+ | try{ | ||
+ | InputStream in = httpExchange.getRequestBody(); | ||
+ | |||
+ | String requestBody = ""; | ||
+ | int i;char c; | ||
+ | while ((i = in.read()) != -1) { | ||
+ | c = (char) i; | ||
+ | requestBody = (String) (requestBody+c); | ||
+ | } | ||
+ | |||
+ | System.out.println(requestBody); | ||
+ | |||
+ | JSONObject json = new JSONObject(requestBody); | ||
+ | if (json.getJSONObject("m2m:sgn").has("vrq")) { | ||
+ | System.out.println("Confirm subscription"); | ||
+ | } else { | ||
+ | JSONObject rep = json.getJSONObject("m2m:sgn").getJSONObject("nev") | ||
+ | .getJSONObject("rep"); | ||
+ | int ty = rep.getInt("ty"); | ||
+ | System.out.println("Resource type: "+ty); | ||
+ | |||
+ | if (ty == 4) { | ||
+ | String con = rep.getString("con"); | ||
+ | System.out.println("Actuator state = "+con); | ||
+ | setActuatorValue(Boolean.parseBoolean(con)); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | String responseBudy =""; | ||
+ | byte[] out = responseBudy.getBytes("UTF-8"); | ||
+ | httpExchange.sendResponseHeaders(200, out.length); | ||
+ | OutputStream os = httpExchange.getResponseBody(); | ||
+ | os.write(out); | ||
+ | os.close(); | ||
+ | |||
+ | } catch(Exception e){ | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static void setActuatorValue(boolean actuatorValue) { | ||
+ | MyActuator.actuatorValue = actuatorValue; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == MyManager Class == | ||
+ | <source lang="java"> | ||
+ | package org.eclipse.om2m.app; | ||
+ | |||
+ | import java.io.IOException; | ||
+ | import java.io.InputStream; | ||
+ | import java.io.OutputStream; | ||
+ | import java.net.InetSocketAddress; | ||
+ | import java.util.concurrent.Executors; | ||
+ | import org.json.JSONArray; | ||
+ | import org.json.JSONObject; | ||
+ | import com.sun.net.httpserver.HttpExchange; | ||
+ | import com.sun.net.httpserver.HttpHandler; | ||
+ | import com.sun.net.httpserver.HttpServer; | ||
+ | |||
+ | public class MyManager { | ||
+ | |||
+ | private static String originator="admin:admin"; | ||
+ | private static String cseProtocol="http"; | ||
+ | private static String cseIp = "127.0.0.1"; | ||
+ | private static int csePort = 8080; | ||
+ | private static String cseId = "in-cse"; | ||
+ | private static String cseName = "in-name"; | ||
+ | |||
+ | private static String aeName = "mymanager"; | ||
+ | private static String aeProtocol="http"; | ||
+ | private static String aeIp = "127.0.0.1"; | ||
+ | private static int aePort = 1500; | ||
+ | private static String subName="managersub"; | ||
+ | private static String targetSensorContainer="mn-cse/mn-name/mysensor/data"; | ||
+ | private static String targetActuatorContainer="mn-cse/mn-name/myactuator/data"; | ||
+ | |||
+ | private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; | ||
+ | private static String appPoa = aeProtocol+"://"+aeIp+":"+aePort; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | HttpServer server = null; | ||
+ | try { | ||
+ | server = HttpServer.create(new InetSocketAddress(aePort), 0); | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | server.createContext("/", new MyHandler()); | ||
+ | server.setExecutor(Executors.newCachedThreadPool()); | ||
+ | server.start(); | ||
+ | |||
+ | JSONArray array = new JSONArray(); | ||
+ | array.put(appPoa); | ||
+ | JSONObject obj = new JSONObject(); | ||
+ | obj.put("rn", aeName); | ||
+ | obj.put("api", 12346); | ||
+ | obj.put("rr", true); | ||
+ | obj.put("poa",array); | ||
+ | JSONObject resource = new JSONObject(); | ||
+ | resource.put("m2m:ae", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); | ||
+ | |||
+ | array = new JSONArray(); | ||
+ | array.put("/"+cseId+"/"+cseName+"/"+aeName); | ||
+ | obj = new JSONObject(); | ||
+ | obj.put("nu", array); | ||
+ | obj.put("rn", subName); | ||
+ | obj.put("nct", 2); | ||
+ | resource = new JSONObject(); | ||
+ | resource.put("m2m:sub", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+targetSensorContainer, resource.toString(), 23); | ||
+ | } | ||
+ | |||
+ | static class MyHandler implements HttpHandler { | ||
+ | |||
+ | public void handle(HttpExchange httpExchange) { | ||
+ | System.out.println("Event Recieved!"); | ||
+ | |||
+ | try{ | ||
+ | InputStream in = httpExchange.getRequestBody(); | ||
+ | |||
+ | String requestBody = ""; | ||
+ | int i;char c; | ||
+ | while ((i = in.read()) != -1) { | ||
+ | c = (char) i; | ||
+ | requestBody = (String) (requestBody+c); | ||
+ | } | ||
+ | |||
+ | System.out.println(requestBody); | ||
+ | |||
+ | JSONObject json = new JSONObject(requestBody); | ||
+ | if (json.getJSONObject("m2m:sgn").has("vrq")) { | ||
+ | System.out.println("Confirm subscription"); | ||
+ | } else { | ||
+ | JSONObject rep = json.getJSONObject("m2m:sgn").getJSONObject("nev") | ||
+ | .getJSONObject("rep"); | ||
+ | int ty = rep.getInt("ty"); | ||
+ | System.out.println("Resource type: "+ty); | ||
+ | |||
+ | if (ty == 4) { | ||
+ | String con = rep.getString("con"); | ||
+ | int sensorValue = Integer.parseInt(con); | ||
+ | System.out.print("OBSERVATION: Sensor Value "+sensorValue); | ||
+ | |||
+ | boolean actuatorState; | ||
+ | if(sensorValue<250){ | ||
+ | System.out.println(" -> LOW"); | ||
+ | actuatorState=true; | ||
+ | |||
+ | }else{ | ||
+ | System.out.println(" -> HIGH"); | ||
+ | actuatorState=false; | ||
+ | } | ||
+ | System.out.println("ACTION: switch actuator state to "+actuatorState+"\n"); | ||
+ | |||
+ | JSONObject obj = new JSONObject(); | ||
+ | obj = new JSONObject(); | ||
+ | obj.put("cnf", "application/text"); | ||
+ | obj.put("con", actuatorState); | ||
+ | JSONObject resource = new JSONObject(); | ||
+ | resource.put("m2m:cin", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+targetActuatorContainer, resource.toString(), 4); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | String responseBudy =""; | ||
+ | byte[] out = responseBudy.getBytes("UTF-8"); | ||
+ | httpExchange.sendResponseHeaders(200, out.length); | ||
+ | OutputStream os = httpExchange.getResponseBody(); | ||
+ | os.write(out); | ||
+ | os.close(); | ||
+ | |||
+ | } catch(Exception e){ | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == MyMonitor Class == | ||
+ | <source lang="java"> | ||
+ | package org.eclipse.om2m.app; | ||
+ | |||
+ | import java.io.IOException; | ||
+ | import java.io.InputStream; | ||
+ | import java.io.OutputStream; | ||
+ | import java.net.InetSocketAddress; | ||
+ | import java.util.concurrent.Executors; | ||
+ | |||
+ | import org.json.JSONArray; | ||
+ | import org.json.JSONObject; | ||
+ | |||
+ | import com.sun.net.httpserver.HttpExchange; | ||
+ | import com.sun.net.httpserver.HttpHandler; | ||
+ | import com.sun.net.httpserver.HttpServer; | ||
+ | |||
+ | public class MyMonitor { | ||
+ | |||
+ | private static String originator="admin:admin"; | ||
+ | private static String cseProtocol="http"; | ||
+ | private static String cseIp = "127.0.0.1"; | ||
+ | private static int csePort = 8080; | ||
+ | private static String cseId = "in-cse"; | ||
+ | private static String cseName = "in-name"; | ||
+ | |||
+ | private static String aeMonitorName = "mymonitor"; | ||
+ | private static String aeProtocol="http"; | ||
+ | private static String aeIp = "127.0.0.1"; | ||
+ | private static int aePort = 1600; | ||
+ | private static String subName="monitorsub"; | ||
+ | private static String targetCse = "mn-cse/mn-name"; | ||
+ | |||
+ | private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; | ||
+ | private static String appPoa = aeProtocol+"://"+aeIp+":"+aePort; | ||
+ | |||
+ | private static JSONObject ae; | ||
+ | private static JSONObject sub; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | HttpServer server = null; | ||
+ | try { | ||
+ | server = HttpServer.create(new InetSocketAddress(aePort), 0); | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | server.createContext("/", new MyHandler()); | ||
+ | server.setExecutor(Executors.newCachedThreadPool()); | ||
+ | server.start(); | ||
+ | |||
+ | JSONArray array = new JSONArray(); | ||
+ | array.put(appPoa); | ||
+ | JSONObject obj = new JSONObject(); | ||
+ | obj.put("rn", aeMonitorName); | ||
+ | obj.put("api", 12346); | ||
+ | obj.put("rr", true); | ||
+ | obj.put("poa",array); | ||
+ | ae = new JSONObject(); | ||
+ | ae.put("m2m:ae", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, ae.toString(), 2); | ||
+ | |||
+ | array = new JSONArray(); | ||
+ | array.put("/"+cseId+"/"+cseName+"/"+aeMonitorName); | ||
+ | obj = new JSONObject(); | ||
+ | obj.put("nu", array); | ||
+ | obj.put("rn", subName); | ||
+ | obj.put("nct", 2); | ||
+ | sub = new JSONObject(); | ||
+ | sub.put("m2m:sub", obj); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+targetCse, sub.toString(), 23); | ||
+ | |||
+ | System.out.println("[INFO] Discover all containers in "+csePoa); | ||
+ | |||
+ | HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+targetCse+"?fu=1&ty=3"); | ||
+ | JSONObject result = new JSONObject(httpResponse.getBody()); | ||
+ | String[] uril = result.getString("m2m:uril").split(" "); | ||
+ | for (int i=0;i<uril.length;i++){ | ||
+ | RestHttpClient.post(originator, csePoa+"/~"+uril[i], sub.toString(), 23); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | static class MyHandler implements HttpHandler { | ||
+ | |||
+ | public void handle(HttpExchange httpExchange) { | ||
+ | System.out.println("Event Recieved!"); | ||
+ | |||
+ | try{ | ||
+ | InputStream in = httpExchange.getRequestBody(); | ||
+ | |||
+ | String requestBody = ""; | ||
+ | int i;char c; | ||
+ | while ((i = in.read()) != -1) { | ||
+ | c = (char) i; | ||
+ | requestBody = (String) (requestBody+c); | ||
+ | } | ||
+ | |||
+ | System.out.println(requestBody); | ||
+ | |||
+ | JSONObject json = new JSONObject(requestBody); | ||
+ | if (json.getJSONObject("m2m:sgn").has("vrq")) { | ||
+ | System.out.println("Confirm subscription"); | ||
+ | } else { | ||
+ | JSONObject rep = json.getJSONObject("m2m:sgn").getJSONObject("nev") | ||
+ | .getJSONObject("rep"); | ||
+ | int ty = rep.getInt("ty"); | ||
+ | System.out.println("Resource type: "+ty); | ||
+ | |||
+ | if (ty == 2) { | ||
+ | String aeName = rep.getString("rn"); | ||
+ | System.out.println("[INFO] New AE has been registred: "+aeName+"\n"); | ||
+ | System.out.println("[INFO] Wait 3 seconds\n"); | ||
+ | Thread.sleep(3000); | ||
+ | System.out.println("[INFO] Monitor AE "+aeName+" containers"); | ||
+ | HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+targetCse+"/"+aeName+"?rcn=5"); | ||
+ | JSONObject ae = new JSONObject(httpResponse.getBody()); | ||
+ | if(!ae.getJSONObject("m2m:ae").isNull("ch")){ | ||
+ | JSONArray aeChild = ae.getJSONObject("m2m:ae").getJSONArray("ch"); | ||
+ | System.out.println("[INFO] AE "+aeName+" has "+aeChild.length()+" child:"); | ||
+ | for (int j=0; j<aeChild.length(); j++){ | ||
+ | if(aeChild.getJSONObject(j).getInt("ty")==3){ | ||
+ | String cntName = aeChild.getJSONObject(j).getString("rn"); | ||
+ | System.out.println("[INFO] Container: "+cntName+"\n"); | ||
+ | RestHttpClient.post(originator, csePoa+"/~/"+targetCse+"/"+aeName+"/"+cntName, sub.toString(), 23); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }else if(ty == 4){ | ||
+ | String ciName = rep.getString("rn"); | ||
+ | String content = rep.getString("con"); | ||
+ | |||
+ | System.out.println("[INFO] New Content Instance "+ciName+" has been created"); | ||
+ | System.out.println("[INFO] Content: "+content); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | String responseBudy =""; | ||
+ | byte[] out = responseBudy.getBytes("UTF-8"); | ||
+ | httpExchange.sendResponseHeaders(200, out.length); | ||
+ | OutputStream os = httpExchange.getResponseBody(); | ||
+ | os.write(out); | ||
+ | os.close(); | ||
+ | |||
+ | } catch(Exception e){ | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
} | } | ||
} | } | ||
</source> | </source> |
Revision as of 18:52, 13 November 2016
Before doing this tutorial you have to clone and build OM2M using Eclipse
Contents
Develop standalone oneM2M applications
Create a new Maven project
- Open Eclipse IDE
- Got to File -> New -> other -> Maven Project
- Select "Create a simple Project (skip archetype selection)" then click Next
- Enter "org.eclipse.om2m" as Group Id and "org.eclipse.om2m.app" as "Artifact Id" then click on Finish
- Create package "org.eclipse.om2m.app" inside the folder src/main/java
Configure project dependencies
- Add dependencies for HTTP and JSON to your project by adding the following section to your pom.xml file inside the <project> tag.
<dependencies> <dependency> <groupId>com.sun.net.httpserver</groupId> <artifactId>http</artifactId> <version>20070405</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160212</version> </dependency> </dependencies>
RestHttpClient class
package org.eclipse.om2m.app; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.*; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class RestHttpClient { public static HttpResponse get(String originator, String uri) { System.out.println("HTTP GET "+uri); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet= new HttpGet(uri); httpGet.addHeader("X-M2M-Origin",originator); httpGet.addHeader("Accept","application/json"); HttpResponse httpResponse = new HttpResponse(); try { CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpGet); try{ httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode()); httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity())); }finally{ closeableHttpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody()); return httpResponse; } public static HttpResponse post(String originator, String uri, String body, int ty) { System.out.println("HTTP POST "+uri+"\n"+body); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(uri); httpPost.addHeader("X-M2M-Origin",originator); httpPost.addHeader("Accept","application/json"); httpPost.addHeader("Content-Type","application/json;ty="+ty); HttpResponse httpResponse = new HttpResponse(); try { CloseableHttpResponse closeableHttpResponse=null; try{ httpPost.setEntity(new StringEntity(body)); closeableHttpResponse = httpclient.execute(httpPost); httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode()); httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity())); }finally{ closeableHttpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody()); return httpResponse ; } }
HttpResponse class
package org.eclipse.om2m.app; public class HttpResponse { private int statusCode; private String body; public int getStatusCode() { return statusCode; } public void setStatusCode(int statusCode) { this.statusCode = statusCode; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } }
MySensor Class
package org.eclipse.om2m.app; import org.json.JSONObject; public class MySensor { public static int sensorValue; private static String originator="admin:admin"; private static String cseProtocol="http"; private static String cseIp = "127.0.0.1"; private static int csePort = 8282; private static String cseId = "mn-cse"; private static String cseName = "mn-name"; private static String aeName = "mysensor"; private static String cntName = "data"; private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("rn", aeName); obj.put("api", 12345); obj.put("rr", false); JSONObject resource = new JSONObject(); resource.put("m2m:ae", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); obj = new JSONObject(); obj.put("rn", cntName); resource = new JSONObject(); resource.put("m2m:cnt", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 3); while (true){ obj = new JSONObject(); obj.put("cnf", "application/text"); obj.put("con", getSensorValue()); resource = new JSONObject(); resource.put("m2m:cin", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 4); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static int getSensorValue(){ sensorValue = (int)(Math.random()*500); System.out.println("Sensor value = "+sensorValue); return sensorValue; } }
MyActuator Class
package org.eclipse.om2m.app; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.json.JSONArray; import org.json.JSONObject; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class MyActuator { private static boolean actuatorValue; private static String originator="admin:admin"; private static String cseProtocol="http"; private static String cseIp = "127.0.0.1"; private static int csePort = 8282; private static String cseId = "mn-cse"; private static String cseName = "mn-name"; private static String aeName = "myactuator"; private static String cntName = "data"; private static String aeProtocol="http"; private static String aeIp = "127.0.0.1"; private static int aePort = 1400; private static String subName="actuatorsub"; private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; private static String appPoa = aeProtocol+"://"+aeIp+":"+aePort; public static void main(String[] args) { HttpServer server = null; try { server = HttpServer.create(new InetSocketAddress(aePort), 0); } catch (IOException e) { e.printStackTrace(); } server.createContext("/", new MyHandler()); server.setExecutor(Executors.newCachedThreadPool()); server.start(); JSONArray array = new JSONArray(); array.put(appPoa); JSONObject obj = new JSONObject(); obj.put("rn", aeName); obj.put("api", 12345); obj.put("rr", true); obj.put("poa",array); JSONObject resource = new JSONObject(); resource.put("m2m:ae", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); obj = new JSONObject(); obj.put("rn", cntName); resource = new JSONObject(); resource.put("m2m:cnt", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 3); array = new JSONArray(); array.put("/"+cseId+"/"+cseName+"/"+aeName); obj = new JSONObject(); obj.put("nu", array); obj.put("rn", subName); obj.put("nct", 2); resource = new JSONObject(); resource.put("m2m:sub", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 23); } static class MyHandler implements HttpHandler { public void handle(HttpExchange httpExchange) { System.out.println("Event Recieved!"); try{ InputStream in = httpExchange.getRequestBody(); String requestBody = ""; int i;char c; while ((i = in.read()) != -1) { c = (char) i; requestBody = (String) (requestBody+c); } System.out.println(requestBody); JSONObject json = new JSONObject(requestBody); if (json.getJSONObject("m2m:sgn").has("vrq")) { System.out.println("Confirm subscription"); } else { JSONObject rep = json.getJSONObject("m2m:sgn").getJSONObject("nev") .getJSONObject("rep"); int ty = rep.getInt("ty"); System.out.println("Resource type: "+ty); if (ty == 4) { String con = rep.getString("con"); System.out.println("Actuator state = "+con); setActuatorValue(Boolean.parseBoolean(con)); } } String responseBudy =""; byte[] out = responseBudy.getBytes("UTF-8"); httpExchange.sendResponseHeaders(200, out.length); OutputStream os = httpExchange.getResponseBody(); os.write(out); os.close(); } catch(Exception e){ e.printStackTrace(); } } } public static void setActuatorValue(boolean actuatorValue) { MyActuator.actuatorValue = actuatorValue; } }
MyManager Class
package org.eclipse.om2m.app; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.json.JSONArray; import org.json.JSONObject; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class MyManager { private static String originator="admin:admin"; private static String cseProtocol="http"; private static String cseIp = "127.0.0.1"; private static int csePort = 8080; private static String cseId = "in-cse"; private static String cseName = "in-name"; private static String aeName = "mymanager"; private static String aeProtocol="http"; private static String aeIp = "127.0.0.1"; private static int aePort = 1500; private static String subName="managersub"; private static String targetSensorContainer="mn-cse/mn-name/mysensor/data"; private static String targetActuatorContainer="mn-cse/mn-name/myactuator/data"; private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; private static String appPoa = aeProtocol+"://"+aeIp+":"+aePort; public static void main(String[] args) { HttpServer server = null; try { server = HttpServer.create(new InetSocketAddress(aePort), 0); } catch (IOException e) { e.printStackTrace(); } server.createContext("/", new MyHandler()); server.setExecutor(Executors.newCachedThreadPool()); server.start(); JSONArray array = new JSONArray(); array.put(appPoa); JSONObject obj = new JSONObject(); obj.put("rn", aeName); obj.put("api", 12346); obj.put("rr", true); obj.put("poa",array); JSONObject resource = new JSONObject(); resource.put("m2m:ae", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); array = new JSONArray(); array.put("/"+cseId+"/"+cseName+"/"+aeName); obj = new JSONObject(); obj.put("nu", array); obj.put("rn", subName); obj.put("nct", 2); resource = new JSONObject(); resource.put("m2m:sub", obj); RestHttpClient.post(originator, csePoa+"/~/"+targetSensorContainer, resource.toString(), 23); } static class MyHandler implements HttpHandler { public void handle(HttpExchange httpExchange) { System.out.println("Event Recieved!"); try{ InputStream in = httpExchange.getRequestBody(); String requestBody = ""; int i;char c; while ((i = in.read()) != -1) { c = (char) i; requestBody = (String) (requestBody+c); } System.out.println(requestBody); JSONObject json = new JSONObject(requestBody); if (json.getJSONObject("m2m:sgn").has("vrq")) { System.out.println("Confirm subscription"); } else { JSONObject rep = json.getJSONObject("m2m:sgn").getJSONObject("nev") .getJSONObject("rep"); int ty = rep.getInt("ty"); System.out.println("Resource type: "+ty); if (ty == 4) { String con = rep.getString("con"); int sensorValue = Integer.parseInt(con); System.out.print("OBSERVATION: Sensor Value "+sensorValue); boolean actuatorState; if(sensorValue<250){ System.out.println(" -> LOW"); actuatorState=true; }else{ System.out.println(" -> HIGH"); actuatorState=false; } System.out.println("ACTION: switch actuator state to "+actuatorState+"\n"); JSONObject obj = new JSONObject(); obj = new JSONObject(); obj.put("cnf", "application/text"); obj.put("con", actuatorState); JSONObject resource = new JSONObject(); resource.put("m2m:cin", obj); RestHttpClient.post(originator, csePoa+"/~/"+targetActuatorContainer, resource.toString(), 4); } } String responseBudy =""; byte[] out = responseBudy.getBytes("UTF-8"); httpExchange.sendResponseHeaders(200, out.length); OutputStream os = httpExchange.getResponseBody(); os.write(out); os.close(); } catch(Exception e){ e.printStackTrace(); } } } }
MyMonitor Class
package org.eclipse.om2m.app; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.json.JSONArray; import org.json.JSONObject; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class MyMonitor { private static String originator="admin:admin"; private static String cseProtocol="http"; private static String cseIp = "127.0.0.1"; private static int csePort = 8080; private static String cseId = "in-cse"; private static String cseName = "in-name"; private static String aeMonitorName = "mymonitor"; private static String aeProtocol="http"; private static String aeIp = "127.0.0.1"; private static int aePort = 1600; private static String subName="monitorsub"; private static String targetCse = "mn-cse/mn-name"; private static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; private static String appPoa = aeProtocol+"://"+aeIp+":"+aePort; private static JSONObject ae; private static JSONObject sub; public static void main(String[] args) { HttpServer server = null; try { server = HttpServer.create(new InetSocketAddress(aePort), 0); } catch (IOException e) { e.printStackTrace(); } server.createContext("/", new MyHandler()); server.setExecutor(Executors.newCachedThreadPool()); server.start(); JSONArray array = new JSONArray(); array.put(appPoa); JSONObject obj = new JSONObject(); obj.put("rn", aeMonitorName); obj.put("api", 12346); obj.put("rr", true); obj.put("poa",array); ae = new JSONObject(); ae.put("m2m:ae", obj); RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, ae.toString(), 2); array = new JSONArray(); array.put("/"+cseId+"/"+cseName+"/"+aeMonitorName); obj = new JSONObject(); obj.put("nu", array); obj.put("rn", subName); obj.put("nct", 2); sub = new JSONObject(); sub.put("m2m:sub", obj); RestHttpClient.post(originator, csePoa+"/~/"+targetCse, sub.toString(), 23); System.out.println("[INFO] Discover all containers in "+csePoa); HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+targetCse+"?fu=1&ty=3"); JSONObject result = new JSONObject(httpResponse.getBody()); String[] uril = result.getString("m2m:uril").split(" "); for (int i=0;i<uril.length;i++){ RestHttpClient.post(originator, csePoa+"/~"+uril[i], sub.toString(), 23); } } static class MyHandler implements HttpHandler { public void handle(HttpExchange httpExchange) { System.out.println("Event Recieved!"); try{ InputStream in = httpExchange.getRequestBody(); String requestBody = ""; int i;char c; while ((i = in.read()) != -1) { c = (char) i; requestBody = (String) (requestBody+c); } System.out.println(requestBody); JSONObject json = new JSONObject(requestBody); if (json.getJSONObject("m2m:sgn").has("vrq")) { System.out.println("Confirm subscription"); } else { JSONObject rep = json.getJSONObject("m2m:sgn").getJSONObject("nev") .getJSONObject("rep"); int ty = rep.getInt("ty"); System.out.println("Resource type: "+ty); if (ty == 2) { String aeName = rep.getString("rn"); System.out.println("[INFO] New AE has been registred: "+aeName+"\n"); System.out.println("[INFO] Wait 3 seconds\n"); Thread.sleep(3000); System.out.println("[INFO] Monitor AE "+aeName+" containers"); HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+targetCse+"/"+aeName+"?rcn=5"); JSONObject ae = new JSONObject(httpResponse.getBody()); if(!ae.getJSONObject("m2m:ae").isNull("ch")){ JSONArray aeChild = ae.getJSONObject("m2m:ae").getJSONArray("ch"); System.out.println("[INFO] AE "+aeName+" has "+aeChild.length()+" child:"); for (int j=0; j<aeChild.length(); j++){ if(aeChild.getJSONObject(j).getInt("ty")==3){ String cntName = aeChild.getJSONObject(j).getString("rn"); System.out.println("[INFO] Container: "+cntName+"\n"); RestHttpClient.post(originator, csePoa+"/~/"+targetCse+"/"+aeName+"/"+cntName, sub.toString(), 23); } } } }else if(ty == 4){ String ciName = rep.getString("rn"); String content = rep.getString("con"); System.out.println("[INFO] New Content Instance "+ciName+" has been created"); System.out.println("[INFO] Content: "+content); } } String responseBudy =""; byte[] out = responseBudy.getBytes("UTF-8"); httpExchange.sendResponseHeaders(200, out.length); OutputStream os = httpExchange.getResponseBody(); os.write(out); os.close(); } catch(Exception e){ e.printStackTrace(); } } } }