github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/endpoint/entity_storage.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 endpoint
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"time"
    25  
    26  	"github.com/e154/smart-home/common"
    27  	m "github.com/e154/smart-home/models"
    28  )
    29  
    30  // EntityStorageEndpoint ...
    31  type EntityStorageEndpoint struct {
    32  	*CommonEndpoint
    33  }
    34  
    35  // NewEntityStorageEndpoint ...
    36  func NewEntityStorageEndpoint(common *CommonEndpoint) *EntityStorageEndpoint {
    37  	return &EntityStorageEndpoint{
    38  		CommonEndpoint: common,
    39  	}
    40  }
    41  
    42  // GetList ...
    43  func (i *EntityStorageEndpoint) GetList(ctx context.Context, entityIds []common.EntityId, pagination common.PageParams,
    44  	startDate, endDate *time.Time) (result *m.EntityStorageList, total int64, err error) {
    45  
    46  	keyResult := fmt.Sprintf("entity_storage_%d_%d_%s_%s_%v_%s_%s", pagination.Limit, pagination.Offset,
    47  		pagination.Order, pagination.SortBy, entityIds, startDate, endDate)
    48  
    49  	keyTotal := fmt.Sprintf("%s_total", keyResult)
    50  
    51  	if ok, _ := i.cache.IsExist(ctx, keyResult); ok {
    52  		v, _ := i.cache.Get(ctx, keyResult)
    53  		result = v.(*m.EntityStorageList)
    54  		vTotal, _ := i.cache.Get(ctx, keyTotal)
    55  		total = vTotal.(int64)
    56  		return
    57  	}
    58  
    59  	var items []*m.EntityStorage
    60  	if items, total, err = i.adaptors.EntityStorage.List(ctx, pagination.Limit, pagination.Offset,
    61  		pagination.Order, pagination.SortBy, entityIds, startDate, endDate); err != nil {
    62  		return
    63  	}
    64  
    65  	var entitiesList = map[common.EntityId]*m.Entity{}
    66  
    67  	for j := range items {
    68  		entitiesList[items[j].EntityId] = nil
    69  	}
    70  
    71  	if len(entityIds) == 0 {
    72  		for id := range entitiesList {
    73  			entityIds = append(entityIds, id)
    74  		}
    75  	}
    76  
    77  	var entities []*m.Entity
    78  	if entities, err = i.adaptors.Entity.GetByIdsSimple(ctx, entityIds); err != nil {
    79  		return
    80  	}
    81  
    82  	attributes := make(map[common.EntityId]m.Attributes)
    83  
    84  	for _, entity := range entities {
    85  		entitiesList[entity.Id] = entity
    86  		attributes[entity.Id] = entity.Attributes
    87  	}
    88  
    89  	var ok bool
    90  	var entity *m.Entity
    91  	for _, item := range items {
    92  		item.StateDescription = item.State
    93  		if entity, ok = entitiesList[item.EntityId]; !ok {
    94  			continue
    95  		}
    96  		item.EntityDescription = entity.Id.String()
    97  		if entity.Description != "" {
    98  			item.EntityDescription = entity.Description
    99  		}
   100  		for _, state := range entity.States {
   101  			if state.Name == item.State && state.Description != "" {
   102  				item.StateDescription = state.Description
   103  				continue
   104  			}
   105  		}
   106  	}
   107  
   108  	result = &m.EntityStorageList{
   109  		Items:      items,
   110  		Attributes: attributes,
   111  	}
   112  
   113  	_ = i.cache.Put(ctx, keyResult, result, 10*time.Second)
   114  	_ = i.cache.Put(ctx, keyTotal, total, 10*time.Second)
   115  
   116  	return
   117  }