Skip to content

Commit 7310dda

Browse files
committed
Migrate to moteconnection, change argument parsing, add architectures in makefile.
1 parent 357383c commit 7310dda

3 files changed

Lines changed: 148 additions & 90 deletions

File tree

cmd/deviceparameter/Makefile

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,31 @@
33
BUILD_DATE = $(shell date -u '+%Y-%m-%d_%H:%M:%S')
44
BUILD_DISTRO = $(shell lsb_release -sd)
55

6-
all:
7-
go build -o deviceparameter -ldflags "-X main.ApplicationBuildDate $(BUILD_DATE) -X main.ApplicationBuildDistro '$(BUILD_DISTRO)'"
6+
all: build
7+
8+
win32: export GOOS=windows
9+
win32: export GOARCH=386
10+
win32: winbuild
11+
12+
win64: export GOOS=windows
13+
win64: export GOARCH=amd64
14+
win64: winbuild
15+
16+
raspberry: export GOOS=linux
17+
raspberry: export GOARCH=arm
18+
raspberry: export GOARM=6
19+
raspberry: build
20+
21+
raspberry2: export GOOS=linux
22+
raspberry2: export GOARCH=arm
23+
raspberry2: export GOARM=7
24+
raspberry2: build
25+
26+
build:
27+
go build -o deviceparameter -ldflags "-X 'main.ApplicationBuildDate=$(BUILD_DATE)' -X 'main.ApplicationBuildDistro=$(BUILD_DISTRO)'"
28+
29+
winbuild:
30+
go build -o deviceparameter.exe -ldflags "-X 'main.ApplicationBuildDate=$(BUILD_DATE)' -X 'main.ApplicationBuildDistro=$(BUILD_DISTRO)'"
31+
32+
install:
33+
install -m 0755 deviceparameter /usr/local/bin

cmd/deviceparameter/deviceparameter.go

Lines changed: 93 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,90 @@ import "fmt"
77
import "os"
88
import "log"
99
import "time"
10+
import "errors"
1011
import "encoding/hex"
1112
import "encoding/binary"
1213
import "strconv"
1314
import "bytes"
1415

1516
import "github.com/jessevdk/go-flags"
1617
import "github.com/proactivity-lab/go-loggers"
17-
import "github.com/proactivity-lab/go-sfconnection"
18+
import "github.com/proactivity-lab/go-moteconnection"
1819
import "github.com/thinnect/go-devparam"
1920

2021
const ApplicationVersionMajor = 0
21-
const ApplicationVersionMinor = 1
22-
const ApplicationVersionPatch = 3
22+
const ApplicationVersionMinor = 2
23+
const ApplicationVersionPatch = 0
2324

2425
var ApplicationBuildDate string
2526
var ApplicationBuildDistro string
2627

2728
func parseValue(opts Options) ([]byte, error) {
29+
var value []byte
30+
var err error
2831
var t interface{}
29-
if opts.Uint8 {
30-
if v, err := strconv.ParseUint(opts.Value, 10, 8); err == nil {
32+
c := 0
33+
34+
if len(opts.Uint8) > 0 {
35+
if v, err := strconv.ParseUint(opts.Uint8, 10, 8); err == nil {
3136
t = uint8(v)
37+
c++
3238
} else {
3339
return nil, err
3440
}
35-
} else if opts.Uint16 {
36-
if v, err := strconv.ParseUint(opts.Value, 10, 16); err == nil {
41+
}
42+
if len(opts.Uint16) > 0 {
43+
if v, err := strconv.ParseUint(opts.Uint16, 10, 16); err == nil {
3744
t = uint16(v)
45+
c++
3846
} else {
3947
return nil, err
4048
}
41-
} else if opts.Uint32 {
42-
if v, err := strconv.ParseUint(opts.Value, 10, 32); err == nil {
49+
}
50+
if len(opts.Uint32) > 0 {
51+
if v, err := strconv.ParseUint(opts.Uint32, 10, 32); err == nil {
4352
t = uint32(v)
53+
c++
4454
} else {
4555
return nil, err
4656
}
47-
} else if opts.Uint64 {
48-
if v, err := strconv.ParseUint(opts.Value, 10, 64); err == nil {
57+
}
58+
if len(opts.Uint64) > 0 {
59+
if v, err := strconv.ParseUint(opts.Uint64, 10, 64); err == nil {
4960
t = uint64(v)
61+
c++
5062
} else {
5163
return nil, err
5264
}
53-
} else if opts.Int8 {
54-
if v, err := strconv.ParseInt(opts.Value, 10, 8); err == nil {
65+
}
66+
if len(opts.Int8) > 0 {
67+
if v, err := strconv.ParseInt(opts.Int8, 10, 8); err == nil {
5568
t = int8(v)
69+
c++
5670
} else {
5771
return nil, err
5872
}
59-
} else if opts.Int16 {
60-
if v, err := strconv.ParseInt(opts.Value, 10, 16); err == nil {
73+
}
74+
if len(opts.Int16) > 0 {
75+
if v, err := strconv.ParseInt(opts.Int16, 10, 16); err == nil {
6176
t = int16(v)
77+
c++
6278
} else {
6379
return nil, err
6480
}
65-
} else if opts.Int32 {
66-
if v, err := strconv.ParseInt(opts.Value, 10, 32); err == nil {
81+
}
82+
if len(opts.Int32) > 0 {
83+
if v, err := strconv.ParseInt(opts.Int32, 10, 32); err == nil {
6784
t = int32(v)
85+
c++
6886
} else {
6987
return nil, err
7088
}
71-
} else if opts.Int64 {
72-
if v, err := strconv.ParseInt(opts.Value, 10, 64); err == nil {
89+
}
90+
if len(opts.Int64) > 0 {
91+
if v, err := strconv.ParseInt(opts.Int64, 10, 64); err == nil {
7392
t = int64(v)
93+
c++
7494
} else {
7595
return nil, err
7696
}
@@ -83,41 +103,51 @@ func parseValue(opts Options) ([]byte, error) {
83103
if err := binary.Write(buf, binary.BigEndian, t); err != nil {
84104
return nil, err
85105
}
86-
return buf.Bytes(), nil
106+
value = buf.Bytes()
87107
}
88108
}
89109

90-
if opts.String {
91-
return []byte(opts.Value), nil
110+
if len(opts.Value) > 0 {
111+
value, err = hex.DecodeString(opts.Value)
112+
c++
92113
}
93114

94-
return hex.DecodeString(opts.Value)
115+
if len(opts.String) > 0 {
116+
value = []byte(opts.String)
117+
c++
118+
}
119+
120+
if c > 1 {
121+
return nil, errors.New("Multiple values specified for parameter!")
122+
}
123+
124+
return value, err
95125
}
96126

97127
type Options struct {
98128
Positional struct {
99-
ConnectionString string `description:"Connectionstring sf@HOST:PORT"`
129+
ConnectionString string `description:"Connectionstring sf@HOST:PORT or serial@PORT:BAUD"`
100130
} `positional-args:"yes"`
101131

102-
Group sfconnection.AMGroup `short:"g" long:"group" default:"22" description:"Packet AM Group (hex)"`
103-
Address sfconnection.AMAddr `short:"a" long:"address" default:"5678" description:"Source AM address (hex)"`
104-
Destination sfconnection.AMAddr `short:"d" long:"destination" default:"0" description:"Destination AM address (hex)"`
132+
Group moteconnection.AMGroup `short:"g" long:"group" default:"22" description:"Packet AM Group (hex)"`
133+
Address moteconnection.AMAddr `short:"a" long:"address" default:"5678" description:"Source AM address (hex)"`
134+
Destination moteconnection.AMAddr `short:"d" long:"destination" default:"0" description:"Destination AM address (hex)"`
105135

106136
Timeout int `long:"timeout" default:"1" description:"Get/set action timeout (seconds)"`
107137
Retries int `long:"retries" default:"3" description:"Get/set action retries"`
108138

109139
Parameter []string `short:"p" long:"parameter" description:"List of parameter names"`
110-
Value string `short:"v" long:"value" description:"Value to set (single parameter only)"`
111-
112-
String bool `long:"string" description:"Value is string"`
113-
Uint8 bool `long:"uint8" description:"Value is uint8"`
114-
Uint16 bool `long:"uint16" description:"Value is uint16"`
115-
Uint32 bool `long:"uint32" description:"Value is uint32"`
116-
Uint64 bool `long:"uint64" description:"Value is uint64"`
117-
Int8 bool `long:"int8" description:"Value is int8"`
118-
Int16 bool `long:"int16" description:"Value is int16"`
119-
Int32 bool `long:"int32" description:"Value is int32"`
120-
Int64 bool `long:"int64" description:"Value is int64"`
140+
141+
Value string `short:"v" long:"value" description:"Set value, presented as a raw hex buffer"`
142+
String string `long:"str" description:"Set value, type is string"`
143+
Uint8 string `long:"u8" description:"Set value, type is uint8"`
144+
Uint16 string `long:"u16" description:"Set value, type is uint16"`
145+
Uint32 string `long:"u32" description:"Set value, type is uint32"`
146+
Uint64 string `long:"u64" description:"Set value, type is uint64"`
147+
Int8 string `long:"i8" description:"Set value, type is int8"`
148+
Int16 string `long:"i16" description:"Set value, type is int16"`
149+
Int32 string `long:"i32" description:"Set value, type is int32"`
150+
Int64 string `long:"i64" description:"Set value, type is int64"`
121151

122152
Debug []bool `short:"D" long:"debug" description:"Debug mode, print raw packets"`
123153
Quiet []bool `short:"Q" long:"quiet" description:"Quiet mode, print only values"`
@@ -144,43 +174,45 @@ func main() {
144174
os.Exit(1)
145175
}
146176

147-
host, port, err := sfconnection.ParseSfConnectionString(opts.Positional.ConnectionString)
177+
conn, cs, err := moteconnection.CreateConnection(opts.Positional.ConnectionString)
148178
if err != nil {
149179
fmt.Printf("ERROR: %s\n", err)
150180
os.Exit(1)
151181
}
152182

153-
sfc := sfconnection.NewSfConnection()
154-
155183
var dpm *deviceparameters.DeviceParameterManager = nil
156184
if opts.Destination == 0 {
157-
dpm = deviceparameters.NewDeviceParameterManager(sfc)
185+
dpm = deviceparameters.NewDeviceParameterManager(conn)
158186
} else {
159-
dpm = deviceparameters.NewDeviceParameterActiveMessageManager(sfc, opts.Group, opts.Address, opts.Destination)
187+
dpm = deviceparameters.NewDeviceParameterActiveMessageManager(conn, opts.Group, opts.Address, opts.Destination)
160188
}
161189
dpm.SetTimeout(time.Duration(opts.Timeout) * time.Second)
162190
dpm.SetRetries(opts.Retries)
163191

164192
logger := logsetup(len(opts.Debug))
165193
if len(opts.Debug) > 0 {
166-
sfc.SetLoggers(logger)
194+
conn.SetLoggers(logger)
167195
}
168196
dpm.SetLoggers(logger)
169197

170-
// Connect to the host
171-
err = sfc.Connect(host, port)
198+
err = conn.Connect()
172199
if err != nil {
173-
logger.Error.Printf("Unable to connect to %s:%d\n", host, port)
200+
logger.Error.Printf("Unable to connect with %s: %s\n", cs, err)
174201
os.Exit(1)
175202
}
176203
if len(opts.Quiet) == 0 {
177-
logger.Info.Printf("Connected to %s:%d\n", host, port)
204+
logger.Info.Printf("Connected with %s\n", cs)
178205
}
179206

180207
success := false
181208

182209
if len(opts.Parameter) > 0 {
183-
if len(opts.Value) == 0 || len(opts.Parameter) > 1 {
210+
value, err := parseValue(opts)
211+
if err != nil {
212+
logger.Error.Printf("%s", err)
213+
} else if value != nil && len(opts.Parameter) > 1 {
214+
logger.Error.Printf("Value and multiple parameters provided\n")
215+
} else if value == nil {
184216
for _, parameter := range opts.Parameter {
185217
if len(opts.Quiet) == 0 {
186218
logger.Info.Printf("Get %s\n", parameter)
@@ -194,21 +226,18 @@ func main() {
194226
}
195227
}
196228
} else { // Set only if value and only a single parameter
197-
value, err := parseValue(opts)
198-
if err == nil {
199-
logger.Info.Printf("Set %s to 0x%X\n", opts.Parameter[0], value)
200-
if val, err := dpm.SetValue(opts.Parameter[0], value); err == nil {
201-
logger.Info.Printf("%s = %s\n", val.Name, val)
202-
success = true
203-
} else {
204-
logger.Info.Printf("Failed: %s\n", err)
205-
}
229+
logger.Info.Printf("Set %s to 0x%X\n", opts.Parameter[0], value)
230+
if val, err := dpm.SetValue(opts.Parameter[0], value); err == nil {
231+
logger.Info.Printf("%s = %s\n", val.Name, val)
232+
success = true
206233
} else {
207-
logger.Error.Printf("%s", err)
234+
logger.Info.Printf("Failed: %s\n", err)
208235
}
209236
}
210237
} else {
211-
logger.Info.Printf("Get parameter list:\n")
238+
if len(opts.Quiet) == 0 {
239+
logger.Info.Printf("Get parameter list:\n")
240+
}
212241
pchan, err := dpm.GetList()
213242
if err == nil {
214243
param := <-pchan
@@ -226,10 +255,13 @@ func main() {
226255
}
227256

228257
dpm.Close()
229-
sfc.Disconnect()
258+
conn.Disconnect()
230259
time.Sleep(100 * time.Millisecond)
231260

232261
if success {
262+
if len(opts.Quiet) == 0 {
263+
logger.Info.Printf("Done")
264+
}
233265
os.Exit(0)
234266
} else {
235267
os.Exit(1)

0 commit comments

Comments
 (0)