Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSGi bundle easing the access to REST API of the M2M contest server.
- Loading branch information
Benjamin Cabé
authored and
Benjamin Cabé
committed
Oct 22, 2011
1 parent
a73fc8b
commit 7ba1b0c
Showing
32 changed files
with
574 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
.DS_Store | ||
|
||
bin/ |
13 changes: 13 additions & 0 deletions
13
samples/osgi/org.eclipsecon.m2mcontest.connector/.classpath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry exported="true" kind="lib" path="lib/asm-3.1.jar"/> | ||
<classpathentry exported="true" kind="lib" path="lib/jersey-client-1.9.1.jar" sourcepath="/Users/kartben/.m2/repository/com/sun/jersey/jersey-client/1.9.1/jersey-client-1.9.1-sources.jar"/> | ||
<classpathentry exported="true" kind="lib" path="lib/jersey-core-1.9.1.jar" sourcepath="/Users/kartben/.m2/repository/com/sun/jersey/jersey-core/1.9.1/jersey-core-1.9.1-sources.jar"/> | ||
<classpathentry exported="true" kind="lib" path="lib/jersey-json-1.9.1.jar" sourcepath="/Users/kartben/.m2/repository/com/sun/jersey/jersey-json/1.9.1/jersey-json-1.9.1-sources.jar"/> | ||
<classpathentry exported="true" kind="lib" path="lib/jettison-1.1.jar" sourcepath="/Users/kartben/.m2/repository/org/codehaus/jettison/jettison/1.1/jettison-1.1-sources.jar"/> | ||
<classpathentry exported="true" kind="lib" path="lib/jsr311-api-1.1.1.jar"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>org.eclipsecon.m2mcontest.connector</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.pde.ManifestBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.pde.SchemaBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.pde.ds.core.builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.pde.PluginNature</nature> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
8 changes: 8 additions & 0 deletions
8
samples/osgi/org.eclipsecon.m2mcontest.connector/.settings/org.eclipse.jdt.core.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#Sat Oct 22 17:34:41 CEST 2011 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
5 changes: 5 additions & 0 deletions
5
samples/osgi/org.eclipsecon.m2mcontest.connector/.settings/org.eclipse.pde.core.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#Sat Oct 22 17:34:43 CEST 2011 | ||
eclipse.preferences.version=1 | ||
pluginProject.equinox=false | ||
pluginProject.extensions=false | ||
resolve.requirebundle=false |
17 changes: 17 additions & 0 deletions
17
samples/osgi/org.eclipsecon.m2mcontest.connector/META-INF/MANIFEST.MF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Manifest-Version: 1.0 | ||
Bundle-ManifestVersion: 2 | ||
Bundle-Name: M2M contest data connector | ||
Bundle-SymbolicName: org.eclipsecon.m2mcontest.connector | ||
Bundle-Version: 0.1.0.qualifier | ||
Import-Package: org.osgi.framework;version="1.3.0", | ||
org.osgi.service.component;version="1.1.0", | ||
org.osgi.service.event;version="1.2.0" | ||
Service-Component: OSGI-INF/*.xml | ||
Bundle-RequiredExecutionEnvironment: JavaSE-1.6 | ||
Bundle-ClassPath: lib/asm-3.1.jar, | ||
lib/jersey-client-1.9.1.jar, | ||
lib/jersey-core-1.9.1.jar, | ||
lib/jersey-json-1.9.1.jar, | ||
lib/jettison-1.1.jar, | ||
lib/jsr311-api-1.1.1.jar, | ||
. |
7 changes: 7 additions & 0 deletions
7
samples/osgi/org.eclipsecon.m2mcontest.connector/OSGI-INF/monitorComponent.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipsecon.m2mcontest.connector.monitor"> | ||
<implementation class="org.eclipsecon.m2mcontest.connector.internal.DataMonitorComponent"/> | ||
<reference bind="setServerAccessor" cardinality="1..1" interface="org.eclipsecon.m2mcontest.connector.IM2MServerAccessor" name="IM2MServerAccessor" policy="static"/> | ||
<reference bind="setEventAdmin" cardinality="0..1" interface="org.osgi.service.event.EventAdmin" name="EventAdmin" policy="static"/> | ||
<property name="pollingInterval" type="Integer" value="2"/> | ||
</scr:component> |
7 changes: 7 additions & 0 deletions
7
samples/osgi/org.eclipsecon.m2mcontest.connector/OSGI-INF/serverAccessorComponent.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipsecon.m2mcontest.connector.accessor"> | ||
<implementation class="org.eclipsecon.m2mcontest.connector.internal.M2MServerAccessor"/> | ||
<service> | ||
<provide interface="org.eclipsecon.m2mcontest.connector.IM2MServerAccessor"/> | ||
</service> | ||
</scr:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Use this bundle to quickly connect your OSGi application to the EclipseCon Europe M2M server! | ||
|
||
* The bundle exposes an IM2MServerAccessor service which does most of the HTTP/REST/JSON stuff for you, and allows you to manipulate query results in your Java code. | ||
* A OSGi Event is posted as soon a new value is available for a sensor. The event topic is m2m/sensor/SENSOR_NAME, the event message is the value, and its timestamp is the time at which the data was acquired |
Binary file added
BIN
+313 Bytes
...on.m2mcontest.connector/bin/org/eclipsecon/m2mcontest/connector/M2MContestConstants.class
Binary file not shown.
11 changes: 11 additions & 0 deletions
11
samples/osgi/org.eclipsecon.m2mcontest.connector/build.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source.. = src/ | ||
output.. = bin/ | ||
bin.includes = META-INF/,\ | ||
.,\ | ||
OSGI-INF/,\ | ||
lib/asm-3.1.jar,\ | ||
lib/jersey-client-1.9.1.jar,\ | ||
lib/jersey-core-1.9.1.jar,\ | ||
lib/jersey-json-1.9.1.jar,\ | ||
lib/jettison-1.1.jar,\ | ||
lib/jsr311-api-1.1.1.jar |
Binary file not shown.
Binary file added
BIN
+127 KB
samples/osgi/org.eclipsecon.m2mcontest.connector/lib/jersey-client-1.9.1.jar
Binary file not shown.
Binary file added
BIN
+449 KB
samples/osgi/org.eclipsecon.m2mcontest.connector/lib/jersey-core-1.9.1.jar
Binary file not shown.
Binary file added
BIN
+144 KB
samples/osgi/org.eclipsecon.m2mcontest.connector/lib/jersey-json-1.9.1.jar
Binary file not shown.
Binary file added
BIN
+66.2 KB
samples/osgi/org.eclipsecon.m2mcontest.connector/lib/jettison-1.1.jar
Binary file not shown.
Binary file added
BIN
+45.3 KB
samples/osgi/org.eclipsecon.m2mcontest.connector/lib/jsr311-api-1.1.1.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions
20
...con.m2mcontest.connector/src/org/eclipsecon/m2mcontest/connector/CONSOLIDATION_RANGE.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2011 Sierra Wireless All rights reserved. This program and the | ||
* accompanying materials are made available under the terms of the Eclipse | ||
* Public License v1.0 which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: Benjamin CabŽ (Sierra Wireless) - initial API and | ||
* implementation | ||
*******************************************************************************/ | ||
package org.eclipsecon.m2mcontest.connector; | ||
|
||
public enum CONSOLIDATION_RANGE { | ||
ONE_MIN("OneMinute"), FIVE_MIN("FiveMinute"), ONE_HOUR("OneHour"); | ||
|
||
public String collectionSuffix; | ||
|
||
private CONSOLIDATION_RANGE(String suffix) { | ||
collectionSuffix = suffix; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...econ.m2mcontest.connector/src/org/eclipsecon/m2mcontest/connector/IM2MServerAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2011 Sierra Wireless All rights reserved. This program and the | ||
* accompanying materials are made available under the terms of the Eclipse | ||
* Public License v1.0 which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: Benjamin CabŽ (Sierra Wireless) - initial API and | ||
* implementation | ||
*******************************************************************************/ | ||
package org.eclipsecon.m2mcontest.connector; | ||
|
||
import org.eclipsecon.m2mcontest.connector.resources.consolidatedData.ConsolidatedData; | ||
import org.eclipsecon.m2mcontest.connector.resources.data.Data; | ||
import org.eclipsecon.m2mcontest.connector.resources.info.Info; | ||
|
||
public interface IM2MServerAccessor { | ||
public abstract Data getSensorData(String criteria, String sort); | ||
|
||
public abstract Data getSensorData(String criteria, String sort, | ||
int maxResults); | ||
|
||
public abstract ConsolidatedData getSensorConsolidatedData( | ||
CONSOLIDATION_RANGE range, String criteria, String sort); | ||
|
||
public abstract ConsolidatedData getSensorConsolidatedData( | ||
CONSOLIDATION_RANGE range, String criteria, String sort, | ||
int maxResults); | ||
|
||
public abstract Info getSensorInfo(String criteria, String sort); | ||
|
||
public abstract Info getSensorInfo(String criteria, String sort, | ||
int maxResults); | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...con.m2mcontest.connector/src/org/eclipsecon/m2mcontest/connector/M2MContestConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2011 Sierra Wireless All rights reserved. This program and the | ||
* accompanying materials are made available under the terms of the Eclipse | ||
* Public License v1.0 which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: Benjamin CabŽ (Sierra Wireless) - initial API and | ||
* implementation | ||
*******************************************************************************/ | ||
package org.eclipsecon.m2mcontest.connector; | ||
|
||
public interface M2MContestConstants { | ||
|
||
static final String BASE_URL = "http://m2mcontest.eclipsecon.org/rest_api/"; | ||
static final String BASE_TOPIC = "m2m/sensor/"; | ||
|
||
} |
103 changes: 103 additions & 0 deletions
103
...test.connector/src/org/eclipsecon/m2mcontest/connector/internal/DataMonitorComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2011 Sierra Wireless All rights reserved. This program and the | ||
* accompanying materials are made available under the terms of the Eclipse | ||
* Public License v1.0 which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: Benjamin CabŽ (Sierra Wireless) - initial API and | ||
* implementation | ||
*******************************************************************************/ | ||
package org.eclipsecon.m2mcontest.connector.internal; | ||
|
||
import java.util.Date; | ||
import java.util.Dictionary; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
import org.eclipsecon.m2mcontest.connector.IM2MServerAccessor; | ||
import org.eclipsecon.m2mcontest.connector.M2MContestConstants; | ||
import org.eclipsecon.m2mcontest.connector.resources.data.Data; | ||
import org.eclipsecon.m2mcontest.connector.resources.info.Info; | ||
import org.eclipsecon.m2mcontest.connector.resources.info.SensorInfo; | ||
import org.osgi.service.component.ComponentContext; | ||
import org.osgi.service.event.Event; | ||
import org.osgi.service.event.EventAdmin; | ||
import org.osgi.service.event.EventConstants; | ||
|
||
/** | ||
* Monitor the arrival of new data on the M2M server.<br> | ||
* Post an {@link Event} with the following format: | ||
* <code>m2m/sensor/SENSOR_ID</code> when it happens.<br> | ||
* The following event properties are set: | ||
* <ul> | ||
* <li> {@link EventConstants#MESSAGE}: the value</li> | ||
* <li>{@link EventConstants#TIMESTAMP}: the timestamp of the value</li> | ||
* </ul> | ||
*/ | ||
public class DataMonitorComponent { | ||
// sensorId --> lastValueTimestamp | ||
private Map<String, Date> _monitoredValues = new HashMap<String, Date>(); | ||
private IM2MServerAccessor _serverAccessor; | ||
private EventAdmin _eventAdmin; | ||
|
||
protected void activate(ComponentContext cctx) { | ||
Integer pollingInterval = (Integer) cctx.getProperties().get( | ||
"pollingInterval"); | ||
if (pollingInterval == null) | ||
pollingInterval = 10; | ||
// retrieve the list of sensors declared on the server | ||
Info info = _serverAccessor.getSensorInfo(null, null, 50); | ||
for (SensorInfo i : info.results) { | ||
_monitoredValues.put(i.sensor, null); | ||
|
||
Timer timer = new Timer(); | ||
timer.schedule(new DataMonitor(i.sensor), 0, pollingInterval * 1000); | ||
} | ||
} | ||
|
||
private class DataMonitor extends TimerTask { | ||
private String _sensor; | ||
|
||
/** | ||
* @param sensor | ||
* the id of the sensor to monitor | ||
*/ | ||
public DataMonitor(String sensor) { | ||
_sensor = sensor; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void run() { | ||
Data data = _serverAccessor.getSensorData("{\"sensor\":\"" | ||
+ _sensor + "\"}", "{\"_id\":-1}", 1); | ||
|
||
Date newestDate = data.results.get(0).getAcquisitionDate(); | ||
Float newestValue = data.results.get(0).value; | ||
|
||
Date previousDate = _monitoredValues.get(_sensor); | ||
if (previousDate == null || newestDate.after(previousDate)) { | ||
_monitoredValues.put(_sensor, newestDate); | ||
if (_eventAdmin != null) { | ||
@SuppressWarnings("rawtypes") | ||
Dictionary p = new Properties(); | ||
p.put(EventConstants.MESSAGE, newestValue); | ||
p.put(EventConstants.TIMESTAMP, newestDate.getTime()); | ||
_eventAdmin.postEvent(new Event( | ||
M2MContestConstants.BASE_TOPIC + _sensor, p)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected void setServerAccessor(IM2MServerAccessor serverAccessor) { | ||
_serverAccessor = serverAccessor; | ||
} | ||
|
||
protected void setEventAdmin(EventAdmin eventAdmin) { | ||
_eventAdmin = eventAdmin; | ||
} | ||
} |
Oops, something went wrong.