add http, store skylab defintions in generated file as string

This commit is contained in:
saji 2023-06-23 16:20:28 -05:00
parent 3373eb3d06
commit aa0b61405b
7 changed files with 64 additions and 45 deletions

View file

@ -3,7 +3,6 @@ package cli
import (
"fmt"
imgui "github.com/AllenDang/cimgui-go"
"github.com/kschamplin/gotelem"
"github.com/kschamplin/gotelem/mprpc"
"github.com/urfave/cli/v2"
@ -31,14 +30,8 @@ Connects to a gotelem server or relay. Can be used to
Action: client,
}
func loop() {
imgui.ShowDemoWindow()
}
func client(ctx *cli.Context) error {
backend := imgui.CreateBackend()
backend.CreateWindow("hi there", 1200, 900, 0)
backend.Run(loop)
return nil
}

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"
@ -51,9 +52,10 @@ type service interface {
// can be extended on certain platforms (see cli/socketcan.go)
// or if certain features are present (see cli/sqlite.go)
var serveThings = []service{
&XBeeService{},
&CanLoggerService{},
&xBeeService{},
&canLoggerService{},
&rpcService{},
&httpService{},
}
func serve(cCtx *cli.Context) error {
@ -141,17 +143,17 @@ func handleCon(conn net.Conn, broker *gotelem.JBroker, l *slog.Logger, done <-ch
// this spins up a new can socket on vcan0 and broadcasts a packet every second. for testing.
type CanLoggerService struct {
type canLoggerService struct {
}
func (c *CanLoggerService) String() string {
func (c *canLoggerService) String() string {
return "CanLoggerService"
}
func (c *CanLoggerService) Status() {
func (c *canLoggerService) Status() {
}
func (c *CanLoggerService) Start(cCtx *cli.Context, broker *gotelem.JBroker, l *slog.Logger) (err error) {
func (c *canLoggerService) Start(cCtx *cli.Context, broker *gotelem.JBroker, l *slog.Logger) (err error) {
rxCh, err := broker.Subscribe("canDump")
if err != nil {
return err
@ -182,19 +184,19 @@ func (c *CanLoggerService) Start(cCtx *cli.Context, broker *gotelem.JBroker, l *
}
}
// XBeeService provides data over an Xbee device, either by serial or TCP
// xBeeService provides data over an Xbee device, either by serial or TCP
// based on the url provided in the xbee flag. see the description for details.
type XBeeService struct {
type xBeeService struct {
session *xbee.Session
}
func (x *XBeeService) String() string {
func (x *xBeeService) String() string {
return "hello"
}
func (x *XBeeService) Status() {
func (x *xBeeService) Status() {
}
func (x *XBeeService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *slog.Logger) (err error) {
func (x *xBeeService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *slog.Logger) (err error) {
if cCtx.String("xbee") == "" {
logger.Info("not using xbee")
return
@ -233,3 +235,22 @@ func (x *XBeeService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *
}
}
type httpService struct {
}
func (h *httpService) String() string {
return "HttpService"
}
func (h *httpService) Status() {
}
func (h *httpService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *slog.Logger) (err error) {
r := gotelem.TelemRouter(logger)
http.ListenAndServe(":8080", r)
return
}

1
go.mod
View file

@ -3,7 +3,6 @@ module github.com/kschamplin/gotelem
go 1.20
require (
github.com/AllenDang/cimgui-go v0.0.0-20230502145512-97518c13c52b
github.com/go-chi/chi/v5 v5.0.8
github.com/jmoiron/sqlx v1.3.5
github.com/mattn/go-sqlite3 v1.14.16

2
go.sum
View file

@ -1,5 +1,3 @@
github.com/AllenDang/cimgui-go v0.0.0-20230502145512-97518c13c52b h1:EpLVWHcO5auIf8X0STrno4WAWThu6s6QgJ1ywJa5Tmo=
github.com/AllenDang/cimgui-go v0.0.0-20230502145512-97518c13c52b/go.mod h1:iNfbIyOBN8k3XScMxULbrwYbPsXEAUD0Jb6UwrspQb8=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=

View file

@ -5,6 +5,7 @@
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
@ -17,36 +18,36 @@ import (
// SkylabFile is a yaml file from skylab.
type SkylabFile struct {
Packets []PacketDef
Boards []BoardSpec
Packets []PacketDef `json:"packets"`
Boards []BoardDef `json:"boards"`
}
type BoardSpec struct {
Name string
Transmit []string
Recieve []string
type BoardDef struct {
Name string `json:"name"`
Transmit []string `json:"transmit"`
Receive []string `json:"receive"`
}
// data field.
type DataField struct {
Name string
Type string
Units string // mostly for documentation
Conversion float32
type FieldDef struct {
Name string `json:"name"`
Type string `json:"type"`
Units string `json:"units"`
Conversion float32 `json:"conversion"`
Bits []struct {
Name string
}
Name string `json:"name"`
} `json:"bits"`
}
// a PacketDef is a full can packet.
type PacketDef struct {
Name string
Description string
Id uint32
BigEndian bool
Repeat int
Offset int
Data []DataField
Name string `json:"name"`
Description string `json:"description"`
Id uint32 `json:"id"`
Endian string `json:"endian"`
Repeat int `json:"repeat"`
Offset int `json:"offset"`
Data []FieldDef `json:"data"`
}
// we need to generate bitfield types.
@ -83,7 +84,7 @@ func MapType(ctype string) string {
return typeMap[ctype]
}
func (d *DataField) ToStructMember(parentName string) string {
func (d *FieldDef) ToStructMember(parentName string) string {
if d.Type == "bitfield" {
bfStructName := parentName + toCamelInitCase(d.Name, true)
@ -93,7 +94,7 @@ func (d *DataField) ToStructMember(parentName string) string {
}
}
func (d *DataField) MakeMarshal(offset int) string {
func (d *FieldDef) MakeMarshal(offset int) string {
fieldName := toCamelInitCase(d.Name, true)
if d.Type == "uint8_t" || d.Type == "int8_t" {
@ -117,7 +118,7 @@ func (d *DataField) MakeMarshal(offset int) string {
return "panic(\"failed to do it\")\n"
}
func (d *DataField) MakeUnmarshal(offset int) string {
func (d *FieldDef) MakeUnmarshal(offset int) string {
fieldName := toCamelInitCase(d.Name, true)
if d.Type == "uint8_t" || d.Type == "int8_t" {
@ -316,6 +317,7 @@ func main() {
"strJoin": strJoin,
"mapf": mapf,
"maptype": MapType,
"json": json.Marshal,
}
tmpl, err := template.New("golang.go.tmpl").Funcs(fnMap).ParseGlob("templates/*.go.tmpl")

File diff suppressed because one or more lines are too long

View file

@ -165,3 +165,6 @@ func FromJson (id uint32, raw []byte) (Packet, error) {
{{template "packet" .}}
{{- end}}
// The json representation that was used to generate this data.
// can be used to share the parsing data for i.e dynamic python gui.
const SkylabDefinitions = `{{json . | printf "%s" }}`