2023-04-14 20:42:16 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-04-20 20:26:29 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kschamplin/gotelem/internal/gotelem"
|
|
|
|
"github.com/tinylib/msgp/msgp"
|
2023-04-14 20:42:16 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2023-04-20 20:26:29 +00:00
|
|
|
const xbeeCategory = "XBee settings"
|
|
|
|
|
2023-04-14 20:42:16 +00:00
|
|
|
var serveCmd = &cli.Command{
|
|
|
|
Name: "serve",
|
|
|
|
Aliases: []string{"server", "s"},
|
|
|
|
Usage: "Start a telemetry server",
|
2023-04-20 20:26:29 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{Name: "xbee", Aliases: []string{"x"}, Usage: "Find and connect to an XBee"},
|
|
|
|
},
|
|
|
|
Action: func(ctx *cli.Context) error {
|
|
|
|
serve()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type session struct {
|
|
|
|
conn net.Conn
|
|
|
|
send chan gotelem.Body
|
|
|
|
recv chan gotelem.Body
|
|
|
|
quit chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve() {
|
|
|
|
ln, err := net.Listen("tcp", ":8082")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error listening: %v\n", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Listening on :8082\n")
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error accepting: %v\n", err)
|
|
|
|
}
|
|
|
|
go handleCon(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleCon(conn net.Conn) {
|
|
|
|
// reader := msgp.NewReader(conn)
|
|
|
|
writer := msgp.NewWriter(conn)
|
|
|
|
for {
|
|
|
|
// data := telemnet.StatusBody{
|
|
|
|
// BatteryPct: 1.2,
|
|
|
|
// ErrCode: 0,
|
|
|
|
// }
|
|
|
|
// data.EncodeMsg(writer)
|
|
|
|
data := gotelem.StatusBody{
|
|
|
|
BatteryPct: 1.2,
|
|
|
|
ErrCode: 0,
|
|
|
|
}
|
|
|
|
data.EncodeMsg(writer)
|
|
|
|
writer.Flush()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2023-04-14 20:42:16 +00:00
|
|
|
}
|