github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/apis/v1/configkey.go (about)

     1  // Copyright 2021 Ewout Prangsma
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Author Ewout Prangsma
    16  //
    17  
    18  package v1
    19  
    20  import (
    21  	"fmt"
    22  	"math"
    23  	"strconv"
    24  )
    25  
    26  // ConfigKey is a strongly typed well known key of a configuration entry of
    27  // a connection of an object type.
    28  type ConfigKey string
    29  
    30  const (
    31  	// ConfigKeyDebug turns on/off debugging information for a specific object.
    32  	// Values are true|false|0|1.
    33  	ConfigKeyDebug ConfigKey = "debug"
    34  	// ConfigKeyServoStraight configures the servo value for the "straight" switch position.
    35  	ConfigKeyServoStraight ConfigKey = "straight"
    36  	// ConfigKeyServoOff configures the servo value for the "off" switch position.
    37  	ConfigKeyServoOff ConfigKey = "off"
    38  	// ConfigKeyServoStep configures the step size for a servo transition from "straight" to "off" position.
    39  	ConfigKeyServoStep ConfigKey = "step"
    40  	// ConfigKeyInvert configures the inversion setting of an i/o pin.
    41  	// Values are true|false|0|1.
    42  	ConfigKeyInvert ConfigKey = "invert"
    43  	// ConfigKeyPulse configures the length of the pulse (for relays) in ms.
    44  	ConfigKeyPulse ConfigKey = "pulse"
    45  	// ConfigKeyThreshold configures the threshold for measured analog values
    46  	// above which a sensor is considered "on".
    47  	ConfigKeyThreshold ConfigKey = "threshold"
    48  )
    49  
    50  // DefaultValue returns the default value for a given configuration key.
    51  func (key ConfigKey) DefaultValue() string {
    52  	switch key {
    53  	case ConfigKeyDebug:
    54  		return "false"
    55  	case ConfigKeyServoStraight:
    56  		return "300"
    57  	case ConfigKeyServoOff:
    58  		return "400"
    59  	case ConfigKeyServoStep:
    60  		return "15"
    61  	case ConfigKeyInvert:
    62  		return "false"
    63  	case ConfigKeyPulse:
    64  		return "1000"
    65  	case ConfigKeyThreshold:
    66  		return "128"
    67  	default:
    68  		return ""
    69  	}
    70  }
    71  
    72  // ValidateValue validates a given value for a given configuration key.
    73  func (key ConfigKey) ValidateValue(value string) error {
    74  	if value == "" {
    75  		// All ok
    76  		return nil
    77  	}
    78  	switch key {
    79  	case ConfigKeyServoStraight, ConfigKeyServoOff, ConfigKeyServoStep:
    80  		if x, err := strconv.Atoi(value); err != nil {
    81  			return fmt.Errorf("not a valid number")
    82  		} else if x < 0 {
    83  			return fmt.Errorf("too small")
    84  		} else if x > 900 {
    85  			return fmt.Errorf("too large")
    86  		}
    87  	case ConfigKeyDebug, ConfigKeyInvert:
    88  		if _, err := strconv.ParseBool(value); err != nil {
    89  			return fmt.Errorf("not a boolean")
    90  		}
    91  	case ConfigKeyPulse:
    92  		if x, err := strconv.Atoi(value); err != nil {
    93  			return fmt.Errorf("not a valid number")
    94  		} else if x <= 0 {
    95  			return fmt.Errorf("too small")
    96  		} else if x > 5000 {
    97  			return fmt.Errorf("too large")
    98  		}
    99  	case ConfigKeyThreshold:
   100  		if x, err := strconv.Atoi(value); err != nil {
   101  			return fmt.Errorf("not a valid number")
   102  		} else if x <= 0 {
   103  			return fmt.Errorf("too small")
   104  		} else if x > math.MaxUint16 {
   105  			return fmt.Errorf("too large")
   106  		}
   107  	}
   108  	return nil
   109  }
   110  
   111  func AllConfigKeys() []ConfigKey {
   112  	return []ConfigKey{
   113  		ConfigKeyDebug,
   114  		ConfigKeyServoStraight,
   115  		ConfigKeyServoOff,
   116  		ConfigKeyServoStep,
   117  		ConfigKeyInvert,
   118  		ConfigKeyThreshold,
   119  	}
   120  }