-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
50 lines (45 loc) · 1.26 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cayennelpp
import (
"errors"
"fmt"
"strconv"
"strings"
)
const (
valueKey = "value"
digitalInputKey = "digital_in"
digitalOutputKey = "digital_out"
analogInputKey = "analog_in"
analogOutputKey = "analog_out"
luminosityKey = "luminosity"
presenceKey = "presence"
temperatureKey = "temperature"
relativeHumidityKey = "relative_humidity"
accelerometerKey = "accelerometer"
barometricPressureKey = "barometric_pressure"
gyrometerKey = "gyrometer"
gpsKey = "gps"
)
func formatName(key string, channel uint8) string {
return fmt.Sprintf("%s_%d", key, channel)
}
func parseName(name string) (string, uint8, error) {
parts := strings.Split(name, "_")
if len(parts) < 2 {
return "", 0, errors.New("Invalid name")
}
key := strings.Join(parts[:len(parts)-1], "_")
if key == "" {
return "", 0, errors.New("Invalid key")
}
channel, err := strconv.Atoi(parts[len(parts)-1])
if err != nil {
return "", 0, err
}
if channel < 0 || channel > 255 {
return "", 0, errors.New("Invalid range")
}
return key, uint8(channel), nil
}