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 "generate_vibration_json_file.h"
17
18 #include <fstream>
19 #include <iostream>
20 #include <vector>
21
22 #include "json/json.h"
23
24 #include "sensor_log.h"
25 #include "sensors_errors.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "GenerateVibrationJsonFile"
29
30 namespace OHOS {
31 namespace Sensors {
32
GenerateJsonFile(std::vector<HapticEvent> & hapticEvents)33 int32_t GenerateVibrationJsonFile::GenerateJsonFile(std::vector<HapticEvent> &hapticEvents)
34 {
35 Json::Value meta;
36 meta["Create"] = "2023-04-27";
37 meta["Discription"] = "A json file format demo";
38 meta["Version"] = 1.0;
39 meta["ChannelNumber"] = 1;
40 Json::Value root;
41 root["MetaData"] = meta;
42 Json::Value pattern;
43 for (const auto &event : hapticEvents) {
44 Json::Value eventValue;
45 eventValue["Type"] = ((event.vibrateTag != EVENT_TAG_TRANSIENT) ? "continuous" : "transient");
46 eventValue["StartTime"] = event.startTime;
47 eventValue["Parameters"]["Intensity"] = event.intensity;
48 eventValue["Parameters"]["Frequency"] = event.frequency;
49 if (event.vibrateTag != EVENT_TAG_TRANSIENT) {
50 eventValue["Duration"] = event.duration;
51 }
52 Json::Value ev;
53 ev["Event"] = eventValue;
54 pattern.append(ev);
55 }
56 Json::Value channel;
57 channel["Parameters"]["Index"] = 1;
58 channel["Pattern"] = pattern;
59 Json::Value channels;
60 channels.append(channel);
61 root["Channels"] = channels;
62 std::ofstream ofs("demo.json", std::ios::out);
63 if (!ofs.is_open()) {
64 SEN_HILOGE("File open failed, errno:%{public}d", errno);
65 return Sensors::ERROR;
66 }
67 ofs << root << std::endl;
68 ofs.close();
69 return Sensors::SUCCESS;
70 }
71
72 template<typename T>
DebugJsonFile(const std::string & pathName,const std::vector<T> & srcDatas)73 int32_t GenerateVibrationJsonFile::DebugJsonFile(const std::string &pathName, const std::vector<T> &srcDatas)
74 {
75 if (access(pathName.c_str(), 0) != 0) {
76 SEN_HILOGE("File not exist, errno:%{public}d", errno);
77 return Sensors::ERROR;
78 }
79 if (srcDatas.empty()) {
80 SEN_HILOGE("srcDatas is empty");
81 return Sensors::ERROR;
82 }
83 Json::Value dataValue;
84 for (const auto &data : srcDatas) {
85 dataValue.append(data);
86 }
87 std::ofstream ofs(pathName, std::ios::out);
88 if (!ofs.is_open()) {
89 SEN_HILOGE("File open failed, errno:%{public}d", errno);
90 return Sensors::ERROR;
91 }
92 ofs << dataValue << std::endl;
93 ofs.close();
94 return Sensors::SUCCESS;
95 }
96 } // namespace Sensors
97 } // namespace OHOS
98