-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
58 lines (47 loc) · 913 Bytes
/
common.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
51
52
53
54
55
56
57
58
package main
import (
"strings"
"strconv"
"errors"
"unicode"
"fmt"
)
const MAX_POWER = 255
type Config struct {
Settings struct {
TransitionSpeed string
Latitude float64
Longitude float64
PingIp string
}
}
const (
PingUnknown = iota
PingUp
PingDown
)
func getTransitionSpeed(str string) (int, error) {
if len(str) <= 0 {
return -1, errors.New("No transition time given")
}
speed := strings.Replace(strings.ToLower(strings.TrimSpace(str)), " ", "", -1)
timeType := speed[len(speed)-1:len(speed)]
if !unicode.IsLetter([]rune(timeType)[0]) {
timeType = "s"
speed += "s"
}
timeSpeed, e := strconv.Atoi(speed[:len(speed)-1])
if e != nil {
return -1, e
}
switch timeType {
case "s":
case "m":
timeSpeed *= 60
case "h":
timeSpeed *= 60 * 60
default:
return -1, fmt.Errorf("Time type `%s` given, but is not supported", timeType)
}
return timeSpeed, nil
}