Skip to content

Commit f9bd5ad

Browse files
committed
gateway service config file
1 parent 2a595b2 commit f9bd5ad

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ Included in this repo is an implementation of the Protocol **Gateway** and **Cli
1111
The following clients are also available:
1212

1313
| Client | Platform | Language |
14-
| ---------------------------------------------------------------------------------------------- |:--------:| --------:|
14+
| ---------------------------------------------------------------------------------------------- |:--------:|:--------:|
1515
| [ArduinoSerialToTCPBridgeClient](https://github.com/RoanBrand/ArduinoSerialToTCPBridgeClient) | Arduino | C++ |
1616
| [STM32SerialToTCPBridgeClient](https://github.com/RoanBrand/STM32SerialToTCPBridgeClient) | STM32 | C |
1717

1818
#### Use
19-
- Open a terminal, then run `go get -u github.com/RoanBrand/SerialToTCPBridgeProtocol`.
20-
- Run the binary in `$GOPATH/bin`.
19+
- Install [Go](https://golang.org/) and set your `$GOPATH`.
20+
- Open a terminal, then run `go get -u github.com/RoanBrand/SerialToTCPBridgeProtocol`. This will fetch dependencies and build the gateway service.
21+
- Copy the *config.json* example to **$GOPATH/bin** next to the binary and set it according to your configuration.
22+
- Run the binary.
2123

2224
#### Details
2325
- The protocol provides the app an in order, duplicates free and error checked byte stream by adding a CRC32 and simple retry mechanism. See [this](https://en.wikibooks.org/wiki/Serial_Programming/Error_Correction_Methods) for background.
@@ -32,7 +34,7 @@ The following clients are also available:
3234
- Run `go test -v` in the terminal.
3335

3436
#### Future plans
35-
- Add config. Turn into OS service.
3637
- Add ping option to periodically test serial line and drop upstream connection if timeout.
3738
- Multiple connections per client to servers.
3839
- Capability to scan system and listen on all found COM ports for clients.
40+
- Turn into OS service.

config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"gateways": [
3+
{
4+
"gateway name": "Gateway for Arduino Uno",
5+
"comport name": "COM4",
6+
"baud rate": 115200
7+
}
8+
]
9+
}

example.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,59 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"github.com/RoanBrand/SerialToTCPBridgeProtocol/protocol"
56
"log"
7+
"os"
8+
"sync"
69
"time"
710
)
811

9-
const (
10-
COMPortName = "COM4" // Windows
11-
//COMPortName = "/dev/serial0" // Linux
12-
COMBaudRate = 115200
13-
)
14-
1512
func main() {
16-
com := protocol.NewComGateway(COMPortName, COMBaudRate)
17-
for {
18-
err := com.ServeCOM()
19-
log.Printf("%v: Error: %v\n", COMPortName, err)
20-
log.Println("Retrying in 5s...")
21-
time.Sleep(time.Second * 5)
13+
c, err := loadConfig()
14+
if err != nil {
15+
log.Fatalf(`%v (You must have a valid "config.json" next to the executable)`, err)
16+
}
17+
if len(c.Gateways) == 0 {
18+
log.Fatal("No gateways configured in the config. Exiting.")
19+
}
20+
21+
w := sync.WaitGroup{}
22+
for _, v := range c.Gateways {
23+
w.Add(1)
24+
go func(v gatewayConfig) {
25+
com := protocol.NewComGateway(v.COMPortName, v.COMBaudRate)
26+
for {
27+
err := com.ServeCOM()
28+
log.Printf("%v: Error: %v\nRetrying in 5s...\n", v.COMPortName, err)
29+
time.Sleep(time.Second * 5)
30+
}
31+
w.Done()
32+
}(v)
33+
}
34+
w.Wait()
35+
}
36+
37+
type gatewayConfig struct {
38+
GatewayName string `json:"gateway name"`
39+
COMPortName string `json:"comport name"`
40+
COMBaudRate int `json:"baud rate"`
41+
}
42+
43+
type config struct {
44+
Gateways []gatewayConfig `json:"gateways"`
45+
}
46+
47+
func loadConfig() (*config, error) {
48+
file, err := os.Open("config.json")
49+
if err != nil {
50+
return nil, err
51+
}
52+
decoder := json.NewDecoder(file)
53+
configuration := config{}
54+
err = decoder.Decode(&configuration)
55+
if err != nil {
56+
return nil, err
2257
}
58+
return &configuration, nil
2359
}

0 commit comments

Comments
 (0)