github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/trigger.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 // Trigger ... 28 type Trigger struct{} 29 30 // NewTriggerDto ... 31 func NewTriggerDto() Trigger { 32 return Trigger{} 33 } 34 35 // AddTrigger ... 36 func (r Trigger) AddTrigger(from *stub.ApiNewTriggerRequest) (action *m.NewTrigger) { 37 action = &m.NewTrigger{ 38 Name: from.Name, 39 Description: from.Description, 40 EntityIds: from.EntityIds, 41 ScriptId: from.ScriptId, 42 AreaId: from.AreaId, 43 PluginName: from.PluginName, 44 Payload: AttributeFromApi(from.Attributes), 45 Enabled: from.Enabled, 46 } 47 return 48 } 49 50 // UpdateTrigger ... 51 func (r Trigger) UpdateTrigger(from *stub.TriggerServiceUpdateTriggerJSONBody, id int64) (action *m.UpdateTrigger) { 52 action = &m.UpdateTrigger{ 53 Id: id, 54 Name: from.Name, 55 Description: from.Description, 56 EntityIds: from.EntityIds, 57 ScriptId: from.ScriptId, 58 AreaId: from.AreaId, 59 PluginName: from.PluginName, 60 Payload: AttributeFromApi(from.Attributes), 61 Enabled: from.Enabled, 62 } 63 return 64 } 65 66 // ToSearchResult ... 67 func (r Trigger) ToSearchResult(list []*m.Trigger) *stub.ApiSearchTriggerResult { 68 69 items := make([]stub.ApiTrigger, 0, len(list)) 70 71 for _, i := range list { 72 items = append(items, *r.ToTrigger(i)) 73 } 74 75 return &stub.ApiSearchTriggerResult{ 76 Items: items, 77 } 78 } 79 80 // ToListResult ... 81 func (r Trigger) ToListResult(list []*m.Trigger) []*stub.ApiTrigger { 82 83 items := make([]*stub.ApiTrigger, 0, len(list)) 84 85 for _, trigger := range list { 86 items = append(items, &stub.ApiTrigger{ 87 Id: trigger.Id, 88 Name: trigger.Name, 89 Description: trigger.Description, 90 EntityIds: make([]string, 0, len(trigger.Entities)), 91 ScriptId: trigger.ScriptId, 92 AreaId: trigger.AreaId, 93 Area: GetStubArea(trigger.Area), 94 PluginName: trigger.PluginName, 95 Enabled: trigger.Enabled, 96 IsLoaded: common.Bool(trigger.IsLoaded), 97 CreatedAt: trigger.CreatedAt, 98 UpdatedAt: trigger.UpdatedAt, 99 }) 100 } 101 102 return items 103 } 104 105 // ToTrigger ... 106 func (r Trigger) ToTrigger(action *m.Trigger) (obj *stub.ApiTrigger) { 107 obj = ToTrigger(action) 108 return 109 } 110 111 // ToTrigger ... 112 func ToTrigger(trigger *m.Trigger) (obj *stub.ApiTrigger) { 113 if trigger == nil { 114 return 115 } 116 obj = &stub.ApiTrigger{ 117 Id: trigger.Id, 118 Name: trigger.Name, 119 Description: trigger.Description, 120 Entities: make([]stub.ApiEntityShort, 0, len(trigger.Entities)), 121 EntityIds: make([]string, 0, len(trigger.Entities)), 122 Script: GetStubScript(trigger.Script), 123 ScriptId: trigger.ScriptId, 124 AreaId: trigger.AreaId, 125 Area: GetStubArea(trigger.Area), 126 PluginName: trigger.PluginName, 127 Enabled: trigger.Enabled, 128 IsLoaded: common.Bool(trigger.IsLoaded), 129 Attributes: AttributeToApi(trigger.Payload), 130 CreatedAt: trigger.CreatedAt, 131 UpdatedAt: trigger.UpdatedAt, 132 } 133 for _, entity := range trigger.Entities { 134 obj.Entities = append(obj.Entities, stub.ApiEntityShort{ 135 Id: entity.Id.String(), 136 }) 137 obj.EntityIds = append(obj.EntityIds, entity.Id.String()) 138 } 139 return 140 }