1 /*
2  * Copyright (c) 2023-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 "lux_filter_config_parser.h"
17 
18 #include <unistd.h>
19 #include <json/json.h>
20 
21 #include "config_parser_base.h"
22 #include "display_log.h"
23 
24 namespace OHOS {
25 namespace DisplayPowerMgr {
26 namespace {
27 const std::string CONFIG_NAME = "brightness_lux_filter_method_config";
SetDefault(std::unordered_map<std::string,LuxFilterConfig::Data> & data)28 void SetDefault(std::unordered_map<std::string, LuxFilterConfig::Data>& data)
29 {
30     LuxFilterConfig::Data config{ 7, 5, -1, -1.0f, -1 };
31     data.insert(std::make_pair("meanFilter", config));
32 }
33 } // namespace
34 
35 using namespace OHOS::DisplayPowerMgr;
36 
ParseConfig(int displayId,std::unordered_map<std::string,LuxFilterConfig::Data> & data)37 bool LuxFilterConfigParser::ParseConfig(
38     int displayId, std::unordered_map<std::string, LuxFilterConfig::Data>& data)
39 {
40     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse LuxFilterConfig start!", displayId);
41     const Json::Value root = ConfigParserBase::Get().LoadConfigRoot(displayId, CONFIG_NAME);
42     if (root.isNull()) {
43         SetDefault(data);
44         return false;
45     }
46     if (!root.isArray()) {
47         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "root <%{public}s> is not Array!", CONFIG_NAME.c_str());
48         return false;
49     }
50     for (auto value : root) {
51         LuxFilterConfig::Data config{};
52         if (!value["filterName"].isString() || value["filterName"].asString().empty()) {
53             DISPLAY_HILOGW(FEAT_BRIGHTNESS, "<%{public}s> filterName is not find!", CONFIG_NAME.c_str());
54             continue;
55         }
56         std::string name = value["filterName"].asString();
57         if (value["filterNoFilterNum"].isInt()) {
58             config.filterNoFilterNum = value["filterNoFilterNum"].asInt();
59         }
60         if (value["filterNum"].isInt()) {
61             config.filterNum = value["filterNum"].asInt();
62         }
63         if (value["filterMaxFuncLuxNum"].isInt()) {
64             config.filterMaxFuncLuxNum = value["filterMaxFuncLuxNum"].asInt();
65         }
66         if (value["filterAlpha"].isNumeric()) {
67             config.filterAlpha = value["filterAlpha"].asFloat();
68         }
69         if (value["filterLuxTh"].isInt()) {
70             config.filterLuxTh = value["filterLuxTh"].asInt();
71         }
72         data.insert(std::make_pair(name, config));
73         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "<%{public}s> is insert!", name.c_str());
74     }
75     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse LuxFilterConfig over!", displayId);
76     return true;
77 }
78 
PrintConfig(int displayId,const std::unordered_map<std::string,LuxFilterConfig::Data> & data)79 void LuxFilterConfigParser::PrintConfig(
80     int displayId, const std::unordered_map<std::string, LuxFilterConfig::Data>& data)
81 {
82     std::string text = std::to_string(displayId).append(" ");
83     for (auto [key, value] : data) {
84         text.append("[filterName: ");
85         text.append(key).append(", ");
86         text.append("filterNoFilterNum: ");
87         text.append(std::to_string(value.filterNoFilterNum)).append(", ");
88         text.append("filterNum: ");
89         text.append(std::to_string(value.filterNum)).append(", ");
90         text.append("filterMaxFuncLuxNum: ");
91         text.append(std::to_string(value.filterMaxFuncLuxNum)).append(", ");
92         text.append("filterAlpha: ");
93         text.append(std::to_string(value.filterAlpha)).append(", ");
94         text.append("filterLuxTh: ");
95         text.append(std::to_string(value.filterLuxTh)).append("]");
96     }
97     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "%{public}s", text.c_str());
98 }
99 } // namespace DisplayPowerMgr
100 } // namespace OHOS
101