Sensor API: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Goals ==
Proposal for a new Sensor API. A generic API to retrieve values from sensors.  
Proposal for a new Sensor API. A generic API to retrieve values from sensors.  
The Sensor API allows to retrieve values from sensors available from the device. It is intended to be compatible with existing concrete Web APIs that provide values from device sensors (http://dev.w3.org/geo/api/spec-source-orientation.html). 


Author: José Manuel Cantera (Telefónica I+D)  
Author: José Manuel Cantera (Telefónica I+D)  
Line 5: Line 9:
mailto:jmcf@tid.es
mailto:jmcf@tid.es


The Sensor API allows to retrieve values from sensors available from the device. The API is intended to be compatible with existing concrete Web APIs that provide values from device sensors (http://dev.w3.org/geo/api/spec-source-orientation.html). 
== Status ==


First draft provided


== Proposed API ==
<pre>
interface SensorManager : EventTarget {
interface SensorManager : EventTarget {
    // Creates a new session to interact with a sensor
    // Creates a new session to interact with a sensor
     SensorSession newSession(in DOMString sensorName,in optional SensorSessionOptions options) raises SensorNotFound;
     SensorSession newSession(in DOMString sensorName,in optional SensorSessionOptions options)  
    // The list of open sessions
                                                raises SensorNotFound;  
    // The list of open sessions
     attribute sequence<SensorSession> openSessions;
     attribute sequence<SensorSession> openSessions;
     sequence<Sensor> listSensors(); // List the available sensors on the device
     sequence<Sensor> listSensors(); // List the available sensors on the device
     attribute Function onSensorAvailable;
     attribute Function onSensorAvailable;
}
};


interface Sensor : EventTarget {
interface Sensor : EventTarget {
readonly attribute DOMString type;           // Vocabulary to be defined ("Acceleration", "AmbientLight")
readonly attribute DOMString type;     // Vocabulary to be defined ("Acceleration", "AmbientLight")
readonly attribute DOMString name;           // ("Accelerometer", "Compass", "Thermometer")
readonly attribute DOMString name;   // ("Accelerometer", "Compass", "Thermometer")  
// Another method to retrieve capabilities??
// Another method to retrieve capabilities??
}
};


interface SensorSession : EventTarget {
interface SensorSession : EventTarget {
     readonly attribute Sensor sensor;
     readonly attribute Sensor sensor;
     // Allows to watch (monitor) for the sensor  
     // Allows to watch (monitor) for the sensor  
     void watch(in SensorWatchOptions options);
     void watch(in SensorWatchOptions options);
     // Finish monitoring session
     // Finish monitoring session
     void endWatch();
     void endWatch();
   
     // Reads the value of a sensor
     // Reads the value of a sensor
     void read();
     void read();
     // Close the session with the sensor (endWatch is called if necessary)
     // Close the session with the sensor (endWatch is called if necessary)
     void close();
     void close();
     readonly attribute DOMString status; // ("watching", "closed", ...)
     readonly attribute DOMString status; // ("watching", "closed", ...)
     attribute Function onSensorData;
     attribute Function onSensorData;
     attribute Function onError;
     attribute Function onError;
}
};


interface SensorData {
interface SensorData {
     readonly attribute any value;
     readonly attribute any value;
}
};


// New DOM Event 'sensordata'
// New DOM Event 'sensordata'
Line 55: Line 58:
                                 in boolean cancelable,
                                 in boolean cancelable,
                                 in SensorData data);
                                 in SensorData data);
}
};


interface SensorWatchOptions {
interface SensorWatchOptions {
    attribute  any highThreshold; // High threshold. If the sensor value is higher a data event will be raised
  // High threshold. If the sensor value is higher a data event will be raised
     attribute  any lowThreshold; // Low threshold. If the sensor value is lower a data event will be raised
     attribute  any highThreshold;  
     attribute double relativeThreshold; // Notify only when a relative change in the magnitude meausured by the sensor occurs  
    // Low threshold. If the sensor value is lower a data event will be raised
     attribute double interval; // Interval at which values will be provided by the sensor (milliseconds)
     attribute any lowThreshold;
}
    // Notify only when a relative change in the magnitude meausured by the sensor occurs  
     attribute double relativeThreshold;  
    // Interval at which values will be provided by the sensor (milliseconds)
    attribute double interval;
};
 
</pre>


Example of use
Example of use


var accelerometer = navigator.sensor.newSession('Accelerometer')
<pre>
var accelerometer = navigator.sensor.newSession('Accelerometer');
session.onSensorData = function(e) {
session.onSensorData = function(e) {
   window.console.log('New accelerometer data');
   window.console.log('New accelerometer data');
   window.console.log('Acceleration along the x axis: ' + e.value.x);
   window.console.log('Acceleration along the x axis: ' + e.value.x);
}
};
 
var watchOptions =  { interval: 1.0 }; // Every one millisecond
var watchOptions =  { interval = 1.0 } // Every one millisecond
accelerometer.watch(watchOptions);
session.watch(watchOptions);
</pre>

Latest revision as of 15:06, 9 September 2011

Goals

Proposal for a new Sensor API. A generic API to retrieve values from sensors.

The Sensor API allows to retrieve values from sensors available from the device. It is intended to be compatible with existing concrete Web APIs that provide values from device sensors (http://dev.w3.org/geo/api/spec-source-orientation.html).

Author: José Manuel Cantera (Telefónica I+D)

mailto:jmcf@tid.es

Status

First draft provided

Proposed API

interface SensorManager : EventTarget {
    // Creates a new session to interact with a sensor
     SensorSession newSession(in DOMString sensorName,in optional SensorSessionOptions options) 
                                                raises SensorNotFound;   
    // The list of open sessions
     attribute sequence<SensorSession> openSessions;
     sequence<Sensor> listSensors(); // List the available sensors on the device
     attribute Function onSensorAvailable;
};

interface Sensor : EventTarget {
	readonly attribute DOMString type;     // Vocabulary to be defined ("Acceleration", "AmbientLight")
	readonly attribute DOMString name;   // ("Accelerometer", "Compass", "Thermometer")   
	// Another method to retrieve capabilities??
};

interface SensorSession : EventTarget {
     readonly attribute Sensor sensor;
     // Allows to watch (monitor) for the sensor 
     void watch(in SensorWatchOptions options);
     // Finish monitoring session
     void endWatch();
     // Reads the value of a sensor
     void read();
     // Close the session with the sensor (endWatch is called if necessary)
     void close();
     readonly attribute DOMString status; // ("watching", "closed", ...)
     attribute Function onSensorData;
     attribute Function onError;
};

interface SensorData {
     readonly attribute any value;
};

// New DOM Event 'sensordata'
interface SensorDataEvent : Event {
     readonly attribute SensorData data;
     void initSensorDataEvent(in DOMString type,
                                in boolean bubbles,
                                in boolean cancelable,
                                in SensorData data);
};

interface SensorWatchOptions {
   // High threshold. If the sensor value is higher a data event will be raised
     attribute  any highThreshold; 
    // Low threshold. If the sensor value is lower a data event will be raised
     attribute  any lowThreshold;  
     // Notify only when a relative change in the magnitude meausured by the sensor occurs 
     attribute double relativeThreshold; 
    // Interval at which values will be provided by the sensor (milliseconds)
     attribute double interval; 
};

Example of use

var accelerometer = navigator.sensor.newSession('Accelerometer');
session.onSensorData = function(e) {
   window.console.log('New accelerometer data');
   window.console.log('Acceleration along the x axis: ' + e.value.x);
};
var watchOptions =  { interval: 1.0 }; // Every one millisecond
accelerometer.watch(watchOptions);