github.com/enbility/spine-go@v0.7.0/spine/entity.go (about) 1 package spine 2 3 import ( 4 "sync" 5 6 "github.com/ahmetb/go-linq/v3" 7 "github.com/enbility/spine-go/api" 8 "github.com/enbility/spine-go/model" 9 "github.com/enbility/spine-go/util" 10 ) 11 12 const DeviceInformationEntityId uint = 0 13 14 var DeviceInformationAddressEntity = []model.AddressEntityType{model.AddressEntityType(DeviceInformationEntityId)} 15 16 type Entity struct { 17 eType model.EntityTypeType 18 address *model.EntityAddressType 19 description *model.DescriptionType 20 fIdGenerator func() uint 21 22 muxGenerator sync.Mutex 23 } 24 25 var _ api.EntityInterface = (*Entity)(nil) 26 27 func NewEntity(eType model.EntityTypeType, deviceAddress *model.AddressDeviceType, entityAddress []model.AddressEntityType) *Entity { 28 entity := &Entity{ 29 eType: eType, 30 address: &model.EntityAddressType{ 31 Device: deviceAddress, 32 Entity: entityAddress, 33 }, 34 } 35 if entityAddress[0] == 0 { 36 // Entity 0 Feature addresses start with 0 37 entity.fIdGenerator = newFeatureIdGenerator(0) 38 } else { 39 // Entity 1 and further Feature addresses start with 1 40 entity.fIdGenerator = newFeatureIdGenerator(1) 41 } 42 43 return entity 44 } 45 46 func (r *Entity) Address() *model.EntityAddressType { 47 return r.address 48 } 49 50 func (r *Entity) EntityType() model.EntityTypeType { 51 return r.eType 52 } 53 54 func (r *Entity) Description() *model.DescriptionType { 55 return r.description 56 } 57 58 func (r *Entity) SetDescription(d *model.DescriptionType) { 59 r.description = d 60 } 61 62 func (r *Entity) NextFeatureId() uint { 63 r.muxGenerator.Lock() 64 defer r.muxGenerator.Unlock() 65 66 return r.fIdGenerator() 67 } 68 69 func EntityAddressType(deviceAddress *model.AddressDeviceType, entityAddress []model.AddressEntityType) *model.EntityAddressType { 70 return &model.EntityAddressType{ 71 Device: deviceAddress, 72 Entity: entityAddress, 73 } 74 } 75 76 func NewEntityAddressType(deviceName string, entityIds []uint) *model.EntityAddressType { 77 return &model.EntityAddressType{ 78 Device: util.Ptr(model.AddressDeviceType(deviceName)), 79 Entity: NewAddressEntityType(entityIds), 80 } 81 } 82 83 func NewAddressEntityType(entityIds []uint) []model.AddressEntityType { 84 var addressEntity []model.AddressEntityType 85 linq.From(entityIds).SelectT(func(i uint) model.AddressEntityType { return model.AddressEntityType(i) }).ToSlice(&addressEntity) 86 return addressEntity 87 } 88 89 func newFeatureIdGenerator(id uint) func() uint { 90 return func() uint { 91 defer func() { id += 1 }() 92 return id 93 } 94 }