github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/plugin.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 // Plugin ... 28 type Plugin struct{} 29 30 // NewPluginDto ... 31 func NewPluginDto() Plugin { 32 return Plugin{} 33 } 34 35 // ToPluginListResult ... 36 func (p Plugin) ToPluginListResult(plugins []*m.Plugin) []*stub.ApiPluginShort { 37 38 var items = make([]*stub.ApiPluginShort, 0, len(plugins)) 39 40 for _, item := range plugins { 41 items = append(items, &stub.ApiPluginShort{ 42 Name: item.Name, 43 Version: item.Version, 44 Enabled: item.Enabled, 45 System: item.System, 46 IsLoaded: common.Bool(item.IsLoaded), 47 }) 48 } 49 50 return items 51 } 52 53 // Options ... 54 func (p Plugin) Options(options m.PluginOptions) (result *stub.ApiPluginOptionsResult) { 55 56 var actions = make(map[string]stub.ApiPluginOptionsResultEntityAction) 57 for k, v := range options.ActorActions { 58 actions[k] = stub.ApiPluginOptionsResultEntityAction{ 59 Name: v.Name, 60 Description: v.Description, 61 ImageUrl: common.StringValue(v.ImageUrl), 62 Icon: common.StringValue(v.Icon), 63 } 64 } 65 66 var states = make(map[string]stub.ApiPluginOptionsResultEntityState) 67 for k, v := range options.ActorStates { 68 states[k] = stub.ApiPluginOptionsResultEntityState{ 69 Name: v.Name, 70 Description: v.Description, 71 ImageUrl: common.StringValue(v.ImageUrl), 72 Icon: common.StringValue(v.Icon), 73 } 74 } 75 76 result = &stub.ApiPluginOptionsResult{ 77 Triggers: options.Triggers, 78 Actors: options.Actors, 79 ActorCustomAttrs: options.ActorCustomAttrs, 80 ActorAttrs: AttributeToApi(options.ActorAttrs), 81 ActorCustomActions: options.ActorCustomActions, 82 ActorActions: actions, 83 ActorCustomStates: options.ActorCustomStates, 84 ActorStates: states, 85 ActorCustomSetts: options.ActorCustomSetts, 86 ActorSetts: AttributeToApi(options.ActorSetts), 87 Setts: AttributeToApi(options.Setts), 88 } 89 return 90 } 91 92 // ToSearchResult ... 93 func (p Plugin) ToSearchResult(list []*m.Plugin) *stub.ApiSearchPluginResult { 94 95 items := make([]stub.ApiPluginShort, 0, len(list)) 96 97 for _, i := range list { 98 items = append(items, stub.ApiPluginShort{ 99 Name: i.Name, 100 Version: i.Version, 101 Enabled: i.Enabled, 102 System: i.System, 103 }) 104 } 105 106 return &stub.ApiSearchPluginResult{ 107 Items: items, 108 } 109 } 110 111 func (p Plugin) ToGetPlugin(plugin *m.Plugin, options m.PluginOptions) (result *stub.ApiPlugin) { 112 113 var settings = make(map[string]stub.ApiAttribute) 114 if options.Setts != nil && plugin.Settings != nil { 115 setts := options.Setts.Copy() 116 setts.Deserialize(plugin.Settings) 117 settings = AttributeToApi(setts) 118 } 119 result = &stub.ApiPlugin{ 120 Name: plugin.Name, 121 Version: plugin.Version, 122 Enabled: plugin.Enabled, 123 System: plugin.System, 124 Actor: plugin.Actor, 125 Settings: settings, 126 Options: p.Options(options), 127 IsLoaded: common.Bool(plugin.IsLoaded), 128 } 129 return 130 }