Skip to content

Commit c3dd2c4

Browse files
committed
Add option to set parameters to NULL.
1 parent 5623294 commit c3dd2c4

1 file changed

Lines changed: 40 additions & 31 deletions

File tree

cmd/deviceparameter/deviceparameter.go

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,32 @@
33

44
package main
55

6-
import "fmt"
7-
import "os"
8-
import "log"
9-
import "time"
10-
import "errors"
11-
import "encoding/hex"
12-
import "encoding/binary"
13-
import "strconv"
14-
import "bytes"
15-
16-
import "github.com/jessevdk/go-flags"
17-
import "github.com/proactivity-lab/go-loggers"
18-
import "github.com/proactivity-lab/go-moteconnection"
19-
import "github.com/thinnect/go-devparam"
6+
import (
7+
"bytes"
8+
"encoding/binary"
9+
"encoding/hex"
10+
"errors"
11+
"fmt"
12+
"log"
13+
"os"
14+
"strconv"
15+
"time"
16+
17+
"github.com/jessevdk/go-flags"
18+
"github.com/proactivity-lab/go-loggers"
19+
"github.com/proactivity-lab/go-moteconnection"
20+
21+
deviceparameters "github.com/thinnect/go-devparam"
22+
)
2023

2124
const ApplicationVersionMajor = 0
22-
const ApplicationVersionMinor = 2
23-
const ApplicationVersionPatch = 1
25+
const ApplicationVersionMinor = 3
26+
const ApplicationVersionPatch = 0
2427

2528
var ApplicationBuildDate string
2629
var ApplicationBuildDistro string
2730

28-
func parseValue(opts Options) ([]byte, error) {
31+
func parseValue(opts Options) ([]byte, bool, error) {
2932
var value []byte
3033
var err error
3134
var t interface{}
@@ -36,63 +39,63 @@ func parseValue(opts Options) ([]byte, error) {
3639
t = uint8(v)
3740
c++
3841
} else {
39-
return nil, err
42+
return nil, false, err
4043
}
4144
}
4245
if len(opts.Uint16) > 0 {
4346
if v, err := strconv.ParseUint(opts.Uint16, 10, 16); err == nil {
4447
t = uint16(v)
4548
c++
4649
} else {
47-
return nil, err
50+
return nil, false, err
4851
}
4952
}
5053
if len(opts.Uint32) > 0 {
5154
if v, err := strconv.ParseUint(opts.Uint32, 10, 32); err == nil {
5255
t = uint32(v)
5356
c++
5457
} else {
55-
return nil, err
58+
return nil, false, err
5659
}
5760
}
5861
if len(opts.Uint64) > 0 {
5962
if v, err := strconv.ParseUint(opts.Uint64, 10, 64); err == nil {
6063
t = uint64(v)
6164
c++
6265
} else {
63-
return nil, err
66+
return nil, false, err
6467
}
6568
}
6669
if len(opts.Int8) > 0 {
6770
if v, err := strconv.ParseInt(opts.Int8, 10, 8); err == nil {
6871
t = int8(v)
6972
c++
7073
} else {
71-
return nil, err
74+
return nil, false, err
7275
}
7376
}
7477
if len(opts.Int16) > 0 {
7578
if v, err := strconv.ParseInt(opts.Int16, 10, 16); err == nil {
7679
t = int16(v)
7780
c++
7881
} else {
79-
return nil, err
82+
return nil, false, err
8083
}
8184
}
8285
if len(opts.Int32) > 0 {
8386
if v, err := strconv.ParseInt(opts.Int32, 10, 32); err == nil {
8487
t = int32(v)
8588
c++
8689
} else {
87-
return nil, err
90+
return nil, false, err
8891
}
8992
}
9093
if len(opts.Int64) > 0 {
9194
if v, err := strconv.ParseInt(opts.Int64, 10, 64); err == nil {
9295
t = int64(v)
9396
c++
9497
} else {
95-
return nil, err
98+
return nil, false, err
9699
}
97100
}
98101

@@ -101,7 +104,7 @@ func parseValue(opts Options) ([]byte, error) {
101104
case uint8, uint16, uint32, uint64, int8, int16, int32, int64:
102105
buf := new(bytes.Buffer)
103106
if err := binary.Write(buf, binary.BigEndian, t); err != nil {
104-
return nil, err
107+
return nil, false, err
105108
}
106109
value = buf.Bytes()
107110
}
@@ -117,11 +120,16 @@ func parseValue(opts Options) ([]byte, error) {
117120
c++
118121
}
119122

123+
if len(opts.Null) > 0 {
124+
value = []byte("")
125+
c++
126+
}
127+
120128
if c > 1 {
121-
return nil, errors.New("Multiple values specified for parameter!")
129+
return nil, false, errors.New("Multiple values specified for parameter")
122130
}
123131

124-
return value, err
132+
return value, true, err
125133
}
126134

127135
type Options struct {
@@ -148,6 +156,7 @@ type Options struct {
148156
Int16 string `long:"i16" description:"Set value, type is int16"`
149157
Int32 string `long:"i32" description:"Set value, type is int32"`
150158
Int64 string `long:"i64" description:"Set value, type is int64"`
159+
Null []bool `long:"null" description:"Set value to empty"`
151160

152161
Quiet []bool `short:"Q" long:"quiet" description:"Quiet mode, print only values"`
153162
Debug []bool `short:"D" long:"debug" description:"Debug mode, print raw packets"`
@@ -207,12 +216,12 @@ func main() {
207216
success := false
208217

209218
if len(opts.Parameter) > 0 {
210-
value, err := parseValue(opts)
219+
value, set, err := parseValue(opts)
211220
if err != nil {
212221
logger.Error.Printf("%s", err)
213-
} else if value != nil && len(opts.Parameter) > 1 {
222+
} else if set && len(opts.Parameter) > 1 {
214223
logger.Error.Printf("Value and multiple parameters provided\n")
215-
} else if value == nil {
224+
} else if set == false {
216225
for _, parameter := range opts.Parameter {
217226
if len(opts.Quiet) == 0 {
218227
logger.Info.Printf("Get %s\n", parameter)

0 commit comments

Comments
 (0)