1 /*
2  * Copyright (c) 2022-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 #ifndef BATTERY_CONFIG_H
17 #define BATTERY_CONFIG_H
18 
19 #include <fstream>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 #include <string>
24 
25 #include <json/json.h>
26 #include "nocopyable.h"
27 
28 namespace OHOS {
29 namespace PowerMgr {
30 class BatteryConfig : public NoCopyable {
31 public:
32     struct LightConf {
33         int32_t beginSoc;
34         int32_t endSoc;
35         uint32_t rgb;
36     };
37     static BatteryConfig& GetInstance();
38     static void DestroyInstance();
39     bool ParseConfig();
40     bool IsExist(std::string key) const;
41     int32_t GetInt(std::string key, int32_t defVal = 0) const;
42     std::string GetString(std::string key, std::string defVal = "") const;
43     const std::vector<LightConf>& GetLightConf() const;
44 
45 private:
46     bool OpenFile(std::ifstream& ifsConf, const std::string& configPath);
47     void ParseConfInner();
48     void ParseLightConf(std::string level);
49     Json::Value FindConf(const std::string& key) const;
50     bool SplitKey(const std::string& key, std::vector<std::string>& keys) const;
51     Json::Value GetValue(std::string key) const;
52     Json::Value config_;
53     std::vector<BatteryConfig::LightConf> lightConf_;
54     static std::mutex mutex_;
55     static std::shared_ptr<BatteryConfig> instance_;
56 };
57 }  // namespace PowerMgr
58 }  // namespace OHOS
59 #endif
60