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 "schedule_config.h"
17
18 #include "file_utils.h"
19 #include "update_log.h"
20
21 namespace OHOS {
22 namespace UpdateEngine {
23 uint64_t ScheduleConfig::pullupInterval_ = Startup::PULLUP_INTERVAL;
24 uint64_t ScheduleConfig::idleCheckInterval_ = Startup::IDLE_CHECK_INTERVAL;
25
InitConfig()26 void ScheduleConfig::InitConfig()
27 {
28 ENGINE_LOGI("InitConfig");
29 nlohmann::json root =
30 nlohmann::json::parse(FileUtils::ReadDataFromFile(Constant::DUPDATE_ENGINE_CONFIG_PATH), nullptr, false);
31 if (root.is_discarded()) {
32 ENGINE_LOGI("InitConfig load fail");
33 return;
34 }
35 pullupInterval_ = ParseConfig(root, Startup::PULLUP_INTERVAL_CONFIG, Startup::PULLUP_INTERVAL);
36 idleCheckInterval_ = ParseConfig(root, Startup::IDLE_CHECK_INTERVAL_CONFIG, Startup::IDLE_CHECK_INTERVAL);
37 ENGINE_LOGI("InitConfig pullupInterval: %{public}s, idleCheckInterval: %{public}s",
38 std::to_string(pullupInterval_).c_str(), std::to_string(idleCheckInterval_).c_str());
39 }
40
GetPullupInterval()41 uint64_t ScheduleConfig::GetPullupInterval()
42 {
43 return pullupInterval_;
44 }
45
GetIdleCheckInterval()46 uint64_t ScheduleConfig::GetIdleCheckInterval()
47 {
48 return idleCheckInterval_;
49 }
50
ParseConfig(nlohmann::json & root,const std::string & key,uint64_t defaultValue)51 uint64_t ScheduleConfig::ParseConfig(nlohmann::json &root, const std::string &key, uint64_t defaultValue)
52 {
53 uint64_t value = 0;
54 int32_t ret = JsonUtils::GetValueAndSetTo(root, key, value);
55 if ((ret == CAST_INT(JsonParseError::ERR_OK)) && (value > 0)) {
56 return value;
57 }
58 return defaultValue;
59 }
60 } // namespace UpdateEngine
61 } // namespace OHOS