code.pfad.fr/gohmekit@v0.2.1/hapip/characteristic/metadata.go (about)

     1  package characteristic
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.pfad.fr/gohmekit/hapip"
     7  )
     8  
     9  type format interface {
    10  	bool | uint8 | uint16 | uint32 | uint64 | int32 | float64 | string | TLV8
    11  }
    12  
    13  var _ hapip.Characteristic = Metadata[string]{}
    14  
    15  type Metadata[Format format] struct {
    16  	Typ         string
    17  	Description string
    18  
    19  	Unit        string
    20  	MaxLen      int
    21  	MinValue    *Format
    22  	MaxValue    *Format
    23  	MinStep     Format
    24  	ValidValues []Format
    25  	ValidRange  []Format
    26  	MaxDataLen  int
    27  }
    28  
    29  func (m Metadata[Format]) Type() string {
    30  	return m.Typ
    31  }
    32  func (m Metadata[Format]) Meta() hapip.CharacteristicMeta {
    33  	meta := hapip.CharacteristicMeta{
    34  		Format:      m.format(),
    35  		Description: m.Description,
    36  		Unit:        m.Unit,
    37  		MaxLen:      m.MaxLen,
    38  
    39  		MaxDataLen: m.MaxDataLen,
    40  	}
    41  	if m.MinValue != nil {
    42  		meta.MinValue = m.MinValue
    43  	}
    44  	if m.MaxValue != nil {
    45  		meta.MaxValue = m.MaxValue
    46  	}
    47  	var nilF Format
    48  	if m.MinStep != nilF {
    49  		meta.MinStep = m.MinStep
    50  	}
    51  	if m.ValidValues != nil {
    52  		meta.ValidValues = m.ValidValues
    53  	}
    54  	if m.ValidRange != nil {
    55  		meta.ValidRange = m.ValidRange
    56  	}
    57  	return meta
    58  }
    59  func (m Metadata[Format]) format() string {
    60  	var v Format
    61  	switch any(v).(type) {
    62  	case bool:
    63  		return "bool"
    64  	case uint8:
    65  		return "uint8"
    66  	case uint16:
    67  		return "uint16"
    68  	case uint32:
    69  		return "uint32"
    70  	case uint64:
    71  		return "uint64"
    72  	case int32:
    73  		return "int"
    74  	case float64:
    75  		return "float"
    76  	case string:
    77  		return "string"
    78  	case TLV8:
    79  		return "tlv8"
    80  	default:
    81  		panic(fmt.Sprintf("unexpected type: %T", m.MinValue))
    82  	}
    83  }
    84  
    85  func ValuePointer[Format format](v Format) *Format {
    86  	return &v
    87  }