1 /*
2 * Copyright (c) 2023 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 "algo_relative_still.h"
17
18 namespace OHOS {
19 namespace Msdp {
Init(DevicestatusDataUtils::DevicestatusType type)20 bool AlgoRelativeStill::Init(DevicestatusDataUtils::DevicestatusType type)
21 {
22 DEV_HILOGD(SERVICE, "Enter");
23 algoCallback_ = std::bind(&AlgoRelativeStill::StartAlgorithm, this, std::placeholders::_1, std::placeholders::_2);
24 if (algoCallback_ == nullptr) {
25 DEV_HILOGE(SERVICE, "algoCallback is nullptr");
26 return false;
27 }
28 SensorDataCallback::GetInstance().SubscribeSensorEvent(type, algoCallback_);
29 return true;
30 }
31
StartAlgorithm(int32_t sensorTypeId,AccelData * sensorData)32 bool AlgoRelativeStill::StartAlgorithm(int32_t sensorTypeId, AccelData* sensorData)
33 {
34 DEV_HILOGD(SERVICE, "Enter");
35 if (!GetData(sensorTypeId, sensorData)) {
36 DEV_HILOGE(SERVICE, "Failed to get data");
37 return false;
38 }
39 ExecuteOperation();
40 return true;
41 }
42
ExecuteOperation()43 void AlgoRelativeStill::ExecuteOperation()
44 {
45 DEV_HILOGD(SERVICE, "Enter");
46 algoPara_.resultantAcc =
47 sqrt((algoPara_.x * algoPara_.x) + (algoPara_.y * algoPara_.y) + (algoPara_.z * algoPara_.z));
48 DEV_HILOGD(SERVICE, "resultantAcc:%{public}f", algoPara_.resultantAcc);
49 if ((algoPara_.resultantAcc > RELATIVE_ACC_MIN_THRHD) && (algoPara_.resultantAcc < RELATIVE_ACC_MAX_THRHD)) {
50 if (state_ == RELATIVE_STILL) {
51 return;
52 }
53 counter_--;
54 if (counter_ == 0) {
55 counter_ = COUNTER_THRESHOLD;
56 UpdateStateAndReport(DevicestatusDataUtils::VALUE_ENTER, RELATIVE_STILL,
57 DevicestatusDataUtils::DevicestatusDataUtils::TYPE_RELATIVE_STILL);
58 }
59 } else {
60 counter_ = COUNTER_THRESHOLD;
61 if (state_ == NON_RELATIVE_STILL) {
62 return;
63 }
64 UpdateStateAndReport(DevicestatusDataUtils::VALUE_EXIT, NON_RELATIVE_STILL,
65 DevicestatusDataUtils::DevicestatusDataUtils::TYPE_RELATIVE_STILL);
66 }
67 }
68 } // namespace Msdp
69 } // namespace OHOS
70