src.dualinventive.com/go/dinet/rpc/objects_config.go

106 lines
2.4 KiB
Go

package rpc
import (
"errors"
"math"
)
// Config is a DI-Net configuration item
type Config struct {
uid uint16
label string
typeName string
defaultVal interface{}
}
// ConfigParam describes the param config structure
type ConfigParam struct {
abstractParams
UID uint16 `json:"uid" msgpack:"uid"`
Value interface{} `json:"value,omitempty" msgpack:"value,omitempty"`
}
// ValueInt64 tries to convert the Value to an int64
func (s *ConfigParam) ValueInt64() (int64, error) {
switch t := s.Value.(type) {
case int:
return int64(t), nil
case uint:
return int64(t), nil
case int8:
return int64(t), nil
case uint8:
return int64(t), nil
case int16:
return int64(t), nil
case uint16:
return int64(t), nil
case int32:
return int64(t), nil
case uint32:
return int64(t), nil
case int64:
return t, nil
}
return s.toValueInt64()
}
func (s *ConfigParam) toValueInt64() (int64, error) {
switch t := s.Value.(type) {
case uint64:
if t < math.MaxInt64 {
return int64(t), nil
}
return 0, errors.New("to high number")
case float32:
if t < math.MaxInt64 && t > math.MinInt64 {
return int64(t), nil
}
return 0, errors.New("to high number")
case float64:
if t < math.MaxInt64 && t > math.MinInt64 {
return int64(t), nil
}
return 0, errors.New("to high number")
}
return 0, errors.New("no integer type")
}
// UID returns the unique identifier of a config
func (s *Config) UID() uint16 {
return s.uid
}
// Label returns the label for a config
func (s *Config) Label() string {
return s.label
}
// Data returns a value object
func (s *Config) Data(val interface{}) *ResultConfigValueItem {
return &ResultConfigValueItem{
UID: s.uid,
Value: val,
}
}
// Info returns a info object
func (s *Config) Info() ResultConfigInfoItem {
return ResultConfigInfoItem{
UID: s.uid,
Label: s.label,
Type: s.typeName,
Default: s.defaultVal,
}
}
// nolint: golint, gochecknoglobals
var (
ConfigToken = Config{uid: 1, label: "token", typeName: "bool"}
ConfigActive = Config{uid: 2, label: "active", typeName: "bool"}
ConfigService = Config{uid: 3, label: "service", typeName: "bool"}
CRTMLoraConfigConfiguration = Config{uid: 100, label: "configuration", typeName: "struct"}
CRTMLoraConfigCalibration = Config{uid: 101, label: "calibration", typeName: "bool"}
ConfigEndpoint = Config{uid: 102, label: "endpoint", typeName: "string"}
)