github.com/enbility/spine-go@v0.7.0/spine/entity_remote.go (about)

     1  package spine
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/enbility/spine-go/api"
     7  	"github.com/enbility/spine-go/model"
     8  )
     9  
    10  type EntityRemote struct {
    11  	*Entity
    12  	device   api.DeviceRemoteInterface
    13  	features []api.FeatureRemoteInterface
    14  
    15  	mux sync.Mutex
    16  }
    17  
    18  func NewEntityRemote(device api.DeviceRemoteInterface, eType model.EntityTypeType, entityAddress []model.AddressEntityType) *EntityRemote {
    19  	return &EntityRemote{
    20  		Entity: NewEntity(eType, device.Address(), entityAddress),
    21  		device: device,
    22  	}
    23  }
    24  
    25  var _ api.EntityRemoteInterface = (*EntityRemote)(nil)
    26  
    27  /* EntityRemoteInterface */
    28  
    29  func (r *EntityRemote) Device() api.DeviceRemoteInterface {
    30  	return r.device
    31  }
    32  
    33  func (r *EntityRemote) UpdateDeviceAddress(address model.AddressDeviceType) {
    34  	r.address.Device = &address
    35  }
    36  
    37  func (r *EntityRemote) AddFeature(f api.FeatureRemoteInterface) {
    38  	r.mux.Lock()
    39  	defer r.mux.Unlock()
    40  
    41  	r.features = append(r.features, f)
    42  }
    43  
    44  func (r *EntityRemote) RemoveAllFeatures() {
    45  	r.mux.Lock()
    46  	defer r.mux.Unlock()
    47  
    48  	r.features = nil
    49  }
    50  
    51  func (r *EntityRemote) FeatureOfTypeAndRole(featureType model.FeatureTypeType, role model.RoleType) api.FeatureRemoteInterface {
    52  	r.mux.Lock()
    53  	defer r.mux.Unlock()
    54  
    55  	for _, f := range r.features {
    56  		if f.Type() == featureType && f.Role() == role {
    57  			return f
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func (r *EntityRemote) FeatureOfAddress(addressFeature *model.AddressFeatureType) api.FeatureRemoteInterface {
    65  	r.mux.Lock()
    66  	defer r.mux.Unlock()
    67  
    68  	for _, f := range r.features {
    69  		if addressFeature != nil && f.Address() != nil &&
    70  			*f.Address().Feature == *addressFeature {
    71  			return f
    72  		}
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func (r *EntityRemote) Features() []api.FeatureRemoteInterface {
    79  	r.mux.Lock()
    80  	defer r.mux.Unlock()
    81  
    82  	return r.features
    83  }