diff options
Diffstat (limited to 'wearable_src/Systeminfo/EventWatchSysteminfo.cpp')
-rwxr-xr-x | wearable_src/Systeminfo/EventWatchSysteminfo.cpp | 366 |
1 files changed, 366 insertions, 0 deletions
diff --git a/wearable_src/Systeminfo/EventWatchSysteminfo.cpp b/wearable_src/Systeminfo/EventWatchSysteminfo.cpp new file mode 100755 index 0000000..4863c3d --- /dev/null +++ b/wearable_src/Systeminfo/EventWatchSysteminfo.cpp @@ -0,0 +1,366 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <CommonsJavaScript/Converter.h> +#include <Commons/Exception.h> +#include "EventWatchSysteminfo.h" +#include "Systeminfo.h" + +namespace DeviceAPI { +namespace Systeminfo { + +using namespace WrtDeviceApis::CommonsJavaScript; +using namespace WrtDeviceApis::Commons; + +namespace { + +static Eina_Bool timeout_timer_cb(void* data) +{ + EventWatchSysteminfo *event = static_cast<EventWatchSysteminfo *> (data); + event->timeoutWatch(); + return ECORE_CALLBACK_RENEW; +} + +} + +#define MAX_STORAGE_CNT 2 +DPL::Atomic EventWatchSysteminfo::m_uniqId = 1; + +EventWatchSysteminfo::EventWatchSysteminfo() : m_id(m_uniqId) +{ + m_initTimer = NULL; + m_canceled = false; + m_storageCnt = 0; + m_tmpStorageCnt = 0; + m_Systeminfo = NULL; + m_setLastValue = false; + ++m_uniqId; +} + +EventWatchSysteminfo::~EventWatchSysteminfo() +{ + LoggerD("destroy event data, id=" << m_id); + removeTimer(); +} + +void EventWatchSysteminfo::setWatchOption(const WatchOption& watchoption) { + m_WatchOption = watchoption; +} + +WatchOption EventWatchSysteminfo::getWatchOption() { + return m_WatchOption; +} + +JSCallbackManagerPtr EventWatchSysteminfo::getCallbackManager() { + return DPL::StaticPointerCast< JSCallbackManager >(getPrivateData()); +} + +BasePropertyPtr EventWatchSysteminfo::getBasePropertyPtr() { + return m_BaseProperty; +} + +void EventWatchSysteminfo::setSysteminfoPtr(void* SysteminfoPtr) { + m_Systeminfo = SysteminfoPtr; +} + +void EventWatchSysteminfo::setBasePropertyPtr(const BasePropertyPtr& baseProperty) { + m_BaseProperty = baseProperty; +} + +const char * EventWatchSysteminfo::getProperty() const { + return m_BaseProperty->getProperty(); +} + +const int EventWatchSysteminfo::getWatchType() const { + return m_BaseProperty->getWatchType(); +} + +void EventWatchSysteminfo::getWatchValue(int cnt) +{ + m_tmpStorageCnt = cnt; + processGetValue(); +} + +unsigned long EventWatchSysteminfo::getId() const +{ + return m_id; +} + +void EventWatchSysteminfo::setId(unsigned long id) +{ + m_id = id; +} + +void EventWatchSysteminfo::processGetValue() +{ + LoggerD("thread=" << DPL::Thread::GetCurrentThread()); + JSValueRef lastValue = NULL; + JSValueRef lastValueList[MAX_STORAGE_CNT]; + JSValueRef tmpValue = NULL; + JSValueRef tmpValueList[MAX_STORAGE_CNT]; + JSStringRef propertyName = NULL; + std::string key; + JSObjectRef object = NULL; + JSValueRef value = NULL; + double level = 0.0, brightness = 0.0, load = 0.0; + bool isCharging = false; + double capacity[MAX_STORAGE_CNT]; + double availableCapacity[MAX_STORAGE_CNT]; + bool isRemovable[MAX_STORAGE_CNT]; + int watchType = m_BaseProperty->getWatchType(); + + if(m_canceled) { + LoggerD("Watch event is cancelled aleardy."); + return; + } + if (m_Systeminfo == NULL) { + LoggerE("systeminfo pointer is not set"); + return; + } + + JSCallbackManagerPtr m_cbm = DPL::StaticPointerCast< JSCallbackManager >(getPrivateData()); + JSContextRef context = m_cbm->getContext(); + + if (!m_setLastValue) { + lastValue = m_BaseProperty->getValue(context); + switch(watchType) { + case WATCH_TYPE_BATTERY : + key = "level"; + object = JSValueToObject(context, lastValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + m_propertyValue.batterInfo.level = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + + key = "isCharging"; + object = JSValueToObject(context, lastValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + m_propertyValue.batterInfo.isCharging = JSValueToBoolean(context, JSObjectGetProperty(context, object, propertyName, NULL)); + break; + + case WATCH_TYPE_CPU : + key = "load"; + object = JSValueToObject(context, lastValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + m_propertyValue.cpuInfo.load = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + break; + + case WATCH_TYPE_STORAGE: + if (m_tmpStorageCnt > MAX_STORAGE_CNT) { + Throw(WrtDeviceApis::Commons::Exception); + } + + key = "units"; + object = JSValueToObject(context, lastValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + value = JSObjectGetProperty(context, object, propertyName, NULL); + + for (int i=0; i<m_tmpStorageCnt; i++) { + + lastValueList[i] = JSGetArrayElement(context, JSValueToObject(context, value, NULL), i); + + key = "capacity"; + object = JSValueToObject(context, lastValueList[i], NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + m_propertyValue.storageInfo[i].capacity = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + + key = "availableCapacity"; + object = JSValueToObject(context, lastValueList[i], NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + m_propertyValue.storageInfo[i].availableCapacity = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + + key = "isRemovable"; + object = JSValueToObject(context, lastValueList[i], NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + m_propertyValue.storageInfo[i].isRemovable = JSValueToBoolean(context, JSObjectGetProperty(context, object, propertyName, NULL)); + } + + m_storageCnt = m_tmpStorageCnt; + break; + + default : + LoggerD("default value"); + break; + } + m_setLastValue = true; + return; + } + + if (watchType == WATCH_TYPE_CPU) { + tmpValue = ((Systeminfo*)m_Systeminfo)->getCpuValue(context); + } else { + tmpValue = m_BaseProperty->getValue(context); + } + + if (!tmpValue) { + return; + } + + LoggerD("watchType : " << watchType); + switch(watchType) { + case WATCH_TYPE_BATTERY : + key = "level"; + object = JSValueToObject(context, tmpValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + level = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + + key = "isCharging"; + object = JSValueToObject(context, tmpValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + isCharging = JSValueToBoolean(context, JSObjectGetProperty(context, object, propertyName, NULL)); + + if (m_propertyValue.batterInfo.level != level || m_propertyValue.batterInfo.isCharging != isCharging) { + if (((m_WatchOption.highThreshold > 0) && (level > m_WatchOption.highThreshold)) + || ((m_WatchOption.lowThreshold > 0) && (level < m_WatchOption.lowThreshold)) + || ((m_WatchOption.highThreshold == 0) && (m_WatchOption.lowThreshold == 0))) { + LoggerD("make callback"); + m_cbm->callOnSuccess(tmpValue); + setTimer(); + } + } + m_propertyValue.batterInfo.level = level; + m_propertyValue.batterInfo.isCharging = isCharging; + break; + + case WATCH_TYPE_CPU : + key = "load"; + object = JSValueToObject(context, tmpValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + load = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + + if (m_propertyValue.cpuInfo.load != load) { + if (((m_WatchOption.highThreshold > 0) && (load > m_WatchOption.highThreshold)) + || ((m_WatchOption.lowThreshold > 0) && (load < m_WatchOption.lowThreshold)) + || ((m_WatchOption.highThreshold == 0) && (m_WatchOption.lowThreshold == 0))) { + m_cbm->callOnSuccess(tmpValue); + setTimer(); + } + } + m_propertyValue.cpuInfo.load = load; + break; + + case WATCH_TYPE_STORAGE : + if (m_tmpStorageCnt > MAX_STORAGE_CNT) { + Throw(Exception); + } + + key = "units"; + object = JSValueToObject(context, tmpValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + value = JSObjectGetProperty(context, object, propertyName, NULL); + + for (int i=0; i<m_tmpStorageCnt; i++) { + tmpValueList[i] = JSGetArrayElement(context, JSValueToObject(context, value, NULL), i); + key = "capacity"; + object = JSValueToObject(context, tmpValueList[i], NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + capacity[i] = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + + key = "availableCapacity"; + object = JSValueToObject(context, tmpValueList[i], NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + availableCapacity[i] = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + + key = "isRemovable"; + object = JSValueToObject(context, tmpValueList[i], NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + isRemovable[i] = JSValueToBoolean(context, JSObjectGetProperty(context, object, propertyName, NULL)); + } + + if (m_tmpStorageCnt == m_storageCnt) { + for (int k=0; k<m_tmpStorageCnt; k++) { + if (m_propertyValue.storageInfo[k].capacity != capacity[k] || m_propertyValue.storageInfo[k].availableCapacity != availableCapacity[k] + || m_propertyValue.storageInfo[k].isRemovable != isRemovable[k]) { + LoggerD("make callback / m_tmpStorageCnt : " << m_tmpStorageCnt); + m_cbm->callOnSuccess(tmpValue); + setTimer(); + break; + } + } + } else { + LoggerD("make callback / m_tmpStorageCnt : " << m_tmpStorageCnt); + m_cbm->callOnSuccess(tmpValue); + setTimer(); + } + + m_storageCnt = m_tmpStorageCnt; + for (int j=0; j<m_tmpStorageCnt; j++) { + m_propertyValue.storageInfo[j].capacity = capacity[j]; + m_propertyValue.storageInfo[j].availableCapacity = availableCapacity[j]; + m_propertyValue.storageInfo[j].isRemovable = isRemovable[j]; + } + break; + + case WATCH_TYPE_DISPLAY : + key = "brightness"; + object = JSValueToObject(context, tmpValue, NULL); + propertyName = JSStringCreateWithUTF8CString(key.c_str()); + brightness = JSValueToNumber(context, JSObjectGetProperty(context, object, propertyName, NULL), NULL); + if (((m_WatchOption.highThreshold > 0) && (brightness > m_WatchOption.highThreshold)) + || ((m_WatchOption.lowThreshold > 0) && (brightness < m_WatchOption.lowThreshold)) + || ((m_WatchOption.highThreshold == 0) && (m_WatchOption.lowThreshold == 0))) { + LoggerD("make callback"); + m_cbm->callOnSuccess(tmpValue); + setTimer(); + } + break; + + case WATCH_TYPE_DEVICE_ORIENTATION : + case WATCH_TYPE_PERIPHERAL : + case WATCH_TYPE_LOCALE : + LoggerD("make callback"); + m_cbm->callOnSuccess(tmpValue); + setTimer(); + break; + } +} + +void EventWatchSysteminfo::setTimer() +{ + if (m_WatchOption.timeout > 0) { + if (m_initTimer) { + ecore_timer_freeze(m_initTimer); + ecore_timer_del(m_initTimer); + m_initTimer = NULL; + } + double value = m_WatchOption.timeout/(double)1000; + m_initTimer = ecore_timer_add(value, timeout_timer_cb, this); + ecore_timer_thaw(m_initTimer); + } +} + +void EventWatchSysteminfo::removeTimer() +{ + if (m_initTimer) { + ecore_timer_freeze(m_initTimer); + ecore_timer_del(m_initTimer); + } + m_initTimer = NULL; +} + +void EventWatchSysteminfo::clearWatch() +{ + m_canceled = true; + removeTimer(); + ((Systeminfo*)m_Systeminfo)->clearWatch(m_id); +} + +void EventWatchSysteminfo::timeoutWatch() +{ + clearWatch(); +} + +} +} |