From: Piotr Kosko Date: Mon, 16 Feb 2015 12:49:01 +0000 (+0100) Subject: [Sensor] Refactoring of code X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~425 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=df8ff31cad4d99216f20c47e7ebc6fd32164f97f;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Sensor] Refactoring of code [Feature] Changed JS listener structure and C++ SensorData class added. [Verification] Code compiles. All features work fine. Change-Id: I0d11e3f2f9def07e928473c9c6ccee95393c7bfd Signed-off-by: Piotr Kosko --- diff --git a/src/sensor/sensor_api.js b/src/sensor/sensor_api.js index b179bddc..7becb735 100644 --- a/src/sensor/sensor_api.js +++ b/src/sensor/sensor_api.js @@ -30,29 +30,110 @@ var MagneticSensorAccuracy = { VERYGOOD : 'ACCURACY_VERYGOOD' }; -var _supportedSensors = []; -var _startedSensors = { - LIGHT : false, - MAGNETIC : false, - PRESSURE : false, - PROXIMITY : false, - ULTRAVIOLET : false +// helper class for sensor listeners +var SensorListener = function (type, constructor) { + this.sensorType = type; + this.isStarted = false; + this.callback = undefined; + this.constructor = constructor; +}; + +SensorListener.prototype.tryCall = function (object) { + if (this.callback) { + this.callback(new this.constructor(object)); + } +}; + +SensorListener.prototype.start = function (successCallback, errorCallback) { + if (!this.isStarted) { + // sensor not started + var thisObject = this; + native_.call('Sensor_start', {'sensorType' : thisObject.sensorType}, + function(result) { + if (native_.isFailure(result)) { + if(!T_.isNullOrUndefined(errorCallback)) { + errorCallback(native_.getErrorObject(result)); + } + } else { + thisObject.isStarted = true; + successCallback(); + } + } + ); + } else { + // sensor is already started - just call success callback + setTimeout(function(){successCallback()}, 0); + } +}; + +SensorListener.prototype.stop = function () { + if (this.isStarted) { + var result = native_.callSync('Sensor_stop', {'sensorType' : this.sensorType}); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + this.isStarted = false; + } +}; + +SensorListener.prototype.setListener = function (successCallback) { + if (!this.callback) { + //call platform only if there was no listener registered + var result = native_.callSync('Sensor_setChangeListener', {'sensorType' : this.sensorType}); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + } + this.callback = successCallback; +}; + +SensorListener.prototype.unsetListener = function () { + if (this.callback) { + //unregister in platform only if there is callback registered + this.callback = undefined; + var result = native_.callSync('Sensor_unsetChangeListener', {'sensorType' : this.sensorType}); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + } }; -var _isChecked = false; +SensorListener.prototype.getData = function (successCallback, errorCallback) { + var thisObj = this; + if (!thisObj.isStarted) { + setTimeout(function() { + if (!T_.isNullOrUndefined(errorCallback)) { + errorCallback(new tizen.WebAPIException( + tizen.WebAPIException.SERVICE_NOT_AVAILABLE_ERR, + 'Service is not available.')); + } + }, 0); + } else { + native_.call('Sensor_getData', { type : thisObj.sensorType }, + function(result) { + if (native_.isFailure(result)) { + if(!T_.isNullOrUndefined(errorCallback)) { + errorCallback(native_.getErrorObject(result)); + } + } else { + successCallback(new thisObj.constructor(result)); + } + }); + } +}; + +var _supportedSensors = []; +var _isChecked = false; var _sensorListeners = { - 'LIGHT' : { callback : undefined, constructor : undefined }, - 'MAGNETIC' : { callback : undefined, constructor : undefined }, - 'PRESSURE' : { callback : undefined, constructor : undefined }, - 'PROXIMITY' : { callback : undefined, constructor : undefined }, - 'ULTRAVIOLET' : { callback : undefined, constructor : undefined } -} + 'LIGHT' : {}, + 'MAGNETIC' : {}, + 'PRESSURE' : {}, + 'PROXIMITY' : {}, + 'ULTRAVIOLET' : {} +}; var _listener = function(object) { - if (_sensorListeners[object.sensorType].callback) { - _sensorListeners[object.sensorType].callback( - new _sensorListeners[object.sensorType].constructor(object)); - } + _sensorListeners[object.sensorType].tryCall(object); }; var SENSOR_CHANGED_LISTENER = 'SensorChangedListener'; @@ -130,35 +211,11 @@ Sensor.prototype.start = function() { } ]); - if (!_startedSensors[this.sensorType]) { - // sensor not started - var type = this.sensorType; - native_.call('Sensor_start', {'sensorType' : type}, - function(result) { - if (native_.isFailure(result)) { - if(!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(native_.getErrorObject(result)); - } - } else { - _startedSensors[type] = true; - args.successCallback(); - } - } - ); - } else { - // sensor is already started - just call success callback - setTimeout(function(){args.successCallback()}, 0); - } + _sensorListeners[this.sensorType].start(args.successCallback, args.errorCallback); }; Sensor.prototype.stop = function() { - if (_startedSensors[this.sensorType]) { - var result = native_.callSync('Sensor_stop', {'sensorType' : this.sensorType}); - if (native_.isFailure(result)) { - throw native_.getErrorObject(result); - } - _startedSensors[this.sensorType] = false; - } + _sensorListeners[this.sensorType].stop(); }; Sensor.prototype.setChangeListener = function() { @@ -169,25 +226,11 @@ Sensor.prototype.setChangeListener = function() { } ]); - if (!_sensorListeners[this.sensorType].callback) { - //call platform only if there was no listener registered - var result = native_.callSync('Sensor_setChangeListener', {'sensorType' : this.sensorType}); - if (native_.isFailure(result)) { - throw native_.getErrorObject(result); - } - } - _sensorListeners[this.sensorType].callback = args.successCallback; + _sensorListeners[this.sensorType].setListener(args.successCallback); }; Sensor.prototype.unsetChangeListener = function() { - if (_sensorListeners[this.sensorType].callback) { - //unregister in platform only if there is callback registered - _sensorListeners[this.sensorType].callback = undefined; - var result = native_.callSync('Sensor_unsetChangeListener', {'sensorType' : this.sensorType}); - if (native_.isFailure(result)) { - throw native_.getErrorObject(result); - } - } + _sensorListeners[this.sensorType].unsetListener(); }; //// LightSensor @@ -213,26 +256,7 @@ LightSensor.prototype.getLightSensorData = function() { } ]); - if (!_startedSensors[this.sensorType]) { - setTimeout(function() { - if (!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(new tizen.WebAPIException( - tizen.WebAPIException.SERVICE_NOT_AVAILABLE_ERR, - 'Service is not available.')); - } - }, 0); - } else { - native_.call('Sensor_getData', { type : this.sensorType }, - function(result) { - if (native_.isFailure(result)) { - if(!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(native_.getErrorObject(result)); - } - } else { - args.successCallback(new SensorLightData(result)); - } - }); - } + _sensorListeners[this.sensorType].getData(args.successCallback, args.errorCallback); }; //// MagneticSensor @@ -258,26 +282,7 @@ MagneticSensor.prototype.getMagneticSensorData = function() { } ]); - if (!_startedSensors[this.sensorType]) { - setTimeout(function() { - if (!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(new tizen.WebAPIException( - tizen.WebAPIException.SERVICE_NOT_AVAILABLE_ERR, - 'Service is not available.')); - } - }, 0); - } else { - native_.call('Sensor_getData', { type : this.sensorType }, - function(result) { - if (native_.isFailure(result)) { - if(!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(native_.getErrorObject(result)); - } - } else { - args.successCallback(new SensorMagneticData(result)); - } - }); - } + _sensorListeners[this.sensorType].getData(args.successCallback, args.errorCallback); }; //// PressureSensor @@ -303,26 +308,7 @@ PressureSensor.prototype.getPressureSensorData = function() { } ]); - if (!_startedSensors[this.sensorType]) { - setTimeout(function() { - if (!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(new tizen.WebAPIException( - tizen.WebAPIException.SERVICE_NOT_AVAILABLE_ERR, - 'Service is not available.')); - } - }, 0); - } else { - native_.call('Sensor_getData', { type : this.sensorType }, - function(result) { - if (native_.isFailure(result)) { - if(!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(native_.getErrorObject(result)); - } - } else { - args.successCallback(new SensorPressureData(result)); - } - }); - } + _sensorListeners[this.sensorType].getData(args.successCallback, args.errorCallback); }; //// ProximitySensor @@ -348,26 +334,7 @@ ProximitySensor.prototype.getProximitySensorData = function() { } ]); - if (!_startedSensors[this.sensorType]) { - setTimeout(function() { - if (!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(new tizen.WebAPIException( - tizen.WebAPIException.SERVICE_NOT_AVAILABLE_ERR, - 'Service is not available.')); - } - }, 0); - } else { - native_.call('Sensor_getData', { type : this.sensorType }, - function(result) { - if (native_.isFailure(result)) { - if(!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(native_.getErrorObject(result)); - } - } else { - args.successCallback(new SensorProximityData(result)); - } - }); - } + _sensorListeners[this.sensorType].getData(args.successCallback, args.errorCallback); }; //// UltravioletSensor @@ -393,26 +360,7 @@ UltravioletSensor.prototype.getUltravioletSensorData = function() { } ]); - if (!_startedSensors[this.sensorType]) { - setTimeout(function() { - if (!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(new tizen.WebAPIException( - tizen.WebAPIException.SERVICE_NOT_AVAILABLE_ERR, - 'Service is not available.')); - } - }, 0); - } else { - native_.call('Sensor_getData', { type : this.sensorType }, - function(result) { - if (native_.isFailure(result)) { - if(!T_.isNullOrUndefined(args.errorCallback)) { - args.errorCallback(native_.getErrorObject(result)); - } - } else { - args.successCallback(new SensorUltravioletData(result)); - } - }); - } + _sensorListeners[this.sensorType].getData(args.successCallback, args.errorCallback); }; ////////////////////// Sensor Data classes///////////////////////////////////////////////////// @@ -432,7 +380,8 @@ SensorLightData.prototype = new SensorData(); SensorLightData.prototype.constructor = SensorData; -_sensorListeners[SensorType.LIGHT].constructor = SensorLightData; +_sensorListeners[SensorType.LIGHT] = new SensorListener(SensorType.LIGHT, + SensorLightData); //// SensorMagneticData var SensorMagneticData = function(data) { @@ -449,7 +398,8 @@ SensorMagneticData.prototype = new SensorData(); SensorMagneticData.prototype.constructor = SensorData; -_sensorListeners[SensorType.MAGNETIC].constructor = SensorMagneticData; +_sensorListeners[SensorType.MAGNETIC] = new SensorListener(SensorType.MAGNETIC, + SensorMagneticData); //// SensorPressureData var SensorPressureData = function(data) { @@ -463,7 +413,8 @@ SensorPressureData.prototype = new SensorData(); SensorPressureData.prototype.constructor = SensorData; -_sensorListeners[SensorType.PRESSURE].constructor = SensorPressureData; +_sensorListeners[SensorType.PRESSURE] = new SensorListener(SensorType.PRESSURE, + SensorPressureData); //// SensorProximityData var SensorProximityData = function(data) { @@ -477,7 +428,8 @@ SensorProximityData.prototype = new SensorData(); SensorProximityData.prototype.constructor = SensorData; -_sensorListeners[SensorType.PROXIMITY].constructor = SensorProximityData; +_sensorListeners[SensorType.PROXIMITY] = new SensorListener(SensorType.PROXIMITY, + SensorProximityData); //// SensorUltravioletData var SensorUltravioletData = function(data) { @@ -492,7 +444,8 @@ SensorUltravioletData.prototype = new SensorData(); SensorUltravioletData.prototype.constructor = SensorData; -_sensorListeners[SensorType.ULTRAVIOLET].constructor = SensorUltravioletData; +_sensorListeners[SensorType.ULTRAVIOLET] = new SensorListener(SensorType.ULTRAVIOLET, + SensorUltravioletData); // Exports exports = new SensorService(); diff --git a/src/sensor/sensor_service.cc b/src/sensor/sensor_service.cc index 93238f84..3266ab08 100644 --- a/src/sensor/sensor_service.cc +++ b/src/sensor/sensor_service.cc @@ -71,26 +71,48 @@ static const std::string kListenerId = "listenerId"; static const std::string kSensorChangedListener = "SensorChangedListener"; } -SensorService::SensorService() { +SensorService::SensorData::SensorData(sensor_type_e type) : + handle_(nullptr), + listener_(nullptr), + type_enum_(type) { } -SensorService::~SensorService() { - if (light_sensor_.listener) { - sensor_destroy_listener(light_sensor_.listener); - } - if (magnetic_sensor_.listener) { - sensor_destroy_listener(magnetic_sensor_.listener); - } - if (pressure_sensor_.listener) { - sensor_destroy_listener(pressure_sensor_.listener); +SensorService::SensorData::~SensorData() { + if (listener_) { + sensor_destroy_listener(listener_); } - if (proximity_sensor_.listener) { - sensor_destroy_listener(proximity_sensor_.listener); - } - if (ultraviolet_sensor_.listener) { - sensor_destroy_listener(ultraviolet_sensor_.listener); +} + +common::PlatformResult SensorService::SensorData::CheckInitialization() { + if (!handle_) { + LoggerD("initialization of handle and listener"); + int ret = sensor_get_default_sensor(type_enum_, &handle_); + if (SENSOR_ERROR_NONE != ret) { + LoggerE("ret : %d", ret); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "sensor_get_default_sensor"); + } + + ret = sensor_create_listener(handle_, &listener_); + if (SENSOR_ERROR_NONE != ret) { + LoggerE("ret : %d", ret); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "sensor_create_listener"); + } } + return PlatformResult(ErrorCode::NO_ERROR); +} + +SensorService::SensorService() : + light_sensor_(SENSOR_LIGHT), + magnetic_sensor_(SENSOR_MAGNETIC), + pressure_sensor_(SENSOR_PRESSURE), + proximity_sensor_(SENSOR_PROXIMITY), + ultraviolet_sensor_(SENSOR_ULTRAVIOLET) { + +} + +SensorService::~SensorService() { + } SensorService* SensorService::GetInstance() { @@ -169,14 +191,7 @@ void SensorService::SensorStart(const picojson::value& args, picojson::object& o sensor_type_e type_enum = string_to_type_map[type_str]; auto start = [this, type_enum, type_str](const std::shared_ptr& result) { - PlatformResult res = CheckSensorInitialization(type_enum); - if (res.IsError()) { - LoggerE("Sensor initialization for sensor %s failed", type_str.c_str()); - ReportError(res, &(result->get())); - return; - } SensorData* sensor_data = GetSensorStruct(type_enum); - if (!sensor_data) { LoggerD("Sensor data is null"); ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Sensor data is null"), @@ -184,8 +199,15 @@ void SensorService::SensorStart(const picojson::value& args, picojson::object& o return; } - int ret = sensor_listener_start(sensor_data->listener); - if (ret != SENSOR_ERROR_NONE) { + PlatformResult res = sensor_data->CheckInitialization(); + if (res.IsError()) { + LoggerE("Sensor initialization for sensor %s failed", type_str.c_str()); + ReportError(res, &(result->get())); + return; + } + + int ret = sensor_listener_start(sensor_data->listener_); + if (SENSOR_ERROR_NONE != ret) { LoggerE("ret : %d", ret); ReportError(GetSensorPlatformResult(ret, "sensor_listener_start"), &(result->get())); @@ -214,20 +236,27 @@ void SensorService::SensorStop(const picojson::value& args, picojson::object& ou sensor_type_e type_enum = string_to_type_map[type_str]; - PlatformResult res = CheckSensorInitialization(type_enum); SensorData* sensor_data = GetSensorStruct(type_enum); - if (!sensor_data) { LoggerD("Sensor data is null"); ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Sensor data is null"), &out); return; } - int ret = sensor_listener_stop(sensor_data->listener); - if (ret != SENSOR_ERROR_NONE) { - LOGE("ret : %d", ret); + PlatformResult res = sensor_data->CheckInitialization(); + if (res.IsError()) { + LoggerE("Sensor initialization for sensor %s failed", type_str.c_str()); + ReportError(res, &out); + return; + } + + int ret = sensor_listener_stop(sensor_data->listener_); + if (SENSOR_ERROR_NONE != ret) { + LoggerE("ret : %d", ret); ReportError(GetSensorPlatformResult(ret, "sensor_listener_stop"), &out); + return; } + ReportSuccess(out); } void SensorService::SensorSetChangeListener(const picojson::value& args, picojson::object& out) { @@ -238,21 +267,28 @@ void SensorService::SensorSetChangeListener(const picojson::value& args, picojso sensor_type_e type_enum = string_to_type_map[type_str]; - PlatformResult res = CheckSensorInitialization(type_enum); SensorData* sensor_data = GetSensorStruct(type_enum); - if (!sensor_data) { LoggerD("Sensor data is null"); ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Sensor data is null"), &out); return; } + PlatformResult res = sensor_data->CheckInitialization(); + if (res.IsError()) { + LoggerE("Sensor initialization for sensor %s failed", type_str.c_str()); + ReportError(res, &out); + return; + } + int ret = sensor_listener_set_event_cb( - sensor_data->listener, 100, GetCallbackFunction(type_enum), this); - if (ret != SENSOR_ERROR_NONE) { - LOGE("ret : %d", ret); + sensor_data->listener_, 100, GetCallbackFunction(type_enum), this); + if (SENSOR_ERROR_NONE != ret) { + LoggerE("ret : %d", ret); ReportError(GetSensorPlatformResult(ret, "sensor_listener_set_event_cb"), &out); + return; } + ReportSuccess(out); } void SensorService::SensorUnsetChangeListener(const picojson::value& args, picojson::object& out) { @@ -263,59 +299,27 @@ void SensorService::SensorUnsetChangeListener(const picojson::value& args, picoj sensor_type_e type_enum = string_to_type_map[type_str]; - PlatformResult res = CheckSensorInitialization(type_enum); SensorData* sensor_data = GetSensorStruct(type_enum); - if (!sensor_data) { LoggerD("Sensor data is null"); ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Sensor data is null"), &out); return; } - int ret = sensor_listener_unset_event_cb(sensor_data->listener); - if (ret != SENSOR_ERROR_NONE) { - LOGE("ret : %d", ret); - ReportError(GetSensorPlatformResult(ret, "sensor_listener_unset_event_cb"), &out); - } -} - -PlatformResult SensorService::CheckSensorInitialization(sensor_type_e type_enum) { - LoggerD("Entered"); - std::lock_guard lock(init_mutex); - - SensorData* sensor_data = NULL; - switch(type_enum) { - case SENSOR_LIGHT : - sensor_data = &light_sensor_; - break; - case SENSOR_MAGNETIC : - sensor_data = &magnetic_sensor_; - break; - case SENSOR_PRESSURE : - sensor_data = &pressure_sensor_; - break; - case SENSOR_PROXIMITY : - sensor_data = &proximity_sensor_; - break; - case SENSOR_ULTRAVIOLET : - sensor_data = &ultraviolet_sensor_; - break; + PlatformResult res = sensor_data->CheckInitialization(); + if (res.IsError()) { + LoggerE("Sensor initialization for sensor %s failed", type_str.c_str()); + ReportError(res, &out); + return; } - if (!(sensor_data->handle)) { - LoggerD("initialization of handle and listener"); - int ret = sensor_get_default_sensor(type_enum, &(sensor_data->handle)); - if (ret != SENSOR_ERROR_NONE) { - LoggerE("ret : %d", ret); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "sensor_get_default_sensor"); - } - ret = sensor_create_listener(sensor_data->handle, &(sensor_data->listener)); - if (ret != SENSOR_ERROR_NONE) { - LoggerE("ret : %d", ret); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "sensor_create_listener"); - } + int ret = sensor_listener_unset_event_cb(sensor_data->listener_); + if (SENSOR_ERROR_NONE != ret) { + LoggerE("ret : %d", ret); + ReportError(GetSensorPlatformResult(ret, "sensor_listener_unset_event_cb"), &out); + return; } - return PlatformResult(ErrorCode::NO_ERROR); + ReportSuccess(out); } SensorService::SensorData* SensorService::GetSensorStruct(sensor_type_e type_enum) { @@ -472,7 +476,7 @@ void SensorService::GetSensorData(const picojson::value& args, picojson::object& auto get_data = [this, sensor_type](const std::shared_ptr& result) { sensor_event_s sensor_event; SensorData* sensor = this->GetSensorStruct(sensor_type); - int ret = sensor_listener_read_data(sensor->listener, &sensor_event); + int ret = sensor_listener_read_data(sensor->listener_, &sensor_event); if (SENSOR_ERROR_NONE != ret) { ReportError(GetSensorPlatformResult(ret, type_to_string_map[sensor_type]), diff --git a/src/sensor/sensor_service.h b/src/sensor/sensor_service.h index a33d3ab2..1b206e6b 100644 --- a/src/sensor/sensor_service.h +++ b/src/sensor/sensor_service.h @@ -16,10 +16,16 @@ namespace sensor { typedef void (*CallbackPtr)(sensor_h sensor, sensor_event_s *event, void *user_data); class SensorService { - typedef struct { - sensor_h handle; - sensor_listener_h listener; - } SensorData; + class SensorData { + public: + SensorData(sensor_type_e type_enum); + ~SensorData(); + + common::PlatformResult CheckInitialization(); + sensor_h handle_; + sensor_listener_h listener_; + sensor_type_e type_enum_; + }; public: static SensorService* GetInstance(); @@ -36,7 +42,6 @@ class SensorService { std::string GetSensorErrorMessage(const int error_code); common::PlatformResult GetSensorPlatformResult(const int error_code, const std::string &hint); - common::PlatformResult CheckSensorInitialization(sensor_type_e type_enum); SensorData* GetSensorStruct(sensor_type_e type_enum); CallbackPtr GetCallbackFunction(sensor_type_e type_enum);