Skip to content

Commit 62c8585

Browse files
committed
go-client v0.5.1
1 parent 77fb281 commit 62c8585

File tree

14 files changed

+159
-136
lines changed

14 files changed

+159
-136
lines changed

.github/workflows/build.yml

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ name: build
22
on: [push]
33
jobs:
44

5-
lint:
5+
vetlint:
66
runs-on: ubuntu-latest
7-
name: Go lint
7+
name: Go vet lint
88

99
steps:
1010

11-
- uses: actions/checkout@v3
11+
- uses: actions/checkout@v4
1212

1313
- name: Setup go
14-
uses: actions/setup-go@v3
14+
uses: actions/setup-go@v5
1515
with:
16-
go-version: '1.20'
16+
go-version: '1.22'
1717

18-
- run: |
18+
- name: Vet
19+
run: |
20+
go vet ./...
21+
22+
- name: Lint
23+
run: |
1924
go install golang.org/x/lint/golint@latest
2025
golint ./...
2126
@@ -25,7 +30,7 @@ jobs:
2530
matrix:
2631
goos: [linux]
2732
goarch: [amd64, arm, arm64]
28-
go: ['1.20']
33+
go: ['1.22.1']
2934
fail-fast: false
3035

3136
name: Go ${{ matrix.go }} ${{ matrix.goos }} ${{ matrix.goarch }} build
@@ -34,6 +39,7 @@ jobs:
3439
GOOS: ${{ matrix.goos }}
3540
GOARCH: ${{ matrix.goarch }}
3641
GOARM: 6
42+
GOTOOLCHAIN: go${{ matrix.go }}
3743

3844
steps:
3945

@@ -42,13 +48,13 @@ jobs:
4248
sudo apt-get update
4349
sudo apt-get -y install qemu-user
4450
45-
- uses: actions/checkout@v3
51+
- uses: actions/checkout@v4
4652

4753
- name: Setup go
48-
uses: actions/setup-go@v3
54+
uses: actions/setup-go@v5
4955
with:
50-
go-version: ${{ matrix.go }}
51-
56+
go-version: ${{ matrix.go }}
57+
5258
- name: Get dependencies
5359
run: |
5460
go get -v -t -d ./...
@@ -57,25 +63,24 @@ jobs:
5763
run: |
5864
go build -v ./...
5965
60-
- name: Vet
61-
run: |
62-
go vet ./...
63-
6466
build-macos:
6567
runs-on: macos-latest
6668
strategy:
6769
matrix:
68-
go: ['1.20']
70+
go: ['1.22.1']
6971
fail-fast: false
7072

7173
name: Go ${{ matrix.go }} macOS
74+
75+
env:
76+
GOTOOLCHAIN: go${{ matrix.go }}
7277

7378
steps:
74-
75-
- uses: actions/checkout@v3
79+
80+
- uses: actions/checkout@v4
7681

7782
- name: Setup go
78-
uses: actions/setup-go@v3
83+
uses: actions/setup-go@v5
7984
with:
8085
go-version: ${{ matrix.go }}
8186

@@ -87,25 +92,24 @@ jobs:
8792
run: |
8893
go build -v ./...
8994
90-
- name: Vet
91-
run: |
92-
go vet ./...
93-
9495
build-windows:
9596
runs-on: windows-latest
9697
strategy:
9798
matrix:
98-
go: ['1.20']
99+
go: ['1.22.1']
99100
fail-fast: false
100101

101102
name: Go ${{ matrix.go }} Windows
103+
104+
env:
105+
GOTOOLCHAIN: go${{ matrix.go }}
102106

103107
steps:
104-
105-
- uses: actions/checkout@v3
108+
109+
- uses: actions/checkout@v4
106110

107111
- name: Setup go
108-
uses: actions/setup-go@v3
112+
uses: actions/setup-go@v5
109113
with:
110114
go-version: ${{ matrix.go }}
111115

@@ -116,8 +120,3 @@ jobs:
116120
- name: Build
117121
run: |
118122
go build -v ./...
119-
120-
- name: Vet
121-
run: |
122-
go vet ./...
123-

.github/workflows/reuse.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ jobs:
66
test:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v3
9+
- uses: actions/checkout@v4
1010
- name: REUSE Compliance Check
11-
uses: fsfe/reuse-action@v1.1
11+
uses: fsfe/reuse-action@v2

.reuse/dep5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ Upstream-Contact: stefan.miller_at_sap.com
44
Source: https://github.com/pico-cs/go-client
55

66
Files: *
7-
Copyright: 2021-2023 Stefan Miller
7+
Copyright: 2021-2024 Stefan Miller
88
License: GPL-3.0-or-later

Makefile

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ all:
88
go vet ./...
99
golint -set_exit_status=true ./...
1010
staticcheck -checks all -fail none ./...
11+
golangci-lint run ./...
1112
go test ./...
13+
#see fsfe reuse tool (https://git.fsfe.org/reuse/tool)
1214
@echo "reuse (license) check"
13-
reuse lint
15+
pipx run reuse lint
16+
17+
#go generate
18+
generate:
19+
@echo "generate"
20+
go generate ./...
1421

1522
#install additional tools
1623
tools:
24+
#install stringer
25+
@echo "install latest stringer version"
26+
go install golang.org/x/tools/cmd/stringer@latest
1727
#install linter
1828
@echo "install latest go linter version"
1929
go install golang.org/x/lint/golint@latest
2030
#install staticcheck
2131
@echo "install latest staticcheck version"
2232
go install honnef.co/go/tools/cmd/staticcheck@latest
23-
24-
#install fsfe reuse tool (https://git.fsfe.org/reuse/tool)
25-
# pre-conditions:
26-
# - Python 3.6+
27-
# - pip
28-
# install pre-conditions in Debian like linux distros:
29-
# - sudo apt install python3
30-
# - sudo apt install python3-pip
31-
reuse:
32-
pip3 install --user --upgrade reuse
33+
#install golangci-lint
34+
@echo "install latest golangci-lint version"
35+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
## Licensing
88

9-
Copyright 2021-2023 Stefan Miller and pico-cs contributers. Please see our [LICENSE](LICENSE.md) for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available [via the REUSE tool](https://api.reuse.software/info/github.com/pico-cs/go-client).
9+
Copyright 2021-2024 Stefan Miller and pico-cs contributers. Please see our [LICENSE](LICENSE.md) for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available [via the REUSE tool](https://api.reuse.software/info/github.com/pico-cs/go-client).
1010

client/client.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const (
122122
const (
123123
replyChSize = 1
124124
pushChSize = 100
125-
timeout = 5
125+
timeout = 30
126126
)
127127

128128
// Client represents a command station client instance.
@@ -160,16 +160,33 @@ func (c *Client) shutdown() error {
160160
return err
161161
}
162162

163-
// Reconnect tries to reconnect the client.
163+
func (c *Client) reconnect() error {
164+
var err error
165+
for i := 0; i < reconnectRetry; i++ {
166+
time.Sleep(reconnectWait)
167+
if err = c.conn.Connect(); err == nil {
168+
return nil
169+
}
170+
}
171+
return err
172+
}
173+
174+
// Reconnect reconnects the client.
164175
func (c *Client) Reconnect() error {
165-
c.shutdown() // ignore error
166-
if err := c.conn.Reconnect(); err != nil {
176+
c.shutdown() //nolint: errcheck
177+
if err := c.reconnect(); err != nil {
167178
return err
168179
}
169180
c.startup()
170181
return nil
171182
}
172183

184+
// IsSerialConn returns true if the connection is serial, false otherwise.
185+
func (c *Client) IsSerialConn() bool {
186+
_, ok := c.conn.(*Serial)
187+
return ok
188+
}
189+
173190
// Close closes the client connection.
174191
func (c *Client) Close() error { return c.shutdown() }
175192

@@ -212,12 +229,14 @@ func (c *Client) reader(wg *sync.WaitGroup) (<-chan any, <-chan string) {
212229

213230
scanner := bufio.NewScanner(c.conn)
214231

215-
//TODO check scanner.Error()
216-
217232
multi := false
218233
var multiMsg []string
219234

220235
for scanner.Scan() {
236+
if err := scanner.Err(); err != nil {
237+
replyCh <- err
238+
}
239+
221240
//log.Printf("message: %s", scanner.Text())
222241

223242
rk, msg := c.parseReply(scanner.Bytes())
@@ -244,9 +263,6 @@ func (c *Client) reader(wg *sync.WaitGroup) (<-chan any, <-chan string) {
244263
multi = false
245264
}
246265
}
247-
//if err := scanner.Err(); err != nil {
248-
// fmt.Fprintln(os.Stderr, "reading standard input:", err)
249-
//}
250266

251267
close(replyCh)
252268
close(pushCh)
@@ -270,24 +286,25 @@ func (c *Client) pusher(wg *sync.WaitGroup, pushCh <-chan string, handler func(M
270286
}
271287

272288
func (c *Client) write(cmd string, args []any) error {
273-
c.w.WriteByte(tagStart)
274-
c.w.WriteString(cmd)
289+
c.w.WriteByte(tagStart) //nolint: errcheck
290+
c.w.WriteString(cmd) //nolint: errcheck
275291
for _, arg := range args {
276-
c.w.WriteByte(' ') // argument separator
292+
// argument separator
293+
c.w.WriteByte(' ') //nolint: errcheck
277294

278295
rv := reflect.ValueOf(arg)
279296
switch rv.Kind() {
280297
case reflect.Bool:
281-
c.w.WriteByte(formatBool(rv.Bool()))
298+
c.w.WriteByte(formatBool(rv.Bool())) //nolint: errcheck
282299
case reflect.Uint8, reflect.Uint:
283-
c.w.WriteString(strconv.FormatUint(rv.Uint(), 10))
300+
c.w.WriteString(strconv.FormatUint(rv.Uint(), 10)) //nolint: errcheck
284301
case reflect.String:
285-
c.w.WriteString(rv.String())
302+
c.w.WriteString(rv.String()) //nolint: errcheck
286303
default:
287304
panic(fmt.Sprintf("invalid argument %[1]v type %[1]T", arg)) // should never happen
288305
}
289306
}
290-
c.w.WriteByte('\r')
307+
c.w.WriteByte('\r') //nolint: errcheck
291308
if err := c.w.Flush(); err != nil {
292309
return err
293310
}
@@ -749,7 +766,12 @@ func (c *Client) FlashFormat() (bool, error) {
749766
return strconv.ParseBool(v)
750767
}
751768

769+
var rebootWait = 5 * time.Second
770+
752771
// Reboot reboots the command station (debugging).
753772
func (c *Client) Reboot() error {
754-
return c.call(cmdReboot)
773+
err := c.call(cmdReboot)
774+
// wait some time to be sure that device is re-booted.
775+
time.Sleep(rebootWait)
776+
return err
755777
}

client/client_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ func testRefreshBuffer(c *client.Client, t *testing.T) {
111111

112112
func testRefreshBufferDelete(c *client.Client, t *testing.T) {
113113
// reset refresh buffer
114-
c.RefreshBufferReset()
115-
114+
if _, err := c.RefreshBufferReset(); err != nil {
115+
t.Fatal(err)
116+
}
116117
if _, err := c.SetLocoSpeed128(3, 12); err != nil { // add loco to buffer
117118
t.Fatal(err)
118119
}
@@ -159,6 +160,10 @@ func testReboot(c *client.Client, t *testing.T) {
159160
t.Fatal(err)
160161
}
161162
if err := c.Reconnect(); err != nil {
163+
if c.IsSerialConn() { // after reboot connection port might be different.
164+
t.Log(err)
165+
return
166+
}
162167
t.Fatal(err)
163168
}
164169
testBoard(c, t)

client/conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77

88
const (
99
reconnectRetry = 10
10-
reconnectWait = 5 * time.Second // wait some time to reconnect
10+
reconnectWait = 500 * time.Millisecond
1111
)
1212

1313
// Conn is a stream oriented connection to the pico board.
1414
type Conn interface {
15-
Reconnect() error
15+
Connect() error
1616
io.ReadWriteCloser
1717
}

0 commit comments

Comments
 (0)