-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
144 lines (117 loc) · 3.39 KB
/
utils.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2021 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
package util
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
// NormalizeTopic convert command topics to long form
func NormalizeTopic(topic string) string {
if strings.HasPrefix(topic, "c//") {
topic = strings.Replace(topic, "c", "command", 1)
}
if strings.HasSuffix(topic, "/q/#") {
topic = strings.Replace(topic, "/q/#", "/req/#", 1)
}
return topic
}
// ParseCmdTopic parses a MQTT message topic.
func ParseCmdTopic(topic string) (string, string, string) {
if strings.HasPrefix(topic, "c//") {
end := 3 + strings.IndexRune(topic[3:], '/')
cmdType := topic[end+1 : end+2]
prefix := topic[3:end]
suffix := topic[end+3:]
return cmdType, prefix, suffix
}
if strings.HasPrefix(topic, "command//") {
end := 9 + strings.IndexRune(topic[9:], '/')
cmdType := topic[end+1 : end+4]
prefix := topic[9:end]
suffix := topic[end+5:]
return cmdType, prefix, suffix
}
return "", "", ""
}
// ResponseStatusTopic builds the response status topic, e.g. for a received inbox topic
// "command///req/${req-id}/response" the reply should be "command///res/${req-id}/${status}".
func ResponseStatusTopic(inboxTopic string, status int) string {
_, prefix, suffix := ParseCmdTopic(inboxTopic)
end := strings.IndexRune(suffix, '/')
return fmt.Sprintf("command//%s/res/%s/%v", prefix, suffix[:end], status)
}
// NewHonoUserName returns the Hono connection user name.
func NewHonoUserName(deviceAuthID, hubTenantID string) string {
return deviceAuthID + "@" + hubTenantID
}
// ValidPath returns error if the provided filename is not a valid one.
func ValidPath(filename string) error {
if len(filename) == 0 {
return errors.New("empty path")
}
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return nil
}
// FileExists returns if the file with provided name exists.
func FileExists(filename string) bool {
if len(filename) == 0 {
return false
}
info, err := os.Stat(filename)
if info == nil || os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// ParseTimeout converts timeout from string to time.Duration.
func ParseTimeout(timeout string) time.Duration {
l := len(timeout)
if l > 0 {
t := time.Duration(-1)
switch timeout[l-1] {
case 'm':
if i, err := strconv.Atoi(timeout[:l-1]); err == nil {
t = time.Duration(i) * time.Minute
}
case 's':
if timeout[l-2] == 'm' {
if i, err := strconv.Atoi(timeout[:l-2]); err == nil {
t = time.Duration(i) * time.Millisecond
}
} else {
if i, err := strconv.Atoi(timeout[:l-1]); err == nil {
t = time.Duration(i) * time.Second
}
}
default:
if i, err := strconv.Atoi(timeout); err == nil {
t = time.Duration(i) * time.Second
}
}
if inTimeoutRange(t) {
return t
}
}
return 60 * time.Second
}
func inTimeoutRange(timeout time.Duration) bool {
return timeout >= 0 && timeout < time.Hour
}