-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from pauleyj/feature/refactor-api
Feature/refactor api
- Loading branch information
Showing
35 changed files
with
2,505 additions
and
1,140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2018 John C. Pauley | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright © 2018 NAME HERE <EMAIL ADDRESS> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package cmd | ||
|
||
import ( | ||
client2 "github.com/pauleyj/gobee/_examples/echo/cmd/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// clientCmd represents the client command | ||
var clientCmd = &cobra.Command{ | ||
Use: "client", | ||
Short: "An echo client", | ||
Long: `An echo client that accepts messages from the console, sends them to the echo server and expects the same message in response.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client := client2.NewEchoClient(*port, *baud, *verbose) | ||
err := client.Open() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
done := make(chan struct{}) | ||
<-done | ||
|
||
client.Close() | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(clientCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// clientCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package client | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/pauleyj/gobee" | ||
"github.com/pauleyj/gobee/_examples/echo/cmd/common" | ||
"github.com/pauleyj/gobee/api" | ||
"github.com/pauleyj/gobee/api/rx" | ||
"github.com/pauleyj/gobee/api/tx" | ||
"github.com/tarm/serial" | ||
) | ||
|
||
func NewEchoClient(port string, baud int, verbose bool) *EchoClient { | ||
return &EchoClient{ | ||
port: port, | ||
baud: baud, | ||
verbose: verbose, | ||
done: make(chan struct{}), | ||
} | ||
} | ||
|
||
type EchoClient struct { | ||
port string | ||
baud int | ||
verbose bool | ||
done chan struct{} | ||
|
||
sp *serial.Port | ||
} | ||
|
||
func (c *EchoClient) Open() error { | ||
// | ||
// configure and open serial port | ||
cfg := &serial.Config{ | ||
Name: c.port, | ||
Baud: c.baud, | ||
ReadTimeout: 5 * time.Millisecond, | ||
} | ||
|
||
var err error | ||
c.sp, err = serial.OpenPort(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
// | ||
// build transmitter | ||
transmitter := common.NewTransmitter(c.sp, c.verbose) | ||
// | ||
// build receiver | ||
rx := make(chan rx.Frame) | ||
receiver := common.NewReceiver(rx) | ||
// | ||
// build xbee | ||
xbee := gobee.New(transmitter, receiver, gobee.APIEscapeMode(api.EscapeModeActive)) | ||
// | ||
// serial port rx loop | ||
go c.rx(xbee, c.sp) | ||
// | ||
// console input loop | ||
go c.console(xbee) | ||
// | ||
// echo client | ||
go c.client(xbee, rx) | ||
|
||
return nil | ||
} | ||
|
||
func (c *EchoClient) Close() error { | ||
var err error | ||
// | ||
// exit client, console, and rx loops | ||
close(c.done) | ||
// | ||
// close serial port | ||
if c.sp != nil { | ||
err = c.sp.Close() | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (c *EchoClient) client(xbee *gobee.XBee, ch <-chan rx.Frame) { | ||
go func() { | ||
for { | ||
select { | ||
case f := <-ch: | ||
switch f.(type) { | ||
case *rx.TXStatus: | ||
fmt.Printf("Echo client received (TXS): %v\n", f.(*rx.TXStatus)) | ||
case *rx.ZB: | ||
fmt.Printf("Echo client received (ZB): %s\n", string(f.(*rx.ZB).Data())) | ||
case *rx.AT: | ||
fmt.Printf("Echo client received (AT Response): %s %#0.2x\n", string(f.(*rx.AT).Command()), f.(*rx.AT).Status()) | ||
default: | ||
fmt.Printf("Echo client received unhandled rx frame of type:%+v (%+v)\n", reflect.TypeOf(f), f) | ||
} | ||
case <-c.done: | ||
return | ||
} | ||
} | ||
}() | ||
|
||
fmt.Println("client initialized") | ||
<-c.done | ||
|
||
} | ||
|
||
func (c *EchoClient) console(xbee *gobee.XBee) { | ||
bio := bufio.NewReader(os.Stdin) | ||
// | ||
// forever read line from the console, send to coordinator | ||
for { | ||
buffer, err := bio.ReadBytes('\n') | ||
if err != nil { | ||
fmt.Printf("client failed to read stdio: %v\n", err) | ||
continue | ||
} | ||
|
||
if len(buffer) == 1 { | ||
continue | ||
} | ||
|
||
payload := buffer[:len(buffer)-1] | ||
if c.verbose { | ||
fmt.Printf("client sending data payload: [%s]\n", payload) | ||
} | ||
|
||
msg := tx.NewZB( | ||
tx.FrameID(1), | ||
tx.Addr64(0), | ||
tx.Addr16(0), | ||
tx.BroadcastRadius(0), | ||
tx.Options(0), | ||
tx.Data(payload)) | ||
_, err = xbee.TX(msg) | ||
if err != nil { | ||
fmt.Printf("client failed to transmit frame: %v\n", err) | ||
} | ||
} | ||
} | ||
|
||
func (c *EchoClient) rx(xbee *gobee.XBee, p *serial.Port) { | ||
var buf [256]byte | ||
// | ||
// forever, RX from uart and process, received API frames | ||
// will be handled by receiver | ||
for { | ||
select { | ||
case <-c.done: | ||
return | ||
default: | ||
n, _ := p.Read(buf[:]) | ||
if n != 0 { | ||
if c.verbose { | ||
fmt.Print("rx <-- ") | ||
} | ||
for i := 0; i < n; i++ { | ||
if c.verbose { | ||
fmt.Printf("%#0.2x ", buf[i]) | ||
} | ||
err := xbee.RX(buf[i]) | ||
if err != nil { | ||
fmt.Printf("\necho failed RX: %v\n", err) | ||
} | ||
} | ||
if c.verbose { | ||
fmt.Println() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package common | ||
|
||
import "github.com/pauleyj/gobee/api/rx" | ||
|
||
// NewReceiver constructs a new Receiver | ||
func NewReceiver(rx chan<- rx.Frame) *Receiver { | ||
return &Receiver{ | ||
rx: rx, | ||
} | ||
} | ||
|
||
// Receiver implements gobee.XBeeReceiver. | ||
type Receiver struct { | ||
rx chan<- rx.Frame | ||
} | ||
|
||
// Receive satisfies gobee.XBeeReceiver interface. This simple implementation | ||
// simply puts the received frame onto rx channel. | ||
func (r *Receiver) Receive(f rx.Frame) error { | ||
r.rx <- f | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tarm/serial" | ||
) | ||
|
||
// NewTransmitter constructs a new Transmitter | ||
func NewTransmitter(port *serial.Port, verbose bool) *Transmitter { | ||
return &Transmitter{ | ||
port: port, | ||
verbose: verbose, | ||
} | ||
} | ||
|
||
// Transmitter implements gobee.XBeeTransmitter | ||
type Transmitter struct { | ||
port *serial.Port | ||
verbose bool | ||
} | ||
|
||
// Transmit satisifies the gobee.XBeeTransmitter interface. It transmits | ||
// bytes (tx frame bytes) to the uart. | ||
func (t *Transmitter) Transmit(b []byte) (int, error) { | ||
if t.verbose { | ||
fmt.Print("tx --> ") | ||
for _, b := range b { | ||
fmt.Printf("%#0.2x ", b) | ||
} | ||
fmt.Println() | ||
} | ||
|
||
return t.port.Write(b) | ||
} |
Oops, something went wrong.