1 /*
2  * Copyright (c) 2024 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 SCAN_SYSTEM_DATA_H
17 #define SCAN_SYSTEM_DATA_H
18 
19 #include <string>
20 #include <map>
21 #include <nlohmann/json.hpp>
22 #include "scanner_info.h"
23 
24 namespace OHOS {
25 namespace Scan {
26 
27 class ScanSystemData {
28 public:
GetInstance()29     static ScanSystemData& GetInstance()
30     {
31         static ScanSystemData instance;
32         return instance;
33     }
34     bool Init();
35     void InsertScannerInfo(const std::string &uniqueId, const ScanDeviceInfo &scannerInfo);
36     bool DeleteScannerInfo(const std::string &uniqueId);
37     bool UpdateScannerNameByUniqueId(const std::string &uniqueId, const std::string &deviceName);
38     bool UpdateScannerInfoByUniqueId(const std::string &uniqueId, const ScanDeviceInfo &scannerInfo);
39     bool UpdateScannerIdByUsbDevicePort(const std::string &uniqueId, const std::string &usbDevicePort);
40     bool QueryScannerNameByUniqueId(const std::string &uniqueId, std::string &deviceName);
41     bool QueryScannerInfoByUniqueId(const std::string &uniqueId, ScanDeviceInfo &scannerInfo);
42     void RefreshUsbDeviceId();
43     void GetAddedScannerInfoList(std::vector<ScanDeviceInfo> &infoList);
44     bool SaveScannerMap();
45     bool IsContainScanner(const std::string &uniqueId);
46     std::pair<std::string, std::string> UpdateNetScannerByUuid(const std::string &uuid, const std::string& ip);
47     static std::string ReplaceDeviceIdUsbPort(const std::string& deviceId, const std::string& usbPort);
48     static std::map<std::string, std::string> usbSnToPortMap_;
49 
50 private:
51     bool ParseScannerListJsonV1(nlohmann::json& jsonObject);
52     bool CheckJsonObjectValue(const nlohmann::json& object);
53     void FormatUsbPort(std::string &port);
54     std::string GetNewDeviceId(std::string oldDeviceId, std::string usbDevicePort);
55     std::map<std::string, std::shared_ptr<ScanDeviceInfo>> addedScannerMap_;
56     std::mutex addedScannerMapLock_;
57 };
58 
59 }  // namespace Scan
60 }  // namespace OHOS
61 #endif  // SCAN_SYSTEM_DATA_H
62