Skip to content

Commit

Permalink
Merge pull request #3 from pauleyj/feature/refactor-api
Browse files Browse the repository at this point in the history
Feature/refactor api
  • Loading branch information
pauleyj authored Feb 10, 2019
2 parents c7e7a62 + 84bd1eb commit 0dc3c73
Show file tree
Hide file tree
Showing 35 changed files with 2,505 additions and 1,140 deletions.
12 changes: 7 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
language: go
sudo: false
go:
- 1.6
- 1.7
- tip
- "1.7"
- "1.8"
- "1.9"
- "1.10"
- "tip"
before_install:
- go get github.com/mattn/goveralls
- go get github.com/mattn/goveralls
script:
- $HOME/gopath/bin/goveralls -service=travis-ci
- $HOME/gopath/bin/goveralls -service=travis-ci
notifications:
slack:
secure: Az/L2UjJj6ogWQAgRMjsFEVpqLPoOCazIZ9fMhq5hQmQUnbzbxW4dZVxE3WN+3b2NBZb+Eisd2mxFRetBXwCmDH+R0g0LjL3HM6i9rXMsn2lzuXIeRXARL+hoC++9XqHjCk2D2zDqpExXIL9ICDhllKJKsihntYYJFIhhJTdELBGiVgVw6ByXNyHsnR1c0FV8cv3xyBjT34rQcBfuCZbv5KdI9BMM1v/ZpfUPmn9FThz/5K/kQKfpQJTbpgJZ3bwcUr3yQG0ZfDJg8ka9Fha73NvZoWyaMSb/aZXJVDGOAdHKRXL4Jf+EnUKc4NWzF8tldy7hRQUzpxotqUgaPVdMKy0eUwBMXsBrQ3Xm0k9YA1mOtL/L+vbhWNr5WOpLOoN5dmXf4JBG0eTZYLjQ++i1v8HI1b6t5PSRVDkPCe1MwLWQJia577NsO6PPcFx5PmBzkLOtDMN54CpIsUVabBLag2g/ZHNQNON70FHOarkzi+xb+LdIJQ4fUiccJ6UbXU/tSLc7aDepZGl/Mw/F61hXmT2dl+KOG4JYTgKmVKfIF0he2Q4bugTV3d69aI9rQmaXFhmArdFdhwNwiID+BqKfRdnE0x7wuyHxQX20zTdqGndF+pgw0OXiEt9cWR16lG7Ws9IJpikJzBgSG/w9EsKMCKl1TaNMlJGI8ze7w8LBaQ=
Expand Down
21 changes: 21 additions & 0 deletions _examples/echo/LICENSE
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.
60 changes: 60 additions & 0 deletions _examples/echo/cmd/client.go
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")

}
177 changes: 177 additions & 0 deletions _examples/echo/cmd/client/client.go
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()
}
}
}
}
}
23 changes: 23 additions & 0 deletions _examples/echo/cmd/common/receiver.go
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
}
34 changes: 34 additions & 0 deletions _examples/echo/cmd/common/transmitter.go
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)
}
Loading

0 comments on commit 0dc3c73

Please sign in to comment.