1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "battery_module.h"
17 #include "battery_impl.h"
18
19 namespace OHOS {
20 namespace ACELite {
21 using namespace OHOS::ACELite;
SuccessCallBack(const JSIValue thisVal,const JSIValue args,JSIValue jsiValue)22 void SuccessCallBack(const JSIValue thisVal, const JSIValue args, JSIValue jsiValue)
23 {
24 if (JSI::ValueIsUndefined(args)) {
25 return;
26 }
27 JSIValue success = JSI::GetNamedProperty(args, CB_SUCCESS);
28 JSIValue complete = JSI::GetNamedProperty(args, CB_COMPLETE);
29 if (!JSI::ValueIsUndefined(success)) {
30 if (JSI::ValueIsUndefined(jsiValue)) {
31 JSI::CallFunction(success, thisVal, nullptr, 0);
32 } else {
33 JSI::CallFunction(success, thisVal, &jsiValue, ARGC_ONE);
34 }
35 }
36 if (!JSI::ValueIsUndefined(complete)) {
37 JSI::CallFunction(complete, thisVal, nullptr, 0);
38 }
39 JSI::ReleaseValueList(success, complete, ARGS_END);
40 }
41
GetStatus(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)42 JSIValue BatteryModule::GetStatus(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
43 {
44 JSIValue undefValue = JSI::CreateUndefined();
45 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
46 return undefValue;
47 }
48
49 double level = 0;
50 bool charging = false;
51 (void)GetBatteryStatus(&charging, &level);
52 JSIValue result = JSI::CreateObject();
53 JSI::SetBooleanProperty(result, "charging", charging);
54 JSI::SetNumberProperty(result, "level", level);
55 SuccessCallBack(thisVal, args[0], result);
56 JSI::ReleaseValue(result);
57 return undefValue;
58 }
59
InitBatteryModule(JSIValue exports)60 void InitBatteryModule(JSIValue exports)
61 {
62 JSI::SetModuleAPI(exports, "getStatus", BatteryModule::GetStatus);
63 }
64 } // namespace ACELite
65 } // namespace OHOS
66