1 /*
2 * Copyright (c) 2021-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 #include "task_factory.h"
17
18 #include "anonymous_string.h"
19 #include "disable_task.h"
20 #include "distributed_hardware_log.h"
21 #include "enable_task.h"
22 #include "meta_disable_task.h"
23 #include "meta_enable_task.h"
24 #include "offline_task.h"
25 #include "online_task.h"
26 #include "task_board.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "TaskFactory"
32
33 IMPLEMENT_SINGLE_INSTANCE(TaskFactory);
CreateTask(TaskType taskType,TaskParam taskParam,std::shared_ptr<Task> fatherTask)34 std::shared_ptr<Task> TaskFactory::CreateTask(TaskType taskType, TaskParam taskParam, std::shared_ptr<Task> fatherTask)
35 {
36 DHLOGI("taskType: %{public}d, networkId: %{public}s, uuid: %{public}s, dhId: %{public}s",
37 static_cast<int32_t>(taskType), GetAnonyString(taskParam.networkId).c_str(),
38 GetAnonyString(taskParam.uuid).c_str(), GetAnonyString(taskParam.dhId).c_str());
39 std::shared_ptr<Task> task = nullptr;
40 switch (taskType) {
41 case TaskType::ENABLE: {
42 task = std::make_shared<EnableTask>(taskParam.networkId, taskParam.uuid, taskParam.udid,
43 taskParam.dhId, taskParam.dhType);
44 break;
45 }
46 case TaskType::DISABLE: {
47 task = std::make_shared<DisableTask>(taskParam.networkId, taskParam.uuid, taskParam.udid,
48 taskParam.dhId, taskParam.dhType);
49 break;
50 }
51 case TaskType::ON_LINE: {
52 task = std::make_shared<OnLineTask>(taskParam.networkId, taskParam.uuid, taskParam.udid,
53 taskParam.dhId, taskParam.dhType);
54 break;
55 }
56 case TaskType::OFF_LINE: {
57 task = std::make_shared<OffLineTask>(taskParam.networkId, taskParam.uuid, taskParam.udid,
58 taskParam.dhId, taskParam.dhType);
59 break;
60 }
61 case TaskType::META_ENABLE: {
62 task = std::make_shared<MetaEnableTask>(taskParam.networkId, taskParam.uuid, taskParam.udid,
63 taskParam.dhId, taskParam.dhType);
64 break;
65 }
66 case TaskType::META_DISABLE: {
67 task = std::make_shared<MetaDisableTask>(taskParam.networkId, taskParam.uuid, taskParam.udid,
68 taskParam.dhId, taskParam.dhType);
69 break;
70 }
71 default: {
72 DHLOGE("CreateTask type invalid, type: %{public}d", taskType);
73 return nullptr;
74 }
75 }
76
77 if (fatherTask != nullptr) {
78 task->SetFatherTask(fatherTask);
79 fatherTask->AddChildrenTask(task);
80 }
81 TaskBoard::GetInstance().AddTask(task);
82
83 return task;
84 }
85 } // namespace DistributedHardware
86 } // namespace OHOS
87