github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/automation.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package dto 20 21 import ( 22 stub "github.com/e154/smart-home/api/stub" 23 "github.com/e154/smart-home/common" 24 m "github.com/e154/smart-home/models" 25 ) 26 27 // Automation ... 28 type Automation struct{} 29 30 // NewAutomationDto ... 31 func NewAutomationDto() Automation { 32 return Automation{} 33 } 34 35 // AddTask ... 36 func (r Automation) AddTask(obj *stub.ApiNewTaskRequest) (task *m.NewTask) { 37 if obj == nil { 38 return 39 } 40 task = &m.NewTask{ 41 Name: obj.Name, 42 Description: obj.Description, 43 Enabled: obj.Enabled, 44 Condition: common.ConditionType(obj.Condition), 45 TriggerIds: obj.TriggerIds, 46 ConditionIds: obj.ConditionIds, 47 ActionIds: obj.ActionIds, 48 AreaId: obj.AreaId, 49 } 50 return 51 } 52 53 // UpdateTask ... 54 func (r Automation) UpdateTask(obj *stub.AutomationServiceUpdateTaskJSONBody, id int64) (task *m.UpdateTask) { 55 task = &m.UpdateTask{ 56 Id: id, 57 Name: obj.Name, 58 Description: obj.Description, 59 Enabled: obj.Enabled, 60 Condition: common.ConditionType(obj.Condition), 61 TriggerIds: obj.TriggerIds, 62 ConditionIds: obj.ConditionIds, 63 ActionIds: obj.ActionIds, 64 AreaId: obj.AreaId, 65 } 66 67 return 68 } 69 70 // ToListResult ... 71 func (r Automation) ToListResult(list []*m.Task) []*stub.ApiTask { 72 73 items := make([]*stub.ApiTask, 0, len(list)) 74 75 for _, i := range list { 76 items = append(items, r.GetTask(i)) 77 } 78 79 return items 80 } 81 82 // GetTask ... 83 func (r Automation) GetTask(task *m.Task) (obj *stub.ApiTask) { 84 85 obj = &stub.ApiTask{ 86 Id: task.Id, 87 Name: task.Name, 88 Description: task.Description, 89 Enabled: task.Enabled, 90 IsLoaded: common.Bool(task.IsLoaded), 91 Area: GetStubArea(task.Area), 92 AreaId: task.AreaId, 93 Condition: string(task.Condition), 94 CreatedAt: task.CreatedAt, 95 UpdatedAt: task.UpdatedAt, 96 } 97 98 // triggers 99 for _, tr := range task.Triggers { 100 obj.Triggers = append(obj.Triggers, stub.ApiTrigger{ 101 Id: tr.Id, 102 Name: tr.Name, 103 }) 104 obj.TriggerIds = append(obj.TriggerIds, tr.Id) 105 } 106 107 // conditions 108 for _, con := range task.Conditions { 109 obj.Conditions = append(obj.Conditions, stub.ApiCondition{ 110 Id: con.Id, 111 Name: con.Name, 112 }) 113 obj.ConditionIds = append(obj.ConditionIds, con.Id) 114 } 115 116 // actions 117 for _, action := range task.Actions { 118 obj.Actions = append(obj.Actions, stub.ApiAction{ 119 Id: action.Id, 120 Name: action.Name, 121 }) 122 obj.ActionIds = append(obj.ActionIds, action.Id) 123 } 124 125 // telemetry 126 if task.Telemetry != nil { 127 obj.Telemetry = make([]stub.ApiTelemetryItem, 0) 128 } 129 130 for _, item := range task.Telemetry { 131 stateItem := stub.ApiTelemetryItem{ 132 Name: item.Name, 133 Num: int32(item.Num), 134 Start: item.Start.UnixNano(), 135 End: nil, 136 TimeEstimate: int64(item.TimeEstimate), 137 Attributes: item.Attributes, 138 Status: string(item.Status), 139 Level: int32(item.Level), 140 } 141 if item.End != nil { 142 stateItem.End = common.Int64(item.End.UnixNano()) 143 } 144 obj.Telemetry = append(obj.Telemetry, stateItem) 145 } 146 147 return 148 }