Skip to content

Commit

Permalink
OSGi bundle easing the access to REST API of the M2M contest server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Cabé authored and Benjamin Cabé committed Oct 22, 2011
1 parent a73fc8b commit 7ba1b0c
Show file tree
Hide file tree
Showing 32 changed files with 574 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,2 +1,2 @@
.DS_Store

bin/
13 changes: 13 additions & 0 deletions samples/osgi/org.eclipsecon.m2mcontest.connector/.classpath
@@ -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>
33 changes: 33 additions & 0 deletions samples/osgi/org.eclipsecon.m2mcontest.connector/.project
@@ -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>
@@ -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
@@ -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
@@ -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,
.
@@ -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>
@@ -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>
4 changes: 4 additions & 0 deletions samples/osgi/org.eclipsecon.m2mcontest.connector/README.md
@@ -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 not shown.
11 changes: 11 additions & 0 deletions samples/osgi/org.eclipsecon.m2mcontest.connector/build.properties
@@ -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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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;
}
}
@@ -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);

}
@@ -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/";

}
@@ -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;
}
}

0 comments on commit 7ba1b0c

Please sign in to comment.