github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/entity.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  	"fmt"
    23  	"github.com/e154/smart-home/api/stub"
    24  	"github.com/e154/smart-home/common"
    25  	m "github.com/e154/smart-home/models"
    26  )
    27  
    28  // Entity ...
    29  type Entity struct{}
    30  
    31  // NewEntityDto ...
    32  func NewEntityDto() Entity {
    33  	return Entity{}
    34  }
    35  
    36  // AddEntity ...
    37  func (r Entity) AddEntity(obj *stub.ApiNewEntityRequest) (entity *m.Entity) {
    38  
    39  	entity = &m.Entity{
    40  		Id:           common.EntityId(fmt.Sprintf("%s.%s", obj.PluginName, obj.Name)),
    41  		Description:  obj.Description,
    42  		PluginName:   obj.PluginName,
    43  		AutoLoad:     obj.AutoLoad,
    44  		RestoreState: obj.RestoreState,
    45  		Icon:         obj.Icon,
    46  		Attributes:   AttributeFromApi(obj.Attributes),
    47  		Settings:     AttributeFromApi(obj.Settings),
    48  		Metrics:      AddMetric(obj.Metrics),
    49  		ImageId:      obj.ImageId,
    50  		AreaId:       obj.AreaId,
    51  	}
    52  
    53  	// actions
    54  	for _, a := range obj.Actions {
    55  		action := &m.EntityAction{
    56  			Name:        a.Name,
    57  			Description: a.Description,
    58  			Icon:        a.Icon,
    59  			Type:        a.Type,
    60  			ImageId:     a.ImageId,
    61  			ScriptId:    a.ScriptId,
    62  		}
    63  
    64  		entity.Actions = append(entity.Actions, action)
    65  	}
    66  	// states
    67  	for _, s := range obj.States {
    68  		state := &m.EntityState{
    69  			Name:        s.Name,
    70  			Description: s.Description,
    71  			Icon:        s.Icon,
    72  			Style:       s.Style,
    73  			ImageId:     s.ImageId,
    74  		}
    75  
    76  		entity.States = append(entity.States, state)
    77  	}
    78  
    79  	// scripts
    80  	for _, id := range obj.ScriptIds {
    81  		script := &m.Script{
    82  			Id: id,
    83  		}
    84  		entity.Scripts = append(entity.Scripts, script)
    85  	}
    86  
    87  	// tags
    88  	for _, name := range obj.Tags {
    89  		entity.Tags = append(entity.Tags, &m.Tag{
    90  			Name: name,
    91  		})
    92  	}
    93  
    94  	// parent
    95  	if obj.ParentId != nil {
    96  		parentId := common.EntityId(*obj.ParentId)
    97  		entity.ParentId = &parentId
    98  	}
    99  
   100  	return
   101  }
   102  
   103  // UpdateEntity ...
   104  func (r Entity) UpdateEntity(obj *stub.EntityServiceUpdateEntityJSONBody) (entity *m.Entity) {
   105  	entity = &m.Entity{
   106  		Id:           common.EntityId(obj.Id),
   107  		Description:  obj.Description,
   108  		PluginName:   obj.PluginName,
   109  		Actions:      make([]*m.EntityAction, 0),
   110  		States:       make([]*m.EntityState, 0),
   111  		Scripts:      make([]*m.Script, 0),
   112  		AutoLoad:     obj.AutoLoad,
   113  		RestoreState: obj.RestoreState,
   114  		Icon:         obj.Icon,
   115  		Attributes:   AttributeFromApi(obj.Attributes),
   116  		Settings:     AttributeFromApi(obj.Settings),
   117  		ParentId:     nil,
   118  		Metrics:      AddMetric(obj.Metrics),
   119  		ImageId:      obj.ImageId,
   120  		AreaId:       obj.AreaId,
   121  	}
   122  
   123  	// actions
   124  	for _, a := range obj.Actions {
   125  		action := &m.EntityAction{
   126  			Name:        a.Name,
   127  			Description: a.Description,
   128  			Icon:        a.Icon,
   129  			Type:        a.Type,
   130  			ImageId:     a.ImageId,
   131  			ScriptId:    a.ScriptId,
   132  		}
   133  
   134  		entity.Actions = append(entity.Actions, action)
   135  	}
   136  	// states
   137  	for _, s := range obj.States {
   138  		state := &m.EntityState{
   139  			Name:        s.Name,
   140  			Description: s.Description,
   141  			Icon:        s.Icon,
   142  			Style:       s.Style,
   143  			ImageId:     s.ImageId,
   144  		}
   145  
   146  		entity.States = append(entity.States, state)
   147  	}
   148  
   149  	// scripts
   150  	for _, id := range obj.ScriptIds {
   151  		script := &m.Script{
   152  			Id: id,
   153  		}
   154  		entity.Scripts = append(entity.Scripts, script)
   155  	}
   156  
   157  	// tags
   158  	for _, name := range obj.Tags {
   159  		entity.Tags = append(entity.Tags, &m.Tag{
   160  			Name: name,
   161  		})
   162  	}
   163  
   164  	// parent
   165  	if obj.ParentId != nil {
   166  		parentId := common.EntityId(*obj.ParentId)
   167  		entity.ParentId = &parentId
   168  	}
   169  	return
   170  }
   171  
   172  // ToSearchResult ...
   173  func (r Entity) ToSearchResult(list []*m.Entity) *stub.ApiSearchEntityResult {
   174  
   175  	items := make([]stub.ApiEntityShort, 0, len(list))
   176  
   177  	for _, i := range list {
   178  		items = append(items, *r.ToEntityShort(i))
   179  	}
   180  
   181  	return &stub.ApiSearchEntityResult{
   182  		Items: items,
   183  	}
   184  }
   185  
   186  // ToListResult ...
   187  func (r Entity) ToListResult(list []*m.Entity) []*stub.ApiEntityShort {
   188  
   189  	items := make([]*stub.ApiEntityShort, 0, len(list))
   190  
   191  	for _, i := range list {
   192  		items = append(items, r.ToEntityShort(i))
   193  	}
   194  
   195  	return items
   196  }
   197  
   198  // ToEntityShort ...
   199  func (r Entity) ToEntityShort(entity *m.Entity) (obj *stub.ApiEntityShort) {
   200  	obj = &stub.ApiEntityShort{
   201  		Id:           entity.Id.String(),
   202  		PluginName:   entity.PluginName,
   203  		Description:  entity.Description,
   204  		Icon:         entity.Icon,
   205  		AutoLoad:     entity.AutoLoad,
   206  		RestoreState: entity.RestoreState,
   207  		CreatedAt:    entity.CreatedAt,
   208  		UpdatedAt:    entity.UpdatedAt,
   209  		ParentId:     entity.ParentId.StringPtr(),
   210  		IsLoaded:     common.Bool(entity.IsLoaded),
   211  	}
   212  	// area
   213  	if entity.Area != nil {
   214  		obj.Area = &stub.ApiArea{
   215  			Id:          entity.Area.Id,
   216  			Name:        entity.Area.Name,
   217  			Description: entity.Area.Description,
   218  		}
   219  	}
   220  	// tags
   221  	for _, tag := range entity.Tags {
   222  		obj.Tags = append(obj.Tags, tag.Name)
   223  	}
   224  	return
   225  }
   226  
   227  // ToEntity ...
   228  func ToEntity(entity *m.Entity) (obj *stub.ApiEntity) {
   229  	if entity == nil {
   230  		return
   231  	}
   232  	imageDto := NewImageDto()
   233  	scriptDto := NewScriptDto()
   234  	obj = &stub.ApiEntity{
   235  		Id:           entity.Id.String(),
   236  		PluginName:   entity.PluginName,
   237  		Description:  entity.Description,
   238  		Icon:         entity.Icon,
   239  		AutoLoad:     entity.AutoLoad,
   240  		RestoreState: entity.RestoreState,
   241  		IsLoaded:     common.Bool(entity.IsLoaded),
   242  		Actions:      make([]stub.ApiEntityAction, 0, len(entity.Actions)),
   243  		States:       make([]stub.ApiEntityState, 0, len(entity.States)),
   244  		Scripts:      make([]stub.ApiScript, 0, len(entity.Scripts)),
   245  		CreatedAt:    entity.CreatedAt,
   246  		UpdatedAt:    entity.UpdatedAt,
   247  		Attributes:   AttributeToApi(entity.Attributes),
   248  		Settings:     AttributeToApi(entity.Settings),
   249  		Metrics:      Metrics(entity.Metrics),
   250  	}
   251  	// area
   252  	if entity.Area != nil {
   253  		obj.Area = &stub.ApiArea{
   254  			Id:          entity.Area.Id,
   255  			Name:        entity.Area.Name,
   256  			Description: entity.Area.Description,
   257  		}
   258  	}
   259  	// image
   260  	if entity.Image != nil {
   261  		obj.Image = imageDto.ToImageShort(entity.Image)
   262  	}
   263  	// parent
   264  	if entity.ParentId != nil {
   265  		obj.Parent = &stub.ApiEntityParent{
   266  			Id: entity.ParentId.String(),
   267  		}
   268  	}
   269  	// actions
   270  	for _, a := range entity.Actions {
   271  		action := stub.ApiEntityAction{
   272  			Name:        a.Name,
   273  			Description: a.Description,
   274  			Icon:        a.Icon,
   275  			Type:        a.Type,
   276  		}
   277  		// images
   278  		if a.Image != nil {
   279  			action.Image = imageDto.ToImage(a.Image)
   280  		}
   281  		// script
   282  		if a.Script != nil {
   283  			action.Script = scriptDto.GetStubScriptShort(a.Script)
   284  			action.ScriptId = common.Int64(a.Script.Id)
   285  		}
   286  		obj.Actions = append(obj.Actions, action)
   287  	}
   288  	// states
   289  	for _, s := range entity.States {
   290  		state := stub.ApiEntityState{
   291  			Name:        s.Name,
   292  			Description: s.Description,
   293  			Icon:        s.Icon,
   294  			Image:       nil,
   295  			Style:       s.Style,
   296  		}
   297  		// images
   298  		if s.Image != nil {
   299  			state.Image = imageDto.ToImage(s.Image)
   300  		}
   301  		obj.States = append(obj.States, state)
   302  	}
   303  	// scripts
   304  	for _, s := range entity.Scripts {
   305  		script := scriptDto.GetStubScriptShort(s)
   306  		obj.Scripts = append(obj.Scripts, *script)
   307  		obj.ScriptIds = append(obj.ScriptIds, s.Id)
   308  	}
   309  	//tags
   310  	for _, tag := range entity.Tags {
   311  		obj.Tags = append(obj.Tags, tag.Name)
   312  	}
   313  	return
   314  }
   315  
   316  func (r Entity) ImportEntity(from *stub.ApiEntity) (to *m.Entity) {
   317  
   318  	areaId, area := ImportArea(from.Area)
   319  	var parentId *common.EntityId
   320  	if from.Parent != nil {
   321  		parentId = common.NewEntityId(from.Parent.Id)
   322  	}
   323  
   324  	_, image := ImportImage(from.Image)
   325  	to = &m.Entity{
   326  		Id:           common.EntityId(from.Id),
   327  		Description:  from.Description,
   328  		PluginName:   from.PluginName,
   329  		Icon:         from.Icon,
   330  		Image:        image,
   331  		Actions:      make([]*m.EntityAction, 0, len(from.Actions)),
   332  		States:       make([]*m.EntityState, 0, len(from.States)),
   333  		Area:         area,
   334  		AreaId:       areaId,
   335  		Metrics:      AddMetric(from.Metrics),
   336  		Scripts:      make([]*m.Script, 0, len(from.Scripts)),
   337  		Attributes:   AttributeFromApi(from.Attributes),
   338  		Settings:     AttributeFromApi(from.Settings),
   339  		AutoLoad:     from.AutoLoad,
   340  		RestoreState: from.RestoreState,
   341  		ParentId:     parentId,
   342  	}
   343  
   344  	// ACTIONS
   345  	for _, action := range from.Actions {
   346  		imageId, img := ImportImage(action.Image)
   347  		scriptId, script := ImportScript(action.Script)
   348  		to.Actions = append(to.Actions, &m.EntityAction{
   349  			Name:        action.Name,
   350  			Description: action.Description,
   351  			Icon:        action.Icon,
   352  			Image:       img,
   353  			ImageId:     imageId,
   354  			Script:      script,
   355  			ScriptId:    scriptId,
   356  			Type:        action.Type,
   357  		})
   358  	}
   359  
   360  	// STATES
   361  	for _, state := range from.States {
   362  		imageId, img := ImportImage(state.Image)
   363  		to.States = append(to.States, &m.EntityState{
   364  			Name:        state.Name,
   365  			Description: state.Description,
   366  			Icon:        state.Icon,
   367  			Image:       img,
   368  			ImageId:     imageId,
   369  			Style:       state.Style,
   370  		})
   371  	}
   372  
   373  	// SCRIPTS
   374  	for _, script := range from.Scripts {
   375  		_, _script := ImportScript(&script)
   376  		to.Scripts = append(to.Scripts, _script)
   377  	}
   378  
   379  	// TAGS
   380  	for _, name := range from.Tags {
   381  		to.Tags = append(to.Tags, &m.Tag{
   382  			Name: name,
   383  		})
   384  	}
   385  	return
   386  }