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 #ifndef APP_TIMER_ADAPTER_H
16 #define APP_TIMER_ADAPTER_H
17 
18 #include <vector>
19 #include <mutex>
20 #include <string>
21 #include <map>
22 #include "IAppTimer.h"
23 #include "ISceneTimerInfrastructure.h"
24 
25 class AppTimerAdapter : public IAppTimer, public ISceneTimerInfrastructure::ICb {
26 public:
27     const static unsigned int timeoutPeriod = 5 * 1000; // 5s
28 
29     AppTimerAdapter(int userId, ISceneTimerInfrastructure* infra);
30     ~AppTimerAdapter() override;
31     void Start(std::string key) override;
32     void Stop(std::string key) override;
33     void Expired(int id) override;
34     void SetCb(IAppTimer::ICb* cb);
35 private:
36     const int userId;
37 
38     IAppTimer::ICb* cb{nullptr};
39     ISceneTimerInfrastructure* impl;
40     std::vector<int> sessions;
41     std::mutex mut;
42 
43     std::vector<int> ids = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
44                             11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
45                             21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
46     std::map<int, std::string> idToBundle;
47     std::map<std::string, int> bundleToId;
48 
49     void ValidateDuplication(const int id);
50     void ValidateExistence(const int id);
51 
52     std::string IdToKey(const int id);
53     int KeyToId(const std::string& key);
54     int AssignId(const std::string& key);
55     void RecycleId(const int id);
56     void ValidateExistKey(const std::string& key);
57 };
58 #endif
59